Reworked how call logs are handled, should be more efficient

This commit is contained in:
Sylvain Berfini 2022-03-21 17:33:53 +01:00
parent 70f7f8c100
commit c6b49080af
6 changed files with 37 additions and 68 deletions

View file

@ -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<HistoryDetailFragmentBinding>() {
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<HistoryDetailFragmentBinding>() {
navigateToEmptyCallHistory()
}
}
override fun onResume() {
super.onResume()
viewModel.enableListener(true)
}
override fun onPause() {
viewModel.enableListener(false)
super.onPause()
}
}

View file

@ -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<HistoryConfDetailFragmen
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()
}

View file

@ -192,7 +192,7 @@ class MasterCallLogsFragment : MasterFragment<HistoryMasterFragmentBinding, Call
val headerItemDecoration = RecyclerViewHeaderDecoration(requireContext(), adapter)
binding.callLogsList.addItemDecoration(headerItemDecoration)
listViewModel.displayedCallLogs.observe(
listViewModel.callLogs.observe(
viewLifecycleOwner
) { callLogs ->
adapter.submitList(callLogs)

View file

@ -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 <T : ViewModel> create(modelClass: Class<T>): 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)
}
}
}

View file

@ -31,7 +31,7 @@ import org.linphone.utils.LinphoneUtils
import org.linphone.utils.TimestampUtils
class CallLogsListViewModel : ViewModel() {
val displayedCallLogs = MutableLiveData<List<GroupedCallLogData>>()
val callLogs = MutableLiveData<List<GroupedCallLogData>>()
val filter = MutableLiveData<CallLogsFilter>()
@ -41,17 +41,8 @@ class CallLogsListViewModel : ViewModel() {
MutableLiveData<Event<Boolean>>()
}
private val callLogs = MutableLiveData<ArrayList<GroupedCallLogData>>()
private val missedCallLogs = MutableLiveData<ArrayList<GroupedCallLogData>>()
private val conferenceCallLogs = MutableLiveData<ArrayList<GroupedCallLogData>>()
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<GroupedCallLogData>()
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
}
}

View file

@ -114,7 +114,7 @@
<ImageView
android:onClick="@{editClickListener}"
android:enabled="@{!viewModel.displayedCallLogs.empty}"
android:enabled="@{!viewModel.callLogs.empty}"
android:contentDescription="@string/content_description_enter_edition_mode"
android:layout_width="0dp"
android:layout_height="match_parent"
@ -145,7 +145,7 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/no_call_history"
android:visibility="@{viewModel.displayedCallLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.ALL ? View.VISIBLE : View.GONE}" />
android:visibility="@{viewModel.callLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.ALL ? View.VISIBLE : View.GONE}" />
<TextView
style="@style/empty_list_font"
@ -153,7 +153,7 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/no_missed_call_history"
android:visibility="@{viewModel.displayedCallLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.MISSED ? View.VISIBLE : View.GONE}" />
android:visibility="@{viewModel.callLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.MISSED ? View.VISIBLE : View.GONE}" />
<TextView
style="@style/empty_list_font"
@ -161,7 +161,7 @@
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/no_conference_call_history"
android:visibility="@{viewModel.displayedCallLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.CONFERENCE ? View.VISIBLE : View.GONE}" />
android:visibility="@{viewModel.callLogs.empty &amp;&amp; viewModel.filter == CallLogsFilter.CONFERENCE ? View.VISIBLE : View.GONE}" />
<View
android:layout_width="1dp"