Skip to content

Instantly share code, notes, and snippets.

View harmittaa's full-sized avatar
💻
📲

Matti Mäki-Kihniä harmittaa

💻
📲
View GitHub Profile
@harmittaa
harmittaa / WeatherRepository.kt
Created July 13, 2019 17:03
Koin 2.0 and Retrofit 2.6.0 example
import com.github.harmittaa.koinexample.networking.WeatherApi
import org.koin.dsl.module
val forecastModule = module {
factory { WeatherRepository(get()) }
}
class WeatherRepository(private val weatherApi: WeatherApi) {
suspend fun getWeather() = weatherApi.getForecast()
}
@harmittaa
harmittaa / WeatherApi.kt
Last active July 13, 2019 17:02
Koin 2.0 and Retrofit 2.6.0 example
import com.github.harmittaa.koinexample.model.Weather
import retrofit2.http.GET
interface WeatherApi {
@GET("weather?q=Helsinki&units=metric")
suspend fun getForecast(): Weather
}
@harmittaa
harmittaa / AuthInterceptor.kt
Created July 13, 2019 16:48
Koin 2.0 and Retrofit 2.6.0 example
import okhttp3.Interceptor
import okhttp3.Response
class AuthInterceptor() : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var req = chain.request()
// DONT INCLUDE API KEYS IN YOUR SOURCE CODE
val url = req.url().newBuilder().addQueryParameter("APPID", "your_key_here").build()
req = req.newBuilder().url(url).build()
return chain.proceed(req)
@harmittaa
harmittaa / RetrofitClient.kt
Last active January 13, 2021 10:30
Koin 2.0 Retrofit example
import com.github.harmittaa.koinexample.BuildConfig
import okhttp3.OkHttpClient
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
val networkModule = module {
factory { AuthInterceptor() }
factory { provideOkHttpClient(get()) }
factory { provideForecastApi(get()) }
@harmittaa
harmittaa / ExampleViewModel.kt
Created July 13, 2019 14:53
Koin 2.0 example ViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.github.harmittaa.koinexample.persistence.ExamplePreferences
import org.koin.dsl.module
val viewModelModule = module {
factory { ExampleViewModel(get()) }
}
class ExampleViewModel(preferences: ExamplePreferences) : ViewModel() {
@harmittaa
harmittaa / ExampleFragment.kt
Created July 13, 2019 14:51
Koin 2.0 example fragment
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.github.harmittaa.koinexample.R
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.dsl.module
@harmittaa
harmittaa / MainActivity.kt
Created July 13, 2019 14:30
Koin 2.0 example Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.github.harmittaa.koinexample.R
import com.github.harmittaa.koinexample.fragment.ExampleFragment
import com.github.harmittaa.koinexample.persistence.ExamplePreferences
import org.koin.android.ext.android.inject
class MainActivity : AppCompatActivity() {
private val preferences: ExamplePreferences by inject()
private val exampleFragment: ExampleFragment by inject()
@harmittaa
harmittaa / ExamplePreferences.kt
Created July 13, 2019 14:11
Koin 2.0 example module
import android.content.Context
import android.content.SharedPreferences
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
val prefModule = module {
single { ExamplePreferences(androidContext()) }
}
class ExamplePreferences(context: Context) {
@harmittaa
harmittaa / ButtonWithImage.swift
Created October 5, 2017 06:18
UIButton with label and right aligned image
import UIKit
class ButtonWithImage: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
if imageView != nil {
imageEdgeInsets = UIEdgeInsets(top: 5, left: (bounds.width - 35), bottom: 5, right: 5)
titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: (imageView?.frame.width)!)
}
@harmittaa
harmittaa / CameraFocusController.cs
Last active July 21, 2023 00:49
CameraFocusController.cs for Vuforia ARCamera camera focus mode example
// full tutorial here:
// https://medium.com/@harmittaa/setting-camera-focus-mode-for-vuforia-arcamera-in-unity-6b3745297c3d
using UnityEngine;
using System.Collections;
using Vuforia;
public class CameraFocusController: MonoBehaviour {
// code from Vuforia Developer Library