-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
378 additions
and
209 deletions.
There are no files selected for viewing
338 changes: 181 additions & 157 deletions
338
app/src/main/java/one/mixin/android/db/MessageProvider.kt
Large diffs are not rendered by default.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
app/src/main/java/one/mixin/android/extension/LiveDataExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package one.mixin.android.extension | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
|
||
fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { | ||
observe( | ||
lifecycleOwner, | ||
object : Observer<T> { | ||
override fun onChanged(t: T?) { | ||
observer.onChanged(t) | ||
removeObserver(this) | ||
} | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/src/main/java/one/mixin/android/util/chat/FixedLimitOffsetDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package one.mixin.android.util.chat; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.database.Cursor; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.RestrictTo; | ||
import androidx.paging.PositionalDataSource; | ||
import androidx.room.InvalidationTracker; | ||
import androidx.room.RoomDatabase; | ||
import androidx.room.RoomSQLiteQuery; | ||
import timber.log.Timber; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@SuppressLint("RestrictedApi") | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
public abstract class FixedLimitOffsetDataSource<T> extends PositionalDataSource<T> { | ||
private final static int FIXED_LOAD_SIZE = 45; | ||
|
||
private final RoomSQLiteQuery mSourceQuery; | ||
private final String mLimitOffsetQuery; | ||
private final int mTotalCount; | ||
private final RoomDatabase mDb; | ||
@SuppressWarnings("FieldCanBeLocal") | ||
private final InvalidationTracker.Observer mObserver; | ||
|
||
protected FixedLimitOffsetDataSource(RoomDatabase db, RoomSQLiteQuery query, int unreadCount, String... tables) { | ||
mDb = db; | ||
mTotalCount = unreadCount + FIXED_LOAD_SIZE / 2; | ||
mSourceQuery = query; | ||
mLimitOffsetQuery = "SELECT * FROM ( " + mSourceQuery.getSql() + " ) LIMIT ? OFFSET ?"; | ||
mObserver = new InvalidationTracker.Observer(tables) { | ||
@Override | ||
public void onInvalidated(@NonNull Set<String> tables) { | ||
invalidate(); | ||
} | ||
}; | ||
db.getInvalidationTracker().addWeakObserver(mObserver); | ||
} | ||
|
||
@Override | ||
public boolean isInvalid() { | ||
mDb.getInvalidationTracker().refreshVersionsSync(); | ||
return super.isInvalid(); | ||
} | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
protected abstract List<T> convertRows(Cursor cursor); | ||
|
||
private List<T> initialList; | ||
|
||
@Override | ||
public void loadInitial(@NonNull LoadInitialParams params, | ||
@NonNull LoadInitialCallback<T> callback) { | ||
final int firstLoadPosition = computeInitialLoadPosition(params, mTotalCount); | ||
final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, mTotalCount); | ||
|
||
initialList = loadRange(firstLoadPosition, firstLoadSize); | ||
if (initialList != null) { | ||
try { | ||
callback.onResult(initialList, firstLoadPosition, mTotalCount); | ||
} catch (IllegalArgumentException e) { | ||
// workaround with paging initial load size NOT to be a multiple of page size | ||
Timber.w(e); | ||
try { | ||
callback.onResult(initialList, firstLoadPosition, firstLoadPosition + initialList.size()); | ||
} catch (IllegalArgumentException iae) { | ||
// workaround with paging incorrect tiling | ||
Timber.w(iae); | ||
} | ||
} | ||
} else { | ||
// null list, or size doesn't match request - DB modified between count and load | ||
invalidate(); | ||
} | ||
} | ||
|
||
@Override | ||
public void loadRange(@NonNull LoadRangeParams params, | ||
@NonNull LoadRangeCallback<T> callback) { | ||
// Left empty | ||
} | ||
|
||
@Nullable | ||
public List<T> loadRange(int startPosition, int loadCount) { | ||
final RoomSQLiteQuery sqLiteQuery = RoomSQLiteQuery.acquire(mLimitOffsetQuery, | ||
mSourceQuery.getArgCount() + 2); | ||
sqLiteQuery.copyArgumentsFrom(mSourceQuery); | ||
sqLiteQuery.bindLong(sqLiteQuery.getArgCount() - 1, loadCount); | ||
sqLiteQuery.bindLong(sqLiteQuery.getArgCount(), startPosition); | ||
Cursor cursor = mDb.query(sqLiteQuery); | ||
//noinspection TryFinallyCanBeTryWithResources | ||
try { | ||
return convertRows(cursor); | ||
} finally { | ||
cursor.close(); | ||
sqLiteQuery.release(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters