From: Damyan Ivanov Date: Thu, 4 Apr 2019 12:58:53 +0000 (+0300) Subject: ObservableList: add ability to block (delay) change notifications X-Git-Tag: v0.9~6 X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=commitdiff_plain;h=b29d3473e0c0516e15d2f5ac470b97700322e9dc;hp=99c3bfb3451ebb1fc55d728d8d1741849cf789db ObservableList: add ability to block (delay) change notifications --- diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java index 3add1bd4..b5ddb878 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java @@ -42,14 +42,21 @@ import androidx.annotation.RequiresApi; public class ObservableList extends Observable implements List { private List list; private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private int notificationBlocks = 0; + private boolean notificationWasBlocked = false; public ObservableList(List 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 extends Observable implements List { 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