com.abubusoft.kripton.androidx.livedata.KriptonXDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kripton-android-core Show documentation
Show all versions of kripton-android-core Show documentation
Kripton Persistence Library for Android platform - core module for android modules
package com.abubusoft.kripton.androidx.livedata;
import java.util.Collections;
import java.util.List;
import android.arch.paging.DataSource;
import android.arch.paging.PositionalDataSource;
import android.support.annotation.NonNull;
import androidx.lifecycle.Observer;
/**
* A {@link PositionalDataSource} that loads entities based on an ObjectBox
* {@link Query} using offset and limit to implement paging support. The data
* source is invalidated if the query results change.
*/
public class KriptonXDataSource extends PositionalDataSource {
private final PagedLiveData query;
private final Observer observer;
public static class Factory- extends DataSource.Factory
{
private final PagedLiveData- query;
public Factory(PagedLiveData
- query) {
this.query = query;
}
@Override
public KriptonXDataSource
- create() {
return new KriptonXDataSource<>(query);
}
}
public KriptonXDataSource(PagedLiveData
query) {
this.query = query;
observer = new Observer() {
@Override
public void onChanged(T t) {
invalidate();
}
};
query.observeForever(observer);
// observer will be automatically removed once GC'ed
// query. subscribe().onlyChanges().weak().observer(observer);
}
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
// note: limiting to int should be fine for Android apps
// TODO ripristinare query.count();
int totalCount = query.getTotalElements();
if (totalCount == 0) {
callback.onResult(Collections.emptyList(), 0, 0);
return;
}
int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);
List list = loadRange(position, loadSize);
if (list.size() == loadSize) {
callback.onResult(list, position, totalCount);
} else {
invalidate(); // size doesn't match request - DB modified between
// count and load
}
}
@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {
callback.onResult(loadRange(params.startPosition, params.loadSize));
}
private List loadRange(int startPosition, int loadCount) {
/* private void loadRange(int startPosition, int loadCount) { */
// note: find interprets loadCount 0 as no limit
//query.moveTo(startPosition, loadCount);
query.createPageRequestBuilder().offset(startPosition).pageSize(loadCount).apply();
return null;
}
}