Skip to content

Instantly share code, notes, and snippets.

View mochadwi's full-sized avatar
💭
I may be slow to respond.

Mochamad Iqbal Dwi Cahyo mochadwi

💭
I may be slow to respond.
View GitHub Profile
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@qamarelsafadi
qamarelsafadi / OnBackPressedAlternativeWay.kt
Last active April 4, 2023 10:09
OnBackPressed Alternative way for fragments and activities
/* ----------------------------------------- Activity ------------------------------------------ */
/* make sure you have at least 'androidx.activity:activity-ktx:1.6.0-rc01' at your dependencies
(just to let you know this dependency is not stable yet )
*/
implementation 'androidx.activity:activity-ktx:1.6.0-rc01'
fun AppCompatActivity.onBackPressed(isEnabled: Boolean, callback: () -> Unit) {
onBackPressedDispatcher.addCallback(this,
@hrach
hrach / SavedMutableStateFlow.kt
Created November 1, 2021 13:04
savedMutableStateFlow
fun <T> ViewModel<*>.savedMutableStateFlow(
initialValue: T,
key: String? = null,
): ReadOnlyProperty<ViewModel<*>, MutableStateFlow<T>> {
var mutableStateFlow: MutableStateFlow<T>? = null
return ReadOnlyProperty { _, property ->
if (mutableStateFlow != null) {
return@ReadOnlyProperty mutableStateFlow!!
}
@EugeneTheDev
EugeneTheDev / DotsLoaders.kt
Created March 18, 2021 23:15
Dots loading animations with Jetpack Compose
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@gmk57
gmk57 / Sending events to UI.kt
Last active February 13, 2024 15:17
Sending events to UI with Channel/Flow + custom collector (see my first comment for reasons behind it)
/**
* Starts collecting a flow when the lifecycle is started, and **cancels** the collection on stop.
* This is different from `lifecycleScope.launchWhenStarted { flow.collect{...} }`, in which case
* the coroutine is just suspended on stop.
*/
inline fun <reified T> Flow<T>.collectWhileStarted(
lifecycleOwner: LifecycleOwner,
noinline action: suspend (T) -> Unit
) {
object : DefaultLifecycleObserver {
@ThomasG77
ThomasG77 / README.md
Last active April 30, 2024 15:17
Recipe to get JSON using pagination using command line tools e.g curl, jq, bc, cat
@antoniolg
antoniolg / MainActivity.kt
Last active March 2, 2023 08:10
Shows how to use permissions with registerForActivityResult(). Code for https://devexperto.com/permisos-android-11
import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
@rharter
rharter / Scoped.kt
Last active August 9, 2022 14:58
A kotlin property delegate to scope any object to an android lifecycle. Blog post at https://ryanharter.com/blog/easy-android-scopes/
import androidx.lifecycle.get
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
/**
* Returns a property delegate to access the wrapped value, which will be retained for the
* duration of the lifecycle of this [ViewModelStoreOwner].
*
@minhcasi
minhcasi / Flutter Clean.md
Last active April 25, 2024 02:33
These are common issues on Flutter and solutions to fix

Quick Clean Cache

  1. Open android studio Tools->Flutter->Clean
  2. Go to File -> Invalidate Caches / Restart
  3. Or open terminal run "flutter clean"
  4. Remove pubspec.lock
  5. Double check the Flutter SDK Path config correcty - https://tppr.me/qn6dP

Or open the terminal and try this script:

flutter clean
@krossovochkin
krossovochkin / Error.kt
Created February 23, 2020 08:15
From RxJava to Kotlin Flow: Error Handling
package by.krossovochkin.testflow
import io.reactivex.Observable
import io.reactivex.exceptions.CompositeException
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import org.junit.Test
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.CountDownLatch