show voting screen for open private polls

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-22 16:37:58 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent b504af1cd9
commit fbd5e5f5ed
6 changed files with 59 additions and 42 deletions

View File

@ -12,7 +12,6 @@ import androidx.lifecycle.ViewModelProvider
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogPollMainBinding import com.nextcloud.talk.databinding.DialogPollMainBinding
import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.viewmodels.PollMainViewModel import com.nextcloud.talk.polls.viewmodels.PollMainViewModel
import javax.inject.Inject import javax.inject.Inject
@ -59,22 +58,9 @@ class PollMainDialogFragment(
viewModel.viewState.observe(viewLifecycleOwner) { state -> viewModel.viewState.observe(viewLifecycleOwner) { state ->
when (state) { when (state) {
PollMainViewModel.InitialState -> {} PollMainViewModel.InitialState -> {}
is PollMainViewModel.PollVoteHiddenState -> showVoteFragment()
is PollMainViewModel.PollResultState -> { is PollMainViewModel.PollVoteState -> showVoteFragment()
if (state.poll.resultMode == Poll.RESULT_MODE_HIDDEN) { is PollMainViewModel.PollResultState -> showResultsFragment()
showVoteFragment()
} else {
showResultsFragment()
}
}
is PollMainViewModel.PollVoteState -> {
if (state.poll.status == Poll.STATUS_CLOSED) {
showResultsFragment()
} else {
showVoteFragment()
}
}
} }
} }

View File

@ -156,12 +156,12 @@ class PollResultsFragment(
private fun initCloseButton(showCloseButton: Boolean) { private fun initCloseButton(showCloseButton: Boolean) {
if (showCloseButton) { if (showCloseButton) {
_binding?.closeVoteButton?.visibility = View.VISIBLE _binding?.pollResultsClosePollButton?.visibility = View.VISIBLE
_binding?.closeVoteButton?.setOnClickListener { _binding?.pollResultsClosePollButton?.setOnClickListener {
parentViewModel.closePoll() parentViewModel.closePoll()
} }
} else { } else {
_binding?.closeVoteButton?.visibility = View.GONE _binding?.pollResultsClosePollButton?.visibility = View.GONE
} }
} }

View File

@ -72,17 +72,11 @@ class PollVoteFragment(
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
parentViewModel.viewState.observe(viewLifecycleOwner) { state -> parentViewModel.viewState.observe(viewLifecycleOwner) { state ->
if (state is PollMainViewModel.PollVoteState) { if (state is PollMainViewModel.PollVoteState) {
val poll = state.poll initPollOptions(state.poll)
binding.radioGroup.removeAllViews() binding.pollVoteHiddenHint.visibility = View.GONE
poll.options?.map { option -> } else if (state is PollMainViewModel.PollVoteHiddenState) {
RadioButton(context).apply { text = option } initPollOptions(state.poll)
}?.forEachIndexed { index, radioButton -> binding.pollVoteHiddenHint.visibility = View.VISIBLE
radioButton.id = index
binding.radioGroup.addView(radioButton)
}
} else if (state is PollMainViewModel.PollResultState && state.poll.resultMode == Poll.RESULT_MODE_HIDDEN) {
Log.d(TAG, "show vote screen also for resultMode hidden poll when already voted")
// TODO: other text for submit button
} }
} }
@ -109,6 +103,16 @@ class PollVoteFragment(
} }
} }
private fun initPollOptions(poll: Poll) {
binding.radioGroup.removeAllViews()
poll.options?.map { option ->
RadioButton(context).apply { text = option }
}?.forEachIndexed { index, radioButton ->
radioButton.id = index
binding.radioGroup.addView(radioButton)
}
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
_binding = null _binding = null

View File

@ -37,6 +37,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
sealed interface ViewState sealed interface ViewState
object InitialState : ViewState object InitialState : ViewState
open class PollVoteState(val poll: Poll) : ViewState open class PollVoteState(val poll: Poll) : ViewState
open class PollVoteHiddenState(val poll: Poll) : ViewState
open class PollResultState( open class PollResultState(
val poll: Poll, val poll: Poll,
val showDetails: Boolean, val showDetails: Boolean,
@ -102,21 +103,35 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
} }
override fun onComplete() { override fun onComplete() {
if (editPoll) { if (votedForOpenHiddenPoll(poll)) {
_viewState.value = PollVoteHiddenState(poll)
} else if (editPoll && poll.status == Poll.STATUS_OPEN) {
_viewState.value = PollVoteState(poll) _viewState.value = PollVoteState(poll)
editPoll = false editPoll = false
} else if (poll.status == Poll.STATUS_CLOSED || poll.votedSelf?.isNotEmpty() == true) {
setPollResultState(poll)
} else if (poll.votedSelf.isNullOrEmpty()) { } else if (poll.votedSelf.isNullOrEmpty()) {
_viewState.value = PollVoteState(poll) _viewState.value = PollVoteState(poll)
} else { } else {
val showEditButton = poll.status == Poll.STATUS_OPEN && poll.resultMode == Poll.RESULT_MODE_PUBLIC Log.w(TAG, "unknown poll state")
val showDetails = poll.status == Poll.STATUS_CLOSED && poll.resultMode == Poll.RESULT_MODE_PUBLIC
val showCloseButton = poll.status == Poll.STATUS_OPEN && isPollCreatedByCurrentUser(poll)
_viewState.value = PollResultState(poll, showDetails, showEditButton, showCloseButton)
} }
} }
} }
fun setPollResultState(poll: Poll) {
val showDetails = poll.status == Poll.STATUS_CLOSED && poll.resultMode == Poll.RESULT_MODE_PUBLIC
val showEditButton = poll.status == Poll.STATUS_OPEN && poll.resultMode == Poll.RESULT_MODE_PUBLIC
val showCloseButton = poll.status == Poll.STATUS_OPEN && isPollCreatedByCurrentUser(poll)
_viewState.value = PollResultState(poll, showDetails, showEditButton, showCloseButton)
}
fun votedForOpenHiddenPoll(poll: Poll): Boolean {
return poll.status == Poll.STATUS_OPEN &&
poll.resultMode == Poll.RESULT_MODE_HIDDEN &&
poll.votedSelf?.isNotEmpty() == true
}
fun isPollCreatedByCurrentUser(poll: Poll): Boolean { fun isPollCreatedByCurrentUser(poll: Poll): Boolean {
return userUtils.currentUser?.userId == poll.actorId return userUtils.currentUser?.userId == poll.actorId
} }

View File

@ -49,7 +49,7 @@
tools:text="Poll results - 93 votes" /> tools:text="Poll results - 93 votes" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/close_vote_button" android:id="@+id/poll_results_close_poll_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"

View File

@ -21,25 +21,37 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:padding="@dimen/standard_padding"
tools:background="@color/white"> tools:background="@color/white">
<RadioGroup <RadioGroup
android:id="@+id/radioGroup" android:id="@+id/radioGroup"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content" android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_height="400dp" tools:layout_height="400dp"
tools:layout_width="match_parent" /> tools:layout_width="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/submitVote" android:id="@+id/submitVote"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:cornerRadius="@dimen/button_corner_radius" app:cornerRadius="@dimen/button_corner_radius"
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/nc_common_submit" android:text="@string/nc_common_submit"
android:theme="@style/Button.Primary" android:theme="@style/Button.Primary"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<TextView
android:id="@+id/poll_vote_hidden_hint"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="You already voted for this private poll."
android:layout_marginTop="24dp"
android:textColor="@color/low_emphasis_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" /> app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>