Skip to content

Instantly share code, notes, and snippets.

View marcellogalhardo's full-sized avatar
🇧🇷
Gradle is building...

Marcello Galhardo marcellogalhardo

🇧🇷
Gradle is building...
View GitHub Profile
@marcellogalhardo
marcellogalhardo / rememberRetained.kt
Last active April 23, 2024 13:53
rememberRetained
@Composable
internal fun <T> rememberRetained(
owner: ViewModelStoreOwner = LocalViewModelStoreOwner.current!!
key: String? = null,
block: @DisallowComposableCalls CreationExtras.() -> T,
): T {
val wrapperViewModel = viewModel(owner, key) { WrapperViewModel(block()) }
return remember(owner, key) { wrapperViewModel.instance }
}
@marcellogalhardo
marcellogalhardo / MutableProviderImpl.kt
Created February 25, 2023 16:14
Simple dependency providers primitives.
package provider
internal class MutableProviderImpl<T>(
scope: ProviderScope? = null,
private val factory: InstanceFactory<T>? = null,
) : ReadWriteProvider<T> {
init {
require(scope is ProviderScopeImpl)
scope.listeners.add { value = null }
@marcellogalhardo
marcellogalhardo / LogClient.kt
Last active February 25, 2023 15:59
Helper functions to make logging easier.
package logcat
public object LogClient {
private var isInitialized: Boolean = false
@PublishedApi
internal var isEnabled: Boolean = false
@PublishedApi
@marcellogalhardo
marcellogalhardo / CoroutineResult.kt
Created February 3, 2023 16:58
Handle exceptions like a pro with Coroutines!
package coroutines
/**
* Calls the specified function [block] and returns its encapsulated result if invocation was
* successful, catching any [Throwable] exception that was thrown from the block function execution
* and encapsulating it as a failure.
*
* Unlike [runCatching], [suspendRunCatching] does not break structured concurrency by rethrowing
* any [CancellationException].
*
@marcellogalhardo
marcellogalhardo / SavedStateHandle.kt
Last active January 10, 2023 15:44
Helper functions to manually create your own SavedStateHandle with ease.
fun ComponentActivity.lazySavedStateHandle(
key: String = this::class.java.canonicalName,
defaultState: Bundle? = intent?.extras,
): Lazy<SavedStateHandle> = lazy(LazyThreadSafetyMode.NONE) {
SavedStateHandle(savedStateRegistry, key, defaultState)
}
fun Fragment.lazySavedStateHandle(
key: String = this::class.java.canonicalName,
defaultState: Bundle? = arguments,
import kotlinx.coroutines.CancellationException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/**
* A discriminated union that encapsulates a successful outcome with a value of type [T]
* or a failure with an arbitrary [Throwable] exception.
*
* While using Kotlin's [Result] with [runCatching] method is very simple, there are some flaws:
@marcellogalhardo
marcellogalhardo / PermissionState.kt
Last active March 18, 2024 15:36
A abstraction to handle permissions in a Compose-way.
import android.app.Activity
import android.content.pm.PackageManager
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@marcellogalhardo
marcellogalhardo / GuardClauses.kt
Last active February 19, 2022 16:42
The missing guard clauses for Kotlin. A simple and elegant way to guard against invariants.
/**
* Invokes [otherwise] if the [value] is false.
*/
@OptIn(ExperimentalContracts::class)
inline fun guard(value: Boolean, otherwise: () -> Unit) {
contract {
returns() implies value
}
if (!value) otherwise()
@marcellogalhardo
marcellogalhardo / gradle.properties
Created October 26, 2021 08:59
A simple Android Gradle Properties with all configurations I use.
# Android
android.enableJetifier=false
android.nonTransitiveRClass=true
android.useAndroidX=true
# Android Experimental
android.enableR8.fullMode=true
android.experimental.cacheCompileLibResources=true
android.experimental.enableSourceSetPathsMap=true
# Build Features: https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/BuildFeatures
android.defaults.buildfeatures.aidl=true
@marcellogalhardo
marcellogalhardo / FragmentManager.kt
Last active June 3, 2021 07:24
Convenience FragmentManager extension functions.
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.FragmentManager
/**
* Create a new instance of a [Fragment] with the given class name. This uses
* [FragmentFactory.loadFragmentClass] and the empty constructor of the resulting
* [Class] by default.
*