Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package by.citech.zwimming.appandroid.domain.swim
- import androidx.lifecycle.MutableLiveData
- import by.citech.zwimming.appandroid.data.database.DatabaseAgent
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.Dispatchers.IO
- import kotlinx.coroutines.launch
- typealias Z = List<SwimDownloadDomain>
- class SwimDownloadRepositoryImpl2 {
- /** первый раз берем с бд, потом с сервера**/
- private val swims = MutableLiveData<List<SwimDownloadDomain>>()
- private var maxLastChangeTs = 0L
- fun getAllSwims() = swims
- fun update1() {
- CoroutineScope(IO).launch {
- var currList = swims.value
- if (currList == null) {
- currList = DatabaseAgent.api.swimDownloadDao().getAllSwims().map { it.convertToSwimUploadDomain() }
- val maxLastChangeTsFromDb = System.currentTimeMillis()
- maxLastChangeTs = maxLastChangeTsFromDb
- }
- val fetched = Net.downloadSwims(maxLastChangeTs)
- if (fetched.isNotEmpty()) {
- val maxLastChangeTsFromNet = System.currentTimeMillis()
- if (maxLastChangeTsFromNet > maxLastChangeTs)
- maxLastChangeTs = maxLastChangeTsFromNet
- }
- val newList = mergeSwims(currList, fetched)
- DatabaseAgent.api.swimDownloadDao().saveSwims(newList.map { })
- swims.postValue(newList)
- }
- }
- private fun mergeSwims(currList: Z?, newList: Z): Z? {
- val mergedList: Z? = null
- return mergedList
- }
- object Net {
- fun downloadSwims(fromTs: Long) = listOf<SwimDownloadDomain>()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement