Skip to content

Instantly share code, notes, and snippets.

View igorwojda's full-sized avatar

Igor Wojda igorwojda

View GitHub Profile
android {
splits {
// Configures multiple APKs based on ABI.
abi {
// Enables building multiple APKs per ABI.
enable true
// By default all ABIs are included, so use reset() and include to specify that we only
// want APKs for x86, armeabi-v7a, and mips.
reset()
@igorwojda
igorwojda / gist:5b4e9348836dc2446c3fa07c838ed1b5
Last active August 29, 2020 07:59
Configure apk split for release builds
android {
splits {
abi {
def isReleaseBuild = false
gradle.startParameter.taskNames.find {
// Enable split for release builds in different build flavors
// (assemblePaidRelease, assembleFreeRelease, etc.).
if (it ==~ /:app:assemble.*Release/) {
isReleaseBuild = true
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()
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)
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")
@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 / gist:42c3391b4bfb3527a368be4065867f77
Last active June 7, 2019 12:14
Side effects - modify variable outside function
val patient:Patient? = null
fun setPatient(patient:Patient) {
this.patient = patient
}
@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")