diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt index b6399e7..a3ff1e0 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt @@ -100,19 +100,24 @@ class GroupsFragment : Fragment(), CommandClientHandler { commandClient = null } + private var displayed = false + private fun updateDisplayed(newValue: Boolean) { + if (displayed != newValue) { + displayed = newValue + binding.statusText.isVisible = !displayed + binding.container.isVisible = displayed + } + } + override fun connected() { - val binding = _binding ?: return lifecycleScope.launch(Dispatchers.Main) { - binding.statusText.isVisible = false - binding.container.isVisible = true + updateDisplayed(true) } } override fun disconnected(message: String?) { - val binding = _binding ?: return lifecycleScope.launch(Dispatchers.Main) { - binding.statusText.isVisible = true - binding.container.isVisible = false + updateDisplayed(false) } } @@ -123,6 +128,7 @@ class GroupsFragment : Fragment(), CommandClientHandler { groups.add(message.next()) } activity?.runOnUiThread { + updateDisplayed(groups.isNotEmpty()) adapter.groups = groups adapter.notifyDataSetChanged() } diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt index 771d9be..be3feda 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/main/DashboardFragment.kt @@ -34,6 +34,7 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { private fun onCreate() { val activity = activity ?: return binding.dashboardPager.adapter = Adapter(this) + binding.dashboardPager.offscreenPageLimit = Page.values().size TabLayoutMediator(binding.dashboardTabLayout, binding.dashboardPager) { tab, position -> tab.setText(Page.values()[position].titleRes) }.attach() @@ -42,8 +43,7 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { Status.Stopped -> { binding.fab.setImageResource(R.drawable.ic_play_arrow_24) binding.fab.show() - binding.dashboardTabLayout.isVisible = false - binding.dashboardPager.isUserInputEnabled = false + disablePager() } Status.Starting -> { @@ -53,12 +53,12 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { Status.Started -> { binding.fab.setImageResource(R.drawable.ic_stop_24) binding.fab.show() - binding.dashboardTabLayout.isVisible = true - binding.dashboardPager.isUserInputEnabled = true + enablePager() } Status.Stopping -> { binding.fab.hide() + disablePager() } else -> {} @@ -79,6 +79,17 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { } } + private fun enablePager() { + binding.dashboardTabLayout.isVisible = true + binding.dashboardPager.isUserInputEnabled = true + } + + private fun disablePager() { + binding.dashboardTabLayout.isVisible = false + binding.dashboardPager.isUserInputEnabled = false + binding.dashboardPager.setCurrentItem(0, false) + } + enum class Page(@StringRes val titleRes: Int, val fragmentClass: Class) { Overview(R.string.title_overview, OverviewFragment::class.java), Groups(R.string.title_groups, GroupsFragment::class.java);