]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java
no direct interface to ObservableList's value
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / ObservableList.java
index 9893c5d1250644fe9899e0c3721ae8a8c8376f19..9e44faef76d1578a05d1f8409854ab58ed1575b2 100644 (file)
@@ -1,26 +1,24 @@
 /*
  * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your opinion), any later version.
  *
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License terms for details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.utils;
 
 import android.os.Build;
-import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
-import android.support.annotation.RequiresApi;
+import android.util.Log;
 
 import java.util.Collection;
 import java.util.Comparator;
@@ -34,7 +32,11 @@ import java.util.function.Predicate;
 import java.util.function.UnaryOperator;
 import java.util.stream.Stream;
 
-public class ObservableList<T> extends Observable {
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+
+public class ObservableList<T> extends Observable implements List<T> {
     private List<T> list;
     public ObservableList(List<T> list) {
         this.list = list;
@@ -43,9 +45,9 @@ public class ObservableList<T> extends Observable {
         setChanged();
         notifyObservers();
     }
-    private void forceNotify(Object arg) {
+    private void forceNotify(int index) {
         setChanged();
-        notifyObservers(arg);
+        notifyObservers(index);
     }
     public int size() {
         return list.size();
@@ -56,6 +58,7 @@ public class ObservableList<T> extends Observable {
     public boolean contains(@Nullable Object o) {
         return list.contains(o);
     }
+    @NonNull
     public Iterator<T> iterator() {
         return list.iterator();
     }
@@ -75,6 +78,9 @@ public class ObservableList<T> extends Observable {
         if (result) forceNotify();
         return result;
     }
+    public T removeQuietly(int index) {
+        return list.remove(index);
+    }
     public boolean containsAll(@NonNull Collection<?> c) {
         return list.containsAll(c);
     }
@@ -88,6 +94,9 @@ public class ObservableList<T> extends Observable {
         if (result) forceNotify();
         return result;
     }
+    public boolean addAllQuietly(int index, Collection<? extends T> c) {
+        return list.addAll(index, c);
+    }
     public boolean removeAll(@NonNull Collection<?> c) {
         boolean result = list.removeAll(c);
         if (result) forceNotify();
@@ -167,11 +176,23 @@ public class ObservableList<T> extends Observable {
     public void forEach(Consumer<? super T> action) {
         list.forEach(action);
     }
+    public List<T> getList() {
+        return list;
+    }
     public void setList(List<T> aList) {
         list = aList;
         forceNotify();
     }
-    public Iterable<? extends T> getList() {
-        return list;
+    public void triggerItemChangedNotification(T item) {
+        int index = list.indexOf(item);
+        if (index == -1) {
+            Log.d("ObList", "??? not sending notifications for item not found in the list");
+            return;
+        }
+        Log.d("ObList", "Notifying item change observers");
+        triggerItemChangedNotification(index);
+    }
+    public void triggerItemChangedNotification(int index) {
+        forceNotify(index);
     }
 }
\ No newline at end of file