Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.calendarApp.weekTab
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.Transformations
- import com.calendarApp.common.roomDb.EventItemDao
- import com.calendarApp.common.utils.toLocalDateTime
- import com.google.gson.Gson
- import com.paytion.coreLib.common.DayItem
- import com.paytion.coreLib.common.Errors
- import com.paytion.coreLib.common.EventItem
- import com.paytion.coreLib.network.CoreApi
- import com.paytion.coreLib.network.Result
- import kotlinx.coroutines.GlobalScope
- import kotlinx.coroutines.launch
- import org.threeten.bp.LocalDateTime
- import org.threeten.bp.ZoneOffset
- class WeekInteractor(val api: CoreApi, val dao: EventItemDao) {
- val periodLiveData: MutableLiveData <Pair<LocalDateTime, LocalDateTime>> = MutableLiveData()
- var liveData: LiveData<Result<MutableList<DayItem>>> = Transformations.switchMap(periodLiveData) { pair ->
- updateData(pair.first, pair.second)
- val daoData = dao.getEventsForPeriod(pair.first.toEpochSecond(ZoneOffset.UTC), pair.second.toEpochSecond(ZoneOffset.UTC))
- return@switchMap Transformations.switchMap(daoData){ it->
- val d = MutableLiveData<Result<MutableList<DayItem>>>()
- d.value = Result.Success(mapEventsInDays(pair.first, pair.second, it))
- d
- }
- }
- fun getData(): LiveData<Result<MutableList<DayItem>>> {
- return liveData
- }
- fun updateData(dateFrom: LocalDateTime, dateTo: LocalDateTime) {
- val result = api.getEvents(dateFrom.toString(), dateTo.toString())
- GlobalScope.launch {
- try {
- val response = result.await()
- if (response.isSuccessful) {
- response.body()?.let {
- val list = it.map { item ->
- item.getEventItem()
- }
- dao.insert(list)
- }
- } else if (response.code() in 400..500) {
- val errors = Gson().fromJson(response.errorBody()?.string(), Errors::class.java)
- (liveData as MutableLiveData).postValue(Result.Error(null, errors.errors?.firstOrNull()?.errorMessage))
- }
- } catch (exception: Exception) {
- (liveData as MutableLiveData).postValue(Result.Error(exception))
- }
- }
- }
- private fun mapEventsInDays(startTime: LocalDateTime, endTime: LocalDateTime, events: List<EventItem>): MutableList<DayItem> {
- val dayList = emptyList<DayItem>().toMutableList()
- val dateList= emptyList<LocalDateTime>().toMutableList()
- var start = startTime
- var end = endTime
- end = end.plusDays(1)
- while (!start.toLocalDate().isEqual(end.toLocalDate())){
- dateList.add(start)
- start = start.plusDays(1)
- }
- val dayMap:MutableMap<Long, List<EventItem>> = HashMap()
- dateList.forEach { dayMap[it.toLocalDate().toEpochDay()] = emptyList() }
- events.groupBy {
- it.dateFrom.toLocalDateTime().toLocalDate().toEpochDay()
- }.entries.forEach {
- dayMap[it.key] = it.value
- }
- dayMap.forEach{
- dayList.add(DayItem(it.value, it.key))
- }
- return dayList.sortedBy { it.date }.toMutableList()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement