Skip to content

Commit

Permalink
Render mocked data get from use case
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaturel committed Jan 4, 2023
1 parent 77d3b7d commit 8de86e7
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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?"
),
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ package im.vector.app.features.roomprofile.polls

import im.vector.app.core.platform.VectorViewModelAction

sealed class RoomPollsAction : VectorViewModelAction
sealed interface RoomPollsAction : VectorViewModelAction {
data class SetFilter(val filter: RoomPollsFilter) : RoomPollsAction
}
Original file line number Diff line number Diff line change
@@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ 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 kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class RoomPollsViewModel @AssistedInject constructor(
@Assisted initialState: RoomPollsViewState,
private val getPollsUseCase: GetPollsUseCase,
) : VectorViewModel<RoomPollsViewState, RoomPollsAction, RoomPollsViewEvent>(initialState) {

@AssistedFactory
Expand All @@ -35,7 +39,24 @@ class RoomPollsViewModel @AssistedInject constructor(

companion object : MavericksViewModelFactory<RoomPollsViewModel, RoomPollsViewState> by hiltMavericksViewModelFactory()

private var pollsCollectionJob: Job? = null

// TODO add unit tests
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import com.airbnb.mvrx.parentFragmentViewModel
import com.airbnb.mvrx.withState
import dagger.hilt.android.AndroidEntryPoint
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.databinding.FragmentRoomPollsListBinding
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 javax.inject.Inject

Expand All @@ -51,7 +54,7 @@ class RoomActivePollsFragment :

private fun setupList() {
roomActivePollsController.listener = this
views.activePollsList.adapter = roomActivePollsController.adapter
views.activePollsList.configureWith(roomActivePollsController)
}

override fun onDestroyView() {
Expand All @@ -64,6 +67,11 @@ class RoomActivePollsFragment :
roomActivePollsController.listener = null
}

override fun onResume() {
super.onResume()
viewModel.handle(RoomPollsAction.SetFilter(RoomPollsFilter.ACTIVE))
}

override fun invalidate() = withState(viewModel) { viewState ->
renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
}
Expand Down
3 changes: 2 additions & 1 deletion vector/src/main/res/layout/fragment_room_polls_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/activePollsList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_marginHorizontal="@dimen/layout_horizontal_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down

0 comments on commit 8de86e7

Please sign in to comment.