Skip to content

Instantly share code, notes, and snippets.

View psteiger's full-sized avatar
🎯
Focusing

Patrick Steiger psteiger

🎯
Focusing
View GitHub Profile
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
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
@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 */ }
@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-api.kt
Created November 20, 2020 03:24
LocationFetcher library API
suspend fun requestLocationPermissions(): LocationFetcher.PermissionStatus
suspend fun requestEnableLocationSettings(): LocationFetcher.SettingsStatus
@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 / 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
}
class NearbyUsersViewModel @ViewModelInject constructor(
nearbyUsersRepository: NearbyUsersRepository
) : ViewModel() {
val locations get() = nearbyUsersRepository.locations
}
@AndroidEntryPoint
class NearbyUsersActivity : AppCompatActivity() {
private val viewModel: NearbyUsersViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
viewModel.locations.observe(this) { state: State ->
// Update views with the data.
}
}
@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)
private fun GeoQuery.asFlow() = callbackFlow {