Skip to content
Snippets Groups Projects
Commit 353becb9 authored by Taras's avatar Taras
Browse files

Use filter datasource on select timelines screen

parent 5a607082
No related branches found
No related tags found
No related merge requests found
......@@ -8,40 +8,35 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.update
import org.futo.circles.core.extensions.createResult
import org.futo.circles.core.extensions.getOrThrow
import org.futo.circles.core.model.toFilterTimelinesListItem
import org.futo.circles.core.provider.MatrixSessionProvider
import org.futo.circles.core.utils.getTimelineRoomFor
import org.matrix.android.sdk.api.session.getRoom
import javax.inject.Inject
class FilterTimelinesDataSource @Inject constructor(
savedStateHandle: SavedStateHandle
savedStateHandle: SavedStateHandle,
private val circleFilterAccountDataManager: CircleFilterAccountDataManager
) {
private val circleId: String = savedStateHandle.getOrThrow("circleId")
private val session = MatrixSessionProvider.getSessionOrThrow()
val circleSummaryLiveData = session.roomService().getRoomSummaryLive(circleId)
private val selectedTimelinesIds = MutableStateFlow(getCircleFilter())
private val selectedTimelinesIds =
MutableStateFlow(circleFilterAccountDataManager.getCircleFilter(circleId))
val timelinesLiveData = combine(
circleSummaryLiveData.asFlow(),
selectedTimelinesIds
) { _, selectedIds ->
getAllTimelinesIds().mapNotNull {
circleFilterAccountDataManager.getAllTimelinesIds(circleId).mapNotNull {
session.getRoom(it)?.roomSummary()?.toFilterTimelinesListItem(selectedIds.contains(it))
}
}.flowOn(Dispatchers.IO).asLiveData()
suspend fun applyFilter() = createResult {
session.getRoom(circleId)?.roomAccountDataService()
?.updateAccountData(
CIRCLE_FILTER_EVENT_TYPE,
mapOf(TIMELINES_KEY to selectedTimelinesIds.value)
)
}
suspend fun applyFilter() =
circleFilterAccountDataManager.updateFilter(circleId, selectedTimelinesIds.value)
fun toggleItemSelected(roomId: String) {
val isItemSelected = selectedTimelinesIds.value.contains(roomId)
......@@ -54,30 +49,7 @@ class FilterTimelinesDataSource @Inject constructor(
}
fun selectAllTimelines() {
selectedTimelinesIds.update { getAllTimelinesIds() }
}
private fun getCircleFilter(): Set<String> {
val content = session.getRoom(circleId)?.roomAccountDataService()
?.getAccountDataEvent(CIRCLE_FILTER_EVENT_TYPE)?.content ?: return getAllTimelinesIds()
return (content[TIMELINES_KEY] as? List<*>)?.map { it.toString() }?.toSet()
?: getAllTimelinesIds()
selectedTimelinesIds.update { circleFilterAccountDataManager.getAllTimelinesIds(circleId) }
}
private fun getAllTimelinesIds(): Set<String> {
val children = session.getRoom(circleId)?.roomSummary()?.spaceChildren ?: emptyList()
val myTimelineId = getTimelineRoomFor(circleId)?.roomId
return children.mapNotNull {
val timelineSummary =
session.getRoom(it.childRoomId)?.roomSummary()?.takeIf { summary ->
summary.membership.isActive() && summary.roomId != myTimelineId
}
timelineSummary?.roomId
}.toSet()
}
companion object {
private const val CIRCLE_FILTER_EVENT_TYPE = "m.circle.filter"
private const val TIMELINES_KEY = "timelines"
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment