Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
FIx state tracking of app-update download
Browse files Browse the repository at this point in the history
Fix: #119
  • Loading branch information
nullxception committed Jun 3, 2023
1 parent 48b329d commit 09fc861
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/data/repository/booru/entity/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ class Post with _$Post {
factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);

static const empty = Post();
static const appReserved = Post(id: -100);
}
9 changes: 9 additions & 0 deletions lib/presentation/provider/app_updater.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';

import 'package:boorusphere/constant/app.dart';
import 'package:boorusphere/data/repository/booru/entity/post.dart';
import 'package:boorusphere/data/repository/download/entity/download_entry.dart';
import 'package:boorusphere/data/repository/download/entity/download_progress.dart';
import 'package:boorusphere/data/repository/version/datasource/version_network_source.dart';
import 'package:boorusphere/data/repository/version/entity/app_version.dart';
Expand Down Expand Up @@ -47,6 +49,7 @@ class AppUpdater {
query: 'SELECT * FROM task WHERE file_name LIKE \'%.apk\'');
if (tasks == null) return;
for (var task in tasks) {
await ref.read(downloadStateProvider.notifier).remove(task.taskId);
await FlutterDownloader.remove(
taskId: task.taskId,
shouldDeleteContent: removeFile,
Expand Down Expand Up @@ -78,6 +81,12 @@ class AppUpdater {
if (newId != null) {
_version = version;
id = newId;
final entry = DownloadEntry(
id: newId,
post: Post.appReserved,
destination: appDir.absolute.path,
);
await ref.read(downloadStateProvider.notifier).add(entry);
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/presentation/provider/download/download_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ extension DownloadItemsExt on Iterable<DownloadItem> {
final item = firstWhereOrNull((it) => it.entry.post == post);
return item?.progress ?? DownloadProgress.none;
}

Iterable<DownloadItem> whereNotReserved() {
return whereNot((it) => it.entry.post == Post.appReserved);
}
}
2 changes: 1 addition & 1 deletion lib/presentation/screens/downloads/downloads_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DownloadsPage extends HookConsumerWidget {
ref.read(serverSettingStateProvider.select((it) => it.lastActiveId));
final session = this.session ?? SearchSession(serverId: savedServerId);
final serverData = ref.watch(serverDataStateProvider);
final downloadState = ref.watch(downloadStateProvider);
final downloadState = ref.watch(downloadStateProvider).whereNotReserved();
final groupByServer = ref
.watch(downloadSettingStateProvider.select((it) => it.groupByServer));
final filter = useState(DownloadFilter.none);
Expand Down
6 changes: 5 additions & 1 deletion lib/presentation/screens/post/post_toolbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ class PostDownloadButton extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final progress = ref.watch(downloadStateProvider).getProgressByPost(post);
final progress = ref
.watch(downloadStateProvider)
.whereNotReserved()
.getProgressByPost(post);

final pending = useState(false);

return Stack(
Expand Down

0 comments on commit 09fc861

Please sign in to comment.