mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-22 13:05:31 +03:00
Notify that data set changed automatically when display item changes
Instead of explicitly calling "notifyDataSetChanged" after setting values on a ParticipantDisplayItem now the adapter observes all its items and calls "notifyDataSetChanged" automatically when any of them changes. Although this adds some boilerplate code it will make possible to update the ParticipantDisplayItems and automatically propagate the changes to the adapter when a model changes, rather than having to explicitly do it from the CallActivity. Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
parent
d67b04dff8
commit
8a316d94f5
4 changed files with 97 additions and 19 deletions
|
@ -740,6 +740,10 @@ public class CallActivity extends CallBaseActivity {
|
|||
}
|
||||
});
|
||||
|
||||
if (participantsAdapter != null) {
|
||||
participantsAdapter.destroy();
|
||||
}
|
||||
|
||||
participantsAdapter = new ParticipantsAdapter(
|
||||
this,
|
||||
participantDisplayItems,
|
||||
|
@ -1853,17 +1857,11 @@ public class CallActivity extends CallBaseActivity {
|
|||
String userId = userIdsBySessionId.get(sessionId);
|
||||
if (userId != null) {
|
||||
runOnUiThread(() -> {
|
||||
boolean notifyDataSetChanged = false;
|
||||
if (participantDisplayItems.get(sessionId + "-video") != null) {
|
||||
participantDisplayItems.get(sessionId + "-video").setUserId(userId);
|
||||
notifyDataSetChanged = true;
|
||||
}
|
||||
if (participantDisplayItems.get(sessionId + "-screen") != null) {
|
||||
participantDisplayItems.get(sessionId + "-screen").setUserId(userId);
|
||||
notifyDataSetChanged = true;
|
||||
}
|
||||
if (notifyDataSetChanged) {
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2527,17 +2525,11 @@ public class CallActivity extends CallBaseActivity {
|
|||
private void onOfferOrAnswer(String nick) {
|
||||
this.nick = nick;
|
||||
|
||||
boolean notifyDataSetChanged = false;
|
||||
if (participantDisplayItems.get(sessionId + "-video") != null) {
|
||||
participantDisplayItems.get(sessionId + "-video").setNick(nick);
|
||||
notifyDataSetChanged = true;
|
||||
}
|
||||
if (participantDisplayItems.get(sessionId + "-screen") != null) {
|
||||
participantDisplayItems.get(sessionId + "-screen").setNick(nick);
|
||||
notifyDataSetChanged = true;
|
||||
}
|
||||
if (notifyDataSetChanged) {
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2582,7 +2574,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
runOnUiThread(() -> {
|
||||
if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setAudioEnabled(true);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2592,7 +2583,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
runOnUiThread(() -> {
|
||||
if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setAudioEnabled(false);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2602,7 +2592,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
runOnUiThread(() -> {
|
||||
if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setStreamEnabled(true);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2612,7 +2601,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
runOnUiThread(() -> {
|
||||
if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setStreamEnabled(false);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2622,7 +2610,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
runOnUiThread(() -> {
|
||||
if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setNick(nick);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2664,7 +2651,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
ParticipantDisplayItem participantDisplayItem = participantDisplayItems.get(participantDisplayItemId);
|
||||
participantDisplayItem.setMediaStream(mediaStream);
|
||||
participantDisplayItem.setStreamEnabled(hasAtLeastOneVideoStream);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -2675,7 +2661,6 @@ public class CallActivity extends CallBaseActivity {
|
|||
updateSelfVideoViewIceConnectionState(iceConnectionState);
|
||||
} else if (participantDisplayItems.get(participantDisplayItemId) != null) {
|
||||
participantDisplayItems.get(participantDisplayItemId).setIceConnectionState(iceConnectionState);
|
||||
participantsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
if (iceConnectionState == PeerConnection.IceConnectionState.CLOSED) {
|
||||
|
|
|
@ -9,6 +9,13 @@ import org.webrtc.MediaStream;
|
|||
import org.webrtc.PeerConnection;
|
||||
|
||||
public class ParticipantDisplayItem {
|
||||
|
||||
public interface Observer {
|
||||
void onChange();
|
||||
}
|
||||
|
||||
private final ParticipantDisplayItemNotifier participantDisplayItemNotifier = new ParticipantDisplayItemNotifier();
|
||||
|
||||
private final String baseUrl;
|
||||
private final String defaultGuestNick;
|
||||
private final EglBase rootEglBase;
|
||||
|
@ -43,6 +50,8 @@ public class ParticipantDisplayItem {
|
|||
this.userId = userId;
|
||||
|
||||
this.updateUrlForAvatar();
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
|
@ -52,6 +61,8 @@ public class ParticipantDisplayItem {
|
|||
|
||||
public void setIceConnectionState(PeerConnection.IceConnectionState iceConnectionState) {
|
||||
this.iceConnectionState = iceConnectionState;
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public String getNick() {
|
||||
|
@ -66,6 +77,8 @@ public class ParticipantDisplayItem {
|
|||
this.nick = nick;
|
||||
|
||||
this.updateUrlForAvatar();
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public String getUrlForAvatar() {
|
||||
|
@ -86,6 +99,8 @@ public class ParticipantDisplayItem {
|
|||
|
||||
public void setMediaStream(MediaStream mediaStream) {
|
||||
this.mediaStream = mediaStream;
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public boolean isStreamEnabled() {
|
||||
|
@ -94,6 +109,8 @@ public class ParticipantDisplayItem {
|
|||
|
||||
public void setStreamEnabled(boolean streamEnabled) {
|
||||
this.streamEnabled = streamEnabled;
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public EglBase getRootEglBase() {
|
||||
|
@ -106,6 +123,16 @@ public class ParticipantDisplayItem {
|
|||
|
||||
public void setAudioEnabled(boolean audioEnabled) {
|
||||
isAudioEnabled = audioEnabled;
|
||||
|
||||
participantDisplayItemNotifier.notifyChange();
|
||||
}
|
||||
|
||||
public void addObserver(Observer observer) {
|
||||
participantDisplayItemNotifier.addObserver(observer);
|
||||
}
|
||||
|
||||
public void removeObserver(Observer observer) {
|
||||
participantDisplayItemNotifier.removeObserver(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Nextcloud Talk application
|
||||
*
|
||||
* @author Daniel Calviño Sánchez
|
||||
* Copyright (C) 2022 Daniel Calviño Sánchez <danxuliu@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.nextcloud.talk.adapters;
|
||||
|
||||
import com.nextcloud.talk.signaling.SignalingMessageReceiver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Helper class to register and notify ParticipantDisplayItem.Observers.
|
||||
*
|
||||
* This class is only meant for internal use by ParticipantDisplayItem; observers must register themselves against a
|
||||
* ParticipantDisplayItem rather than against a ParticipantDisplayItemNotifier.
|
||||
*/
|
||||
class ParticipantDisplayItemNotifier {
|
||||
|
||||
private final Set<ParticipantDisplayItem.Observer> participantDisplayItemObservers = new LinkedHashSet<>();
|
||||
|
||||
public synchronized void addObserver(ParticipantDisplayItem.Observer observer) {
|
||||
if (observer == null) {
|
||||
throw new IllegalArgumentException("ParticipantDisplayItem.Observer can not be null");
|
||||
}
|
||||
|
||||
participantDisplayItemObservers.add(observer);
|
||||
}
|
||||
|
||||
public synchronized void removeObserver(ParticipantDisplayItem.Observer observer) {
|
||||
participantDisplayItemObservers.remove(observer);
|
||||
}
|
||||
|
||||
public synchronized void notifyChange() {
|
||||
for (ParticipantDisplayItem.Observer observer : new ArrayList<>(participantDisplayItemObservers)) {
|
||||
observer.onChange();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,6 +29,8 @@ public class ParticipantsAdapter extends BaseAdapter {
|
|||
|
||||
private static final String TAG = "ParticipantsAdapter";
|
||||
|
||||
private final ParticipantDisplayItem.Observer participantDisplayItemObserver = this::notifyDataSetChanged;
|
||||
|
||||
private final Context mContext;
|
||||
private final ArrayList<ParticipantDisplayItem> participantDisplayItems;
|
||||
private final RelativeLayout gridViewWrapper;
|
||||
|
@ -50,8 +52,17 @@ public class ParticipantsAdapter extends BaseAdapter {
|
|||
|
||||
this.participantDisplayItems = new ArrayList<>();
|
||||
this.participantDisplayItems.addAll(participantDisplayItems.values());
|
||||
|
||||
for (ParticipantDisplayItem participantDisplayItem : this.participantDisplayItems) {
|
||||
participantDisplayItem.addObserver(participantDisplayItemObserver);
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
for (ParticipantDisplayItem participantDisplayItem : participantDisplayItems) {
|
||||
participantDisplayItem.removeObserver(participantDisplayItemObserver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
|
|
Loading…
Reference in a new issue