Skip to content

Instantly share code, notes, and snippets.

@marcelpinto
Created October 14, 2018 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelpinto/6813f79ea14475311e67a0da023b2112 to your computer and use it in GitHub Desktop.
Save marcelpinto/6813f79ea14475311e67a0da023b2112 to your computer and use it in GitHub Desktop.
data class HomePage(
val locationInfo: LocationInfo? = null,
val travelInfo: TravelInfo? = null,
val weddingLocation: WeddingLocation? = null,
val weddingInfo: WeddingInfo? = null,
val weddingDate: WeddingDate? = null,
val welcome: WelcomeItem? = null
)
class HomeRepo(userRepo: UserRepo, private val firebaseInstance: DatabaseReference) {
private val emptyLiveData: LiveData<Resource<HomePage>> by lazy {
MutableLiveData<Resource<HomePage>>().apply {
value = Resource.loading()
}
}
private var internalLiveData: LiveData<Resource<HomePage>>? = null
private lateinit var currentLanguage: String
private val homeLiveData: LiveData<Resource<HomePage>> = Transformations.switchMap(userRepo.getUser()) {
val language = it.data?.language.orEmpty()
when {
language.isBlank() -> emptyLiveData
internalLiveData == null || language != currentLanguage -> {
currentLanguage = language
FirebaseLiveData(
reference = firebaseInstance.child(PATH_HOME).child(currentLanguage),
resourceType = HomePage::class.java
).apply {
internalLiveData = this
}
}
else -> internalLiveData
}
}
fun getHomePage(): LiveData<Resource<HomePage>> {
return homeLiveData
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment