From 09bbe05f08f62387e6db8eddb6432e3b779df036 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 22 Aug 2023 17:24:25 +0200 Subject: [PATCH] Started bottom nav bar viewmodel + scroll to top when adding new items to calls history list --- .../main/calls/fragment/CallsListFragment.kt | 1 + .../ui/main/fragment/BottomNavBarFragment.kt | 22 +++--- .../main/viewmodel/BottomNavBarViewModel.kt | 74 +++++++++++++++++++ .../main/res/layout-land/bottom_nav_bar.xml | 39 +++------- app/src/main/res/layout/bottom_nav_bar.xml | 39 +++------- .../res/layout/conversations_fragment.xml | 1 - 6 files changed, 112 insertions(+), 64 deletions(-) create mode 100644 app/src/main/java/org/linphone/ui/main/viewmodel/BottomNavBarViewModel.kt diff --git a/app/src/main/java/org/linphone/ui/main/calls/fragment/CallsListFragment.kt b/app/src/main/java/org/linphone/ui/main/calls/fragment/CallsListFragment.kt index dd20acb55..565757c2e 100644 --- a/app/src/main/java/org/linphone/ui/main/calls/fragment/CallsListFragment.kt +++ b/app/src/main/java/org/linphone/ui/main/calls/fragment/CallsListFragment.kt @@ -156,6 +156,7 @@ class CallsListFragment : GenericFragment() { listViewModel.callLogs.observe(viewLifecycleOwner) { adapter.submitList(it) + binding.callsList.scrollToPosition(0) (view.parent as? ViewGroup)?.doOnPreDraw { startPostponedEnterTransition() diff --git a/app/src/main/java/org/linphone/ui/main/fragment/BottomNavBarFragment.kt b/app/src/main/java/org/linphone/ui/main/fragment/BottomNavBarFragment.kt index c134e679c..8aebb1b0e 100644 --- a/app/src/main/java/org/linphone/ui/main/fragment/BottomNavBarFragment.kt +++ b/app/src/main/java/org/linphone/ui/main/fragment/BottomNavBarFragment.kt @@ -27,9 +27,9 @@ import android.view.ViewGroup import androidx.annotation.UiThread import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider -import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R import org.linphone.databinding.BottomNavBarBinding +import org.linphone.ui.main.viewmodel.BottomNavBarViewModel import org.linphone.ui.main.viewmodel.SharedMainViewModel import org.linphone.utils.Event import org.linphone.utils.setKeyboardInsetListener @@ -38,6 +38,8 @@ import org.linphone.utils.setKeyboardInsetListener class BottomNavBarFragment : Fragment() { private lateinit var binding: BottomNavBarBinding + private lateinit var viewModel: BottomNavBarViewModel + private lateinit var sharedViewModel: SharedMainViewModel override fun onCreateView( @@ -52,12 +54,17 @@ class BottomNavBarFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + binding.lifecycleOwner = viewLifecycleOwner + + viewModel = requireActivity().run { + ViewModelProvider(this)[BottomNavBarViewModel::class.java] + } + binding.viewModel = viewModel + sharedViewModel = requireActivity().run { ViewModelProvider(this)[SharedMainViewModel::class.java] } - binding.lifecycleOwner = viewLifecycleOwner - binding.root.setKeyboardInsetListener { keyboardVisible -> val portraitOrientation = resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE binding.bottomNavBar.visibility = if (!portraitOrientation || !keyboardVisible) View.VISIBLE else View.GONE @@ -86,12 +93,9 @@ class BottomNavBarFragment : Fragment() { } sharedViewModel.currentlyDisplayedFragment.observe(viewLifecycleOwner) { - binding.contactsSelected = it == R.id.contactsFragment - binding.callsSelected = it == R.id.callsFragment - binding.conversationsSelected = it == R.id.conversationsFragment + viewModel.contactsSelected.value = it == R.id.contactsFragment + viewModel.callsSelected.value = it == R.id.callsFragment + viewModel.conversationsSelected.value = it == R.id.conversationsFragment } - - binding.hideConversations = corePreferences.disableChat || true // TODO - binding.hideMeetings = true // TODO } } diff --git a/app/src/main/java/org/linphone/ui/main/viewmodel/BottomNavBarViewModel.kt b/app/src/main/java/org/linphone/ui/main/viewmodel/BottomNavBarViewModel.kt new file mode 100644 index 000000000..628a83fbd --- /dev/null +++ b/app/src/main/java/org/linphone/ui/main/viewmodel/BottomNavBarViewModel.kt @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2010-2023 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * 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 org.linphone.ui.main.viewmodel + +import androidx.annotation.UiThread +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import org.linphone.LinphoneApplication.Companion.coreContext +import org.linphone.LinphoneApplication.Companion.corePreferences +import org.linphone.core.Call +import org.linphone.core.Core +import org.linphone.core.CoreListenerStub + +class BottomNavBarViewModel @UiThread constructor() : ViewModel() { + companion object { + const val TAG = "[Bottom Navigation Bar ViewModel]" + } + + val contactsSelected = MutableLiveData() + + val callsSelected = MutableLiveData() + + val conversationsSelected = MutableLiveData() + + val meetingsSelected = MutableLiveData() + + val hideConversations = MutableLiveData() + + val hideMeetings = MutableLiveData() + + private val coreListener = object : CoreListenerStub() { + override fun onCallStateChanged( + core: Core, + call: Call, + state: Call.State?, + message: String + ) { + } + } + + init { + coreContext.postOnCoreThread { core -> + core.addListener(coreListener) + } + + hideConversations.value = corePreferences.disableChat || true // TODO + hideMeetings.value = true // TODO + } + + override fun onCleared() { + super.onCleared() + + coreContext.postOnCoreThread { core -> + core.removeListener(coreListener) + } + } +} diff --git a/app/src/main/res/layout-land/bottom_nav_bar.xml b/app/src/main/res/layout-land/bottom_nav_bar.xml index ec2cc1263..674859e40 100644 --- a/app/src/main/res/layout-land/bottom_nav_bar.xml +++ b/app/src/main/res/layout-land/bottom_nav_bar.xml @@ -19,23 +19,8 @@ name="onMeetingsClicked" type="View.OnClickListener" /> - - - - - + name="viewModel" + type="org.linphone.ui.main.viewmodel.BottomNavBarViewModel" /> - - - - - + name="viewModel" + type="org.linphone.ui.main.viewmodel.BottomNavBarViewModel" />