From fb445628249d165e73f5e4e60339723c69ae88b0 Mon Sep 17 00:00:00 2001
From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com>
Date: Wed, 1 Feb 2023 14:53:05 +0100
Subject: [PATCH] Using navigator and new activity for the new screen
---
vector/src/main/AndroidManifest.xml | 1 +
.../roomprofile/RoomProfileActivity.kt | 10 ----
.../roomprofile/RoomProfileSharedAction.kt | 1 -
.../roomprofile/polls/RoomPollsAction.kt | 2 -
.../roomprofile/polls/RoomPollsFragment.kt | 33 +----------
.../roomprofile/polls/RoomPollsViewEvent.kt | 1 -
.../roomprofile/polls/RoomPollsViewModel.kt | 11 ----
.../roomprofile/polls/RoomPollsViewState.kt | 4 --
.../polls/detail/RoomPollDetailActivity.kt | 56 +++++++++++++++++++
.../polls/detail/RoomPollDetailController.kt | 5 +-
.../polls/detail/RoomPollDetailFragment.kt | 1 -
.../polls/detail/RoomPollDetailViewModel.kt | 5 +-
.../polls/detail/RoomPollDetailViewState.kt | 8 +--
.../polls/list/ui/RoomPollsListFragment.kt | 12 ++--
.../polls/list/ui/RoomPollsListNavigator.kt | 29 ++++++++++
15 files changed, 100 insertions(+), 79 deletions(-)
create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailActivity.kt
create mode 100644 vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListNavigator.kt
diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml
index 9c8186b2d4..ed9800b4f7 100644
--- a/vector/src/main/AndroidManifest.xml
+++ b/vector/src/main/AndroidManifest.xml
@@ -327,6 +327,7 @@
+
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileActivity.kt b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileActivity.kt
index 97636acb70..3ee1ed867c 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileActivity.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileActivity.kt
@@ -37,7 +37,6 @@ import im.vector.app.features.roomprofile.members.RoomMemberListFragment
import im.vector.app.features.roomprofile.notifications.RoomNotificationSettingsFragment
import im.vector.app.features.roomprofile.permissions.RoomPermissionsFragment
import im.vector.app.features.roomprofile.polls.RoomPollsFragment
-import im.vector.app.features.roomprofile.polls.detail.RoomPollDetailFragment
import im.vector.app.features.roomprofile.settings.RoomSettingsFragment
import im.vector.app.features.roomprofile.uploads.RoomUploadsFragment
import im.vector.lib.core.utils.compat.getParcelableCompat
@@ -106,7 +105,6 @@ class RoomProfileActivity :
RoomProfileSharedAction.OpenRoomUploads -> openRoomUploads()
RoomProfileSharedAction.OpenBannedRoomMembers -> openBannedRoomMembers()
RoomProfileSharedAction.OpenRoomNotificationSettings -> openRoomNotificationSettings()
- is RoomProfileSharedAction.OpenPollDetails -> handleOpenPollDetails(sharedAction)
}
}
.launchIn(lifecycleScope)
@@ -132,14 +130,6 @@ class RoomProfileActivity :
finish()
}
- private fun handleOpenPollDetails(sharedAction: RoomProfileSharedAction.OpenPollDetails) {
- addFragmentToBackstack(
- views.simpleFragmentContainer,
- RoomPollDetailFragment::class.java,
- roomProfileArgs.copy(selectedPollId = sharedAction.pollId)
- )
- }
-
private fun openRoomPolls() {
addFragmentToBackstack(views.simpleFragmentContainer, RoomPollsFragment::class.java, roomProfileArgs)
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileSharedAction.kt b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileSharedAction.kt
index 7948b0dcd9..b243ceb206 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileSharedAction.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileSharedAction.kt
@@ -26,7 +26,6 @@ sealed class RoomProfileSharedAction : VectorSharedAction {
object OpenRoomAliasesSettings : RoomProfileSharedAction()
object OpenRoomPermissionsSettings : RoomProfileSharedAction()
object OpenRoomPolls : RoomProfileSharedAction()
- data class OpenPollDetails(val pollId: String) : RoomProfileSharedAction()
object OpenRoomUploads : RoomProfileSharedAction()
object OpenRoomMembers : RoomProfileSharedAction()
object OpenBannedRoomMembers : RoomProfileSharedAction()
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt
index 297e0048d7..3fedbfc4a8 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsAction.kt
@@ -20,6 +20,4 @@ import im.vector.app.core.platform.VectorViewModelAction
sealed interface RoomPollsAction : VectorViewModelAction {
object LoadMorePolls : RoomPollsAction
- data class OnRoomPollsTypeChange(val roomPollsType: RoomPollsType) : RoomPollsAction
- data class OnPollSelected(val selectedPollId: String) : RoomPollsAction
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsFragment.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsFragment.kt
index a20c5cd350..9f7e704135 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsFragment.kt
@@ -20,9 +20,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
-import androidx.lifecycle.DefaultLifecycleObserver
-import androidx.lifecycle.LifecycleOwner
-import androidx.viewpager2.widget.ViewPager2
import com.airbnb.mvrx.args
import com.airbnb.mvrx.fragmentViewModel
import com.google.android.material.tabs.TabLayoutMediator
@@ -69,35 +66,9 @@ class RoomPollsFragment : VectorBaseFragment() {
tabLayoutMediator = TabLayoutMediator(views.roomPollsTabs, views.roomPollsViewPager) { tab, position ->
when (position) {
- RoomPollsType.ACTIVE.ordinal -> {
- tab.text = getString(R.string.room_polls_active)
- }
- RoomPollsType.ENDED.ordinal -> {
- tab.text = getString(R.string.room_polls_ended)
- }
+ RoomPollsType.ACTIVE.ordinal -> tab.text = getString(R.string.room_polls_active)
+ RoomPollsType.ENDED.ordinal -> tab.text = getString(R.string.room_polls_ended)
}
}.also { it.attach() }
-
- val onPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
- override fun onPageSelected(position: Int) {
- when (position) {
- RoomPollsType.ACTIVE.ordinal -> {
- viewModel.handle(RoomPollsAction.OnRoomPollsTypeChange(RoomPollsType.ACTIVE))
- }
- RoomPollsType.ENDED.ordinal -> {
- viewModel.handle(RoomPollsAction.OnRoomPollsTypeChange(RoomPollsType.ENDED))
- }
- }
- }
- }
-
- viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
- override fun onCreate(owner: LifecycleOwner) {
- views.roomPollsViewPager.registerOnPageChangeCallback(onPageChangeCallback)
- }
- override fun onDestroy(owner: LifecycleOwner) {
- views.roomPollsViewPager.unregisterOnPageChangeCallback(onPageChangeCallback)
- }
- })
}
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewEvent.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewEvent.kt
index df60d86613..cb2069d824 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewEvent.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewEvent.kt
@@ -20,5 +20,4 @@ import im.vector.app.core.platform.VectorViewEvents
sealed class RoomPollsViewEvent : VectorViewEvents {
object LoadingError : RoomPollsViewEvent()
- data class NavigateToPollDetail(val selectedPollId: String) : RoomPollsViewEvent()
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt
index 6a94f11c14..2beda47816 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModel.kt
@@ -89,20 +89,9 @@ class RoomPollsViewModel @AssistedInject constructor(
override fun handle(action: RoomPollsAction) {
when (action) {
RoomPollsAction.LoadMorePolls -> handleLoadMore()
- is RoomPollsAction.OnPollSelected -> handleOnPollSelected(action)
- is RoomPollsAction.OnRoomPollsTypeChange -> handleOnRoomPollsTypeChange(action)
}
}
- private fun handleOnRoomPollsTypeChange(action: RoomPollsAction.OnRoomPollsTypeChange) {
- setState { copy(selectedRoomPollsType = action.roomPollsType) }
- }
-
- private fun handleOnPollSelected(action: RoomPollsAction.OnPollSelected) {
- setState { copy(selectedPollId = action.selectedPollId) }
- _viewEvents.post(RoomPollsViewEvent.NavigateToPollDetail(action.selectedPollId))
- }
-
private fun handleLoadMore() = withState { viewState ->
viewModelScope.launch {
setState { copy(isLoadingMore = true) }
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 63638257b0..4a5c138b6a 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
@@ -27,14 +27,10 @@ data class RoomPollsViewState(
val canLoadMore: Boolean = true,
val nbSyncedDays: Int = 0,
val isSyncing: Boolean = false,
- val selectedPollId: String? = null,
- val selectedRoomPollsType: RoomPollsType = RoomPollsType.ACTIVE,
) : MavericksState {
constructor(roomProfileArgs: RoomProfileArgs) : this(roomId = roomProfileArgs.roomId)
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/RoomPollDetailActivity.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailActivity.kt
new file mode 100644
index 0000000000..6995340a9e
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailActivity.kt
@@ -0,0 +1,56 @@
+/*
+ * Copyright 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.detail
+
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import com.airbnb.mvrx.Mavericks
+import dagger.hilt.android.AndroidEntryPoint
+import im.vector.app.core.extensions.addFragment
+import im.vector.app.core.platform.VectorBaseActivity
+import im.vector.app.databinding.ActivitySimpleBinding
+import im.vector.lib.core.utils.compat.getParcelableExtraCompat
+
+/**
+ * Display the details of a given poll.
+ */
+@AndroidEntryPoint
+class RoomPollDetailActivity : VectorBaseActivity() {
+
+ override fun getBinding() = ActivitySimpleBinding.inflate(layoutInflater)
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ if (isFirstCreation()) {
+ addFragment(
+ container = views.simpleFragmentContainer,
+ fragmentClass = RoomPollDetailFragment::class.java,
+ params = intent.getParcelableExtraCompat(Mavericks.KEY_ARG)
+ )
+ }
+ }
+
+ companion object {
+ fun newIntent(context: Context, pollId: String): Intent {
+ return Intent(context, RoomPollDetailActivity::class.java).apply {
+ putExtra(Mavericks.KEY_ARG, RoomPollDetailArgs(pollId))
+ }
+ }
+ }
+}
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
index 647c036513..cd2ca1c711 100644
--- 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
@@ -22,9 +22,8 @@ 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() {
+class RoomPollDetailController @Inject constructor()
+ : TypedEpoxyController() {
override fun buildModels(viewState: RoomPollDetailViewState?) {
viewState ?: return
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 cfab132947..dd7b40560f 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
@@ -36,7 +36,6 @@ import javax.inject.Inject
@Parcelize
data class RoomPollDetailArgs(
- val roomId: String,
val pollId: String,
) : Parcelable
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewModel.kt
index 5d65db78b4..7368d28c8f 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewModel.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewModel.kt
@@ -25,10 +25,11 @@ class RoomPollDetailViewModel @AssistedInject constructor(
) : VectorViewModel(initialState) {
init {
- // Subscribe to the poll event and map it
+ // TODO observe poll using TimelineService.getTimelineEventLive
+ // TODO create a dedicated useCase and mapper
}
override fun handle(action: RoomPollDetailAction) {
-
+ // TODO handle go to timeline action
}
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewState.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewState.kt
index d7e31a0424..2d7303f555 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewState.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/detail/RoomPollDetailViewState.kt
@@ -19,11 +19,9 @@ package im.vector.app.features.roomprofile.polls.detail
import com.airbnb.mvrx.MavericksState
data class RoomPollDetailViewState(
- val roomId: String,
- val pollId: String,
- val pollDetail: RoomPollDetail? = null,
+ val pollId: String,
+ val pollDetail: RoomPollDetail? = null,
) : MavericksState {
- constructor(roomPollDetailArgs: RoomPollDetailArgs)
- : this(roomId = roomPollDetailArgs.roomId, pollId = roomPollDetailArgs.pollId)
+ constructor(roomPollDetailArgs: RoomPollDetailArgs) : this(pollId = roomPollDetailArgs.pollId)
}
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListFragment.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListFragment.kt
index e4e64d229a..1c6ae518da 100644
--- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListFragment.kt
@@ -29,8 +29,6 @@ import im.vector.app.core.extensions.configureWith
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.core.resources.StringProvider
import im.vector.app.databinding.FragmentRoomPollsListBinding
-import im.vector.app.features.roomprofile.RoomProfileSharedAction
-import im.vector.app.features.roomprofile.RoomProfileSharedActionViewModel
import im.vector.app.features.roomprofile.polls.RoomPollsAction
import im.vector.app.features.roomprofile.polls.RoomPollsLoadingError
import im.vector.app.features.roomprofile.polls.RoomPollsType
@@ -49,8 +47,10 @@ abstract class RoomPollsListFragment :
@Inject
lateinit var stringProvider: StringProvider
+ @Inject
+ lateinit var viewNavigator: RoomPollsListNavigator
+
private val viewModel: RoomPollsViewModel by parentFragmentViewModel(RoomPollsViewModel::class)
- private lateinit var sharedActionViewModel: RoomProfileSharedActionViewModel
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomPollsListBinding {
return FragmentRoomPollsListBinding.inflate(inflater, container, false)
@@ -58,7 +58,6 @@ abstract class RoomPollsListFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
- sharedActionViewModel = activityViewModelProvider[RoomProfileSharedActionViewModel::class.java]
observeViewEvents()
setupList()
setupLoadMoreButton()
@@ -68,9 +67,6 @@ abstract class RoomPollsListFragment :
viewModel.observeViewEvents { viewEvent ->
when (viewEvent) {
RoomPollsViewEvent.LoadingError -> showErrorInSnackbar(RoomPollsLoadingError())
- is RoomPollsViewEvent.NavigateToPollDetail -> {
- sharedActionViewModel.post(RoomProfileSharedAction.OpenPollDetails(viewEvent.selectedPollId))
- }
}
}
}
@@ -132,7 +128,7 @@ abstract class RoomPollsListFragment :
}
override fun onPollClicked(pollId: String) {
- viewModel.handle(RoomPollsAction.OnPollSelected(pollId))
+ viewNavigator.goToPollDetails(requireContext(), pollId)
}
override fun onLoadMoreClicked() {
diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListNavigator.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListNavigator.kt
new file mode 100644
index 0000000000..35440e89a9
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/ui/RoomPollsListNavigator.kt
@@ -0,0 +1,29 @@
+/*
+ * 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.list.ui
+
+import android.content.Context
+import im.vector.app.features.roomprofile.polls.detail.RoomPollDetailActivity
+import javax.inject.Inject
+
+// TODO add unit tests
+class RoomPollsListNavigator @Inject constructor() {
+
+ fun goToPollDetails(context: Context, pollId: String) {
+ context.startActivity(RoomPollDetailActivity.newIntent(context, pollId))
+ }
+}