Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. package com.calendarApp.weekTab
  2.  
  3. import androidx.lifecycle.LiveData
  4. import androidx.lifecycle.MutableLiveData
  5. import androidx.lifecycle.Transformations
  6. import com.calendarApp.common.roomDb.EventItemDao
  7. import com.calendarApp.common.utils.toLocalDateTime
  8. import com.google.gson.Gson
  9. import com.paytion.coreLib.common.DayItem
  10. import com.paytion.coreLib.common.Errors
  11. import com.paytion.coreLib.common.EventItem
  12. import com.paytion.coreLib.network.CoreApi
  13. import com.paytion.coreLib.network.Result
  14. import kotlinx.coroutines.GlobalScope
  15. import kotlinx.coroutines.launch
  16. import org.threeten.bp.LocalDateTime
  17. import org.threeten.bp.ZoneOffset
  18.  
  19. class WeekInteractor(val api: CoreApi, val dao: EventItemDao) {
  20.  
  21.  
  22.  
  23. val periodLiveData: MutableLiveData <Pair<LocalDateTime, LocalDateTime>> = MutableLiveData()
  24. var liveData: LiveData<Result<MutableList<DayItem>>> = Transformations.switchMap(periodLiveData) { pair ->
  25. updateData(pair.first, pair.second)
  26. val daoData = dao.getEventsForPeriod(pair.first.toEpochSecond(ZoneOffset.UTC), pair.second.toEpochSecond(ZoneOffset.UTC))
  27. return@switchMap Transformations.switchMap(daoData){ it->
  28. val d = MutableLiveData<Result<MutableList<DayItem>>>()
  29. d.value = Result.Success(mapEventsInDays(pair.first, pair.second, it))
  30. d
  31. }
  32. }
  33.  
  34. fun getData(): LiveData<Result<MutableList<DayItem>>> {
  35. return liveData
  36. }
  37.  
  38. fun updateData(dateFrom: LocalDateTime, dateTo: LocalDateTime) {
  39. val result = api.getEvents(dateFrom.toString(), dateTo.toString())
  40. GlobalScope.launch {
  41. try {
  42. val response = result.await()
  43. if (response.isSuccessful) {
  44. response.body()?.let {
  45. val list = it.map { item ->
  46. item.getEventItem()
  47. }
  48. dao.insert(list)
  49. }
  50. } else if (response.code() in 400..500) {
  51. val errors = Gson().fromJson(response.errorBody()?.string(), Errors::class.java)
  52. (liveData as MutableLiveData).postValue(Result.Error(null, errors.errors?.firstOrNull()?.errorMessage))
  53. }
  54.  
  55. } catch (exception: Exception) {
  56. (liveData as MutableLiveData).postValue(Result.Error(exception))
  57. }
  58. }
  59. }
  60.  
  61. private fun mapEventsInDays(startTime: LocalDateTime, endTime: LocalDateTime, events: List<EventItem>): MutableList<DayItem> {
  62. val dayList = emptyList<DayItem>().toMutableList()
  63. val dateList= emptyList<LocalDateTime>().toMutableList()
  64. var start = startTime
  65. var end = endTime
  66. end = end.plusDays(1)
  67. while (!start.toLocalDate().isEqual(end.toLocalDate())){
  68. dateList.add(start)
  69. start = start.plusDays(1)
  70. }
  71. val dayMap:MutableMap<Long, List<EventItem>> = HashMap()
  72. dateList.forEach { dayMap[it.toLocalDate().toEpochDay()] = emptyList() }
  73. events.groupBy {
  74. it.dateFrom.toLocalDateTime().toLocalDate().toEpochDay()
  75. }.entries.forEach {
  76. dayMap[it.key] = it.value
  77. }
  78. dayMap.forEach{
  79. dayList.add(DayItem(it.value, it.key))
  80. }
  81. return dayList.sortedBy { it.date }.toMutableList()
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement