From 53c7249e15f21425a954d3a9895d09c7e8ddebe1 Mon Sep 17 00:00:00 2001 From: Paul Fauchon Date: Wed, 27 Sep 2023 18:31:35 +0800 Subject: [PATCH 1/6] sync history in foreground service if it is enable, otherwise sync when the app starts --- lib/app/states/app.dart | 12 ++--- lib/foreground_service.dart | 4 ++ lib/service.dart | 53 +++++++++++++++---- lib/settings/views/screens/notifications.dart | 22 +++++--- lib/videos/models/db/history_video_cache.dart | 2 +- 5 files changed, 68 insertions(+), 25 deletions(-) diff --git a/lib/app/states/app.dart b/lib/app/states/app.dart index 6b11cafc..fb3b80fb 100644 --- a/lib/app/states/app.dart +++ b/lib/app/states/app.dart @@ -38,18 +38,14 @@ class AppCubit extends Cubit { selectedIndex = 0; } selectIndex(selectedIndex); - syncHistory(); - } - syncHistory() async { - if (isLoggedIn) { - var history = await service.getUserHistory(1, 200); - for (String videoId in history) { - db.saveProgress(Progress.named(progress: 1, videoId: videoId)); - } + // we only sync the history when the app doesn't run the foreground service + if((db.getSettings(BACKGROUND_NOTIFICATIONS)?.value ?? "false") == "false") { + service.syncHistory(); } } + @override close() async { state.intentDataStreamSubscription.cancel(); diff --git a/lib/foreground_service.dart b/lib/foreground_service.dart index 555eb61e..82dd7f2d 100644 --- a/lib/foreground_service.dart +++ b/lib/foreground_service.dart @@ -73,6 +73,8 @@ onStart(ServiceInstance service) async { await _restartTimer(); }); + // we run the background stuff once when it starts + _backgroundCheck(); _restartTimer(); } @@ -105,6 +107,8 @@ _backgroundCheck() async { sendNotification(title, locals.foregroundServiceUpdatingPlaylist, type: NotificationTypes.foregroundService); await _handlePlaylistNotifications(); sendNotification(title, locals.foregroundServiceNotificationContent(refreshRate), type: NotificationTypes.foregroundService); + + service.syncHistory(); } catch (e) { print('we have a background service error: ${e}'); } finally { diff --git a/lib/service.dart b/lib/service.dart index d125c4dc..30517e80 100644 --- a/lib/service.dart +++ b/lib/service.dart @@ -14,6 +14,7 @@ import 'package:invidious/search/models/search_sort_by.dart'; import 'package:invidious/search/models/search_type.dart'; import 'package:invidious/settings/models/db/video_filter.dart'; import 'package:invidious/settings/models/errors/invidiousServiceError.dart'; +import 'package:invidious/videos/models/db/progress.dart'; import 'package:invidious/videos/models/dislike.dart'; import 'package:invidious/videos/models/sponsor_segment.dart'; import 'package:invidious/videos/models/userFeed.dart'; @@ -70,7 +71,8 @@ class Service { handleResponse(Response response) { var body = utf8.decode(response.bodyBytes); - log.info("Response from ${response.request?.method} ${urlFormatForLog(response.request?.url)}, status: ${response.statusCode}"); + log.info( + "Response from ${response.request?.method} ${urlFormatForLog(response.request?.url)}, status: ${response.statusCode}"); if (body.isNotEmpty) { var decoded = jsonDecode(body); @@ -89,7 +91,8 @@ class Service { return decoded; } else if (response.statusCode < 200 || response.statusCode >= 400) { - log.severe('Error making request to ${response.request?.url}, \n status: ${response.statusCode}, \n Body: ${response.body}'); + log.severe( + 'Error making request to ${response.request?.url}, \n status: ${response.statusCode}, \n Body: ${response.body}'); throw InvidiousServiceError('Couldn\'t make request, response code: ${response.statusCode}'); } } @@ -134,7 +137,8 @@ class Service { handleErrors(Response response) {} Future