From 5d89d8b098b1689ef1836f9c77785a2afe626998 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 15 Apr 2021 09:58:17 +0200 Subject: [PATCH] Finished ViewModel to Data rework --- .../ConferenceParticipantData.kt} | 8 ++-- .../call/viewmodels/CallViewModel.kt | 1 - .../call/viewmodels/CallsViewModel.kt | 3 ++ .../call/viewmodels/ConferenceViewModel.kt | 7 +-- .../call/views/ConferenceParticipantView.kt | 8 ++-- .../chat/adapters/ChatMessagesListAdapter.kt | 18 ++++---- .../ChatRoomCreationContactsAdapter.kt | 2 +- .../adapters/GroupInfoParticipantsAdapter.kt | 4 +- .../main/chat/adapters/ImdnAdapter.kt | 4 +- .../adapters/RecordingsListAdapter.kt | 4 +- .../org/linphone/contact/ContactAvatarView.kt | 12 ++--- .../main/res/layout/call_conference_cell.xml | 4 +- .../layout/call_conference_participant.xml | 12 ++--- app/src/main/res/layout/call_paused.xml | 2 +- .../layout/call_statistics_cell_header.xml | 2 +- .../main/res/layout/chat_event_list_cell.xml | 8 ++-- .../res/layout/chat_message_list_cell.xml | 44 +++++++++---------- .../chat_room_creation_contact_cell.xml | 18 ++++---- .../layout/chat_room_devices_group_cell.xml | 2 +- .../chat_room_group_info_participant_cell.xml | 24 +++++----- .../res/layout/chat_room_imdn_fragment.xml | 2 +- .../chat_room_imdn_participant_cell.xml | 10 ++--- .../main/res/layout/chat_room_list_cell.xml | 2 +- app/src/main/res/layout/contact_list_cell.xml | 2 +- app/src/main/res/layout/history_list_cell.xml | 2 +- .../main/res/layout/recording_list_cell.xml | 18 ++++---- 26 files changed, 113 insertions(+), 110 deletions(-) rename app/src/main/java/org/linphone/activities/call/{viewmodels/ConferenceParticipantViewModel.kt => data/ConferenceParticipantData.kt} (90%) diff --git a/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceParticipantViewModel.kt b/app/src/main/java/org/linphone/activities/call/data/ConferenceParticipantData.kt similarity index 90% rename from app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceParticipantViewModel.kt rename to app/src/main/java/org/linphone/activities/call/data/ConferenceParticipantData.kt index b18cee149..2af5a7a8a 100644 --- a/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceParticipantViewModel.kt +++ b/app/src/main/java/org/linphone/activities/call/data/ConferenceParticipantData.kt @@ -17,19 +17,19 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.linphone.activities.call.viewmodels +package org.linphone.activities.call.data import androidx.lifecycle.MutableLiveData -import org.linphone.contact.GenericContactViewModel +import org.linphone.contact.GenericContactData import org.linphone.core.Conference import org.linphone.core.Participant import org.linphone.core.tools.Log -class ConferenceParticipantViewModel( +class ConferenceParticipantData( private val conference: Conference, val participant: Participant ) : - GenericContactViewModel(participant.address) { + GenericContactData(participant.address) { private val isAdmin = MutableLiveData() val isMeAdmin = MutableLiveData() diff --git a/app/src/main/java/org/linphone/activities/call/viewmodels/CallViewModel.kt b/app/src/main/java/org/linphone/activities/call/viewmodels/CallViewModel.kt index 7836a93ab..5aeff9443 100644 --- a/app/src/main/java/org/linphone/activities/call/viewmodels/CallViewModel.kt +++ b/app/src/main/java/org/linphone/activities/call/viewmodels/CallViewModel.kt @@ -124,7 +124,6 @@ open class CallViewModel(val call: Call) : GenericContactViewModel(call.remoteAd } fun destroy() { - // TODO: call it from CallsViewModel (after conference rework merge) call.removeListener(listener) } diff --git a/app/src/main/java/org/linphone/activities/call/viewmodels/CallsViewModel.kt b/app/src/main/java/org/linphone/activities/call/viewmodels/CallsViewModel.kt index 1aac1e78e..ed235e5a4 100644 --- a/app/src/main/java/org/linphone/activities/call/viewmodels/CallsViewModel.kt +++ b/app/src/main/java/org/linphone/activities/call/viewmodels/CallsViewModel.kt @@ -53,6 +53,7 @@ class CallsViewModel : ViewModel() { val currentCall = core.currentCall if (currentCall == null) { + currentCallViewModel.value?.destroy() currentCallViewModel.value = null } else if (currentCallViewModel.value == null) { currentCallViewModel.value = CallViewModel(currentCall) @@ -89,6 +90,7 @@ class CallsViewModel : ViewModel() { val currentCall = coreContext.core.currentCall if (currentCall != null) { + currentCallViewModel.value?.destroy() currentCallViewModel.value = CallViewModel(currentCall) } @@ -142,6 +144,7 @@ class CallsViewModel : ViewModel() { for (pausedCallViewModel in list) { if (pausedCallViewModel.call == call) { + pausedCallViewModel.destroy() list.remove(pausedCallViewModel) break } diff --git a/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceViewModel.kt b/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceViewModel.kt index 925c4a4be..164a067bb 100644 --- a/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceViewModel.kt +++ b/app/src/main/java/org/linphone/activities/call/viewmodels/ConferenceViewModel.kt @@ -22,6 +22,7 @@ package org.linphone.activities.call.viewmodels import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import org.linphone.LinphoneApplication.Companion.coreContext +import org.linphone.activities.call.data.ConferenceParticipantData import org.linphone.core.* import org.linphone.core.tools.Log @@ -32,7 +33,7 @@ class ConferenceViewModel : ViewModel() { val conferenceAddress = MutableLiveData
() - val conferenceParticipants = MutableLiveData>() + val conferenceParticipants = MutableLiveData>() val isInConference = MutableLiveData() @@ -144,10 +145,10 @@ class ConferenceViewModel : ViewModel() { } private fun updateParticipantsList(conference: Conference) { - val participants = arrayListOf() + val participants = arrayListOf() for (participant in conference.participantList) { Log.i("[Conference VM] Participant found: ${participant.address.asStringUriOnly()}") - val viewModel = ConferenceParticipantViewModel(conference, participant) + val viewModel = ConferenceParticipantData(conference, participant) participants.add(viewModel) } conferenceParticipants.value = participants diff --git a/app/src/main/java/org/linphone/activities/call/views/ConferenceParticipantView.kt b/app/src/main/java/org/linphone/activities/call/views/ConferenceParticipantView.kt index 484288dbc..16499951f 100644 --- a/app/src/main/java/org/linphone/activities/call/views/ConferenceParticipantView.kt +++ b/app/src/main/java/org/linphone/activities/call/views/ConferenceParticipantView.kt @@ -26,7 +26,7 @@ import android.view.LayoutInflater import android.widget.LinearLayout import androidx.databinding.DataBindingUtil import org.linphone.R -import org.linphone.activities.call.viewmodels.ConferenceParticipantViewModel +import org.linphone.activities.call.data.ConferenceParticipantData import org.linphone.core.tools.Log import org.linphone.databinding.CallConferenceParticipantBinding @@ -58,11 +58,11 @@ class ConferenceParticipantView : LinearLayout { ) } - fun setViewModel(viewModel: ConferenceParticipantViewModel) { - binding.viewModel = viewModel + fun setData(data: ConferenceParticipantData) { + binding.data = data val currentTimeSecs = System.currentTimeMillis() - val participantTime = viewModel.participant.creationTime * 1000 // Linphone timestamps are in seconds + val participantTime = data.participant.creationTime * 1000 // Linphone timestamps are in seconds val diff = currentTimeSecs - participantTime Log.i("[Conference Participant] Participant joined conference at $participantTime == ${diff / 1000} seconds ago.") binding.callTimer.base = SystemClock.elapsedRealtime() - diff diff --git a/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatMessagesListAdapter.kt b/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatMessagesListAdapter.kt index 44ac99b3a..d6e0f2de5 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatMessagesListAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatMessagesListAdapter.kt @@ -119,7 +119,7 @@ class ChatMessagesListAdapter( override fun onViewRecycled(holder: RecyclerView.ViewHolder) { when (holder) { - is ChatMessageViewHolder -> holder.binding.viewModel?.destroy() + is ChatMessageViewHolder -> holder.binding.data?.destroy() } } @@ -141,7 +141,7 @@ class ChatMessagesListAdapter( val chatMessage = eventLog.chatMessage chatMessage ?: return val chatMessageViewModel = ChatMessageData(chatMessage, contentClickedListener) - viewModel = chatMessageViewModel + data = chatMessageViewModel lifecycleOwner = viewLifecycleOwner @@ -248,7 +248,7 @@ class ChatMessagesListAdapter( } private fun resendMessage() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { chatMessage.userData = adapterPosition resendMessageEvent.value = Event(chatMessage) @@ -256,7 +256,7 @@ class ChatMessagesListAdapter( } private fun copyTextToClipboard() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { val content = chatMessage.contents.find { content -> content.isText } if (content != null) { @@ -269,21 +269,21 @@ class ChatMessagesListAdapter( } private fun forwardMessage() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { forwardMessageEvent.value = Event(chatMessage) } } private fun showImdnDeliveryFragment() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { showImdnForMessageEvent.value = Event(chatMessage) } } private fun deleteMessage() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { chatMessage.userData = adapterPosition deleteMessageEvent.value = Event(chatMessage) @@ -291,7 +291,7 @@ class ChatMessagesListAdapter( } private fun addSenderToContacts() { - val chatMessage = binding.viewModel?.chatMessage + val chatMessage = binding.data?.chatMessage if (chatMessage != null) { val copy = chatMessage.fromAddress.clone() copy.clean() // To remove gruu if any @@ -306,7 +306,7 @@ class ChatMessagesListAdapter( fun bind(eventLog: EventLog) { with(binding) { val eventViewModel = EventData(eventLog) - viewModel = eventViewModel + data = eventViewModel binding.lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatRoomCreationContactsAdapter.kt b/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatRoomCreationContactsAdapter.kt index 830806546..aef208d05 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatRoomCreationContactsAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/adapters/ChatRoomCreationContactsAdapter.kt @@ -75,7 +75,7 @@ class ChatRoomCreationContactsAdapter( fun bind(searchResult: SearchResult) { with(binding) { val searchResultViewModel = ChatRoomCreationContactData(searchResult) - viewModel = searchResultViewModel + data = searchResultViewModel lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/activities/main/chat/adapters/GroupInfoParticipantsAdapter.kt b/app/src/main/java/org/linphone/activities/main/chat/adapters/GroupInfoParticipantsAdapter.kt index 1e52b7dea..fb1301dab 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/adapters/GroupInfoParticipantsAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/adapters/GroupInfoParticipantsAdapter.kt @@ -56,7 +56,7 @@ class GroupInfoParticipantsAdapter( } override fun onViewRecycled(holder: RecyclerView.ViewHolder) { - (holder as ViewHolder).binding.viewModel?.destroy() + (holder as ViewHolder).binding.data?.destroy() } fun showAdminControls(show: Boolean) { @@ -71,7 +71,7 @@ class GroupInfoParticipantsAdapter( with(binding) { val participantViewModel = GroupInfoParticipantData(participant) participantViewModel.showAdminControls.value = showAdmin - viewModel = participantViewModel + data = participantViewModel lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/activities/main/chat/adapters/ImdnAdapter.kt b/app/src/main/java/org/linphone/activities/main/chat/adapters/ImdnAdapter.kt index c756bbad2..0827e49b8 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/adapters/ImdnAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/adapters/ImdnAdapter.kt @@ -52,7 +52,7 @@ class ImdnAdapter( } override fun onViewRecycled(holder: RecyclerView.ViewHolder) { - (holder as ViewHolder).binding.viewModel?.destroy() + (holder as ViewHolder).binding.data?.destroy() } inner class ViewHolder( @@ -60,7 +60,7 @@ class ImdnAdapter( ) : RecyclerView.ViewHolder(binding.root) { fun bind(participantImdnState: ParticipantImdnState) { with(binding) { - viewModel = ImdnParticipantData(participantImdnState) + data = ImdnParticipantData(participantImdnState) lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/activities/main/recordings/adapters/RecordingsListAdapter.kt b/app/src/main/java/org/linphone/activities/main/recordings/adapters/RecordingsListAdapter.kt index 693507eb1..8151bf69f 100644 --- a/app/src/main/java/org/linphone/activities/main/recordings/adapters/RecordingsListAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/recordings/adapters/RecordingsListAdapter.kt @@ -64,7 +64,7 @@ class RecordingsListAdapter( } override fun onViewRecycled(holder: RecyclerView.ViewHolder) { - (holder as ViewHolder).binding.viewModel?.destroy() + (holder as ViewHolder).binding.data?.destroy() } inner class ViewHolder( @@ -72,7 +72,7 @@ class RecordingsListAdapter( ) : RecyclerView.ViewHolder(binding.root) { fun bind(recording: RecordingData) { with(binding) { - viewModel = recording + data = recording lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/contact/ContactAvatarView.kt b/app/src/main/java/org/linphone/contact/ContactAvatarView.kt index 1c40e219f..669f8b8ef 100644 --- a/app/src/main/java/org/linphone/contact/ContactAvatarView.kt +++ b/app/src/main/java/org/linphone/contact/ContactAvatarView.kt @@ -51,27 +51,27 @@ class ContactAvatarView : LinearLayout { ) } - fun setViewModel(viewModel: ContactDataInterface) { - val contact: Contact? = viewModel.contact.value + fun setData(data: ContactDataInterface) { + val contact: Contact? = data.contact.value val initials = if (contact != null) { AppUtils.getInitials(contact.fullName ?: contact.firstName + " " + contact.lastName) } else { - AppUtils.getInitials(viewModel.displayName) + AppUtils.getInitials(data.displayName) } binding.initials = initials binding.generatedAvatarVisibility = initials.isNotEmpty() && initials != "+" - binding.groupChatAvatarVisibility = viewModel.showGroupChatAvatar + binding.groupChatAvatarVisibility = data.showGroupChatAvatar binding.imagePath = contact?.getContactThumbnailPictureUri() binding.borderVisibility = corePreferences.showBorderOnContactAvatar - binding.securityIcon = when (viewModel.securityLevel) { + binding.securityIcon = when (data.securityLevel) { ChatRoomSecurityLevel.Safe -> R.drawable.security_2_indicator ChatRoomSecurityLevel.Encrypted -> R.drawable.security_1_indicator else -> R.drawable.security_alert_indicator } - binding.securityContentDescription = when (viewModel.securityLevel) { + binding.securityContentDescription = when (data.securityLevel) { ChatRoomSecurityLevel.Safe -> R.string.content_description_security_level_safe ChatRoomSecurityLevel.Encrypted -> R.string.content_description_security_level_encrypted else -> R.string.content_description_security_level_unsafe diff --git a/app/src/main/res/layout/call_conference_cell.xml b/app/src/main/res/layout/call_conference_cell.xml index c47b3f0e5..f829398c4 100644 --- a/app/src/main/res/layout/call_conference_cell.xml +++ b/app/src/main/res/layout/call_conference_cell.xml @@ -5,12 +5,12 @@ + type="org.linphone.activities.call.data.ConferenceParticipantData" /> + bind:data="@{data}"/> \ No newline at end of file diff --git a/app/src/main/res/layout/call_conference_participant.xml b/app/src/main/res/layout/call_conference_participant.xml index c8a331066..0f8ef7c20 100644 --- a/app/src/main/res/layout/call_conference_participant.xml +++ b/app/src/main/res/layout/call_conference_participant.xml @@ -6,8 +6,8 @@ + name="data" + type="org.linphone.activities.call.data.ConferenceParticipantData" /> + app:data="@{data}"/> + app:justifyContent="@{data.chatMessage.outgoing ? JustifyContent.FLEX_END : JustifyContent.FLEX_START}"/> @@ -19,7 +19,7 @@ + app:showLimeCapability="@{data.hasLimeX3DHCapability}" + app:data="@{data}"/> diff --git a/app/src/main/res/layout/chat_room_devices_group_cell.xml b/app/src/main/res/layout/chat_room_devices_group_cell.xml index 35461e5c1..259e78908 100644 --- a/app/src/main/res/layout/chat_room_devices_group_cell.xml +++ b/app/src/main/res/layout/chat_room_devices_group_cell.xml @@ -31,7 +31,7 @@ android:layout_margin="5dp" android:gravity="center" app:showSecurityLevel="@{true}" - app:viewModel="@{data}" + app:data="@{data}" tools:layout="@layout/contact_avatar" /> @@ -84,8 +84,8 @@ @@ -121,14 +121,14 @@ android:orientation="vertical"> @@ -22,11 +22,11 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" tools:layout="@layout/contact_avatar" - app:viewModel="@{viewModel}"/> + app:data="@{data}"/> + app:data="@{viewModel}"/> + app:data="@{viewModel}"/> @@ -79,7 +79,7 @@ android:text=" - " /> @@ -95,7 +95,7 @@ android:orientation="horizontal"> @@ -106,7 +106,7 @@ android:text="/" /> @@ -114,9 +114,9 @@