From b29d3473e0c0516e15d2f5ac470b97700322e9dc Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 4 Apr 2019 15:58:53 +0300 Subject: [PATCH] ObservableList: add ability to block (delay) change notifications --- .../ktnx/mobileledger/utils/ObservableList.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 -- 2.39.2