Skip to content

Commit 8de86e7

Browse files
committed
Render mocked data get from use case
1 parent 77d3b7d commit 8de86e7

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.roomprofile.polls
18+
19+
import kotlinx.coroutines.flow.Flow
20+
import kotlinx.coroutines.flow.emptyFlow
21+
import kotlinx.coroutines.flow.flowOf
22+
import kotlinx.coroutines.flow.map
23+
import javax.inject.Inject
24+
25+
class GetPollsUseCase @Inject constructor() {
26+
27+
fun execute(filter: RoomPollsFilter): Flow<List<PollSummary>> {
28+
// TODO unmock and add unit tests
29+
return when (filter) {
30+
RoomPollsFilter.ACTIVE -> getActivePolls()
31+
RoomPollsFilter.ENDED -> emptyFlow()
32+
}.map { it.sortedByDescending { poll -> poll.creationTimestamp } }
33+
}
34+
35+
private fun getActivePolls(): Flow<List<PollSummary.ActivePoll>> {
36+
return flowOf(
37+
listOf(
38+
PollSummary.ActivePoll(
39+
id = "id1",
40+
// 2022/06/28 UTC+1
41+
creationTimestamp = 1656367200000,
42+
title = "Which charity would you like to support?"
43+
),
44+
PollSummary.ActivePoll(
45+
id = "id2",
46+
// 2022/06/26 UTC+1
47+
creationTimestamp = 1656194400000,
48+
title = "Which sport should the pupils do this year?"
49+
),
50+
PollSummary.ActivePoll(
51+
id = "id3",
52+
// 2022/06/24 UTC+1
53+
creationTimestamp = 1656021600000,
54+
title = "What type of food should we have at the party?"
55+
),
56+
PollSummary.ActivePoll(
57+
id = "id4",
58+
// 2022/06/22 UTC+1
59+
creationTimestamp = 1655848800000,
60+
title = "What film should we show at the end of the year party?"
61+
),
62+
)
63+
)
64+
}
65+
}

vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ package im.vector.app.features.roomprofile.polls
1818

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

21-
sealed class RoomPollsAction : VectorViewModelAction
21+
sealed interface RoomPollsAction : VectorViewModelAction {
22+
data class SetFilter(val filter: RoomPollsFilter) : RoomPollsAction
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.roomprofile.polls
18+
19+
enum class RoomPollsFilter {
20+
ACTIVE,
21+
ENDED,
22+
}

vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ import dagger.assisted.AssistedInject
2323
import im.vector.app.core.di.MavericksAssistedViewModelFactory
2424
import im.vector.app.core.di.hiltMavericksViewModelFactory
2525
import im.vector.app.core.platform.VectorViewModel
26+
import kotlinx.coroutines.Job
27+
import kotlinx.coroutines.flow.launchIn
28+
import kotlinx.coroutines.flow.onEach
2629

2730
class RoomPollsViewModel @AssistedInject constructor(
2831
@Assisted initialState: RoomPollsViewState,
32+
private val getPollsUseCase: GetPollsUseCase,
2933
) : VectorViewModel<RoomPollsViewState, RoomPollsAction, RoomPollsViewEvent>(initialState) {
3034

3135
@AssistedFactory
@@ -35,7 +39,24 @@ class RoomPollsViewModel @AssistedInject constructor(
3539

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

42+
private var pollsCollectionJob: Job? = null
43+
44+
// TODO add unit tests
3845
override fun handle(action: RoomPollsAction) {
39-
// do nothing for now
46+
when (action) {
47+
is RoomPollsAction.SetFilter -> handleSetFilter(action.filter)
48+
}
49+
}
50+
51+
override fun onCleared() {
52+
pollsCollectionJob = null
53+
super.onCleared()
54+
}
55+
56+
private fun handleSetFilter(filter: RoomPollsFilter) {
57+
pollsCollectionJob?.cancel()
58+
pollsCollectionJob = getPollsUseCase.execute(filter)
59+
.onEach { setState { copy(polls = it) } }
60+
.launchIn(viewModelScope)
4061
}
4162
}

vector/src/main/java/im/vector/app/features/roomprofile/polls/active/RoomActivePollsFragment.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import com.airbnb.mvrx.parentFragmentViewModel
2424
import com.airbnb.mvrx.withState
2525
import dagger.hilt.android.AndroidEntryPoint
2626
import im.vector.app.core.extensions.cleanup
27+
import im.vector.app.core.extensions.configureWith
2728
import im.vector.app.core.platform.VectorBaseFragment
2829
import im.vector.app.databinding.FragmentRoomPollsListBinding
2930
import im.vector.app.features.roomprofile.polls.PollSummary
31+
import im.vector.app.features.roomprofile.polls.RoomPollsAction
32+
import im.vector.app.features.roomprofile.polls.RoomPollsFilter
3033
import im.vector.app.features.roomprofile.polls.RoomPollsViewModel
3134
import javax.inject.Inject
3235

@@ -51,7 +54,7 @@ class RoomActivePollsFragment :
5154

5255
private fun setupList() {
5356
roomActivePollsController.listener = this
54-
views.activePollsList.adapter = roomActivePollsController.adapter
57+
views.activePollsList.configureWith(roomActivePollsController)
5558
}
5659

5760
override fun onDestroyView() {
@@ -64,6 +67,11 @@ class RoomActivePollsFragment :
6467
roomActivePollsController.listener = null
6568
}
6669

70+
override fun onResume() {
71+
super.onResume()
72+
viewModel.handle(RoomPollsAction.SetFilter(RoomPollsFilter.ACTIVE))
73+
}
74+
6775
override fun invalidate() = withState(viewModel) { viewState ->
6876
renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
6977
}

vector/src/main/res/layout/fragment_room_polls_list.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
<androidx.recyclerview.widget.RecyclerView
99
android:id="@+id/activePollsList"
1010
android:layout_width="0dp"
11-
android:layout_height="wrap_content"
11+
android:layout_height="0dp"
1212
android:layout_marginHorizontal="@dimen/layout_horizontal_margin"
13+
app:layout_constraintBottom_toBottomOf="parent"
1314
app:layout_constraintEnd_toEndOf="parent"
1415
app:layout_constraintStart_toStartOf="parent"
1516
app:layout_constraintTop_toTopOf="parent"

0 commit comments

Comments
 (0)