Skip to content

Instantly share code, notes, and snippets.

View OKatrych's full-sized avatar
🇺🇦

Oleksandr Katrych OKatrych

🇺🇦
  • NordVPN
  • Warsaw, Poland
View GitHub Profile
@OKatrych
OKatrych / FlowExt.kt
Created March 28, 2024 13:25
Flow exponential backoff
fun <T> Flow<T>.retryWithPolicy(
retryPolicy: RetryPolicy,
predicate: suspend FlowCollector<T>.(cause: Throwable) -> Boolean
): Flow<T> {
var currentDelay = retryPolicy.delayMillis
val delayFactor = retryPolicy.delayFactor
return retryWhen { cause, attempt ->
if (predicate(cause) && attempt < retryPolicy.numRetries) {
delay(currentDelay)
currentDelay = (currentDelay * delayFactor).coerceAtMost(retryPolicy.maxDelayMillis)
@OKatrych
OKatrych / ViewModels.kt
Created September 20, 2023 19:22
Composable scoped viewmodel
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import timber.log.Timber
@OKatrych
OKatrych / VMScope.kt
Created April 17, 2023 13:35
Scope ViewModelStoreOwner to Composable lifecycle
@Composable
internal fun ProvidesViewModelStoreOwner(
ownerKey: String = rememberSaveable { UUID.randomUUID().toString() },
ownerHolder: CompositionScopedVMStoreOwnerHolder = CompositionScopedVMStoreOwnerHolder(),
content: @Composable () -> Unit,
) {
val context = LocalContext.current
remember {
object : RememberObserver {