This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AccessTokenInterceptor : Interceptor { | |
| override fun intercept(chain: Interceptor.Chain): Response = chain | |
| .request() | |
| .newBuilder() | |
| .let { builder -> | |
| val accessToken = accountRepository.accessToken ?: "" | |
| builder.setAuthHeader(builder, accessToken) | |
| } | |
| .build() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val okHttpClient = OkHttpClient.Builder() | |
| .addInterceptor(AuthInterceptor()) | |
| .build() | |
| val retrofit = Retrofit.Builder() | |
| .baseUrl("https://igor.wojda.com/app") | |
| .client(okHttpClient) | |
| .build() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| KotlinViewModelProvider.of(context) { RegisterViewModel(instance()) } //context is a Fragment |
NewerOlder