Observe timeline event of the selected poll

This commit is contained in:
Maxime NATUREL 2023-02-01 15:47:40 +01:00
parent 60d3ae6cc5
commit afe036dd9d
7 changed files with 83 additions and 15 deletions

View file

@ -0,0 +1,43 @@
/*
* 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 im.vector.app.core.event
import androidx.lifecycle.asFlow
import im.vector.app.core.di.ActiveSessionHolder
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.flow.unwrap
import javax.inject.Inject
// TODO add unit tests
class GetTimelineEventUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
fun execute(roomId: String, eventId: String): Flow<TimelineEvent> {
return activeSessionHolder
.getActiveSession()
.roomService()
.getRoom(roomId)
?.timelineService()
?.getTimelineEventLive(eventId)
?.asFlow()
?.unwrap()
?: emptyFlow()
}
}

View file

@ -47,9 +47,13 @@ class RoomPollDetailActivity : VectorBaseActivity<ActivitySimpleBinding>() {
}
companion object {
fun newIntent(context: Context, pollId: String): Intent {
fun newIntent(context: Context, pollId: String, roomId: String): Intent {
return Intent(context, RoomPollDetailActivity::class.java).apply {
putExtra(Mavericks.KEY_ARG, RoomPollDetailArgs(pollId))
val args = RoomPollDetailArgs(
pollId = pollId,
roomId = roomId,
)
putExtra(Mavericks.KEY_ARG, args)
}
}
}

View file

@ -36,6 +36,7 @@ import javax.inject.Inject
@Parcelize
data class RoomPollDetailArgs(
val pollId: String,
val roomId: String,
) : Parcelable
@AndroidEntryPoint
@ -80,13 +81,9 @@ class RoomPollDetailFragment : VectorBaseFragment<FragmentRoomPollDetailBinding>
override fun invalidate() = withState(viewModel) { state ->
state.pollDetail ?: return@withState
// TODO should we update the title when the poll status changes?
setupToolbar(state.pollDetail.isEnded)
/*
state.getSelectedPoll()?.let { _ ->
roomPollDetailController.setData(state)
}
Unit
*/
// TODO update data of the controller
}
}

View file

@ -18,15 +18,25 @@ package im.vector.app.features.roomprofile.polls.detail.ui
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import im.vector.app.core.event.GetTimelineEventUseCase
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.flow.launchIn
class RoomPollDetailViewModel @AssistedInject constructor(
@Assisted initialState: RoomPollDetailViewState,
private val getTimelineEventUseCase: GetTimelineEventUseCase,
) : VectorViewModel<RoomPollDetailViewState, RoomPollDetailAction, RoomPollDetailViewEvent>(initialState) {
init {
// TODO observe poll using TimelineService.getTimelineEventLive
// TODO create a dedicated useCase and mapper
observePollDetails(
pollId = initialState.pollId,
roomId = initialState.roomId,
)
}
private fun observePollDetails(pollId: String, roomId: String) {
getTimelineEventUseCase.execute(roomId = roomId, eventId = pollId)
.launchIn(viewModelScope)
}
override fun handle(action: RoomPollDetailAction) {

View file

@ -20,8 +20,12 @@ import com.airbnb.mvrx.MavericksState
data class RoomPollDetailViewState(
val pollId: String,
val roomId: String,
val pollDetail: RoomPollDetail? = null,
) : MavericksState {
constructor(roomPollDetailArgs: RoomPollDetailArgs) : this(pollId = roomPollDetailArgs.pollId)
constructor(roomPollDetailArgs: RoomPollDetailArgs) : this(
pollId = roomPollDetailArgs.pollId,
roomId = roomPollDetailArgs.roomId,
)
}

View file

@ -127,8 +127,12 @@ abstract class RoomPollsListFragment :
views.roomPollsLoadMoreWhenEmptyProgress.isVisible = viewState.hasNoPollsAndCanLoadMore() && viewState.isLoadingMore
}
override fun onPollClicked(pollId: String) {
viewNavigator.goToPollDetails(requireContext(), pollId)
override fun onPollClicked(pollId: String) = withState(viewModel) {
viewNavigator.goToPollDetails(
context = requireContext(),
pollId = pollId,
roomId = it.roomId,
)
}
override fun onLoadMoreClicked() {

View file

@ -23,7 +23,13 @@ import javax.inject.Inject
// TODO add unit tests
class RoomPollsListNavigator @Inject constructor() {
fun goToPollDetails(context: Context, pollId: String) {
context.startActivity(RoomPollDetailActivity.newIntent(context, pollId))
fun goToPollDetails(context: Context, pollId: String, roomId: String) {
context.startActivity(
RoomPollDetailActivity.newIntent(
context = context,
pollId = pollId,
roomId = roomId,
)
)
}
}