From 8dfb6ca5aaa076ba58797e365688af058967ba48 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Fri, 22 Jul 2022 19:04:21 +0200 Subject: [PATCH] add loading screen this also avoids to doubleclick on end poll button Signed-off-by: Marcel Hibbe --- .../talk/polls/ui/PollLoadingFragment.kt | 74 +++++++++++++++++++ .../talk/polls/ui/PollMainDialogFragment.kt | 16 ++++ .../polls/viewmodels/PollMainViewModel.kt | 4 + .../main/res/layout/dialog_poll_loading.xml | 33 +++++++++ 4 files changed, 127 insertions(+) create mode 100644 app/src/main/java/com/nextcloud/talk/polls/ui/PollLoadingFragment.kt create mode 100644 app/src/main/res/layout/dialog_poll_loading.xml diff --git a/app/src/main/java/com/nextcloud/talk/polls/ui/PollLoadingFragment.kt b/app/src/main/java/com/nextcloud/talk/polls/ui/PollLoadingFragment.kt new file mode 100644 index 000000000..77b46f8aa --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/polls/ui/PollLoadingFragment.kt @@ -0,0 +1,74 @@ +/* + * Nextcloud Talk application + * + * @author Marcel Hibbe + * Copyright (C) 2022 Marcel Hibbe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.polls.ui + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.os.bundleOf +import androidx.fragment.app.Fragment +import autodagger.AutoInjector +import com.nextcloud.talk.application.NextcloudTalkApplication +import com.nextcloud.talk.databinding.DialogPollLoadingBinding + +@AutoInjector(NextcloudTalkApplication::class) +class PollLoadingFragment : Fragment() { + + private lateinit var binding: DialogPollLoadingBinding + + var fragmentHeight = 0 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) + fragmentHeight = arguments?.getInt(KEY_FRAGMENT_HEIGHT)!! + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + binding = DialogPollLoadingBinding.inflate(inflater, container, false) + binding.root.layoutParams.height = fragmentHeight + return binding.root + } + + companion object { + private val TAG = PollLoadingFragment::class.java.simpleName + private const val KEY_FRAGMENT_HEIGHT = "keyFragmentHeight" + + @JvmStatic + fun newInstance( + fragmentHeight: Int + ): PollLoadingFragment { + + val args = bundleOf( + KEY_FRAGMENT_HEIGHT to fragmentHeight, + ) + + val fragment = PollLoadingFragment() + fragment.arguments = args + return fragment + } + } +} 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 c432ff9bd..85cba1f64 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 @@ -93,6 +93,9 @@ class PollMainDialogFragment : DialogFragment() { initVotersAmount(state.showVotersAmount, state.poll.numVoters, true) showResultsScreen() } + is PollMainViewModel.LoadingState -> { + showLoadingScreen() + } is PollMainViewModel.DismissDialogState -> { dismiss() } @@ -101,6 +104,19 @@ class PollMainDialogFragment : DialogFragment() { } } + private fun showLoadingScreen() { + binding.root.post { + run() { + val fragmentHeight = binding.messagePollContentFragment.measuredHeight + + val contentFragment = PollLoadingFragment.newInstance(fragmentHeight) + val transaction = childFragmentManager.beginTransaction() + transaction.replace(binding.messagePollContentFragment.id, contentFragment) + transaction.commit() + } + } + } + private fun showVoteScreen() { val contentFragment = PollVoteFragment.newInstance() 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 1ecb9a03e..734b93fdf 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 @@ -50,6 +50,8 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito sealed interface ViewState object InitialState : ViewState object DismissDialogState : ViewState + object LoadingState : ViewState + open class PollVoteState( val poll: Poll, val showVotersAmount: Boolean, @@ -94,6 +96,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito } private fun loadPoll() { + _viewState.value = LoadingState repository.getPoll(roomToken, pollId) .doOnSubscribe { disposable = it } ?.subscribeOn(Schedulers.io()) @@ -102,6 +105,7 @@ class PollMainViewModel @Inject constructor(private val repository: PollReposito } fun endPoll() { + _viewState.value = LoadingState repository.closePoll(roomToken, pollId) .doOnSubscribe { disposable = it } ?.subscribeOn(Schedulers.io()) diff --git a/app/src/main/res/layout/dialog_poll_loading.xml b/app/src/main/res/layout/dialog_poll_loading.xml new file mode 100644 index 000000000..07e00ecd0 --- /dev/null +++ b/app/src/main/res/layout/dialog_poll_loading.xml @@ -0,0 +1,33 @@ + + + + + + +