From dba9e298812a8ef2cfc8e416efaeeffbe86c1883 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com> Date: Wed, 11 Jan 2023 15:37:31 +0100 Subject: [PATCH] Fake loading more process --- .../roomprofile/polls/RoomPollsAction.kt | 4 ++- .../roomprofile/polls/RoomPollsViewModel.kt | 21 +++++++++++-- .../roomprofile/polls/RoomPollsViewState.kt | 4 +++ .../polls/{ => list}/PollSummary.kt | 4 +-- .../polls/list/RoomPollLoadMoreItem.kt | 8 +++++ .../polls/list/RoomPollsController.kt | 23 ++++++++------ .../polls/list/RoomPollsListFragment.kt | 18 ++++++----- .../{ => list/domain}/GetPollsUseCase.kt | 13 +++++--- .../polls/list/domain/LoadMorePollsUseCase.kt | 31 +++++++++++++++++++ .../main/res/layout/item_poll_load_more.xml | 1 + .../polls/RoomPollsViewModelTest.kt | 2 ++ 11 files changed, 102 insertions(+), 27 deletions(-) rename vector/src/main/java/im/vector/app/features/roomprofile/polls/{ => list}/PollSummary.kt (92%) rename vector/src/main/java/im/vector/app/features/roomprofile/polls/{ => list/domain}/GetPollsUseCase.kt (91%) create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/polls/list/domain/LoadMorePollsUseCase.kt diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt index c18142a306..3fedbfc4a8 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt @@ -18,4 +18,6 @@ package im.vector.app.features.roomprofile.polls import im.vector.app.core.platform.VectorViewModelAction -sealed interface RoomPollsAction : VectorViewModelAction +sealed interface RoomPollsAction : VectorViewModelAction { + object LoadMorePolls : RoomPollsAction +} diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt index 95cb4717ca..220690fe90 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt @@ -23,12 +23,16 @@ import dagger.assisted.AssistedInject import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.VectorViewModel +import im.vector.app.features.roomprofile.polls.list.domain.GetPollsUseCase +import im.vector.app.features.roomprofile.polls.list.domain.LoadMorePollsUseCase import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch class RoomPollsViewModel @AssistedInject constructor( @Assisted initialState: RoomPollsViewState, private val getPollsUseCase: GetPollsUseCase, + private val loadMorePollsUseCase: LoadMorePollsUseCase, ) : VectorViewModel(initialState) { @AssistedFactory @@ -42,13 +46,24 @@ class RoomPollsViewModel @AssistedInject constructor( observePolls() } - private fun observePolls() { - getPollsUseCase.execute() + private fun observePolls() = withState { viewState -> + getPollsUseCase.execute(viewState.roomId) .onEach { setState { copy(polls = it) } } .launchIn(viewModelScope) } + // TODO add unit tests override fun handle(action: RoomPollsAction) { - // do nothing for now + when (action) { + RoomPollsAction.LoadMorePolls -> handleLoadMore() + } + } + + private fun handleLoadMore() = withState { viewState -> + viewModelScope.launch { + setState { copy(isLoadingMore = true) } + loadMorePollsUseCase.execute(viewState.roomId) + setState { copy(isLoadingMore = false) } + } } } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewState.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewState.kt index 74794c99b1..7a0a005e58 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewState.kt @@ -18,10 +18,14 @@ package im.vector.app.features.roomprofile.polls import com.airbnb.mvrx.MavericksState import im.vector.app.features.roomprofile.RoomProfileArgs +import im.vector.app.features.roomprofile.polls.list.PollSummary +// TODO parameter to know whether load more is possible +// TODO parameter to know whether initial loading is in progress data class RoomPollsViewState( val roomId: String, val polls: List = emptyList(), + val isLoadingMore: Boolean = false, ) : MavericksState { constructor(roomProfileArgs: RoomProfileArgs) : this(roomId = roomProfileArgs.roomId) diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/PollSummary.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/PollSummary.kt similarity index 92% rename from vector/src/main/java/im/vector/app/features/roomprofile/polls/PollSummary.kt rename to vector/src/main/java/im/vector/app/features/roomprofile/polls/list/PollSummary.kt index f24ac8b8a6..fc83c482bd 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/PollSummary.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/PollSummary.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package im.vector.app.features.roomprofile.polls +package im.vector.app.features.roomprofile.polls.list import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/RoomPollLoadMoreItem.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/RoomPollLoadMoreItem.kt index a684fa5c9e..a643fbbec7 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/RoomPollLoadMoreItem.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/RoomPollLoadMoreItem.kt @@ -17,6 +17,8 @@ package im.vector.app.features.roomprofile.polls.list import android.widget.Button +import android.widget.ProgressBar +import androidx.core.view.isVisible import com.airbnb.epoxy.EpoxyAttribute import com.airbnb.epoxy.EpoxyModelClass import im.vector.app.R @@ -28,15 +30,21 @@ import im.vector.app.core.epoxy.onClick @EpoxyModelClass abstract class RoomPollLoadMoreItem : VectorEpoxyModel(R.layout.item_poll_load_more) { + @EpoxyAttribute + var loadingMore: Boolean = false + @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var clickListener: ClickListener? = null override fun bind(holder: Holder) { super.bind(holder) + holder.loadMoreButton.isEnabled = loadingMore.not() holder.loadMoreButton.onClick(clickListener) + holder.loadMoreProgressBar.isVisible = loadingMore } class Holder : VectorEpoxyHolder() { val loadMoreButton by bind