From ec4226b5d3d23740426882a7f2c5b8e6acc4dbfe Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Tue, 31 Jan 2023 12:19:30 +0300 Subject: [PATCH] Render poll detail. --- .../roomprofile/polls/RoomPollsViewState.kt | 1 + .../polls/detail/RoomPollDetailController.kt | 40 +++++++++++++++++++ .../polls/detail/RoomPollDetailFragment.kt | 16 ++++++-- .../res/layout/fragment_room_poll_detail.xml | 1 + 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailController.kt 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 6be16d14b9..63638257b0 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 @@ -36,4 +36,5 @@ data class RoomPollsViewState( fun hasNoPolls() = polls.isEmpty() fun hasNoPollsAndCanLoadMore() = !isSyncing && hasNoPolls() && canLoadMore fun getSelectedPoll() = polls.find { it.id == selectedPollId } + fun canVoteSelectedPoll() = selectedRoomPollsType == RoomPollsType.ACTIVE } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailController.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailController.kt new file mode 100644 index 0000000000..72ce1b2f56 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailController.kt @@ -0,0 +1,40 @@ +/* + * 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. + * 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.detail + +import com.airbnb.epoxy.TypedEpoxyController +import im.vector.app.features.home.room.detail.timeline.item.PollItem_ +import im.vector.app.features.roomprofile.polls.RoomPollsViewState +import im.vector.lib.core.utils.epoxy.charsequence.toEpoxyCharSequence +import javax.inject.Inject + +class RoomPollDetailController @Inject constructor( + +) : TypedEpoxyController() { + + override fun buildModels(viewState: RoomPollsViewState?) { + viewState ?: return + val pollSummary = viewState.getSelectedPoll() ?: return + + PollItem_() + .eventId(pollSummary.id) + .pollQuestion(pollSummary.title.toEpoxyCharSequence()) + .canVote(viewState.canVoteSelectedPoll()) + .optionViewStates(pollSummary.optionViewStates) + .ended(viewState.canVoteSelectedPoll().not()) + } +} diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailFragment.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailFragment.kt index 684ba45892..ec8e8ea004 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailFragment.kt @@ -26,15 +26,19 @@ import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import dagger.hilt.android.AndroidEntryPoint import im.vector.app.R +import im.vector.app.core.extensions.configureWith import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.databinding.FragmentRoomPollDetailBinding import im.vector.app.features.roomprofile.RoomProfileArgs import im.vector.app.features.roomprofile.polls.RoomPollsType import im.vector.app.features.roomprofile.polls.RoomPollsViewModel +import javax.inject.Inject @AndroidEntryPoint class RoomPollDetailFragment : VectorBaseFragment() { + @Inject lateinit var roomPollDetailController: RoomPollDetailController + private val viewModel: RoomPollsViewModel by activityViewModel() private val roomProfileArgs: RoomProfileArgs by args() @@ -45,11 +49,14 @@ class RoomPollDetailFragment : VectorBaseFragment override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - initToolbar() + views.pollDetailRecyclerView.configureWith( + roomPollDetailController, + hasFixedSize = true, + ) } - private fun initToolbar() = withState(viewModel) { state -> - val title = if (state.selectedRoomPollsType == RoomPollsType.ACTIVE) getString(R.string.room_polls_active) + private fun setupToolbar(roomPollsType: RoomPollsType) { + val title = if (roomPollsType == RoomPollsType.ACTIVE) getString(R.string.room_polls_active) else getString(R.string.room_polls_ended) setupToolbar(views.roomPollDetailToolbar) @@ -58,7 +65,10 @@ class RoomPollDetailFragment : VectorBaseFragment } override fun invalidate() = withState(viewModel) { state -> + setupToolbar(state.selectedRoomPollsType) + state.getSelectedPoll()?.let { _ -> + roomPollDetailController.setData(state) } Unit } diff --git a/vector/src/main/res/layout/fragment_room_poll_detail.xml b/vector/src/main/res/layout/fragment_room_poll_detail.xml index 7a7334bf8d..7ed8d1125e 100644 --- a/vector/src/main/res/layout/fragment_room_poll_detail.xml +++ b/vector/src/main/res/layout/fragment_room_poll_detail.xml @@ -22,6 +22,7 @@