Skip to content

Instantly share code, notes, and snippets.

View ConorGarry's full-sized avatar

Conor Garry ConorGarry

View GitHub Profile
@ConorGarry
ConorGarry / Logging.kt
Last active October 25, 2023 09:56
Implementation of kermit-core multiplatform logger. Adds clickable line number, and uses calling file name as tag if none is provided.
import co.touchlab.kermit.BaseLogger
import co.touchlab.kermit.LoggerConfig
import co.touchlab.kermit.Severity
import co.touchlab.kermit.mutableLoggerConfigInit
import co.touchlab.kermit.platformLogWriter
@Suppress("ClassName") // lower case class name.
open class log(
config: LoggerConfig,
) : BaseLogger(config) {
@ConorGarry
ConorGarry / LiveDataTestUtils.kt
Created November 8, 2021 16:28
LiveData test extensions.
package ie.conorgarry.patreontemplate
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
// kotlin-coroutines-end/app/src/test/java/com/example/android/kotlincoroutines/LiveDataTestUtil.kt
@ConorGarry
ConorGarry / gist:00f93a80dac701f757b7c4a07a96e4af
Created November 8, 2021 16:26
MVP Dependencies. The Fundamental dependencies for Android, Kotlin, and testing.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
@ConorGarry
ConorGarry / UiState.kt
Last active August 27, 2021 09:27
Monad for handing UI state representation with a clean consumable API.
sealed class UiState<out T> {
fun interface SuccessHandler<T> {
fun invoke(data: T)
}
fun interface FailureHandler {
fun invoke(error: Throwable)
}
@ConorGarry
ConorGarry / GroupByPredicatesExtension.kt
Last active July 24, 2020 07:22
Kotlin extension function for paritioning >2 predicates.
/**
* Similar to [groupBy] or [partition], except an arbitrary amount of predicates can be returned.
* Think [partition] for > 2 predicates.
*
*
* @return [List] with size n for predicates where all items will satisfy one of the predicates,
* or size n + 1 for n predicates, where there will be remaining items after condition check.
*
* @throws [IndexOutOfBoundsException] If using declaration deconstruction where val count is > possible return size.
*
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
class CoroutineApiViewModel : ViewModel() {
@ConorGarry
ConorGarry / StateFlowExt.kt
Last active May 11, 2020 07:07
Extension functions for StateFlow
// Scope to LifeCycle ON_START
@ExperimentalCoroutinesApi
fun <T> LifecycleOwner.stateFlow(stateFlow: StateFlow<T>, funCollect: (T) -> Unit) {
lifecycleScope.launchWhenStarted {
stateFlow.collect {
funCollect(it)
}
}
}