diff --git a/lib/services/fetch_sources_list.dart b/lib/services/fetch_sources_list.dart index d9674015..40bf71f5 100644 --- a/lib/services/fetch_sources_list.dart +++ b/lib/services/fetch_sources_list.dart @@ -36,36 +36,34 @@ Future fetchSourcesList({ ) .toList(); - isar.writeTxnSync(() async { - if (id != null) { - final matchingSource = sourceList.firstWhere( - (source) => source.id == id, - orElse: () => Source(), - ); - if (matchingSource.id != null && - matchingSource.sourceCodeUrl!.isNotEmpty) { - await _updateSource(matchingSource, ref, repo, itemType); + if (id != null) { + final matchingSource = sourceList.firstWhere( + (source) => source.id == id, + orElse: () => Source(), + ); + if (matchingSource.id != null && matchingSource.sourceCodeUrl!.isNotEmpty) { + await _updateSource(matchingSource, ref, repo, itemType); + } + } else { + for (var source in sourceList) { + final existingSource = await isar.sources.get(source.id!); + if (existingSource == null) { + await _addNewSource(source, ref, repo, itemType); + continue; } - } else { - for (var source in sourceList) { - final existingSource = isar.sources.getSync(source.id!); - if (existingSource != null) { - if (existingSource.isAdded! && - compareVersions(existingSource.version!, source.version!) < 0) { - if (ref.watch(autoUpdateExtensionsStateProvider)) { - await _updateSource(source, ref, repo, itemType); - } else { - isar.sources.putSync( - existingSource..versionLast = source.version, - ); - } - } - } else { - _addNewSource(source, ref, repo, itemType); - } + final shouldUpdate = + existingSource.isAdded! && + compareVersions(existingSource.version!, source.version!) < 0; + if (!shouldUpdate) continue; + if (ref.read(autoUpdateExtensionsStateProvider)) { + await _updateSource(source, ref, repo, itemType); + } else { + await isar.writeTxn(() async { + isar.sources.put(existingSource..versionLast = source.version); + }); } } - }); + } checkIfSourceIsObsolete(sourceList, repo!, itemType, ref); } @@ -109,9 +107,7 @@ Future _updateSource( ..notes = source.notes ..repo = repo; - isar.writeTxnSync(() { - isar.sources.putSync(updatedSource); - }); + await isar.writeTxn(() async => isar.sources.put(updatedSource)); ref .read(synchingProvider(syncId: 1).notifier) .addChangedPart( @@ -122,7 +118,12 @@ Future _updateSource( ); } -void _addNewSource(Source source, Ref ref, Repo? repo, ItemType itemType) { +Future _addNewSource( + Source source, + Ref ref, + Repo? repo, + ItemType itemType, +) async { final newSource = Source() ..sourceCodeUrl = source.sourceCodeUrl ..id = source.id @@ -146,27 +147,27 @@ void _addNewSource(Source source, Ref ref, Repo? repo, ItemType itemType) { ..isObsolete = false ..notes = source.notes ..repo = repo; - isar.sources.putSync(newSource); + await isar.writeTxn(() async => isar.sources.put(newSource)); ref .read(synchingProvider(syncId: 1).notifier) .addChangedPart(ActionType.addExtension, null, newSource.toJson(), false); } -void checkIfSourceIsObsolete( +Future checkIfSourceIsObsolete( List sourceList, Repo repo, ItemType itemType, Ref ref, -) { +) async { if (sourceList.isEmpty) return; - final sources = isar.sources + final sources = await isar.sources .filter() .idIsNotNull() .itemTypeEqualTo(itemType) .and() .isLocalEqualTo(false) - .findAllSync(); + .findAll(); if (sources.isEmpty) return; @@ -177,7 +178,7 @@ void checkIfSourceIsObsolete( if (sourceIds.isEmpty) return; - isar.writeTxnSync(() { + await isar.writeTxn(() async { for (var source in sources) { final isNowObsolete = !sourceIds.contains(source.id) && @@ -185,7 +186,7 @@ void checkIfSourceIsObsolete( if (source.isObsolete != isNowObsolete) { source.isObsolete = isNowObsolete; - isar.sources.putSync(source); + await isar.sources.put(source); ref .read(synchingProvider(syncId: 1).notifier)