Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package by.citech.zwimming.appandroid.domain.swim
  2.  
  3. import androidx.lifecycle.MutableLiveData
  4. import by.citech.zwimming.appandroid.data.database.DatabaseAgent
  5. import kotlinx.coroutines.CoroutineScope
  6. import kotlinx.coroutines.Dispatchers.IO
  7. import kotlinx.coroutines.launch
  8.  
  9. typealias Z = List<SwimDownloadDomain>
  10.  
  11. class SwimDownloadRepositoryImpl2 {
  12.  
  13. /** первый раз берем с бд, потом с сервера**/
  14. private val swims = MutableLiveData<List<SwimDownloadDomain>>()
  15. private var maxLastChangeTs = 0L
  16.  
  17. fun getAllSwims() = swims
  18.  
  19. fun update1() {
  20. CoroutineScope(IO).launch {
  21. var currList = swims.value
  22. if (currList == null) {
  23. currList = DatabaseAgent.api.swimDownloadDao().getAllSwims().map { it.convertToSwimUploadDomain() }
  24. val maxLastChangeTsFromDb = System.currentTimeMillis()
  25. maxLastChangeTs = maxLastChangeTsFromDb
  26. }
  27. val fetched = Net.downloadSwims(maxLastChangeTs)
  28. if (fetched.isNotEmpty()) {
  29. val maxLastChangeTsFromNet = System.currentTimeMillis()
  30. if (maxLastChangeTsFromNet > maxLastChangeTs)
  31. maxLastChangeTs = maxLastChangeTsFromNet
  32. }
  33. val newList = mergeSwims(currList, fetched)
  34. DatabaseAgent.api.swimDownloadDao().saveSwims(newList.map { })
  35. swims.postValue(newList)
  36. }
  37. }
  38.  
  39. private fun mergeSwims(currList: Z?, newList: Z): Z? {
  40. val mergedList: Z? = null
  41. return mergedList
  42. }
  43.  
  44. object Net {
  45. fun downloadSwims(fromTs: Long) = listOf<SwimDownloadDomain>()
  46. }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement