Skip to content

Instantly share code, notes, and snippets.

View igorwojda's full-sized avatar

Igor Wojda igorwojda

View GitHub Profile
class AccessTokenInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = chain
.request()
.newBuilder()
.let { builder ->
val accessToken = accountRepository.accessToken ?: ""
builder.setAuthHeader(builder, accessToken)
}
.build()
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(AuthInterceptor())
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://igor.wojda.com/app")
.client(okHttpClient)
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://igorwojda.com/app")
.build()
val userService = retrofit.create(UserService::class.java)
val accountRepository = AccountRepository(context)
// Wee need this because userService.login function is suspended
CoroutineScope(Dispatchers.IO).launch {
val response = userService.login("email", "password")
class AccountRepository(context: Context) {
companion object {
const val ACCOUNT_NAME = "ABC app account"
const val ACCOUNT_TYPE = "com.igorwojda.abc"
const val REFRESH_TOKEN = "refreshToken"
}
private val accountManager: AccountManager = AccountManager.get(context)
@igorwojda
igorwojda / Evolution.kt
Created January 17, 2020 09:27
Evolution of the class property
// EVOLUTION OF CLASS PROPERTY
// The presented code is in Kotlin, but the problem itself is not language-specific
// Client usage
fun main() {
Evolution1().setName("Igor")
Evolution2().name = "Igor"
Evolution3().name = "Igor"
Evolution4().name = "Igor"
}
// Can be used as generic method to retrieve NavArgs? in the BaseViewModel
// usage val viewModel by navArgsReflection()
class NavArgsReflectionLazy<Args : NavArgs?>(
private val navArgsClass: KClass<out Fragment>,
private val arguments: () -> Bundle?
) :
ReadOnlyProperty<Any?, Args?> {
private var navArgs: NavArgsLazy<*>? = null
@igorwojda
igorwojda / RegisterViewModelFactory.kt
Last active January 7, 2019 19:48
Register ViewModel Factory
class RegisterViewModelFactory @Inject constructor(
private val repository: UserRepository
): ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(RegisterViewModel::class.java!!)) {
RegisterViewModel(repository) as T
} else {
throw IllegalArgumentException("ViewModel Not Found")
@igorwojda
igorwojda / ClassisViewModel.kt
Last active January 7, 2019 19:39
Classic ViewModel
class RegisterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel = ViewModelProviders.of(this).get(RegisterViewModel::class.java)
//..observe live steams exposed by view model
}
}
@igorwojda
igorwojda / FragmentExtensions.kt
Last active January 7, 2019 19:28
Fragment withViewModel
inline fun <reified T : ViewModel> Fragment.withViewModel(
crossinline factory: () -> T,
body: T.() -> Unit
): T = getViewModel(factory).also { it.body() }
inline fun <reified T : ViewModel> Fragment.getViewModel(crossinline factory: () -> T): T {
val vmFactory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <U : ViewModel> create(modelClass: Class<U>): U = factory() as U
@igorwojda
igorwojda / KotlinViewModelProviderUsage.kt
Created January 7, 2019 07:44
KotlinViewModelProvider usage
KotlinViewModelProvider.of(context) { RegisterViewModel(instance()) } //context is a Fragment