UpdatesState.kt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package eu.kanade.presentation.updates
  2. import androidx.compose.runtime.Stable
  3. import androidx.compose.runtime.derivedStateOf
  4. import androidx.compose.runtime.getValue
  5. import androidx.compose.runtime.mutableStateOf
  6. import androidx.compose.runtime.setValue
  7. import eu.kanade.core.util.insertSeparators
  8. import eu.kanade.tachiyomi.ui.recent.updates.UpdatesItem
  9. import eu.kanade.tachiyomi.ui.recent.updates.UpdatesPresenter
  10. import eu.kanade.tachiyomi.util.lang.toDateKey
  11. import java.util.Date
  12. @Stable
  13. interface UpdatesState {
  14. val isLoading: Boolean
  15. val items: List<UpdatesItem>
  16. val selected: List<UpdatesItem>
  17. val selectionMode: Boolean
  18. val uiModels: List<UpdatesUiModel>
  19. var dialog: UpdatesPresenter.Dialog?
  20. }
  21. fun UpdatesState(): UpdatesState = UpdatesStateImpl()
  22. class UpdatesStateImpl : UpdatesState {
  23. override var isLoading: Boolean by mutableStateOf(true)
  24. override var items: List<UpdatesItem> by mutableStateOf(emptyList())
  25. override val selected: List<UpdatesItem> by derivedStateOf {
  26. items.filter { it.selected }
  27. }
  28. override val selectionMode: Boolean by derivedStateOf { selected.isNotEmpty() }
  29. override val uiModels: List<UpdatesUiModel> by derivedStateOf {
  30. items.toUpdateUiModel()
  31. }
  32. override var dialog: UpdatesPresenter.Dialog? by mutableStateOf(null)
  33. }
  34. fun List<UpdatesItem>.toUpdateUiModel(): List<UpdatesUiModel> {
  35. return this.map {
  36. UpdatesUiModel.Item(it)
  37. }
  38. .insertSeparators { before, after ->
  39. val beforeDate = before?.item?.update?.dateFetch?.toDateKey() ?: Date(0)
  40. val afterDate = after?.item?.update?.dateFetch?.toDateKey() ?: Date(0)
  41. when {
  42. beforeDate.time != afterDate.time && afterDate.time != 0L ->
  43. UpdatesUiModel.Header(afterDate)
  44. // Return null to avoid adding a separator between two items.
  45. else -> null
  46. }
  47. }
  48. }