diff --git a/app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt b/app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt index 3be8fa879..1ee30ee68 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt @@ -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() } } diff --git a/app/src/main/java/com/nextcloud/talk/polls/ui/PollResultsFragment.kt b/app/src/main/java/com/nextcloud/talk/polls/ui/PollResultsFragment.kt index 1aa27c153..b4262060c 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/ui/PollResultsFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/ui/PollResultsFragment.kt @@ -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 } } diff --git a/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt b/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt index 668650b05..d3b700811 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt @@ -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 diff --git a/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollMainViewModel.kt b/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollMainViewModel.kt index 8d15d3e4b..3e7955d23 100644 --- a/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollMainViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollMainViewModel.kt @@ -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 } diff --git a/app/src/main/res/layout/dialog_poll_results.xml b/app/src/main/res/layout/dialog_poll_results.xml index 5b8008e31..49308a76c 100644 --- a/app/src/main/res/layout/dialog_poll_results.xml +++ b/app/src/main/res/layout/dialog_poll_results.xml @@ -49,7 +49,7 @@ tools:text="Poll results - 93 votes" /> + tools:layout_width="match_parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + +