X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Futils%2FObservableList.java;h=363a8a1a053f00ae1b18fe611091e867f01e2aef;hp=eac023767186d668583be3c7bcd9328547d4a9e4;hb=bd5da50ef980c0c9657ec1e9c3e681ab5092f438;hpb=7165b89c8ff2b9d8f69e02354197127ec27a4a47 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 eac02376..363a8a1a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java @@ -18,7 +18,8 @@ package net.ktnx.mobileledger.utils; import android.os.Build; -import android.util.Log; + +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.Comparator; @@ -37,17 +38,26 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import static net.ktnx.mobileledger.utils.Logger.debug; + 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); } @@ -203,21 +213,25 @@ public class ObservableList extends Observable implements List { return list.lastIndexOf(o); } } + @NotNull public ListIterator listIterator() { if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException( "Iterators break encapsulation and ignore locking. Write-lock first"); return list.listIterator(); } + @NotNull public ListIterator listIterator(int index) { if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException( "Iterators break encapsulation and ignore locking. Write-lock first"); return list.listIterator(index); } + @NotNull public List subList(int fromIndex, int toIndex) { try (LockHolder lh = lockForReading()) { return list.subList(fromIndex, toIndex); } } + @NotNull @RequiresApi(api = Build.VERSION_CODES.N) public Spliterator spliterator() { if (!lock.isWriteLockedByCurrentThread()) throw new RuntimeException( @@ -267,10 +281,10 @@ public class ObservableList extends Observable implements List { 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); } } @@ -291,4 +305,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