This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Singleton | |
| class NearbyUsersRepository @Inject constructor( | |
| nearbyUsersDataSource: NearbyUsersDataSource | |
| ) { | |
| val locations get() = nearbyUsersDataSource.locations | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| suspend fun requestLocationPermissions(): LocationFetcher.PermissionStatus | |
| suspend fun requestEnableLocationSettings(): LocationFetcher.SettingsStatus |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyActivity : AppCompatActivity() { | |
| private val locationFetcher: LocationFetcher by lazy { | |
| LocationFetcher.create(this) | |
| } | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| with (locationFetcher) { | |
| location | |
| .onEach { /* Location received */ } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |