mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-25 05:27:42 +00:00
Add support for granular conversation data changes.
This commit is contained in:
@@ -11,15 +11,19 @@ android {
|
||||
|
||||
minSdkVersion MINIMUM_SDK
|
||||
targetSdkVersion TARGET_SDK
|
||||
multiDexEnabled true
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
coreLibraryDesugaringEnabled true
|
||||
sourceCompatibility JAVA_VERSION
|
||||
targetCompatibility JAVA_VERSION
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||
implementation 'com.google.android.material:material:1.2.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.signal.pagingtest;
|
||||
|
||||
interface EventListener {
|
||||
void onItemClicked(String key);
|
||||
}
|
||||
11
paging/app/src/main/java/org/signal/pagingtest/Item.java
Normal file
11
paging/app/src/main/java/org/signal/pagingtest/Item.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.signal.pagingtest;
|
||||
|
||||
public final class Item {
|
||||
final String key;
|
||||
final long timestamp;
|
||||
|
||||
public Item(String key, long timestamp) {
|
||||
this.key = key;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.signal.pagingtest;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
@@ -16,22 +18,26 @@ import org.signal.paging.PagingController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
public class MainActivity extends AppCompatActivity implements EventListener {
|
||||
|
||||
private MainViewModel viewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
MyAdapter adapter = new MyAdapter();
|
||||
MyAdapter adapter = new MyAdapter(this);
|
||||
RecyclerView list = findViewById(R.id.list);
|
||||
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
||||
|
||||
list.setAdapter(adapter);
|
||||
list.setLayoutManager(layoutManager);
|
||||
|
||||
MainViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
|
||||
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
|
||||
adapter.setPagingController(viewModel.getPagingController());
|
||||
viewModel.getList().observe(this, newList -> {
|
||||
adapter.submitList(newList);
|
||||
@@ -51,21 +57,30 @@ public class MainActivity extends AppCompatActivity {
|
||||
layoutManager.scrollToPosition(target);
|
||||
});
|
||||
|
||||
findViewById(R.id.append_btn).setOnClickListener(v -> {
|
||||
viewModel.appendItems();
|
||||
findViewById(R.id.prepend_btn).setOnClickListener(v -> {
|
||||
viewModel.prependItems();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClicked(String key) {
|
||||
viewModel.onItemClicked(key);
|
||||
}
|
||||
|
||||
static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
|
||||
|
||||
private final static int TYPE_NORMAL = 1;
|
||||
private final static int TYPE_PLACEHOLDER = -1;
|
||||
|
||||
private PagingController controller;
|
||||
private final EventListener listener;
|
||||
private final List<Item> data;
|
||||
|
||||
private final List<String> data = new ArrayList<>();
|
||||
private PagingController<String> controller;
|
||||
|
||||
public MyAdapter(@NonNull EventListener listener) {
|
||||
this.listener = listener;
|
||||
this.data = new ArrayList<>();
|
||||
|
||||
public MyAdapter() {
|
||||
setHasStableIds(true);
|
||||
}
|
||||
|
||||
@@ -81,14 +96,18 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
Item item = getItem(position);
|
||||
if (item != null) {
|
||||
return item.key.hashCode();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
switch (viewType) {
|
||||
case TYPE_NORMAL:
|
||||
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
|
||||
case TYPE_PLACEHOLDER:
|
||||
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
|
||||
default:
|
||||
@@ -98,24 +117,53 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
|
||||
holder.bind(getItem(position));
|
||||
holder.bind(getItem(position), position, listener);
|
||||
}
|
||||
|
||||
private String getItem(int index) {
|
||||
private Item getItem(int index) {
|
||||
if (controller != null) {
|
||||
controller.onDataNeededAroundIndex(index);
|
||||
}
|
||||
return data.get(index);
|
||||
}
|
||||
|
||||
void setPagingController(PagingController pagingController) {
|
||||
void setPagingController(PagingController<String> pagingController) {
|
||||
this.controller = pagingController;
|
||||
}
|
||||
|
||||
void submitList(List<String> list) {
|
||||
void submitList(List<Item> list) {
|
||||
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
String oldKey = Optional.ofNullable(data.get(oldItemPosition)).map(item -> item.key).orElse(null);
|
||||
String newKey = Optional.ofNullable(list.get(newItemPosition)).map(item -> item.key).orElse(null);
|
||||
|
||||
return Objects.equals(oldKey, newKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
Long oldKey = Optional.ofNullable(data.get(oldItemPosition)).map(item -> item.timestamp).orElse(null);
|
||||
Long newKey = Optional.ofNullable(list.get(newItemPosition)).map(item -> item.timestamp).orElse(null);
|
||||
|
||||
return Objects.equals(oldKey, newKey);
|
||||
}
|
||||
}, false);
|
||||
|
||||
result.dispatchUpdatesTo(this);
|
||||
|
||||
data.clear();
|
||||
data.addAll(list);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,8 +176,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
textView = itemView.findViewById(R.id.text);
|
||||
}
|
||||
|
||||
void bind(@NonNull String s) {
|
||||
textView.setText(s == null ? "PLACEHOLDER" : s);
|
||||
void bind(@Nullable Item item, int position, @NonNull EventListener listener) {
|
||||
if (item != null) {
|
||||
textView.setText(position + " | " + item.key.substring(0, 13) + " | " + System.currentTimeMillis());
|
||||
textView.setOnClickListener(v -> {
|
||||
listener.onItemClicked(item.key);
|
||||
});
|
||||
} else {
|
||||
textView.setText(position + " | PLACEHOLDER");
|
||||
textView.setOnClickListener(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.signal.pagingtest;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.paging.PagedDataSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.UUID;
|
||||
|
||||
class MainDataSource implements PagedDataSource<String, Item> {
|
||||
|
||||
private final List<Item> items = new ArrayList<>();
|
||||
|
||||
MainDataSource(int size) {
|
||||
buildItems(size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull List<Item> load(int start, int length, @NonNull CancellationSignal cancellationSignal) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return items.subList(start, start + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Item load(String key) {
|
||||
return items.stream().filter(item -> item.key.equals(key)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getKey(@NonNull Item item) {
|
||||
return item.key;
|
||||
}
|
||||
|
||||
public void updateItem(@NonNull String key) {
|
||||
ListIterator<Item> iter = items.listIterator();
|
||||
while (iter.hasNext()) {
|
||||
if (iter.next().key.equals(key)) {
|
||||
iter.set(new Item(key, System.currentTimeMillis()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public @NonNull String prepend() {
|
||||
Item item = new Item(UUID.randomUUID().toString(), System.currentTimeMillis());
|
||||
items.add(0, item);
|
||||
return item.key;
|
||||
}
|
||||
|
||||
private void buildItems(int size) {
|
||||
items.clear();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
items.add(new Item(UUID.randomUUID().toString(), System.currentTimeMillis()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,71 +4,40 @@ import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.signal.paging.PagedDataSource;
|
||||
import org.signal.paging.PagingController;
|
||||
import org.signal.paging.PagingConfig;
|
||||
import org.signal.paging.PagedData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainViewModel extends ViewModel {
|
||||
|
||||
private final PagedData<String> pagedData;
|
||||
private final MyDataSource dataSource;
|
||||
private final PagedData<String, Item> pagedData;
|
||||
private final MainDataSource dataSource;
|
||||
|
||||
public MainViewModel() {
|
||||
this.dataSource = new MyDataSource(1000);
|
||||
this.dataSource = new MainDataSource(1000);
|
||||
this.pagedData = PagedData.create(dataSource, new PagingConfig.Builder().setBufferPages(3)
|
||||
.setPageSize(25)
|
||||
.build());
|
||||
}
|
||||
|
||||
public @NonNull LiveData<List<String>> getList() {
|
||||
public void onItemClicked(@NonNull String key) {
|
||||
dataSource.updateItem(key);
|
||||
pagedData.getController().onDataItemChanged(key);
|
||||
}
|
||||
|
||||
public @NonNull LiveData<List<Item>> getList() {
|
||||
return pagedData.getData();
|
||||
}
|
||||
|
||||
public @NonNull PagingController getPagingController() {
|
||||
public @NonNull PagingController<String> getPagingController() {
|
||||
return pagedData.getController();
|
||||
}
|
||||
|
||||
public void appendItems() {
|
||||
dataSource.setSize(dataSource.size() + 1);
|
||||
pagedData.getController().onDataInvalidated();
|
||||
public void prependItems() {
|
||||
String key = dataSource.prepend();
|
||||
pagedData.getController().onDataItemInserted(key, 0);
|
||||
}
|
||||
|
||||
private static class MyDataSource implements PagedDataSource<String> {
|
||||
|
||||
private int size;
|
||||
|
||||
MyDataSource(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> load(int start, int length, @NonNull CancellationSignal cancellationSignal) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
List<String> data = new ArrayList<>(length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
data.add(String.valueOf(start + i) + " (" + System.currentTimeMillis() + ")");
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,10 +53,10 @@
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/append_btn"
|
||||
android:id="@+id/prepend_btn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Append" />
|
||||
android:text="Prepend" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="8dp"
|
||||
android:fontFamily="monospace"
|
||||
tools:text="Spider-Man"/>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user