PollHistory : simplify so we only have one Node. Also enrich PollHistoryState.

This commit is contained in:
ganfra 2023-12-06 19:27:50 +01:00
parent 4a2cbb1ed4
commit aa9693126f
19 changed files with 376 additions and 255 deletions

View file

@ -16,6 +16,7 @@
package io.element.android.features.poll.impl.history
import io.element.android.features.poll.impl.history.model.PollHistoryFilter
import io.element.android.libraries.matrix.api.core.EventId
sealed interface PollHistoryEvents {
@ -23,4 +24,5 @@ sealed interface PollHistoryEvents {
data class PollAnswerSelected(val pollStartId: EventId, val answerId: String) : PollHistoryEvents
data class PollEndClicked(val pollStartId: EventId) : PollHistoryEvents
data object EditPoll : PollHistoryEvents
data class OnFilterSelected(val filter: PollHistoryFilter) : PollHistoryEvents
}

View file

@ -1,69 +0,0 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.poll.impl.history
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.bumble.appyx.core.lifecycle.subscribe
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.architecture.NodeInputs
import io.element.android.libraries.architecture.inputs
import io.element.android.libraries.di.RoomScope
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
import io.element.android.services.analytics.api.AnalyticsService
@ContributesNode(RoomScope::class)
class PollHistoryLoadedNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
presenterFactory: PollHistoryPresenter.Factory,
analyticsService: AnalyticsService,
) : Node(
buildContext = buildContext,
plugins = plugins
) {
data class Inputs(
val pollHistory: MatrixTimeline,
) : NodeInputs
private val inputs: Inputs = inputs()
private val presenter = presenterFactory.create(
inputs.pollHistory,
)
init {
lifecycle.subscribe(
onResume = {
// analyticsService.screen(MobileScreen(screenName = MobileScreen.ScreenName.CreatePollView)) // TODO
}
)
}
@Composable
override fun View(modifier: Modifier) {
PollHistoryView(
state = presenter.present(),
modifier = modifier,
goBack = this::navigateUp,
)
}
}

View file

@ -1,50 +0,0 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.poll.impl.history
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.designsystem.theme.components.CircularProgressIndicator
import io.element.android.libraries.di.RoomScope
@ContributesNode(RoomScope::class)
class PollHistoryLoadingNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
) : Node(
buildContext = buildContext,
plugins = plugins
) {
@Composable
override fun View(modifier: Modifier) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
CircularProgressIndicator()
}
}
}

View file

@ -16,96 +16,32 @@
package io.element.android.features.poll.impl.history
import android.os.Parcelable
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import com.bumble.appyx.core.composable.Children
import com.bumble.appyx.core.lifecycle.subscribe
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
import com.bumble.appyx.navmodel.backstack.BackStack
import com.bumble.appyx.navmodel.backstack.operation.newRoot
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.architecture.BackstackNode
import io.element.android.libraries.architecture.animation.rememberDefaultTransitionHandler
import io.element.android.libraries.architecture.createNode
import io.element.android.libraries.di.RoomScope
import io.element.android.libraries.matrix.api.room.MatrixRoom
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
@ContributesNode(RoomScope::class)
class PollHistoryNode @AssistedInject constructor(
private val room: MatrixRoom,
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
) : BackstackNode<PollHistoryNode.NavTarget>(
backstack = BackStack(
initialElement = NavTarget.PollHistoryLoading,
savedStateMap = buildContext.savedStateMap,
),
private val presenter: PollHistoryPresenter,
) : Node(
buildContext = buildContext,
plugins = plugins,
) {
sealed interface NavTarget : Parcelable {
@Parcelize
data object PollHistoryLoading : NavTarget
@Parcelize
data object PollHistoryLoaded : NavTarget
}
private var pollHistory: MatrixTimeline? = null
override fun onBuilt() {
super.onBuilt()
lifecycle.subscribe(
onCreate = {
lifecycleScope.launch {
runCatching {
room.pollHistory()
}.onSuccess {
pollHistory = it
backstack.newRoot(NavTarget.PollHistoryLoaded)
}
}
},
onDestroy = {
pollHistory?.close()
},
)
}
override fun resolve(
navTarget: NavTarget,
buildContext: BuildContext
): Node = when (navTarget) {
is NavTarget.PollHistoryLoading -> createNode<PollHistoryLoadingNode>(
buildContext = buildContext,
)
is NavTarget.PollHistoryLoaded -> {
createNode<PollHistoryLoadedNode>(
buildContext = buildContext,
plugins = listOf(
PollHistoryLoadedNode.Inputs(
pollHistory = pollHistory ?: error("Poll history not loaded"),
)
),
)
}
}
@Composable
override fun View(modifier: Modifier) {
Children(
navModel = backstack,
PollHistoryView(
state = presenter.present(),
modifier = modifier,
transitionHandler = rememberDefaultTransitionHandler(),
goBack = this::navigateUp,
)
}
}

View file

@ -19,53 +19,62 @@ package io.element.android.features.poll.impl.history
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import io.element.android.features.poll.api.actions.EndPollAction
import io.element.android.features.poll.api.actions.SendPollResponseAction
import io.element.android.features.poll.impl.history.model.PollHistoryFilter
import io.element.android.features.poll.impl.history.model.PollHistoryItems
import io.element.android.features.poll.impl.history.model.PollHistoryItemsFactory
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.matrix.api.room.MatrixRoom
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
import kotlinx.collections.immutable.toImmutableList
import io.element.android.libraries.matrix.ui.room.rememberPollHistory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
class PollHistoryPresenter @AssistedInject constructor(
@Assisted private val pollHistory: MatrixTimeline,
class PollHistoryPresenter @Inject constructor(
private val room: MatrixRoom,
private val appCoroutineScope: CoroutineScope,
private val sendPollResponseAction: SendPollResponseAction,
private val endPollAction: EndPollAction,
private val pollHistoryItemFactory: PollHistoryItemsFactory,
) : Presenter<PollHistoryState> {
@AssistedFactory
interface Factory {
fun create(
pollHistory: MatrixTimeline,
): PollHistoryPresenter
}
@Composable
override fun present(): PollHistoryState {
val pollHistory = room.rememberPollHistory()
val paginationState by pollHistory.paginationState.collectAsState()
val timelineItemsFlow = remember {
val pollHistoryItemsFlow = remember {
pollHistory.timelineItems.map { items ->
pollHistoryItemFactory.create(items)
}
}
val items by timelineItemsFlow.collectAsState(initial = emptyList())
LaunchedEffect(items.size) {
if (items.isEmpty()) loadMore()
var activeFilter by rememberSaveable {
mutableStateOf(PollHistoryFilter.ONGOING)
}
val pollHistoryItems by pollHistoryItemsFlow.collectAsState(initial = PollHistoryItems())
LaunchedEffect(paginationState, pollHistoryItems.size) {
if (pollHistoryItems.size == 0 && paginationState.canBackPaginate) loadMore(pollHistory)
}
val isLoading by remember {
derivedStateOf {
pollHistoryItems.size == 0 || paginationState.isBackPaginating
}
}
val coroutineScope = rememberCoroutineScope()
fun handleEvents(event: PollHistoryEvents) {
when (event) {
is PollHistoryEvents.LoadMore -> {
coroutineScope.loadMore()
coroutineScope.loadMore(pollHistory)
}
is PollHistoryEvents.PollAnswerSelected -> appCoroutineScope.launch {
sendPollResponseAction.execute(pollStartId = event.pollStartId, answerId = event.answerId)
@ -74,16 +83,31 @@ class PollHistoryPresenter @AssistedInject constructor(
endPollAction.execute(pollStartId = event.pollStartId)
}
PollHistoryEvents.EditPoll -> Unit
is PollHistoryEvents.OnFilterSelected -> {
activeFilter = event.filter
}
}
}
val currentItems by remember {
derivedStateOf {
when (activeFilter) {
PollHistoryFilter.ONGOING -> pollHistoryItems.ongoing
PollHistoryFilter.PAST -> pollHistoryItems.past
}
}
}
return PollHistoryState(
paginationState = paginationState,
pollItems = items.toImmutableList(),
isLoading = isLoading,
hasMoreToLoad = paginationState.hasMoreToLoadBackwards,
currentItems = currentItems,
activeFilter = activeFilter,
eventSink = ::handleEvents,
)
}
private fun CoroutineScope.loadMore() = launch {
pollHistory.paginateBackwards(20, 3)
private fun CoroutineScope.loadMore(pollHistory: MatrixTimeline) = launch {
Timber.d("LoadMore poll history")
pollHistory.paginateBackwards(50, 3)
}
}

View file

@ -16,11 +16,14 @@
package io.element.android.features.poll.impl.history
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
import io.element.android.features.poll.impl.history.model.PollHistoryFilter
import io.element.android.features.poll.impl.history.model.PollHistoryItem
import kotlinx.collections.immutable.ImmutableList
data class PollHistoryState(
val paginationState: MatrixTimeline.PaginationState,
val pollItems: ImmutableList<PollHistoryItem>,
val isLoading: Boolean,
val hasMoreToLoad: Boolean,
val activeFilter: PollHistoryFilter,
val currentItems: ImmutableList<PollHistoryItem>,
val eventSink: (PollHistoryEvents) -> Unit,
)

View file

@ -17,8 +17,48 @@
package io.element.android.features.poll.impl.history
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.features.poll.api.pollcontent.PollContentState
import io.element.android.features.poll.api.pollcontent.aPollContentState
import io.element.android.features.poll.impl.history.model.PollHistoryFilter
import io.element.android.features.poll.impl.history.model.PollHistoryItem
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
class PollHistoryStateProvider : PreviewParameterProvider<PollHistoryState> {
override val values: Sequence<PollHistoryState>
get() = sequenceOf() // TODO
get() = sequenceOf(
aPollHistoryState(
isLoading = false,
hasMoreToLoad = false,
activeFilter = PollHistoryFilter.ONGOING,
),
aPollHistoryState(
isLoading = true,
hasMoreToLoad = true,
activeFilter = PollHistoryFilter.PAST,
),
)
}
private fun aPollHistoryState(
isLoading: Boolean = false,
hasMoreToLoad: Boolean = false,
activeFilter: PollHistoryFilter = PollHistoryFilter.ONGOING,
currentItems: ImmutableList<PollHistoryItem> = persistentListOf(
aPollHistoryItem(),
),
) = PollHistoryState(
isLoading = isLoading,
hasMoreToLoad = hasMoreToLoad,
activeFilter = activeFilter,
currentItems = currentItems,
eventSink = {},
)
private fun aPollHistoryItem(
formattedDate: String = "01/12/2023",
state: PollContentState = aPollContentState(),
) = PollHistoryItem(
formattedDate = formattedDate,
state = state,
)

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.poll.impl.history.model
enum class PollHistoryFilter {
ONGOING,
PAST,
}

View file

@ -14,10 +14,11 @@
* limitations under the License.
*/
package io.element.android.features.poll.impl.history
package io.element.android.features.poll.impl.history.model
import io.element.android.features.poll.api.pollcontent.PollContentState
sealed interface PollHistoryItem {
data class PollContent(val formattedDate: String, val state: PollContentState) : PollHistoryItem
}
data class PollHistoryItem(
val formattedDate: String,
val state: PollContentState,
)

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.poll.impl.history.model
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
data class PollHistoryItems(
val ongoing: ImmutableList<PollHistoryItem> = persistentListOf(),
val past: ImmutableList<PollHistoryItem> = persistentListOf(),
) {
val size = ongoing.size + past.size
}

View file

@ -14,13 +14,14 @@
* limitations under the License.
*/
package io.element.android.features.poll.impl.history
package io.element.android.features.poll.impl.history.model
import io.element.android.features.poll.api.pollcontent.PollContentStateFactory
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.dateformatter.api.DaySeparatorFormatter
import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem
import io.element.android.libraries.matrix.api.timeline.item.event.PollContent
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.withContext
import javax.inject.Inject
@ -30,8 +31,22 @@ class PollHistoryItemsFactory @Inject constructor(
private val dispatchers: CoroutineDispatchers,
) {
suspend fun create(timelineItems: List<MatrixTimelineItem>): List<PollHistoryItem> = withContext(dispatchers.computation) {
timelineItems.mapNotNull { create(it) }.reversed()
suspend fun create(timelineItems: List<MatrixTimelineItem>): PollHistoryItems = withContext(dispatchers.computation) {
val past = ArrayList<PollHistoryItem>()
val ongoing = ArrayList<PollHistoryItem>()
for (index in timelineItems.indices.reversed()) {
val timelineItem = timelineItems[index]
val pollHistoryItem = create(timelineItem) ?: continue
if (pollHistoryItem.state.isPollEnded) {
past.add(pollHistoryItem)
} else {
ongoing.add(pollHistoryItem)
}
}
PollHistoryItems(
ongoing = ongoing.toPersistentList(),
past = past.toPersistentList()
)
}
private suspend fun create(timelineItem: MatrixTimelineItem): PollHistoryItem? {
@ -39,7 +54,7 @@ class PollHistoryItemsFactory @Inject constructor(
is MatrixTimelineItem.Event -> {
val pollContent = timelineItem.event.content as? PollContent ?: return null
val pollContentState = pollContentStateFactory.create(timelineItem.event, pollContent)
PollHistoryItem.PollContent(
PollHistoryItem(
formattedDate = daySeparatorFormatter.format(timelineItem.event.timestamp),
state = pollContentState
)