move details text to PollMainViewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-22 19:55:41 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent fbd5e5f5ed
commit 8c898404d1
7 changed files with 46 additions and 59 deletions

View File

@ -10,8 +10,10 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.R
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
@ -58,16 +60,23 @@ 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.PollVoteHiddenState -> {
is PollMainViewModel.PollVoteState -> showVoteFragment() binding.pollDetailsText.visibility = View.VISIBLE
is PollMainViewModel.PollResultState -> showResultsFragment() 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) viewModel.initialize(roomToken, pollId)
} }
private fun showVoteFragment() { private fun showVoteScreen() {
val contentFragment = PollVoteFragment( val contentFragment = PollVoteFragment(
viewModel, viewModel,
roomToken, roomToken,
@ -78,7 +87,9 @@ class PollMainDialogFragment(
transaction.commit() transaction.commit()
} }
private fun showResultsFragment() { private fun showResultsScreen(poll: Poll) {
initVotersAmount(poll.numVoters)
val contentFragment = PollResultsFragment( val contentFragment = PollResultsFragment(
viewModel, viewModel,
roomToken, roomToken,
@ -89,6 +100,14 @@ class PollMainDialogFragment(
transaction.commit() 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 * Fragment creator
*/ */

View File

@ -30,7 +30,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.R
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogPollResultsBinding import com.nextcloud.talk.databinding.DialogPollResultsBinding
import com.nextcloud.talk.polls.adapters.PollResultItem import com.nextcloud.talk.polls.adapters.PollResultItem
@ -79,9 +78,8 @@ class PollResultsFragment(
parentViewModel.viewState.observe(viewLifecycleOwner) { state -> parentViewModel.viewState.observe(viewLifecycleOwner) { state ->
if (state is PollMainViewModel.PollResultState) { if (state is PollMainViewModel.PollResultState) {
initAdapter(state.showDetails) initAdapter(state.showParticipants)
initPollResults(state.poll) initPollResults(state.poll)
initAmountVotersInfo(state)
initEditButton(state.showEditButton) initEditButton(state.showEditButton)
initCloseButton(state.showCloseButton) 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) { private fun initEditButton(showEditButton: Boolean) {
if (showEditButton) { if (showEditButton) {
_binding?.editVoteButton?.visibility = View.VISIBLE _binding?.editVoteButton?.visibility = View.VISIBLE

View File

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

View File

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

View File

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

View File

@ -21,7 +21,6 @@
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">
<LinearLayout <LinearLayout
@ -38,16 +37,6 @@
tools:listitem="@layout/poll_result_item" /> tools:listitem="@layout/poll_result_item" />
</LinearLayout> </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 <com.google.android.material.button.MaterialButton
android:id="@+id/poll_results_close_poll_button" android:id="@+id/poll_results_close_poll_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@ -21,7 +21,6 @@
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
@ -44,14 +43,4 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" /> 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> </androidx.constraintlayout.widget.ConstraintLayout>