Render mocked data get from use case
This commit is contained in:
parent
77d3b7da04
commit
8de86e7480
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.features.roomprofile.polls
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.emptyFlow
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class GetPollsUseCase @Inject constructor() {
|
||||||
|
|
||||||
|
fun execute(filter: RoomPollsFilter): Flow<List<PollSummary>> {
|
||||||
|
// TODO unmock and add unit tests
|
||||||
|
return when (filter) {
|
||||||
|
RoomPollsFilter.ACTIVE -> getActivePolls()
|
||||||
|
RoomPollsFilter.ENDED -> emptyFlow()
|
||||||
|
}.map { it.sortedByDescending { poll -> poll.creationTimestamp } }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getActivePolls(): Flow<List<PollSummary.ActivePoll>> {
|
||||||
|
return flowOf(
|
||||||
|
listOf(
|
||||||
|
PollSummary.ActivePoll(
|
||||||
|
id = "id1",
|
||||||
|
// 2022/06/28 UTC+1
|
||||||
|
creationTimestamp = 1656367200000,
|
||||||
|
title = "Which charity would you like to support?"
|
||||||
|
),
|
||||||
|
PollSummary.ActivePoll(
|
||||||
|
id = "id2",
|
||||||
|
// 2022/06/26 UTC+1
|
||||||
|
creationTimestamp = 1656194400000,
|
||||||
|
title = "Which sport should the pupils do this year?"
|
||||||
|
),
|
||||||
|
PollSummary.ActivePoll(
|
||||||
|
id = "id3",
|
||||||
|
// 2022/06/24 UTC+1
|
||||||
|
creationTimestamp = 1656021600000,
|
||||||
|
title = "What type of food should we have at the party?"
|
||||||
|
),
|
||||||
|
PollSummary.ActivePoll(
|
||||||
|
id = "id4",
|
||||||
|
// 2022/06/22 UTC+1
|
||||||
|
creationTimestamp = 1655848800000,
|
||||||
|
title = "What film should we show at the end of the year party?"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,4 +18,6 @@ package im.vector.app.features.roomprofile.polls
|
||||||
|
|
||||||
import im.vector.app.core.platform.VectorViewModelAction
|
import im.vector.app.core.platform.VectorViewModelAction
|
||||||
|
|
||||||
sealed class RoomPollsAction : VectorViewModelAction
|
sealed interface RoomPollsAction : VectorViewModelAction {
|
||||||
|
data class SetFilter(val filter: RoomPollsFilter) : RoomPollsAction
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 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.features.roomprofile.polls
|
||||||
|
|
||||||
|
enum class RoomPollsFilter {
|
||||||
|
ACTIVE,
|
||||||
|
ENDED,
|
||||||
|
}
|
|
@ -23,9 +23,13 @@ import dagger.assisted.AssistedInject
|
||||||
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
||||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||||
import im.vector.app.core.platform.VectorViewModel
|
import im.vector.app.core.platform.VectorViewModel
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
|
||||||
class RoomPollsViewModel @AssistedInject constructor(
|
class RoomPollsViewModel @AssistedInject constructor(
|
||||||
@Assisted initialState: RoomPollsViewState,
|
@Assisted initialState: RoomPollsViewState,
|
||||||
|
private val getPollsUseCase: GetPollsUseCase,
|
||||||
) : VectorViewModel<RoomPollsViewState, RoomPollsAction, RoomPollsViewEvent>(initialState) {
|
) : VectorViewModel<RoomPollsViewState, RoomPollsAction, RoomPollsViewEvent>(initialState) {
|
||||||
|
|
||||||
@AssistedFactory
|
@AssistedFactory
|
||||||
|
@ -35,7 +39,24 @@ class RoomPollsViewModel @AssistedInject constructor(
|
||||||
|
|
||||||
companion object : MavericksViewModelFactory<RoomPollsViewModel, RoomPollsViewState> by hiltMavericksViewModelFactory()
|
companion object : MavericksViewModelFactory<RoomPollsViewModel, RoomPollsViewState> by hiltMavericksViewModelFactory()
|
||||||
|
|
||||||
|
private var pollsCollectionJob: Job? = null
|
||||||
|
|
||||||
|
// TODO add unit tests
|
||||||
override fun handle(action: RoomPollsAction) {
|
override fun handle(action: RoomPollsAction) {
|
||||||
// do nothing for now
|
when (action) {
|
||||||
|
is RoomPollsAction.SetFilter -> handleSetFilter(action.filter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCleared() {
|
||||||
|
pollsCollectionJob = null
|
||||||
|
super.onCleared()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleSetFilter(filter: RoomPollsFilter) {
|
||||||
|
pollsCollectionJob?.cancel()
|
||||||
|
pollsCollectionJob = getPollsUseCase.execute(filter)
|
||||||
|
.onEach { setState { copy(polls = it) } }
|
||||||
|
.launchIn(viewModelScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,12 @@ import com.airbnb.mvrx.parentFragmentViewModel
|
||||||
import com.airbnb.mvrx.withState
|
import com.airbnb.mvrx.withState
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import im.vector.app.core.extensions.cleanup
|
import im.vector.app.core.extensions.cleanup
|
||||||
|
import im.vector.app.core.extensions.configureWith
|
||||||
import im.vector.app.core.platform.VectorBaseFragment
|
import im.vector.app.core.platform.VectorBaseFragment
|
||||||
import im.vector.app.databinding.FragmentRoomPollsListBinding
|
import im.vector.app.databinding.FragmentRoomPollsListBinding
|
||||||
import im.vector.app.features.roomprofile.polls.PollSummary
|
import im.vector.app.features.roomprofile.polls.PollSummary
|
||||||
|
import im.vector.app.features.roomprofile.polls.RoomPollsAction
|
||||||
|
import im.vector.app.features.roomprofile.polls.RoomPollsFilter
|
||||||
import im.vector.app.features.roomprofile.polls.RoomPollsViewModel
|
import im.vector.app.features.roomprofile.polls.RoomPollsViewModel
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@ -51,7 +54,7 @@ class RoomActivePollsFragment :
|
||||||
|
|
||||||
private fun setupList() {
|
private fun setupList() {
|
||||||
roomActivePollsController.listener = this
|
roomActivePollsController.listener = this
|
||||||
views.activePollsList.adapter = roomActivePollsController.adapter
|
views.activePollsList.configureWith(roomActivePollsController)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
|
@ -64,6 +67,11 @@ class RoomActivePollsFragment :
|
||||||
roomActivePollsController.listener = null
|
roomActivePollsController.listener = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
viewModel.handle(RoomPollsAction.SetFilter(RoomPollsFilter.ACTIVE))
|
||||||
|
}
|
||||||
|
|
||||||
override fun invalidate() = withState(viewModel) { viewState ->
|
override fun invalidate() = withState(viewModel) { viewState ->
|
||||||
renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
|
renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/activePollsList"
|
android:id="@+id/activePollsList"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="0dp"
|
||||||
android:layout_marginHorizontal="@dimen/layout_horizontal_margin"
|
android:layout_marginHorizontal="@dimen/layout_horizontal_margin"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
|
Loading…
Reference in New Issue