get variables from parentViewModel

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-07-19 17:12:48 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 139c4412ad
commit dca7e39905
4 changed files with 11 additions and 68 deletions

View file

@ -106,10 +106,7 @@ class PollMainDialogFragment : DialogFragment() {
} }
private fun showVoteScreen() { private fun showVoteScreen() {
val contentFragment = PollVoteFragment.newInstance( val contentFragment = PollVoteFragment.newInstance()
viewModel.roomToken,
viewModel.pollId
)
val transaction = childFragmentManager.beginTransaction() val transaction = childFragmentManager.beginTransaction()
transaction.replace(binding.messagePollContentFragment.id, contentFragment) transaction.replace(binding.messagePollContentFragment.id, contentFragment)
@ -117,9 +114,7 @@ class PollMainDialogFragment : DialogFragment() {
} }
private fun showResultsScreen() { private fun showResultsScreen() {
val contentFragment = PollResultsFragment.newInstance( val contentFragment = PollResultsFragment.newInstance()
viewModel.user
)
val transaction = childFragmentManager.beginTransaction() val transaction = childFragmentManager.beginTransaction()
transaction.replace(binding.messagePollContentFragment.id, contentFragment) transaction.replace(binding.messagePollContentFragment.id, contentFragment)

View file

@ -33,7 +33,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.R import com.nextcloud.talk.R
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.databinding.DialogPollResultsBinding import com.nextcloud.talk.databinding.DialogPollResultsBinding
import com.nextcloud.talk.polls.adapters.PollResultHeaderItem import com.nextcloud.talk.polls.adapters.PollResultHeaderItem
import com.nextcloud.talk.polls.adapters.PollResultItemClickListener import com.nextcloud.talk.polls.adapters.PollResultItemClickListener
@ -51,8 +50,6 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
private lateinit var parentViewModel: PollMainViewModel private lateinit var parentViewModel: PollMainViewModel
lateinit var viewModel: PollResultsViewModel lateinit var viewModel: PollResultsViewModel
lateinit var user: User
lateinit var binding: DialogPollResultsBinding lateinit var binding: DialogPollResultsBinding
private var adapter: PollResultsAdapter? = null private var adapter: PollResultsAdapter? = null
@ -61,12 +58,7 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory)[PollResultsViewModel::class.java] viewModel = ViewModelProvider(this, viewModelFactory)[PollResultsViewModel::class.java]
parentViewModel = ViewModelProvider(requireParentFragment(), viewModelFactory)[ parentViewModel = ViewModelProvider(requireParentFragment(), viewModelFactory)[PollMainViewModel::class.java]
PollMainViewModel::class
.java
]
user = arguments?.getParcelable(KEY_USER_ENTITY)!!
} }
override fun onCreateView( override fun onCreateView(
@ -91,7 +83,7 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
} }
viewModel.items.observe(viewLifecycleOwner) { viewModel.items.observe(viewLifecycleOwner) {
val adapter = PollResultsAdapter(user, this).apply { val adapter = PollResultsAdapter(parentViewModel.user, this).apply {
if (it != null) { if (it != null) {
list = it list = it
} }
@ -101,7 +93,7 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
} }
private fun initAdapter() { private fun initAdapter() {
adapter = PollResultsAdapter(user, this) adapter = PollResultsAdapter(parentViewModel.user, this)
binding.pollResultsList.adapter = adapter binding.pollResultsList.adapter = adapter
binding.pollResultsList.layoutManager = LinearLayoutManager(context) binding.pollResultsList.layoutManager = LinearLayoutManager(context)
} }
@ -140,17 +132,9 @@ class PollResultsFragment : Fragment(), PollResultItemClickListener {
} }
companion object { companion object {
private const val KEY_USER_ENTITY = "keyUserEntity"
@JvmStatic @JvmStatic
fun newInstance( fun newInstance(): PollResultsFragment {
user: User return PollResultsFragment()
): PollResultsFragment {
val args = Bundle()
args.putParcelable(KEY_USER_ENTITY, user)
val fragment = PollResultsFragment()
fragment.arguments = args
return fragment
} }
} }
} }

View file

@ -47,10 +47,6 @@ import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
class PollVoteFragment : Fragment() { class PollVoteFragment : Fragment() {
lateinit var roomToken: String
lateinit var pollId: String
@Inject @Inject
lateinit var viewModelFactory: ViewModelProvider.Factory lateinit var viewModelFactory: ViewModelProvider.Factory
@ -64,13 +60,7 @@ class PollVoteFragment : Fragment() {
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory)[PollVoteViewModel::class.java] viewModel = ViewModelProvider(this, viewModelFactory)[PollVoteViewModel::class.java]
parentViewModel = ViewModelProvider(requireParentFragment(), viewModelFactory)[ parentViewModel = ViewModelProvider(requireParentFragment(), viewModelFactory)[PollMainViewModel::class.java]
PollMainViewModel::class
.java
]
roomToken = arguments?.getString(KEY_ROOM_TOKEN)!!
pollId = arguments?.getString(KEY_POLL_ID)!!
} }
override fun onCreateView( override fun onCreateView(
@ -120,7 +110,7 @@ class PollVoteFragment : Fragment() {
} }
binding.pollVoteSubmitButton.setOnClickListener { binding.pollVoteSubmitButton.setOnClickListener {
viewModel.vote(roomToken, pollId) viewModel.vote(parentViewModel.roomToken, parentViewModel.pollId)
} }
binding.pollVoteEditDismiss.setOnClickListener { binding.pollVoteEditDismiss.setOnClickListener {
@ -209,20 +199,9 @@ class PollVoteFragment : Fragment() {
private val TAG = PollVoteFragment::class.java.simpleName private val TAG = PollVoteFragment::class.java.simpleName
private const val UNLIMITED_VOTES = 0 private const val UNLIMITED_VOTES = 0
private const val KEY_ROOM_TOKEN = "keyRoomToken"
private const val KEY_POLL_ID = "keyPollId"
@JvmStatic @JvmStatic
fun newInstance( fun newInstance(): PollVoteFragment {
roomTokenParam: String, return PollVoteFragment()
pollId: String
): PollVoteFragment {
val args = Bundle()
args.putString(KEY_ROOM_TOKEN, roomTokenParam)
args.putString(KEY_POLL_ID, pollId)
val fragment = PollVoteFragment()
fragment.arguments = args
return fragment
} }
} }
} }

View file

@ -34,17 +34,6 @@ import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import javax.inject.Inject import javax.inject.Inject
/**
* @startuml
* hide empty description
* [*] --> InitialState
* InitialState --> PollOpenState
* note left
* Open second viewmodel for child fragment
* end note
* InitialState --> PollClosedState
* @enduml
*/
class PollMainViewModel @Inject constructor(private val repository: PollRepository) : ViewModel() { class PollMainViewModel @Inject constructor(private val repository: PollRepository) : ViewModel() {
@Inject @Inject
@ -190,10 +179,6 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito
return userUtils.currentUser?.userId == poll.actorId return userUtils.currentUser?.userId == poll.actorId
} }
fun setIsOwnerOrModerator(ownerOrModerator: Boolean) {
isOwnerOrModerator = ownerOrModerator
}
companion object { companion object {
private val TAG = PollMainViewModel::class.java.simpleName private val TAG = PollMainViewModel::class.java.simpleName
} }