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

View File

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

View File

@ -72,17 +72,11 @@ class PollVoteFragment(
super.onViewCreated(view, savedInstanceState)
parentViewModel.viewState.observe(viewLifecycleOwner) { state ->
if (state is PollMainViewModel.PollVoteState) {
val poll = state.poll
binding.radioGroup.removeAllViews()
poll.options?.map { option ->
RadioButton(context).apply { text = option }
}?.forEachIndexed { index, radioButton ->
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
initPollOptions(state.poll)
binding.pollVoteHiddenHint.visibility = View.GONE
} else if (state is PollMainViewModel.PollVoteHiddenState) {
initPollOptions(state.poll)
binding.pollVoteHiddenHint.visibility = View.VISIBLE
}
}
@ -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() {
super.onDestroyView()
_binding = null

View File

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

View File

@ -49,7 +49,7 @@
tools:text="Poll results - 93 votes" />
<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_height="wrap_content"
android:layout_marginTop="16dp"

View File

@ -21,25 +21,37 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:padding="@dimen/standard_padding"
tools:background="@color/white">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent"
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
android:id="@+id/submitVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cornerRadius="@dimen/button_corner_radius"
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/nc_common_submit"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>