Skip to content

Instantly share code, notes, and snippets.

View lamvann's full-sized avatar

Eli Ivann Ruiz lamvann

View GitHub Profile
@lamvann
lamvann / Either.kt
Last active May 24, 2022 16:36
Implementation of Either monad
/**
* Custom implementation of the Either monad.
*
* @param F Type of a Left (Failure)
* @param S Type of a Right (Success)
*/
sealed class Either<out F, out S> {
data class Left<out F>(val left: F) : Either<F, Nothing>()
@lamvann
lamvann / runCommand.kt
Created September 9, 2020 17:06
Run a shell command from Kotlin
import java.io.ByteArrayOutputStream
fun runCommand(command: String): String {
return runCatching {
val stdout = ByteArrayOutputStream()
exec {
commandLine(*command.split(' ').toTypedArray())
standardOutput = stdout
}
fun refreshUi(refresh: UiStateType.() -> UiStateType) {
val newState = uiState.run(refresh)
check(!(newState === uiState)) { "BaseUiState is the same object. Use .copy" }
_uiStateLiveData.value = newState
}
@lamvann
lamvann / build.gradle.kts
Last active May 25, 2020 19:17
Extension function to apply the kotlin plugin to a project
fun Project.setupAsKotlinLibrary() {
println("\tKotlin library")
apply(plugin = "kotlin")
}
@lamvann
lamvann / build.gradle.kts
Last active May 25, 2020 19:12
Extension function on Project object to set up an android library
fun Project.setupAsAndroidLibrary() {
println("\tAndroid Library")
apply(plugin = "com.android.library")
apply(plugin = "org.jetbrains.kotlin.android")
apply(plugin = "org.jetbrains.kotlin.android.extensions")
apply(plugin = "org.jetbrains.kotlin.kapt")
configure<com.android.build.gradle.LibraryExtension> {
compileOptions {
@lamvann
lamvann / build.gradle.kts
Last active May 25, 2020 19:20
Starting point of a top-level project gradle file in Kotlin DSL
plugins {
`kotlin-dsl`
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
@lamvann
lamvann / build.gradle.kts
Created May 24, 2020 18:57
The ideal structure of a Gradle file only declares the dependencies it needs.
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${getKotlinPluginVersion()}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5")
implementation("com.jakewharton.threetenabp:threetenabp:1.2.3")
implementation("com.squareup.okhttp3:okhttp:4.5.0")
testImplementation("junit:junit:4.13")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5")
@lamvann
lamvann / BaseFragment.kt
Created April 28, 2020 19:55
Adding viewModel property to BaseFragment using Koin with reflection
private val viewModelClass =
(javaClass
.genericSuperclass as ParameterizedType)
.actualTypeArguments[0] as Class<ViewModelType>
val viewModel: ViewModelType by lazy { getViewModel(clazz = viewModelClass.kotlin) }
@lamvann
lamvann / ApiCall.kt
Created March 15, 2020 15:05
API call using custom dispatcher for coroutines in K/N
private class MainDispatcher: CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_main_queue()) { block.run() } // This line gets highlighted when the error happens
}
}
internal class MainScope: CoroutineScope {
private val dispatcher = MainDispatcher()
private val job = Job()
import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readText
import io.ktor.http.URLProtocol.Companion.HTTPS
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch