Skip to content

Instantly share code, notes, and snippets.

import android.view.View
/**
* Lightweight alternative for [android.view.ViewStub]
*/
class LazyView<T : View>(
private val init: () -> T,
) {
@Andrew0000
Andrew0000 / CollectStarted.kt
Last active October 2, 2024 08:15
collectStarted() extension for contraction of repeatOnLifecycle(Lifecycle.State.STARTED)
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
/*
* Contraction for:
@Andrew0000
Andrew0000 / ComposeShadow.kt
Last active November 16, 2024 23:22
Jetpack Compose custom shadow with dx, dy and radius
import android.graphics.BlurMaskFilter
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
@Andrew0000
Andrew0000 / Retrying.kt
Created June 16, 2023 12:45
Retrying coroutines + additional onError() callback
import kotlinx.coroutines.delay
import kotlin.coroutines.cancellation.CancellationException
@Throws
suspend fun <T> retrying(
fallbackValue: T? = null,
tryCnt: Int = 3,
intervalMillis: (attempt: Int) -> Long = { 2000L * it },
onError: suspend (Throwable) -> Unit = {},
retryCheck: (Throwable) -> Boolean = networkRetryCheck,
fun <T> Flow<T>.throttleLatest(delayMillis: Long) = this
.conflate()
.transform {
emit(it)
delay(delayMillis)
}
import kotlinx.coroutines.ExperimentalCoroutinesApi
@Andrew0000
Andrew0000 / ActivitiesTracker.kt
Created May 16, 2023 07:51
ActivitiesTracker
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle
import timber.log.Timber
import java.lang.ref.WeakReference
class ActivitiesTracker {
private val createdActivities = mutableListOf<WeakReference<Activity>>()
fun <T : Any> Single<T>.withProgress(progress: MutableState<Boolean>): Single<T> = this
.doOnSubscribe { progress.value = true }
.doFinally { progress.value = false }
fun Completable.withProgress(progress: MutableState<Boolean>): Completable = this
.doOnSubscribe { progress.value = true }
.doFinally { progress.value = false }
fun <T : Any> Observable<T>.withProgress(progress: RxValueMutable<Boolean>): Observable<T> = this
@Andrew0000
Andrew0000 / PhotoPick.kt
Created April 20, 2023 08:17
Android: Pick photo from gallery + read by uri without extra permissions
fun launch() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, 2)
}
//TODO use registerForActivityResult() as modern approach
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@Andrew0000
Andrew0000 / LockedObj.kt
Created March 10, 2023 11:27
LockedObj
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
class LockedObj<T>(
/**
* Use it directly only in [withLock]
*/
@Andrew0000
Andrew0000 / DebugAssert.kt
Last active October 13, 2023 13:14
DebugAssert
@Suppress("Unused")
object DebugAssert {
fun ensure(predicate: () -> Boolean) {
ensure(predicate, null)
}
fun ensure(predicate: () -> Boolean, description: String? = null) {
if (BuildConfig.DEBUG && !predicate()) {