package net.ktnx.mobileledger.utils;
import android.os.Build;
-import android.util.Log;
import org.jetbrains.annotations.NotNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
+import static net.ktnx.mobileledger.utils.Logger.debug;
+
public class ObservableList<T> extends Observable implements List<T> {
private List<T> list;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+ private int notificationBlocks = 0;
+ private boolean notificationWasBlocked = false;
public ObservableList(List<T> list) {
this.list = list;
}
private void forceNotify() {
+ if (notificationBlocked()) return;
setChanged();
notifyObservers();
}
+ private boolean notificationBlocked() {
+ return notificationWasBlocked = (notificationBlocks > 0);
+ }
private void forceNotify(int index) {
+ if (notificationBlocked()) return;
setChanged();
notifyObservers(index);
}
try (LockHolder lh = lockForReading()) {
int index = list.indexOf(item);
if (index == -1) {
- Log.d("ObList", "??? not sending notifications for item not found in the list");
+ debug("ObList", "??? not sending notifications for item not found in the list");
return;
}
- Log.d("ObList", "Notifying item change observers");
+ debug("ObList", "Notifying item change observers");
triggerItemChangedNotification(index);
}
}
rLock.lock();
return new LockHolder(rLock);
}
+ public void blockNotifications() {
+ notificationBlocks++;
+ }
+ public void unblockNotifications() {
+ notificationBlocks--;
+ if ((notificationBlocks == 0) && notificationWasBlocked) notifyObservers();
+ }
}
\ No newline at end of file