Skip to content

Commit

Permalink
feat: add "mark_video_watched" as a flag in youtube settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Jul 22, 2024
1 parent 0d15636 commit 995eba0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
/// Shows error if [marked] is not true.
void _onVideoMarkWatchResultError(YTMarkVideoWatchedResult marked) {
if (marked == YTMarkVideoWatchedResult.addedAsPending) {
snackyy(message: 'Failed to mark video as watched.', top: false, isError: true);
snackyy(message: 'Failed to mark video as watched, saved as pending.', top: false, isError: true);
}
}

Expand Down Expand Up @@ -1060,7 +1060,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
// -- older request was initiated, wait to see the value.
markedAsWatched.future.then(
(marked) {
if (marked == YTMarkVideoWatchedResult.noAccount && streamsResult != null) {
if ((marked == YTMarkVideoWatchedResult.noAccount || marked == YTMarkVideoWatchedResult.userDenied) && streamsResult != null) {
YoutubeInfoController.history.markVideoWatched(videoId: item.id, streamResult: streamsResult).then(_onVideoMarkWatchResultError);
}
},
Expand Down
15 changes: 11 additions & 4 deletions lib/controller/settings.youtube.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ class _YoutubeSettings with SettingsFileWriter {
final ytVisibleShorts = <YTVisibleShortPlaces, bool>{}.obs;
final ytVisibleMixes = <YTVisibleMixesPlaces, bool>{}.obs;

bool markVideoWatched = true;
int addToPlaylistsTabIndex = 0;

void save({int? addToPlaylistsTabIndex}) {
void save({
int? addToPlaylistsTabIndex,
bool? markVideoWatched,
}) {
if (addToPlaylistsTabIndex != null) this.addToPlaylistsTabIndex = addToPlaylistsTabIndex;
if (markVideoWatched != null) this.markVideoWatched = markVideoWatched;
_writeToStorage();
}

Expand All @@ -27,9 +32,10 @@ class _YoutubeSettings with SettingsFileWriter {
final json = await prepareSettingsFile_();
if (json == null) return;
try {
ytVisibleShorts.value = (json['ytVisibleShorts'] as Map?)?.map((key, value) => MapEntry(YTVisibleShortPlaces.values.getEnum(key)!, value)) ?? {};
ytVisibleMixes.value = (json['ytVisibleMixes'] as Map?)?.map((key, value) => MapEntry(YTVisibleMixesPlaces.values.getEnum(key)!, value)) ?? {};
addToPlaylistsTabIndex = json['addToPlaylistsTabIndex'] ?? 0;
ytVisibleShorts.value = (json['ytVisibleShorts'] as Map?)?.map((key, value) => MapEntry(YTVisibleShortPlaces.values.getEnum(key)!, value)) ?? ytVisibleShorts.value;
ytVisibleMixes.value = (json['ytVisibleMixes'] as Map?)?.map((key, value) => MapEntry(YTVisibleMixesPlaces.values.getEnum(key)!, value)) ?? ytVisibleMixes.value;
addToPlaylistsTabIndex = json['addToPlaylistsTabIndex'] ?? addToPlaylistsTabIndex;
markVideoWatched = json['markVideoWatched'] ?? markVideoWatched;
} catch (e) {
printy(e, isError: true);
}
Expand All @@ -40,6 +46,7 @@ class _YoutubeSettings with SettingsFileWriter {
'ytVisibleShorts': ytVisibleShorts.map((key, value) => MapEntry(key.convertToString, value)),
'ytVisibleMixes': ytVisibleMixes.map((key, value) => MapEntry(key.convertToString, value)),
'addToPlaylistsTabIndex ': addToPlaylistsTabIndex,
'markVideoWatched ': markVideoWatched,
};

Future<void> _writeToStorage() async => await writeToStorage();
Expand Down
52 changes: 52 additions & 0 deletions lib/ui/widgets/settings/youtube_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,34 @@ class YoutubeSettings extends SettingSubpageProvider {
_YoutubeSettingKeys.onOpeningYTLink: [lang.ON_OPENING_YOUTUBE_LINK],
};

void _showYTFlagsDialog() {
NamidaNavigator.inst.navigateDialog(
dialog: CustomBlurryDialog(
icon: Broken.flag,
title: lang.CONFIGURE,
normalTitleStyle: true,
actions: [
NamidaButton(
text: lang.DONE,
onPressed: NamidaNavigator.inst.closeDialog,
),
],
child: const _YTFlagsOptions(),
),
);
}

@override
Widget build(BuildContext context) {
return SettingsCard(
title: lang.YOUTUBE,
subtitle: lang.YOUTUBE_SETTINGS_SUBTITLE,
icon: Broken.video,
trailing: NamidaIconButton(
icon: Broken.flag,
tooltip: () => lang.REFRESH_LIBRARY,
onPressed: _showYTFlagsDialog,
),
child: Column(
children: [
getItemWrapper(
Expand Down Expand Up @@ -492,3 +514,33 @@ class _ShowItemInListTile<E extends Enum> extends StatelessWidget {
);
}
}

class _YTFlagsOptions extends StatefulWidget {
const _YTFlagsOptions();

@override
State<_YTFlagsOptions> createState() => __YTFlagsOptionsState();
}

class __YTFlagsOptionsState extends State<_YTFlagsOptions> {
@override
Widget build(BuildContext context) {
return SizedBox(
width: context.width,
child: ConstrainedBox(
constraints: BoxConstraints(maxHeight: context.height * 0.6),
child: ListView(
padding: EdgeInsets.zero,
shrinkWrap: true,
children: [
CustomSwitchListTile(
value: settings.youtube.markVideoWatched,
onChanged: (isTrue) => settings.youtube.save(markVideoWatched: !isTrue),
title: 'mark_video_watched'.toUpperCase(),
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class _YoutubeHistoryLinker {

_dbOpenedAccId = accId;
_pendingRequestsDBIdle?.close();
if (accId == null) return;
_pendingRequestsDBIdle = DBWrapper.open(_dbDirectory, 'pending_history_$accId');
_pendingRequestsCompleter?.completeIfWasnt();
_pendingRequestsCompleter = null;
Expand Down Expand Up @@ -66,7 +67,8 @@ class _YoutubeHistoryLinker {
}

void executePendingRequests() async {
if (!_hasConnection) return null;
if (!_hasConnection) return;
if (!settings.youtube.markVideoWatched) return;

if (_pendingRequestsCompleter != null) {
// -- already executing
Expand Down Expand Up @@ -141,6 +143,7 @@ class _YoutubeHistoryLinker {

Future<YTMarkVideoWatchedResult> markVideoWatched({required String videoId, required VideoStreamsResult? streamResult, bool errorOnMissingParam = true}) async {
if (_dbOpenedAccId == null) return YTMarkVideoWatchedResult.noAccount; // no acc signed in
if (!settings.youtube.markVideoWatched) return YTMarkVideoWatchedResult.userDenied;

if (_hasPendingRequests) {
executePendingRequests();
Expand Down Expand Up @@ -191,6 +194,7 @@ class _YoutubeHistoryLinker {

enum YTMarkVideoWatchedResult {
noAccount,
userDenied,
marked,
addedAsPending,
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 3.5.3-beta+240722157
version: 3.5.4-beta+240722218

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit 995eba0

Please sign in to comment.