Created
October 14, 2018 12:20
-
-
Save marcelpinto/6813f79ea14475311e67a0da023b2112 to your computer and use it in GitHub Desktop.
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
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 | |
) |
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 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