Add support for granular conversation data changes.

This commit is contained in:
Greyson Parrelli
2021-08-30 15:08:38 -04:00
parent bca2205945
commit f5a6d61362
28 changed files with 502 additions and 162 deletions

View File

@@ -20,17 +20,17 @@ import java.util.concurrent.Executors;
* contains it. When invalidations come in, this class will just swap out the active controller with
* a new one.
*/
class BufferedPagingController<E> implements PagingController {
class BufferedPagingController<Key, Data> implements PagingController<Key> {
private final PagedDataSource<E> dataSource;
private final PagingConfig config;
private final MutableLiveData<List<E>> liveData;
private final Executor serializationExecutor;
private final PagedDataSource<Key, Data> dataSource;
private final PagingConfig config;
private final MutableLiveData<List<Data>> liveData;
private final Executor serializationExecutor;
private PagingController activeController;
private int lastRequestedIndex;
private PagingController<Key> activeController;
private int lastRequestedIndex;
BufferedPagingController(PagedDataSource<E> dataSource, PagingConfig config, @NonNull MutableLiveData<List<E>> liveData) {
BufferedPagingController(PagedDataSource<Key, Data> dataSource, PagingConfig config, @NonNull MutableLiveData<List<Data>> liveData) {
this.dataSource = dataSource;
this.config = config;
this.liveData = liveData;
@@ -61,4 +61,22 @@ class BufferedPagingController<E> implements PagingController {
activeController.onDataNeededAroundIndex(lastRequestedIndex);
});
}
@Override
public void onDataItemChanged(Key key) {
serializationExecutor.execute(() -> {
if (activeController != null) {
activeController.onDataItemChanged(key);
}
});
}
@Override
public void onDataItemInserted(Key key, int position) {
serializationExecutor.execute(() -> {
if (activeController != null) {
activeController.onDataItemInserted(key, position);
}
});
}
}

View File

@@ -40,4 +40,9 @@ public class CompressedList<E> extends AbstractList<E> {
public E set(int globalIndex, E element) {
return wrapped.set(globalIndex, element);
}
@Override
public void add(int index, E element) {
wrapped.add(index, element);
}
}

View File

@@ -7,7 +7,9 @@ import org.signal.core.util.concurrent.SignalExecutors;
import org.signal.core.util.logging.Log;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
/**
@@ -18,32 +20,34 @@ import java.util.concurrent.Executor;
* which allows it to keep track of pending requests in a thread-safe way, while spinning off
* tasks to fetch data on its own executor.
*/
class FixedSizePagingController<E> implements PagingController {
class FixedSizePagingController<Key, Data> implements PagingController<Key> {
private static final String TAG = FixedSizePagingController.class.getSimpleName();
private static final Executor FETCH_EXECUTOR = SignalExecutors.newCachedSingleThreadExecutor("signal-FixedSizePagingController");
private static final boolean DEBUG = false;
private final PagedDataSource<E> dataSource;
private final PagingConfig config;
private final MutableLiveData<List<E>> liveData;
private final DataStatus loadState;
private final PagedDataSource<Key, Data> dataSource;
private final PagingConfig config;
private final MutableLiveData<List<Data>> liveData;
private final DataStatus loadState;
private final Map<Key, Integer> keyToPosition;
private List<E> data;
private List<Data> data;
private volatile boolean invalidated;
FixedSizePagingController(@NonNull PagedDataSource<E> dataSource,
FixedSizePagingController(@NonNull PagedDataSource<Key, Data> dataSource,
@NonNull PagingConfig config,
@NonNull MutableLiveData<List<E>> liveData,
@NonNull MutableLiveData<List<Data>> liveData,
int size)
{
this.dataSource = dataSource;
this.config = config;
this.liveData = liveData;
this.loadState = DataStatus.obtain(size);
this.data = new CompressedList<>(loadState.size());
this.dataSource = dataSource;
this.config = config;
this.liveData = liveData;
this.loadState = DataStatus.obtain(size);
this.data = new CompressedList<>(loadState.size());
this.keyToPosition = new HashMap<>();
}
/**
@@ -96,17 +100,21 @@ class FixedSizePagingController<E> implements PagingController {
return;
}
List<E> loaded = dataSource.load(loadStart, loadEnd - loadStart, () -> invalidated);
List<Data> loaded = dataSource.load(loadStart, loadEnd - loadStart, () -> invalidated);
if (invalidated) {
Log.w(TAG, buildLog(aroundIndex, "Invalidated! Just after data was loaded."));
return;
}
List<E> updated = new CompressedList<>(data);
List<Data> updated = new CompressedList<>(data);
for (int i = 0, len = Math.min(loaded.size(), data.size() - loadStart); i < len; i++) {
updated.set(loadStart + i, loaded.get(i));
int position = loadStart + i;
Data item = loaded.get(i);
updated.set(position, item);
keyToPosition.put(dataSource.getKey(item), position);
}
data = updated;
@@ -124,6 +132,56 @@ class FixedSizePagingController<E> implements PagingController {
loadState.recycle();
}
@Override
public void onDataItemChanged(Key key) {
FETCH_EXECUTOR.execute(() -> {
Integer position = keyToPosition.get(key);
if (position == null) {
Log.w(TAG, "Notified of key " + key + " but it wasn't in the cache!");
return;
}
Data item = dataSource.load(key);
if (item == null) {
Log.w(TAG, "Notified of key " + key + " but the loaded item was null!");
return;
}
List<Data> updatedList = new CompressedList<>(data);
updatedList.set(position, item);
data = updatedList;
liveData.postValue(updatedList);
});
}
@Override
public void onDataItemInserted(Key key, int position) {
FETCH_EXECUTOR.execute(() -> {
if (keyToPosition.containsKey(key)) {
Log.w(TAG, "Notified of key " + key + " being inserted at " + position + ", but the item already exists!");
return;
}
Data item = dataSource.load(key);
if (item == null) {
Log.w(TAG, "Notified of key " + key + " being inserted at " + position + ", but the loaded item was null!");
return;
}
List<Data> updatedList = new CompressedList<>(data);
updatedList.add(position, item);
keyToPosition.put(dataSource.getKey(item), position);
data = updatedList;
liveData.postValue(updatedList);
});
}
private static String buildLog(int aroundIndex, String message) {
return "onDataNeededAroundIndex(" + aroundIndex + ") " + message;
}

View File

@@ -10,31 +10,31 @@ import java.util.List;
/**
* The primary entry point for creating paged data.
*/
public final class PagedData<E> {
public final class PagedData<Key, Data> {
private final LiveData<List<E>> data;
private final PagingController controller;
private final LiveData<List<Data>> data;
private final PagingController<Key> controller;
@AnyThread
public static <E> PagedData<E> create(@NonNull PagedDataSource<E> dataSource, @NonNull PagingConfig config) {
MutableLiveData<List<E>> liveData = new MutableLiveData<>();
PagingController controller = new BufferedPagingController<>(dataSource, config, liveData);
public static <Key, Data> PagedData<Key, Data> create(@NonNull PagedDataSource<Key, Data> dataSource, @NonNull PagingConfig config) {
MutableLiveData<List<Data>> liveData = new MutableLiveData<>();
PagingController<Key> controller = new BufferedPagingController<>(dataSource, config, liveData);
return new PagedData<>(liveData, controller);
}
private PagedData(@NonNull LiveData<List<E>> data, @NonNull PagingController controller) {
private PagedData(@NonNull LiveData<List<Data>> data, @NonNull PagingController<Key> controller) {
this.data = data;
this.controller = controller;
}
@AnyThread
public @NonNull LiveData<List<E>> getData() {
public @NonNull LiveData<List<Data>> getData() {
return data;
}
@AnyThread
public @NonNull PagingController getController() {
public @NonNull PagingController<Key> getController() {
return controller;
}
}

View File

@@ -1,6 +1,7 @@
package org.signal.paging;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import java.util.List;
@@ -8,7 +9,7 @@ import java.util.List;
/**
* Represents a source of data that can be queried.
*/
public interface PagedDataSource<T> {
public interface PagedDataSource<Key, Data> {
/**
* @return The total size of the data set.
*/
@@ -24,7 +25,13 @@ public interface PagedDataSource<T> {
* If you don't have the full range, just populate what you can.
*/
@WorkerThread
@NonNull List<T> load(int start, int length, @NonNull CancellationSignal cancellationSignal);
@NonNull List<Data> load(int start, int length, @NonNull CancellationSignal cancellationSignal);
@WorkerThread
@Nullable Data load(Key key);
@WorkerThread
@NonNull Key getKey(@NonNull Data data);
interface CancellationSignal {
/**

View File

@@ -1,7 +1,9 @@
package org.signal.paging;
public interface PagingController {
public interface PagingController<Key> {
void onDataNeededAroundIndex(int aroundIndex);
void onDataInvalidated();
void onDataItemChanged(Key key);
void onDataItemInserted(Key key, int position);
}

View File

@@ -1,7 +1,5 @@
package org.signal.paging;
import android.util.Log;
import androidx.annotation.Nullable;
/**
@@ -9,9 +7,9 @@ import androidx.annotation.Nullable;
* to keep a single, static controller, even when the true controller may be changing due to data
* source changes.
*/
public class ProxyPagingController implements PagingController {
public class ProxyPagingController<Key> implements PagingController<Key> {
private PagingController proxied;
private PagingController<Key> proxied;
@Override
public synchronized void onDataNeededAroundIndex(int aroundIndex) {
@@ -27,10 +25,24 @@ public class ProxyPagingController implements PagingController {
}
}
@Override
public void onDataItemChanged(Key key) {
if (proxied != null) {
proxied.onDataItemChanged(key);
}
}
@Override
public void onDataItemInserted(Key key, int position) {
if (proxied != null) {
proxied.onDataItemInserted(key, position);
}
}
/**
* Updates the underlying controller to the one specified.
*/
public synchronized void set(@Nullable PagingController bound) {
public synchronized void set(@Nullable PagingController<Key> bound) {
this.proxied = bound;
}
}