daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data class ScheduleCell(val info: ScheduleInfo, val categoryGroups: List<RecyclerItem>)
  2.  
  3. data class ScheduleDate(val dayOfMonth: Int, val dayOfWeek: String, val month: String)
  4.  
  5. data class ScheduleInfo(val discipline: String, val venue: String)
  6.  
  7. data class ScheduleCategoryGroup(val category: CompetitionCategory?, val program: CompetitionProgram?)
  8.  
  9. data class ScheduleCompetitionTime(val minStartTime: Long, val maxEndTime: Long)
  10.  
  11. fun Schedule.toScheduleInfo() = ScheduleInfo(discipline!!.name, venue!!.name)
  12.  
  13. fun Schedule.toScheduleCategoryGroup() = ScheduleCategoryGroup(category, program)
  14.  
  15. fun List<Schedule>.toScheduleCompetitionTime(): ScheduleCompetitionTime {
  16.     var minStartTime = Long.MAX_VALUE
  17.     var maxEndTime = 0L
  18.     forEach { schedule ->
  19.         if (minStartTime > schedule.startTime) minStartTime = schedule.startTime
  20.         if (maxEndTime < schedule.finishTime) maxEndTime = schedule.finishTime
  21.     }
  22.     return ScheduleCompetitionTime(minStartTime, maxEndTime)
  23. }
  24.  
  25. fun List<Schedule>.toGroupedDisciplines(): Map<ScheduleInfo, Map<ScheduleDate, List<Schedule>>> {
  26.     val sports = asSequence()
  27.             .filter { filter -> filter.name.isEmpty() && filter.discipline != null }
  28.             .sortedWith(compareBy { it.discipline!!.name })
  29.             .groupBy { group -> group.toScheduleInfo() }
  30.  
  31.     val groupedDiscipline =
  32.             mutableMapOf<ScheduleInfo, Map<ScheduleDate, List<Schedule>>>()
  33.     sports.forEach { sport ->
  34.         groupedDiscipline[sport.key] = sport.value.groupBy {
  35.             it.startTime.toScheduleDate()
  36.         }
  37.     }
  38.     return groupedDiscipline
  39. }
  40.  
  41. fun Map<ScheduleInfo, Map<ScheduleDate, List<Schedule>>>.toScheduleCells() =
  42.         map {
  43.     ScheduleCell(it.key, it.value.toGroupedCategoryItems())
  44. }
  45.  
  46. fun Map<ScheduleDate, List<Schedule>>.toGroupedCategoryItems(): List<RecyclerItem> {
  47.     val items = mutableListOf<RecyclerItem>()
  48.  
  49.     forEach { map ->
  50.         items.add(ScheduleDateItem(map.key))
  51.         val groupMap =
  52.                 map.value.asSequence()
  53.                         .sortedWith(compareBy { it.startTime })
  54.                         .groupBy { it.toScheduleCategoryGroup() }
  55.         groupMap.forEach { group ->
  56.             val category = group.key
  57.             if (category.category != null && category.program != null) {
  58.                 val value = group.value
  59.                 items.add(ScheduleCategoryWithProgramItem(
  60.                         ScheduleDisciplineItem.ScheduleCategoryType.HEADER,
  61.                         value[0],
  62.                         value.toScheduleCompetitionTime()))
  63.                 items.addAll(
  64.                         ScheduleCategoryWithProgramItem.generateItem(
  65.                                 ScheduleDisciplineItem.ScheduleCategoryType.ROW,
  66.                                 value))
  67.             } else {
  68.                 items.addAll(ScheduleCategoryItem.generateItems(group.value))
  69.             }
  70.         }
  71.     }
  72.  
  73.     return items
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top