Updated AndroidX

pull/194/head 1.1525
M66B 4 years ago
parent 0a55c92ae5
commit 6558488a68

@ -235,11 +235,11 @@ configurations.all {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
def annotation_version = "1.1.0-beta01" def annotation_version = "1.1.0-rc01"
def core_version = "1.5.0-beta02" def core_version = "1.5.0-beta03"
def appcompat_version = "1.3.0-beta01" def appcompat_version = "1.3.0-beta01"
def activity_version = "1.2.0" def activity_version = "1.2.1"
def fragment_version = "1.3.0" def fragment_version = "1.3.1"
def webkit_version = "1.4.0" def webkit_version = "1.4.0"
def recyclerview_version = "1.2.0-beta02" def recyclerview_version = "1.2.0-beta02"
def coordinatorlayout_version = "1.1.0" def coordinatorlayout_version = "1.1.0"
@ -252,7 +252,7 @@ dependencies {
def lifecycle_version = "2.3.0" def lifecycle_version = "2.3.0"
def lifecycle_extensions_version = "2.2.0" def lifecycle_extensions_version = "2.2.0"
def sqlite_version = "2.1.0" def sqlite_version = "2.1.0"
def room_version = "2.3.0-beta02" def room_version = "2.3.0-beta03"
def paging_version = "2.1.2" // 3.0.0-alpha11 def paging_version = "2.1.2" // 3.0.0-alpha11
def preference_version = "1.1.1" def preference_version = "1.1.1"
def work_version = "2.5.0" def work_version = "2.5.0"

@ -26,4 +26,4 @@ import java.lang.annotation.Target;
*/ */
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@RequiresOptIn() @RequiresOptIn()
@interface ExperimentalRoomApi {} public @interface ExperimentalRoomApi {}

@ -28,6 +28,7 @@ import androidx.sqlite.db.SupportSQLiteQuery;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* A simple data source implementation that uses Limit & Offset to page the query. * A simple data source implementation that uses Limit & Offset to page the query.
@ -38,6 +39,11 @@ import java.util.Set;
* receiving a {@link Cursor} from a large query but avoids the need to manually manage it, and * receiving a {@link Cursor} from a large query but avoids the need to manually manage it, and
* never returns inconsistent data if it is invalidated. * never returns inconsistent data if it is invalidated.
* *
* This class is used for both Paging2 and Pagin3 (via its compat API). When used with Paging3,
* it does lazy registration for observers to be suitable for initialization on the main thread
* whereas in Paging2, it will register observer eagerly to obey Paging2's strict Data Source
* rules. (Paging2 does not let data source to possibly return invalidated data).
*
* @param <T> Data type returned by the data source. * @param <T> Data type returned by the data source.
* *
* @hide * @hide
@ -52,14 +58,40 @@ public abstract class LimitOffsetDataSource<T> extends androidx.paging.Positiona
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
private final InvalidationTracker.Observer mObserver; private final InvalidationTracker.Observer mObserver;
private final boolean mInTransaction; private final boolean mInTransaction;
private final AtomicBoolean mRegisteredObserver = new AtomicBoolean(false);
protected LimitOffsetDataSource(RoomDatabase db, SupportSQLiteQuery query, protected LimitOffsetDataSource(@NonNull RoomDatabase db,
boolean inTransaction, String... tables) { @NonNull SupportSQLiteQuery query,
boolean inTransaction,
@NonNull
String... tables) {
this(db, RoomSQLiteQuery.copyFrom(query), inTransaction, tables); this(db, RoomSQLiteQuery.copyFrom(query), inTransaction, tables);
} }
protected LimitOffsetDataSource(RoomDatabase db, RoomSQLiteQuery query, protected LimitOffsetDataSource(
boolean inTransaction, String... tables) { @NonNull RoomDatabase db,
@NonNull SupportSQLiteQuery query,
boolean inTransaction,
boolean registerObserverImmediately,
@NonNull String... tables) {
this(db, RoomSQLiteQuery.copyFrom(query), inTransaction, registerObserverImmediately,
tables);
}
protected LimitOffsetDataSource(
@NonNull RoomDatabase db,
@NonNull RoomSQLiteQuery query,
boolean inTransaction,
@NonNull String... tables) {
this(db, query, inTransaction, true /*register registerObserverImmediately*/, tables);
}
protected LimitOffsetDataSource(
@NonNull RoomDatabase db,
@NonNull RoomSQLiteQuery query,
boolean inTransaction,
boolean registerObserverImmediately,
@NonNull String... tables) {
mDb = db; mDb = db;
mSourceQuery = query; mSourceQuery = query;
mInTransaction = inTransaction; mInTransaction = inTransaction;
@ -71,7 +103,15 @@ public abstract class LimitOffsetDataSource<T> extends androidx.paging.Positiona
invalidate(); invalidate();
} }
}; };
db.getInvalidationTracker().addWeakObserver(mObserver); if (registerObserverImmediately) {
registerObserverIfNecessary();
}
}
private void registerObserverIfNecessary() {
if (mRegisteredObserver.compareAndSet(false, true)) {
mDb.getInvalidationTracker().addWeakObserver(mObserver);
}
} }
/** /**
@ -81,6 +121,7 @@ public abstract class LimitOffsetDataSource<T> extends androidx.paging.Positiona
*/ */
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public int countItems() { public int countItems() {
registerObserverIfNecessary();
final RoomSQLiteQuery sqLiteQuery = RoomSQLiteQuery.acquire(mCountQuery, final RoomSQLiteQuery sqLiteQuery = RoomSQLiteQuery.acquire(mCountQuery,
mSourceQuery.getArgCount()); mSourceQuery.getArgCount());
sqLiteQuery.copyArgumentsFrom(mSourceQuery); sqLiteQuery.copyArgumentsFrom(mSourceQuery);
@ -98,17 +139,20 @@ public abstract class LimitOffsetDataSource<T> extends androidx.paging.Positiona
@Override @Override
public boolean isInvalid() { public boolean isInvalid() {
registerObserverIfNecessary();
mDb.getInvalidationTracker().refreshVersionsSync(); mDb.getInvalidationTracker().refreshVersionsSync();
return super.isInvalid(); return super.isInvalid();
} }
@NonNull
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
protected abstract List<T> convertRows(Cursor cursor); protected abstract List<T> convertRows(@NonNull Cursor cursor);
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void loadInitial(@NonNull LoadInitialParams params, public void loadInitial(@NonNull LoadInitialParams params,
@NonNull LoadInitialCallback<T> callback) { @NonNull LoadInitialCallback<T> callback) {
registerObserverIfNecessary();
List<T> list = Collections.emptyList(); List<T> list = Collections.emptyList();
int totalCount = 0; int totalCount = 0;
int firstLoadPosition = 0; int firstLoadPosition = 0;

Loading…
Cancel
Save