浏览代码

move details text to PollMainViewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 3 年之前
父节点
当前提交
8c898404d1

+ 24 - 5
app/src/main/java/com/nextcloud/talk/polls/ui/PollMainDialogFragment.kt

@@ -10,8 +10,10 @@ import androidx.appcompat.app.AlertDialog
 import androidx.fragment.app.DialogFragment
 import androidx.lifecycle.ViewModelProvider
 import autodagger.AutoInjector
+import com.nextcloud.talk.R
 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
 
@@ -58,16 +60,23 @@ class PollMainDialogFragment(
         viewModel.viewState.observe(viewLifecycleOwner) { state ->
             when (state) {
                 PollMainViewModel.InitialState -> {}
-                is PollMainViewModel.PollVoteHiddenState -> showVoteFragment()
-                is PollMainViewModel.PollVoteState -> showVoteFragment()
-                is PollMainViewModel.PollResultState -> showResultsFragment()
+                is PollMainViewModel.PollVoteHiddenState -> {
+                    binding.pollDetailsText.visibility = View.VISIBLE
+                    binding.pollDetailsText.text = "You already voted for this private poll"
+                    showVoteScreen()
+                }
+                is PollMainViewModel.PollVoteState -> {
+                    binding.pollDetailsText.visibility = View.GONE
+                    showVoteScreen()
+                }
+                is PollMainViewModel.PollResultState -> showResultsScreen(state.poll)
             }
         }
 
         viewModel.initialize(roomToken, pollId)
     }
 
-    private fun showVoteFragment() {
+    private fun showVoteScreen() {
         val contentFragment = PollVoteFragment(
             viewModel,
             roomToken,
@@ -78,7 +87,9 @@ class PollMainDialogFragment(
         transaction.commit()
     }
 
-    private fun showResultsFragment() {
+    private fun showResultsScreen(poll: Poll) {
+        initVotersAmount(poll.numVoters)
+
         val contentFragment = PollResultsFragment(
             viewModel,
             roomToken,
@@ -89,6 +100,14 @@ class PollMainDialogFragment(
         transaction.commit()
     }
 
+    private fun initVotersAmount(numVoters: Int) {
+        binding.pollDetailsText.visibility = View.VISIBLE
+        binding.pollDetailsText.text = String.format(
+            resources.getString(R.string.polls_amount_voters),
+            numVoters
+        )
+    }
+
     /**
      * Fragment creator
      */

+ 1 - 21
app/src/main/java/com/nextcloud/talk/polls/ui/PollResultsFragment.kt

@@ -30,7 +30,6 @@ import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
 import androidx.recyclerview.widget.LinearLayoutManager
 import autodagger.AutoInjector
-import com.nextcloud.talk.R
 import com.nextcloud.talk.application.NextcloudTalkApplication
 import com.nextcloud.talk.databinding.DialogPollResultsBinding
 import com.nextcloud.talk.polls.adapters.PollResultItem
@@ -79,9 +78,8 @@ class PollResultsFragment(
 
         parentViewModel.viewState.observe(viewLifecycleOwner) { state ->
             if (state is PollMainViewModel.PollResultState) {
-                initAdapter(state.showDetails)
+                initAdapter(state.showParticipants)
                 initPollResults(state.poll)
-                initAmountVotersInfo(state)
                 initEditButton(state.showEditButton)
                 initCloseButton(state.showCloseButton)
             }
@@ -125,24 +123,6 @@ class PollResultsFragment(
         }
     }
 
-    private fun initAmountVotersInfo(state: PollMainViewModel.PollResultState) {
-        _binding?.pollAmountVoters?.text = String.format(
-            resources.getString(R.string.polls_amount_voters),
-            state.poll.numVoters
-        )
-    }
-
-    // private fun initEditButton(state: PollMainViewModel.PollResultState) {
-    //     if (state.poll.status == Poll.STATUS_OPEN && state.poll.resultMode == Poll.RESULT_MODE_PUBLIC) {
-    //         _binding?.editVoteButton?.visibility = View.VISIBLE
-    //         _binding?.editVoteButton?.setOnClickListener {
-    //             parentViewModel.edit()
-    //         }
-    //     } else {
-    //         _binding?.editVoteButton?.visibility = View.GONE
-    //     }
-    // }
-
     private fun initEditButton(showEditButton: Boolean) {
         if (showEditButton) {
             _binding?.editVoteButton?.visibility = View.VISIBLE

+ 2 - 2
app/src/main/java/com/nextcloud/talk/polls/ui/PollVoteFragment.kt

@@ -73,10 +73,10 @@ class PollVoteFragment(
         parentViewModel.viewState.observe(viewLifecycleOwner) { state ->
             if (state is PollMainViewModel.PollVoteState) {
                 initPollOptions(state.poll)
-                binding.pollVoteHiddenHint.visibility = View.GONE
+                // binding.pollVoteHiddenHint.visibility = View.GONE
             } else if (state is PollMainViewModel.PollVoteHiddenState) {
                 initPollOptions(state.poll)
-                binding.pollVoteHiddenHint.visibility = View.VISIBLE
+                // binding.pollVoteHiddenHint.visibility = View.VISIBLE
             }
         }
 

+ 5 - 5
app/src/main/java/com/nextcloud/talk/polls/viewmodels/PollMainViewModel.kt

@@ -40,7 +40,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
     open class PollVoteHiddenState(val poll: Poll) : ViewState
     open class PollResultState(
         val poll: Poll,
-        val showDetails: Boolean,
+        val showParticipants: Boolean,
         val showEditButton: Boolean,
         val showCloseButton: Boolean
     ) : ViewState
@@ -59,7 +59,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
     }
 
     fun voted() {
-        loadPoll() // TODO load other view
+        loadPoll()
     }
 
     fun edit() {
@@ -118,7 +118,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
         }
     }
 
-    fun setPollResultState(poll: Poll) {
+    private 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)
@@ -126,13 +126,13 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
         _viewState.value = PollResultState(poll, showDetails, showEditButton, showCloseButton)
     }
 
-    fun votedForOpenHiddenPoll(poll: Poll): Boolean {
+    private 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 {
+    private fun isPollCreatedByCurrentUser(poll: Poll): Boolean {
         return userUtils.currentUser?.userId == poll.actorId
     }
 

+ 14 - 4
app/src/main/res/layout/dialog_poll_main.xml

@@ -21,14 +21,14 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
+    android:padding="@dimen/standard_padding"
     tools:background="@color/white">
 
     <ImageView
         android:id="@+id/message_poll_icon"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginStart="16dp"
-        android:layout_marginTop="16dp"
+
         android:contentDescription="@null"
         android:src="@drawable/ic_baseline_bar_chart_24"
         app:layout_constraintStart_toStartOf="parent"
@@ -37,7 +37,7 @@
 
     <androidx.emoji.widget.EmojiTextView
         android:id="@+id/message_poll_title"
-        android:layout_width="257dp"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginStart="8dp"
         android:textStyle="bold"
@@ -45,12 +45,22 @@
         app:layout_constraintTop_toTopOf="@+id/message_poll_icon"
         tools:text="This is the poll title?" />
 
+    <TextView
+        android:id="@+id/poll_details_text"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:textColor="@color/low_emphasis_text"
+        app:layout_constraintStart_toStartOf="@id/message_poll_icon"
+        app:layout_constraintTop_toBottomOf="@+id/message_poll_title"
+        tools:text="Poll results - 93 votes" />
+
     <FrameLayout
         android:id="@+id/message_poll_content_fragment"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="16dp"
-        app:layout_constraintTop_toBottomOf="@id/message_poll_title"
+        app:layout_constraintTop_toBottomOf="@id/poll_details_text"
         tools:layout_height="400dp" />
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 0 - 11
app/src/main/res/layout/dialog_poll_results.xml

@@ -21,7 +21,6 @@
     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">
 
     <LinearLayout
@@ -38,16 +37,6 @@
             tools:listitem="@layout/poll_result_item" />
     </LinearLayout>
 
-    <TextView
-        android:id="@+id/poll_amount_voters"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="24dp"
-        android:textColor="@color/low_emphasis_text"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/poll_results_list_wrapper"
-        tools:text="Poll results - 93 votes" />
-
     <com.google.android.material.button.MaterialButton
         android:id="@+id/poll_results_close_poll_button"
         android:layout_width="wrap_content"

+ 0 - 11
app/src/main/res/layout/dialog_poll_vote.xml

@@ -21,7 +21,6 @@
     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
@@ -44,14 +43,4 @@
         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>