This file contains 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
import { ageSelector, regionSelector, isEligibleSelector} from './CoolModuleSelectors.js'; | |
(...) | |
const mapStateToProps = state => ({ | |
age: ageSelector(state), | |
region: regionSelector(state), | |
isEligible: isEligibleSelector(state), | |
}); |
This file contains 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
(...) | |
import { createSelector } from 'reselect'; | |
const birthDateSelector = state => state.user.birthdate; | |
const countrySelector = state => state.user.location.country; | |
export const ageSelector = createSelector( | |
birthDateSelector, | |
dateString => moment().year() - moment(dateString).year(), | |
); |
This file contains 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
const mapStateToProps = state => ({ | |
birthDate: state.user.birthdate, | |
country: state.user.location.country, | |
}); |
This file contains 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 <T> Task<T>.await(): T = suspendCoroutine { continuation -> | |
addOnCompleteListener { task -> | |
when { | |
(task.result as QuerySnapshot).metadata.isFromCache -> | |
continuation.resumeWithException(Exception("without internet")) | |
task.isSuccessful -> continuation.resume(task.result) | |
else -> continuation.resumeWithException(task.exception!!) | |
} | |
} | |
} |
This file contains 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 getAllPets(): List<Pet> { | |
val result = firestoreDataBase.collection("pets").get().await() | |
return setPetsFromSnapshotIntoMutableList(result) | |
} |
This file contains 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
fun getAllPets(): LiveData<List<Pet>> { | |
var pets: MutableLiveData<List<Pet>> = MutableLiveData() | |
firestoreDataBase.collection("pets").get().addOnSuccessListener { | |
pets.value = setPetsFromSnapshotIntoMutableList(it) | |
} | |
return pets | |
} |
This file contains 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 syncPets() { | |
val pets = firestoreWrapper.getAllPets() | |
appDataBase.petDao().deleteAllPets() | |
pets.forEach { | |
appDataBase.petDao().insert(it) | |
} | |
} |
This file contains 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
fun syncPets(): LiveData<List<Pet>> { | |
liveDataMerger.addSource(firestoreWrapper.getAllPets()) { | |
value -> liveDataMerger.value = value | |
appDataBase.petDao().deleteAllPets() | |
value?.forEach { | |
appDataBase.petDao().insert(it) | |
} | |
} | |
return liveDataMerger | |
} |
This file contains 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 syncPets(): String { | |
var message = "Congratulations!!" | |
try { | |
async { repository.syncPets() }.await() | |
} catch (e: Exception) { | |
message = e.message!! | |
} | |
return message | |
} |
This file contains 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 syncPets() = repository.syncPets() |
NewerOlder