From fbd5e5f5ede71fe4182fd78c6fe61389b7182aa0 Mon Sep 17 00:00:00 2001
From: Marcel Hibbe <dev@mhibbe.de>
Date: Wed, 22 Jun 2022 16:37:58 +0200
Subject: [PATCH] show voting screen for open private polls

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
---
 .../talk/polls/ui/PollMainDialogFragment.kt   | 20 +++-----------
 .../talk/polls/ui/PollResultsFragment.kt      |  6 ++---
 .../talk/polls/ui/PollVoteFragment.kt         | 26 ++++++++++--------
 .../polls/viewmodels/PollMainViewModel.kt     | 27 ++++++++++++++-----
 .../main/res/layout/dialog_poll_results.xml   |  2 +-
 app/src/main/res/layout/dialog_poll_vote.xml  | 20 +++++++++++---
 6 files changed, 59 insertions(+), 42 deletions(-)

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" />
 
     <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"
diff --git a/app/src/main/res/layout/dialog_poll_vote.xml b/app/src/main/res/layout/dialog_poll_vote.xml
index 1e71b0244..a1fde993f 100644
--- a/app/src/main/res/layout/dialog_poll_vote.xml
+++ b/app/src/main/res/layout/dialog_poll_vote.xml
@@ -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>