From c6b49080af178b23094a4e73fc58cef6a33c7ed3 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 21 Mar 2022 17:33:53 +0100 Subject: [PATCH] Reworked how call logs are handled, should be more efficient --- .../fragments/DetailCallLogFragment.kt | 19 +++++--- .../DetailConferenceCallLogFragment.kt | 9 +--- .../fragments/MasterCallLogsFragment.kt | 2 +- .../history/viewmodels/CallLogViewModel.kt | 23 ++++------ .../viewmodels/CallLogsListViewModel.kt | 44 +++++-------------- .../res/layout/history_master_fragment.xml | 8 ++-- 6 files changed, 37 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt index 196468bad..3cdb943bc 100644 --- a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt +++ b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt @@ -28,7 +28,6 @@ import org.linphone.R import org.linphone.activities.* import org.linphone.activities.main.* import org.linphone.activities.main.history.viewmodels.CallLogViewModel -import org.linphone.activities.main.history.viewmodels.CallLogViewModelFactory import org.linphone.activities.main.viewmodels.SharedMainViewModel import org.linphone.activities.navigateToContact import org.linphone.activities.navigateToContacts @@ -61,16 +60,12 @@ class DetailCallLogFragment : GenericFragment() { return } - viewModel = ViewModelProvider( - this, - CallLogViewModelFactory(callLogGroup.lastCallLog) - )[CallLogViewModel::class.java] + viewModel = callLogGroup.lastCallLogViewModel binding.viewModel = viewModel + viewModel.addRelatedCallLogs(callLogGroup.callLogs) useMaterialSharedAxisXForwardAnimation = sharedViewModel.isSlidingPaneSlideable.value == false - viewModel.addRelatedCallLogs(callLogGroup.callLogs) - binding.setBackClickListener { goBack() } @@ -149,4 +144,14 @@ class DetailCallLogFragment : GenericFragment() { navigateToEmptyCallHistory() } } + + override fun onResume() { + super.onResume() + viewModel.enableListener(true) + } + + override fun onPause() { + viewModel.enableListener(false) + super.onPause() + } } diff --git a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailConferenceCallLogFragment.kt b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailConferenceCallLogFragment.kt index fa62146a8..d07e4814c 100644 --- a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailConferenceCallLogFragment.kt +++ b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailConferenceCallLogFragment.kt @@ -27,7 +27,6 @@ import org.linphone.R import org.linphone.activities.* import org.linphone.activities.main.* import org.linphone.activities.main.history.viewmodels.CallLogViewModel -import org.linphone.activities.main.history.viewmodels.CallLogViewModelFactory import org.linphone.activities.main.viewmodels.SharedMainViewModel import org.linphone.core.tools.Log import org.linphone.databinding.HistoryConfDetailFragmentBinding @@ -56,16 +55,12 @@ class DetailConferenceCallLogFragment : GenericFragment adapter.submitList(callLogs) diff --git a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt index f01739ee1..d87a50151 100644 --- a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt @@ -20,8 +20,6 @@ package org.linphone.activities.main.history.viewmodels import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList @@ -37,15 +35,6 @@ import org.linphone.utils.Event import org.linphone.utils.LinphoneUtils import org.linphone.utils.TimestampUtils -class CallLogViewModelFactory(private val callLog: CallLog) : - ViewModelProvider.NewInstanceFactory() { - - @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T { - return CallLogViewModel(callLog) as T - } -} - class CallLogViewModel(val callLog: CallLog, private val isRelated: Boolean = false) : GenericContactViewModel(callLog.remoteAddress) { val peerSipUri: String by lazy { LinphoneUtils.getDisplayableAddress(callLog.remoteAddress) @@ -150,8 +139,6 @@ class CallLogViewModel(val callLog: CallLog, private val isRelated: Boolean = fa waitForChatRoomCreation.value = false if (!isRelated) { - coreContext.core.addListener(listener) - val conferenceInfo = callLog.conferenceInfo if (conferenceInfo != null) { conferenceTime.value = TimestampUtils.timeToString(conferenceInfo.dateTime) @@ -181,8 +168,6 @@ class CallLogViewModel(val callLog: CallLog, private val isRelated: Boolean = fa fun destroy() { if (!isRelated) { - coreContext.core.removeListener(listener) - relatedCallLogs.value.orEmpty().forEach(CallLogViewModel::destroy) conferenceParticipantsData.value.orEmpty() .forEach(ConferenceSchedulingParticipantData::destroy) @@ -221,4 +206,12 @@ class CallLogViewModel(val callLog: CallLog, private val isRelated: Boolean = fa relatedCallLogs.value = list } + + fun enableListener(enable: Boolean) { + if (enable) { + coreContext.core.addListener(listener) + } else { + coreContext.core.removeListener(listener) + } + } } diff --git a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogsListViewModel.kt b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogsListViewModel.kt index a4c179fff..320a8f1d6 100644 --- a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogsListViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogsListViewModel.kt @@ -31,7 +31,7 @@ import org.linphone.utils.LinphoneUtils import org.linphone.utils.TimestampUtils class CallLogsListViewModel : ViewModel() { - val displayedCallLogs = MutableLiveData>() + val callLogs = MutableLiveData>() val filter = MutableLiveData() @@ -41,17 +41,8 @@ class CallLogsListViewModel : ViewModel() { MutableLiveData>() } - private val callLogs = MutableLiveData>() - private val missedCallLogs = MutableLiveData>() - private val conferenceCallLogs = MutableLiveData>() - private val listener: CoreListenerStub = object : CoreListenerStub() { - override fun onCallStateChanged( - core: Core, - call: Call, - state: Call.State, - message: String - ) { + override fun onCallLogUpdated(core: Core, log: CallLog) { updateCallLogs() } } @@ -75,9 +66,6 @@ class CallLogsListViewModel : ViewModel() { override fun onCleared() { callLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - missedCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - conferenceCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - displayedCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) coreContext.contactsManager.removeListener(contactsUpdatedListener) coreContext.core.removeListener(listener) @@ -87,17 +75,17 @@ class CallLogsListViewModel : ViewModel() { fun showAllCallLogs() { filter.value = CallLogsFilter.ALL - applyFilter() + updateCallLogs() } fun showOnlyMissedCallLogs() { filter.value = CallLogsFilter.MISSED - applyFilter() + updateCallLogs() } fun showOnlyConferenceCallLogs() { filter.value = CallLogsFilter.CONFERENCE - applyFilter() + updateCallLogs() } fun deleteCallLogGroup(callLog: GroupedCallLogData?) { @@ -157,27 +145,15 @@ class CallLogsListViewModel : ViewModel() { private fun updateCallLogs() { callLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - missedCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - conferenceCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) val allCallLogs = coreContext.core.callLogs - callLogs.value = computeCallLogs(allCallLogs, false, false) - missedCallLogs.value = computeCallLogs(allCallLogs, true, false) - conferenceCallLogs.value = computeCallLogs(allCallLogs, false, true) - applyFilter() - } + Log.i("[Call Logs] ${allCallLogs.size} call logs found") - private fun applyFilter() { - displayedCallLogs.value.orEmpty().forEach(GroupedCallLogData::destroy) - val displayedList = arrayListOf() - - when (filter.value) { - CallLogsFilter.MISSED -> displayedList.addAll(missedCallLogs.value.orEmpty()) - CallLogsFilter.CONFERENCE -> displayedList.addAll(conferenceCallLogs.value.orEmpty()) - else -> displayedList.addAll(callLogs.value.orEmpty()) + callLogs.value = when (filter.value) { + CallLogsFilter.MISSED -> computeCallLogs(allCallLogs, missed = true, conference = false) + CallLogsFilter.CONFERENCE -> computeCallLogs(allCallLogs, missed = false, conference = true) + else -> computeCallLogs(allCallLogs, missed = false, conference = false) } - - displayedCallLogs.value = displayedList } } diff --git a/app/src/main/res/layout/history_master_fragment.xml b/app/src/main/res/layout/history_master_fragment.xml index eedd0f79a..49017f257 100644 --- a/app/src/main/res/layout/history_master_fragment.xml +++ b/app/src/main/res/layout/history_master_fragment.xml @@ -114,7 +114,7 @@ + android:visibility="@{viewModel.callLogs.empty && viewModel.filter == CallLogsFilter.ALL ? View.VISIBLE : View.GONE}" /> + android:visibility="@{viewModel.callLogs.empty && viewModel.filter == CallLogsFilter.MISSED ? View.VISIBLE : View.GONE}" /> + android:visibility="@{viewModel.callLogs.empty && viewModel.filter == CallLogsFilter.CONFERENCE ? View.VISIBLE : View.GONE}" />