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 / ExampleTest.kt
Created September 1, 2020 12:05
With mockito
import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.Store
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.android.external.store4.fresh
import com.dropbox.android.sample.Graph
import com.dropbox.android.sample.data.model.Post
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import kotlinx.coroutines.ExperimentalCoroutinesApi
import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.Store
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.android.external.store4.fresh
import com.dropbox.android.sample.Graph
import com.dropbox.android.sample.data.model.Post
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
@harmittaa
harmittaa / bottom_nav.xml
Created November 23, 2019 19:09
Bottom navigation bar example
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:background="@drawable/bottom_navigation_background"
android:elevation="8dp"
app:itemIconTint="@drawable/bottom_navigation_color_selector"
@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(sdk = [27])
// parameters declared in the params() are injected through the constructor to the test class
class ExampleUnitTest(private val param: Int) {
@Test
fun addition_isCorrect() {
// parameter use in a test
assertEquals(param * 2, param + param)
}
@harmittaa
harmittaa / ExampleFragment.kt
Last active November 20, 2019 19:25
Koin 2.0 and Retrofit 2.6.0
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.github.harmittaa.koinexample.R
@harmittaa
harmittaa / ExampleViewModel.kt
Created July 13, 2019 18:03
Koin 2.0 and Retrofit 2.6.0
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.github.harmittaa.koinexample.model.Weather
import com.github.harmittaa.koinexample.model.WeatherRepository
import org.koin.dsl.module
val viewModelModule = module {
factory { ExampleViewModel(get()) }
}
@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()) }