Skip to content

Commit

Permalink
Merge pull request #326 from lamarios/feature/sync_history_in_foregro…
Browse files Browse the repository at this point in the history
…und_service

sync history in foreground service if it is enable, otherwise sync wh…
  • Loading branch information
lamarios authored Sep 28, 2023
2 parents 6cdb32c + c6178a7 commit c2b95ec
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 139 deletions.
12 changes: 4 additions & 8 deletions lib/app/states/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@ class AppCubit extends Cubit<AppState> {
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();
Expand Down
4 changes: 4 additions & 0 deletions lib/foreground_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ onStart(ServiceInstance service) async {
await _restartTimer();
});

// we run the background stuff once when it starts
_backgroundCheck();
_restartTimer();
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/l10n/app_or.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@
"@notificationsDescription": {
"description": "Setting description for notifications"
},
"playlistNotificationContent": "ଏଠାରେ {count, plural, =0{no new video} =1{1 new video} other{{count} new videos}}ଟି{playlist} playlist} ଭିଡିଓ ଅଛି",
"playlistNotificationContent": "ଏଠାରେ {count, plural, =0{no new video} =1{1 new video} other{{count} new videos}}ଟି{playlist} playlist ଭିଡିଓ ଅଛି",
"@playlistNotificationContent": {
"description": "Content for playlist notification when there are new videos",
"placeholders": {
Expand Down Expand Up @@ -1087,7 +1087,7 @@
"@deletePlaylistNotificationContent": {
"description": "Title for dialog to confirm whether to delete playlist notifications"
},
"channelNotificationContent": "{{count, plural, =0 {no new videos} =1 {1 new video} other {{count} new videos}}ରୁ{channel} ରୁ {count, plural, = 0 {no new videos} = 1 {1 new video}} ଅଛି",
"channelNotificationContent": "ରୁ{channel} ରୁ {count, plural, = 0 {no new videos} = 1 {1 new video} other {{count} new videos}} ଅଛି",
"@channelNotificationContent": {
"description": "Content for channel notification when there are new videos",
"placeholders": {
Expand Down Expand Up @@ -1133,7 +1133,7 @@
"format": "compact"
}
},
"foregroundServiceNotificationContent": "ଥରେ {hours, select, 1 {per hour} 24 {a day} ଅନ୍ୟ {ପ୍ରତ୍ୟେକ {hours} ଘଣ୍ଟା}ନୂତନ ଭିଡିଓଗୁଡିକ ଯାଞ୍ଚ କରିବେ",
"foregroundServiceNotificationContent": "ଥରେ {hours, select, 1 {per hour} 24 {a day} other {ପ୍ରତ୍ୟେକ {hours} ଘଣ୍ଟା}}ନୂତନ ଭିଡିଓଗୁଡିକ ଯାଞ୍ଚ କରିବେ",
"@foregroundServiceNotificationContent": {
"description": "Content for the foreground service running notification when the user wants to receive notifications",
"hours": {
Expand Down
3 changes: 3 additions & 0 deletions lib/player/states/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ class VideoPlayerCubit extends MediaPlayerCubit<VideoPlayerState> {
fit: fillVideo ? BoxFit.cover : BoxFit.contain,
subtitlesConfiguration: BetterPlayerSubtitlesConfiguration(
fontSize: settings.state.subtitleSize,
outlineEnabled: true,
outlineColor: Colors.black,
outlineSize: 1
),
controlsConfiguration: BetterPlayerControlsConfiguration(showControls: false
// customControlsBuilder: (controller, onPlayerVisibilityChanged) => PlayerControls(mediaPlayerCubit: this),
Expand Down
14 changes: 14 additions & 0 deletions lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -531,6 +532,19 @@ class Service {
return List<String>.from(i.map((e) => e as String));
}

void syncHistory() async {
try {
if (db.isLoggedInToCurrentServer()) {
(await getUserHistory(1, 200)).where((element) => db.getVideoProgress(element) == 0).forEach((element) {
db.saveProgress(Progress.named(progress: 1, videoId: element));
log.fine('updated watch status of $element');
});
}
} catch (err) {
log.fine('failed to sync history, probably not logged in');
}
}

Future<void> clearUserHistory() async {
var currentlySelectedServer = db.getCurrentlySelectedServer();

Expand Down
22 changes: 16 additions & 6 deletions lib/settings/views/screens/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class NotificationSettingsScreen extends StatelessWidget {
fontSize: 18,
fontWeight: FontWeight.w400,
)),
Text(locals.notificationFrequencySettingsDescription, style: TextStyle(color: theme.tileDescriptionTextColor)),
Text(locals.notificationFrequencySettingsDescription,
style: TextStyle(color: theme.tileDescriptionTextColor)),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
Expand All @@ -83,15 +84,21 @@ class NotificationSettingsScreen extends StatelessWidget {
min: 1,
max: 24,
divisions: 23,
label: locals.notificationFrequencySliderLabel(state.backgroundNotificationFrequency.toString()),
onChanged: (value) => cubit.setBackgroundCheckFrequency(value.toInt()),
onChangeEnd: (value) => cubit.setBackgroundCheckFrequency(value.toInt()),
label: locals.notificationFrequencySliderLabel(
state.backgroundNotificationFrequency.toString()),
onChanged: state.backgroundNotifications
? (value) => cubit.setBackgroundCheckFrequency(value.toInt())
: null,
onChangeEnd: state.backgroundNotifications
? (value) => cubit.setBackgroundCheckFrequency(value.toInt())
: null,
),
),
SizedBox(
width: 30,
child: Text(
locals.notificationFrequencySliderLabel(state.backgroundNotificationFrequency.toString()),
locals.notificationFrequencySliderLabel(
state.backgroundNotificationFrequency.toString()),
),
)
],
Expand All @@ -117,7 +124,10 @@ class NotificationSettingsScreen extends StatelessWidget {
height: 8,
),
Text(locals.otherNotifications),
TabBar(tabs: [Tab(icon: const Icon(Icons.people), text: locals.channels), Tab(icon: const Icon(Icons.playlist_play), text: locals.playlists)]),
TabBar(tabs: [
Tab(icon: const Icon(Icons.people), text: locals.channels),
Tab(icon: const Icon(Icons.playlist_play), text: locals.playlists)
]),
const Expanded(
child: TabBarView(
children: [ChannelNotificationList(), PlaylistNotificationList()],
Expand Down
2 changes: 1 addition & 1 deletion lib/videos/models/db/history_video_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HistoryVideoCache {
if (cachedVideo == null) {
var vid = await service.getVideo(e);
cachedVideo = HistoryVideoCache(
vid.videoId, vid.title, vid.author, ImageObject.getWorstThumbnail(vid.videoThumbnails)?.url ?? '');
vid.videoId, vid.title, vid.author, ImageObject.getBestThumbnail(vid.videoThumbnails)?.url ?? '');
db.upsertHistoryVideo(cachedVideo);
}
return cachedVideo;
Expand Down
Loading

0 comments on commit c2b95ec

Please sign in to comment.