Skip to content

Instantly share code, notes, and snippets.

View psteiger's full-sized avatar
🎯
Focusing

Patrick Steiger psteiger

🎯
Focusing
View GitHub Profile
@psteiger
psteiger / livedata-endtoend-repository.kt
Last active November 21, 2020 12:47
LiveData End-to-End Repository
@Singleton
class NearbyUsersRepository @Inject constructor(
nearbyUsersDataSource: NearbyUsersDataSource
) {
val locations get() = nearbyUsersDataSource.locations
}
@psteiger
psteiger / livedata-endtoend-datasource.kt
Last active November 21, 2020 12:47
A Data Source using LiveData.
@Singleton
class NearbyUsersDataSource @Inject constructor() {
// Ideally, those should be constructor-injected.
val geoFire = GeoFire(FirebaseDatabase.getInstance().getReference("geofire"))
val geoLocation = GeoLocation(0.0, 0.0)
val radius = 100.0
val geoQuery = geoFire.queryAtLocation(geoLocation, radius)
// Listener for receiving GeoLocations
@psteiger
psteiger / locationfetcher-api.kt
Created November 20, 2020 03:24
LocationFetcher library API
suspend fun requestLocationPermissions(): LocationFetcher.PermissionStatus
suspend fun requestEnableLocationSettings(): LocationFetcher.SettingsStatus
@psteiger
psteiger / locationfetcher-config.kt
Created November 20, 2020 03:23
LocationFetcher library configuration.
LocationFetcher.create(this) {
fastestInterval = 5000
interval = 15000
maxWaitTime = 100000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
smallestDisplacement = 50f
providers = listOf(
LocationRequest.Provider.GPS,
LocationRequest.Provider.Network,
LocationRequest.Provider.Fused
@psteiger
psteiger / locationfetcher-example.kt
Last active November 20, 2020 03:49
LocationFetcher library usage example
class MyActivity : AppCompatActivity() {
private val locationFetcher: LocationFetcher by lazy {
LocationFetcher.create(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
with (locationFetcher) {
location
.onEach { /* Location received */ }
sealed class Resource<out T> {
data class Success<out T>(val data: T) : Resource<T>()
data class Loading<out T>(val partialData: T? = null) : Resource<T>()
data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>()
}
val Resource<T>.extractData: T? get() = when (this) {
is Success -> data
package com.faztudo.common.livedata
import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
import com.faztudo.App
import com.faztudo.INearbyUsersUseCases
import com.faztudo.common.helpers.logd
import com.faztudo.common.helpers.rootRef
import com.faztudo.common.helpers.sortedByDistance
import com.faztudo.common.helpers.usersRef
@psteiger
psteiger / a.kt
Last active September 5, 2019 15:51
open class FirebaseResourceLiveData(ref: DatabaseReference) : LiveData<Resource<DataSnapshot>> {
init {
value = Resource.Loading() // setValue() of LiveData
}
private val listener = MyValueEventListener()
override fun onActive() { // we have observers!
value = Resource.Loading(value?.extractData)
query.addValueEventListener(listener)
sealed class Resource<out T> {
data class Success<out T>(val data: T) : Resource<T>()
data class Loading<out T>(val partialData: T? = null) : Resource<T>()
data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>()
val extractData: T? get() = when (this) {
is Success -> data
is Loading -> partialData
is Failure -> null
inline fun <T> Resource<List<T>>.filterResource(predicate: (T) -> Boolean): Resource<List<T>> = try {
when (this) {
is Resource.Success<List<T>> -> Resource.Success<List<T>>(data.filter(predicate))
is Resource.Loading<List<T>> -> Resource.Loading<List<T>>(partialData?.filter(predicate))
is Resource.Failure<List<T>> -> Resource.Failure<List<T>>(throwable)
}
} catch (e: Throwable) {
Resource.Failure<List<T>>(e)
}