Skip to content

Commit f856142

Browse files
authored
Merge pull request element-hq#7886 from vector-im/feature/mna/past-polls-ui
[Poll] Render past polls list of a room (PSG-1029)
2 parents b7076a1 + 85cfa43 commit f856142

File tree

17 files changed

+385
-196
lines changed

17 files changed

+385
-196
lines changed

changelog.d/7864.wip

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
[Poll] Render active polls list of a room
2+
[Poll] Render past polls list of a room

library/ui-strings/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -3193,6 +3193,8 @@
31933193
<string name="closed_poll_option_description">Results are only revealed when you end the poll</string>
31943194
<string name="room_polls_active">Active polls</string>
31953195
<string name="room_polls_active_no_item">There are no active polls in this room</string>
3196+
<string name="room_polls_ended">Past polls</string>
3197+
<string name="room_polls_ended_no_item">There are no past polls in this room</string>
31963198

31973199
<!-- Location -->
31983200
<string name="location_activity_title_static_sharing">Share location</string>

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

+80-31
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,99 @@
1616

1717
package im.vector.app.features.roomprofile.polls
1818

19+
import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState
1920
import kotlinx.coroutines.flow.Flow
20-
import kotlinx.coroutines.flow.emptyFlow
2121
import kotlinx.coroutines.flow.flowOf
2222
import kotlinx.coroutines.flow.map
2323
import javax.inject.Inject
2424

2525
class GetPollsUseCase @Inject constructor() {
2626

27-
fun execute(filter: RoomPollsFilterType): Flow<List<PollSummary>> {
27+
fun execute(): Flow<List<PollSummary>> {
2828
// TODO unmock and add unit tests
29-
return when (filter) {
30-
RoomPollsFilterType.ACTIVE -> getActivePolls()
31-
RoomPollsFilterType.ENDED -> emptyFlow()
32-
}.map { it.sortedByDescending { poll -> poll.creationTimestamp } }
29+
return flowOf(getActivePolls() + getEndedPolls())
30+
.map { it.sortedByDescending { poll -> poll.creationTimestamp } }
3331
}
3432

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?"
33+
private fun getActivePolls(): List<PollSummary.ActivePoll> {
34+
return listOf(
35+
PollSummary.ActivePoll(
36+
id = "id1",
37+
// 2022/06/28 UTC+1
38+
creationTimestamp = 1656367200000,
39+
title = "Which charity would you like to support?"
40+
),
41+
PollSummary.ActivePoll(
42+
id = "id2",
43+
// 2022/06/26 UTC+1
44+
creationTimestamp = 1656194400000,
45+
title = "Which sport should the pupils do this year?"
46+
),
47+
PollSummary.ActivePoll(
48+
id = "id3",
49+
// 2022/06/24 UTC+1
50+
creationTimestamp = 1656021600000,
51+
title = "What type of food should we have at the party?"
52+
),
53+
PollSummary.ActivePoll(
54+
id = "id4",
55+
// 2022/06/22 UTC+1
56+
creationTimestamp = 1655848800000,
57+
title = "What film should we show at the end of the year party?"
58+
),
59+
)
60+
}
61+
62+
private fun getEndedPolls(): List<PollSummary.EndedPoll> {
63+
return listOf(
64+
PollSummary.EndedPoll(
65+
id = "id1-ended",
66+
// 2022/06/28 UTC+1
67+
creationTimestamp = 1656367200000,
68+
title = "Which charity would you like to support?",
69+
totalVotes = 22,
70+
winnerOptions = listOf(
71+
PollOptionViewState.PollEnded(
72+
optionId = "id1",
73+
optionAnswer = "Cancer research",
74+
voteCount = 13,
75+
votePercentage = 13 / 22.0,
76+
isWinner = true,
77+
)
4978
),
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?"
79+
),
80+
PollSummary.EndedPoll(
81+
id = "id2-ended",
82+
// 2022/06/26 UTC+1
83+
creationTimestamp = 1656194400000,
84+
title = "Where should we do the offsite?",
85+
totalVotes = 92,
86+
winnerOptions = listOf(
87+
PollOptionViewState.PollEnded(
88+
optionId = "id1",
89+
optionAnswer = "Hawaii",
90+
voteCount = 43,
91+
votePercentage = 43 / 92.0,
92+
isWinner = true,
93+
)
5594
),
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?"
95+
),
96+
PollSummary.EndedPoll(
97+
id = "id3-ended",
98+
// 2022/06/24 UTC+1
99+
creationTimestamp = 1656021600000,
100+
title = "What type of food should we have at the party?",
101+
totalVotes = 22,
102+
winnerOptions = listOf(
103+
PollOptionViewState.PollEnded(
104+
optionId = "id1",
105+
optionAnswer = "Brazilian",
106+
voteCount = 13,
107+
votePercentage = 13 / 22.0,
108+
isWinner = true,
109+
)
61110
),
62-
)
111+
),
63112
)
64113
}
65114
}

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,24 @@
1616

1717
package im.vector.app.features.roomprofile.polls
1818

19+
import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState
20+
1921
sealed interface PollSummary {
22+
val id: String
23+
val creationTimestamp: Long
24+
val title: String
25+
2026
data class ActivePoll(
21-
val id: String,
22-
val creationTimestamp: Long,
23-
val title: String,
27+
override val id: String,
28+
override val creationTimestamp: Long,
29+
override val title: String,
30+
) : PollSummary
31+
32+
data class EndedPoll(
33+
override val id: String,
34+
override val creationTimestamp: Long,
35+
override val title: String,
36+
val totalVotes: Int,
37+
val winnerOptions: List<PollOptionViewState.PollEnded>,
2438
) : PollSummary
2539
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ package im.vector.app.features.roomprofile.polls
1818

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

21-
sealed interface RoomPollsAction : VectorViewModelAction {
22-
data class SetFilter(val filter: RoomPollsFilterType) : RoomPollsAction
23-
}
21+
sealed interface RoomPollsAction : VectorViewModelAction

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class RoomPollsFragment : VectorBaseFragment<FragmentRoomPollsBinding>() {
6666

6767
tabLayoutMediator = TabLayoutMediator(views.roomPollsTabs, views.roomPollsViewPager) { tab, position ->
6868
when (position) {
69-
0 -> tab.text = getString(R.string.room_polls_active)
69+
RoomPollsType.ACTIVE.ordinal -> tab.text = getString(R.string.room_polls_active)
70+
RoomPollsType.ENDED.ordinal -> tab.text = getString(R.string.room_polls_ended)
7071
}
7172
}.also { it.attach() }
7273
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ package im.vector.app.features.roomprofile.polls
1919
import androidx.fragment.app.Fragment
2020
import androidx.viewpager2.adapter.FragmentStateAdapter
2121
import im.vector.app.features.roomprofile.polls.active.RoomActivePollsFragment
22+
import im.vector.app.features.roomprofile.polls.ended.RoomEndedPollsFragment
2223

2324
class RoomPollsPagerAdapter(
2425
private val fragment: Fragment
2526
) : FragmentStateAdapter(fragment) {
2627

27-
override fun getItemCount() = 1
28+
override fun getItemCount() = RoomPollsType.values().size
2829

2930
override fun createFragment(position: Int): Fragment {
30-
return instantiateFragment(RoomActivePollsFragment::class.java.name)
31+
return when (position) {
32+
RoomPollsType.ACTIVE.ordinal -> instantiateFragment(RoomActivePollsFragment::class.java.name)
33+
RoomPollsType.ENDED.ordinal -> instantiateFragment(RoomEndedPollsFragment::class.java.name)
34+
else -> throw IllegalArgumentException("position should be between 0 and ${itemCount - 1}, while it was $position")
35+
}
3136
}
3237

3338
private fun instantiateFragment(fragmentName: String): Fragment {

vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsFilterType.kt renamed to vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsType.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package im.vector.app.features.roomprofile.polls
1818

19-
enum class RoomPollsFilterType {
19+
enum class RoomPollsType {
2020
ACTIVE,
2121
ENDED,
2222
}

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package im.vector.app.features.roomprofile.polls
1818

19-
import androidx.annotation.VisibleForTesting
2019
import com.airbnb.mvrx.MavericksViewModelFactory
2120
import dagger.assisted.Assisted
2221
import dagger.assisted.AssistedFactory
2322
import dagger.assisted.AssistedInject
2423
import im.vector.app.core.di.MavericksAssistedViewModelFactory
2524
import im.vector.app.core.di.hiltMavericksViewModelFactory
2625
import im.vector.app.core.platform.VectorViewModel
27-
import kotlinx.coroutines.Job
2826
import kotlinx.coroutines.flow.launchIn
2927
import kotlinx.coroutines.flow.onEach
3028

@@ -40,24 +38,17 @@ class RoomPollsViewModel @AssistedInject constructor(
4038

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

43-
@VisibleForTesting
44-
var pollsCollectionJob: Job? = null
45-
46-
override fun handle(action: RoomPollsAction) {
47-
when (action) {
48-
is RoomPollsAction.SetFilter -> handleSetFilter(action.filter)
49-
}
50-
}
51-
52-
override fun onCleared() {
53-
pollsCollectionJob = null
54-
super.onCleared()
41+
init {
42+
observePolls()
5543
}
5644

57-
private fun handleSetFilter(filter: RoomPollsFilterType) {
58-
pollsCollectionJob?.cancel()
59-
pollsCollectionJob = getPollsUseCase.execute(filter)
45+
private fun observePolls() {
46+
getPollsUseCase.execute()
6047
.onEach { setState { copy(polls = it) } }
6148
.launchIn(viewModelScope)
6249
}
50+
51+
override fun handle(action: RoomPollsAction) {
52+
// do nothing for now
53+
}
6354
}

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

-52
This file was deleted.

0 commit comments

Comments
 (0)