Skip to content

Instantly share code, notes, and snippets.

View vitoksmile's full-sized avatar

Victor Mykhailiv vitoksmile

View GitHub Profile
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* getValue returns only once the exact value, in other cases [default] value will be returned
*/
private class OneShotFieldDelegate<T : Any?>(
private val default: T,
) : ReadWriteProperty<Any?, T> {
@vitoksmile
vitoksmile / LifecycleEventBus.kt
Created June 19, 2019 10:18
LifecycleEventBus - simplifies the communication between components with LifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import java.util.WeakHashMap
/**
* LifecycleEventBus - simplifies the communication between components with LifecycleOwner
*/
object LifecycleEventBus {
@vitoksmile
vitoksmile / FirestoreLiveData.kt
Last active June 22, 2019 09:13
Custom LiveData to works with FirebaseFirestore snapshots
import androidx.lifecycle.LiveData
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ListenerRegistration
import com.google.firebase.firestore.QuerySnapshot
/**
* Custom [LiveData] to works with [FirebaseFirestore] snapshots
*
* @param collection The current collection's name
@vitoksmile
vitoksmile / BufferedLiveData.kt
Last active June 18, 2019 06:54
LiveData to hold not observed values to notify UI about these values after appearance of active observers
/**
* Hold not observed values to notify UI about these values after appearance of active observers
*/
class BufferedLiveData<T> : LiveData<T>() {
/**
* Object to synchronize adding values to buffer in #postValue
*/
private val lock = Any()
@vitoksmile
vitoksmile / CoolJob.kt
Last active December 11, 2018 15:25
Wrapper over Coroutine Job to catch exception
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
/**
* Launch new block in coroutine and invoke callbacks after execution
*
* @param onComplete Called when launch was success
* @param onError Called when launch had an exception
* @param onFinally Called after launch (ignore result)
@vitoksmile
vitoksmile / DeferredX.kt
Created November 1, 2018 07:13
Extension to Deferred to await values without need catch Exception
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
/**
* Coroutine dispatcher that is confined to the Main thread operating with UI objects
*/
val UI = Dispatchers.Main
/**
@vitoksmile
vitoksmile / KoinPropertyDelegate.kt
Last active October 26, 2018 11:11
Saving values to Koin's property with delegating
import org.koin.standalone.KoinComponent
import org.koin.standalone.getKoin
import kotlin.reflect.KProperty
class KoinPropertyDelegate<R, T : Any>(private val key: String, private val default: T) :
KoinComponent {
operator fun getValue(thisRef: R, property: KProperty<*>): T {
return getKoin().getProperty<Any>(key, default) as T
}
@vitoksmile
vitoksmile / KoinUtils.kt
Created October 26, 2018 10:11
Using dependency injections by Koin without implementing KoinComponent
import org.koin.core.KoinContext
import org.koin.core.parameter.ParameterDefinition
import org.koin.core.parameter.emptyParameterDefinition
import org.koin.core.scope.Scope
import org.koin.standalone.StandAloneContext
/**
* inject lazily given dependency
* @param name - bean canonicalName
* @param parameters - injection parameters