]> git.ktnx.net Git - mobile-ledger.git/commitdiff
ObservableList: add ability to block (delay) change notifications
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 4 Apr 2019 12:58:53 +0000 (15:58 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 4 Apr 2019 12:58:53 +0000 (15:58 +0300)
app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java

index 3add1bd488ce940c5f77a6db3a41306f64b254b2..b5ddb8780a368bcd1166061faf8120a276786de3 100644 (file)
@@ -42,14 +42,21 @@ import androidx.annotation.RequiresApi;
 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);
     }
@@ -297,4 +304,11 @@ public class ObservableList<T> extends Observable implements List<T> {
         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