chore(mobile): better second to first assets shown
This commit is contained in:
@@ -43,7 +43,7 @@ class AssetService {
|
||||
|
||||
/// Checks the server for updated assets and updates the local database if
|
||||
/// required. Returns `true` if there were any changes.
|
||||
Future<bool> refreshRemoteAssets() async {
|
||||
Future<bool> refreshRemoteAssets({bool firstSync = false}) async {
|
||||
final syncedUserIds = await _db.eTags.where().idProperty().findAll();
|
||||
final List<User> syncedUsers = syncedUserIds.isEmpty
|
||||
? []
|
||||
@@ -57,6 +57,7 @@ class AssetService {
|
||||
getChangedAssets: _getRemoteAssetChanges,
|
||||
loadAssets: _getRemoteAssets,
|
||||
refreshUsers: _userService.getUsersFromServer,
|
||||
firstSync: firstSync,
|
||||
);
|
||||
debugPrint("refreshRemoteAssets full took ${sw.elapsedMilliseconds}ms");
|
||||
return changes;
|
||||
@@ -97,8 +98,12 @@ class AssetService {
|
||||
}
|
||||
|
||||
/// Returns `null` if the server state did not change, else list of assets
|
||||
Future<List<Asset>?> _getRemoteAssets(User user, DateTime until) async {
|
||||
const int chunkSize = 10000;
|
||||
Future<List<Asset>?> _getRemoteAssets(
|
||||
User user,
|
||||
DateTime until,
|
||||
bool firstSync,
|
||||
) async {
|
||||
int chunkSize = firstSync ? 1000 : 10000;
|
||||
try {
|
||||
final List<Asset> allAssets = [];
|
||||
String? lastId;
|
||||
@@ -120,6 +125,11 @@ class AssetService {
|
||||
allAssets.addAll(assets.map(Asset.remote));
|
||||
if (assets.length != chunkSize) break;
|
||||
lastId = assets.last.id;
|
||||
|
||||
if (firstSync) {
|
||||
// first sync only loads the first chunk
|
||||
break;
|
||||
}
|
||||
}
|
||||
return allAssets;
|
||||
} catch (error, stack) {
|
||||
|
||||
@@ -46,14 +46,18 @@ class SyncService {
|
||||
List<User> users,
|
||||
DateTime since,
|
||||
) getChangedAssets,
|
||||
required FutureOr<List<Asset>?> Function(User user, DateTime until)
|
||||
loadAssets,
|
||||
required FutureOr<List<Asset>?> Function(
|
||||
User user,
|
||||
DateTime until,
|
||||
bool firstSync,
|
||||
) loadAssets,
|
||||
required FutureOr<List<User>?> Function() refreshUsers,
|
||||
required bool firstSync,
|
||||
}) =>
|
||||
_lock.run(
|
||||
() async =>
|
||||
await _syncRemoteAssetChanges(users, getChangedAssets) ??
|
||||
await _syncRemoteAssetsFull(refreshUsers, loadAssets),
|
||||
await _syncRemoteAssetsFull(refreshUsers, loadAssets, firstSync),
|
||||
);
|
||||
|
||||
/// Syncs remote albums to the database
|
||||
@@ -212,7 +216,9 @@ class SyncService {
|
||||
/// Syncs assets by loading and comparing all assets from the server.
|
||||
Future<bool> _syncRemoteAssetsFull(
|
||||
FutureOr<List<User>?> Function() refreshUsers,
|
||||
FutureOr<List<Asset>?> Function(User user, DateTime until) loadAssets,
|
||||
FutureOr<List<Asset>?> Function(User user, DateTime until, bool firstSync)
|
||||
loadAssets,
|
||||
bool firstSync,
|
||||
) async {
|
||||
final serverUsers = await refreshUsers();
|
||||
if (serverUsers == null) {
|
||||
@@ -228,17 +234,19 @@ class SyncService {
|
||||
.findAll();
|
||||
bool changes = false;
|
||||
for (User u in users) {
|
||||
changes |= await _syncRemoteAssetsForUser(u, loadAssets);
|
||||
changes |= await _syncRemoteAssetsForUser(u, loadAssets, firstSync);
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
Future<bool> _syncRemoteAssetsForUser(
|
||||
User user,
|
||||
FutureOr<List<Asset>?> Function(User user, DateTime until) loadAssets,
|
||||
FutureOr<List<Asset>?> Function(User user, DateTime until, bool firstSync)
|
||||
loadAssets,
|
||||
bool firstSync,
|
||||
) async {
|
||||
final DateTime now = DateTime.now().toUtc();
|
||||
final List<Asset>? remote = await loadAssets(user, now);
|
||||
final List<Asset>? remote = await loadAssets(user, now, firstSync);
|
||||
if (remote == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user