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 / KotlinResult.kt
Last active April 9, 2021 07:45
A Result class with Public API compatible to kotlin.Result for a easy migration
import java.io.Serializable
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.
*/
public sealed class Result<out T> : Serializable {
@marcellogalhardo
marcellogalhardo / LifecycleCollect.kt
Last active June 3, 2021 08:46
Utility functions to collect coroutines Flow respecting Android's Lifecycle.
package dev.marcellogalhardo.lifecycle
import android.app.Activity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
package com.marcellogalhardo.viewmodel.factory
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider
/**
* A [ViewModelProvider.Factory] that can hold a ViewModel [producer] function
package com.marcellogalhardo.weak
import java.lang.ref.WeakReference
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Creates a new instance of the [WeakDelegate] for better Kotlin
* syntax using the [referent] parameter as an initial reference.
*
package com.marcellogalhardo.AutoClearedValue
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.observe
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
interface SavedStateViewModelFactory {
fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T
}
private fun SavedStateViewModelFactory.asViewModelProviderFactory(
owner: SavedStateRegistryOwner
): ViewModelProvider.Factory {
return object : AbstractSavedStateViewModelFactory(owner, owner.arguments) {
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
return [email protected](key, modelClass, handle)
@marcellogalhardo
marcellogalhardo / Coordinator.kt
Last active April 29, 2020 19:10
Coordinator base class.
package com.marcellogalhardo.coordinator
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.SavedStateHandle
@marcellogalhardo
marcellogalhardo / ActivityScope.kt
Last active January 20, 2021 10:11
Scoping Dagger Components with ViewModels
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@marcellogalhardo
marcellogalhardo / Lce.kt
Last active December 20, 2018 10:46
The [Lce] interface and its subclasses are a form of container to help handling with loading, data fetch, and error handling on Streams. This class was designed as an Immutable object that contains the data and status of each Stream operation.
/**
* The [Lce] interface and its subclasses are a form of container
* to help handling with loading, data fetch, and error handling
* on Streams. This class was designed as an Immutable object that contains
* the data and status of each Stream operation.
*/
interface Lce<T> {
fun getData(): T?