move variables from fragment to PollMainViewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-07-19 17:04:59 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 6dfed0e5e7
commit 139c4412ad
2 changed files with 32 additions and 31 deletions

View file

@ -27,6 +27,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import autodagger.AutoInjector
@ -40,12 +41,6 @@ import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class)
class PollMainDialogFragment : DialogFragment() {
lateinit var user: User
lateinit var roomToken: String
private var isOwnerOrModerator: Boolean = false
lateinit var pollId: String
lateinit var pollTitle: String
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
@ -58,11 +53,13 @@ class PollMainDialogFragment : DialogFragment() {
viewModel = ViewModelProvider(this, viewModelFactory)[PollMainViewModel::class.java]
user = arguments?.getParcelable(KEY_USER_ENTITY)!!
roomToken = arguments?.getString(KEY_ROOM_TOKEN)!!
isOwnerOrModerator = arguments?.getBoolean(KEY_OWNER_OR_MODERATOR)!!
pollId = arguments?.getString(KEY_POLL_ID)!!
pollTitle = arguments?.getString(KEY_POLL_TITLE)!!
val user: User = arguments?.getParcelable(KEY_USER_ENTITY)!!
val roomToken = arguments?.getString(KEY_ROOM_TOKEN)!!
val isOwnerOrModerator = arguments?.getBoolean(KEY_OWNER_OR_MODERATOR)!!
val pollId = arguments?.getString(KEY_POLL_ID)!!
val pollTitle = arguments?.getString(KEY_POLL_TITLE)!!
viewModel.initialize(user, roomToken, isOwnerOrModerator, pollId, pollTitle)
}
@SuppressLint("InflateParams")
@ -73,7 +70,7 @@ class PollMainDialogFragment : DialogFragment() {
.setView(binding.root)
.create()
binding.messagePollTitle.text = pollTitle
binding.messagePollTitle.text = viewModel.pollTitle
return dialog
}
@ -85,8 +82,6 @@ class PollMainDialogFragment : DialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.setIsOwnerOrModerator(isOwnerOrModerator)
viewModel.viewState.observe(viewLifecycleOwner) { state ->
when (state) {
PollMainViewModel.InitialState -> {}
@ -108,14 +103,12 @@ class PollMainDialogFragment : DialogFragment() {
else -> {}
}
}
viewModel.initialize(roomToken, pollId)
}
private fun showVoteScreen() {
val contentFragment = PollVoteFragment.newInstance(
roomToken,
pollId
viewModel.roomToken,
viewModel.pollId
)
val transaction = childFragmentManager.beginTransaction()
@ -125,7 +118,7 @@ class PollMainDialogFragment : DialogFragment() {
private fun showResultsScreen() {
val contentFragment = PollResultsFragment.newInstance(
user
viewModel.user
)
val transaction = childFragmentManager.beginTransaction()
@ -171,12 +164,15 @@ class PollMainDialogFragment : DialogFragment() {
pollId: String,
name: String
): PollMainDialogFragment {
val args = Bundle()
args.putParcelable(KEY_USER_ENTITY, user)
args.putString(KEY_ROOM_TOKEN, roomTokenParam)
args.putBoolean(KEY_OWNER_OR_MODERATOR, isOwnerOrModerator)
args.putString(KEY_POLL_ID, pollId)
args.putString(KEY_POLL_TITLE, name)
val args = bundleOf(
KEY_USER_ENTITY to user,
KEY_ROOM_TOKEN to roomTokenParam,
KEY_OWNER_OR_MODERATOR to isOwnerOrModerator,
KEY_POLL_ID to pollId,
KEY_POLL_TITLE to name
)
val fragment = PollMainDialogFragment()
fragment.arguments = args
return fragment

View file

@ -24,6 +24,7 @@ import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.repositories.PollRepository
import com.nextcloud.talk.utils.database.user.UserUtils
@ -49,10 +50,11 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
@Inject
lateinit var userUtils: UserUtils
private lateinit var roomToken: String
private lateinit var pollId: String
lateinit var user: User
lateinit var roomToken: String
private var isOwnerOrModerator: Boolean = false
lateinit var pollId: String
lateinit var pollTitle: String
private var editVotes: Boolean = false
@ -84,9 +86,12 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
private var disposable: Disposable? = null
fun initialize(roomToken: String, pollId: String) {
fun initialize(user: User, roomToken: String, isOwnerOrModerator: Boolean, pollId: String, pollTitle: String) {
this.user = user
this.roomToken = roomToken
this.isOwnerOrModerator = isOwnerOrModerator
this.pollId = pollId
this.pollTitle = pollTitle
loadPoll()
}
@ -106,7 +111,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
private fun loadPoll() {
repository.getPoll(roomToken, pollId)
?.doOnSubscribe { disposable = it }
.doOnSubscribe { disposable = it }
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(PollObserver())
@ -114,7 +119,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
fun closePoll() {
repository.closePoll(roomToken, pollId)
?.doOnSubscribe { disposable = it }
.doOnSubscribe { disposable = it }
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(PollObserver())