Skip to content

Commit

Permalink
Merge pull request #36 from harlanx:feat/add-audio-language-preferenc…
Browse files Browse the repository at this point in the history
…e-in-user-profiles

feat: Add option for audio languages in profile
  • Loading branch information
harlanx authored Oct 18, 2024
2 parents fd056c4 + 8410b92 commit 0fe90ac
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 71 deletions.
54 changes: 48 additions & 6 deletions lib/data/show_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ShowNotifier extends InputBasic with ChangeNotifier {
}

void _previewTracks(Video video) {
_assignDefault(video.audios);
_assignDefault(video.subtitles);
_assignDefaultAudio(video.audios);
_assignDefaultSubtitle(video.subtitles);
sortTracks();
for (var audio in video.audios) {
audio.title = TitleScanner.audio(audio, profile);
Expand All @@ -69,13 +69,13 @@ class ShowNotifier extends InputBasic with ChangeNotifier {
}
}

void _assignDefault(List<TrackProperties> tracks) {
void _assignDefaultAudio(List<TrackProperties> tracks) {
List<TrackProperties> matches = [];
for (final flagOrder in profile.defaultFlagOrder) {
if (flagOrder == 'default') {
matches = tracks.where((track) {
final isDefaultLanguage =
track.language.iso6393 == profile.defaultLanguage;
track.language.iso6393 == profile.defaultAudioLanguage;
final orderableFlags = track.flags.values.where((flag) {
return !['enabled', 'default'].contains(flag.definedKey);
}).toList();
Expand All @@ -86,7 +86,7 @@ class ShowNotifier extends InputBasic with ChangeNotifier {
} else {
matches = tracks.where((track) {
final isDefaultLanguage =
track.language.iso6393 == profile.defaultLanguage;
track.language.iso6393 == profile.defaultAudioLanguage;

return track.flags[flagOrder]!.value && isDefaultLanguage;
}).toList();
Expand All @@ -98,7 +98,49 @@ class ShowNotifier extends InputBasic with ChangeNotifier {
}
}
for (final track in tracks) {
if (profile.languages.contains(track.language.iso6393)) {
if (profile.audioLanguages.contains(track.language.iso6393)) {
track.include = true;
if (matches.contains(track)) {
track.flags['default']!.value = true;
} else {
track.flags['default']!.value = false;
}
} else {
track.include = false;
}
}
}

void _assignDefaultSubtitle(List<TrackProperties> tracks) {
List<TrackProperties> matches = [];
for (final flagOrder in profile.defaultFlagOrder) {
if (flagOrder == 'default') {
matches = tracks.where((track) {
final isDefaultLanguage =
track.language.iso6393 == profile.defaultSubtitleLanguage;
final orderableFlags = track.flags.values.where((flag) {
return !['enabled', 'default'].contains(flag.definedKey);
}).toList();

return orderableFlags.every((flag) => flag.value == false) &&
isDefaultLanguage;
}).toList();
} else {
matches = tracks.where((track) {
final isDefaultLanguage =
track.language.iso6393 == profile.defaultSubtitleLanguage;

return track.flags[flagOrder]!.value && isDefaultLanguage;
}).toList();
}
if (matches.isEmpty) {
continue;
} else {
break;
}
}
for (final track in tracks) {
if (profile.subtitleLanguages.contains(track.language.iso6393)) {
track.include = true;
if (matches.contains(track)) {
track.flags['default']!.value = true;
Expand Down
12 changes: 8 additions & 4 deletions lib/data/user_profiles_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class UserProfilesNotifier extends ChangeNotifier {
'hearing_impaired',
'default',
],
defaultLanguage: 'eng',
languages: UserProfile.defaultLanguages,
defaultAudioLanguage: 'eng',
audioLanguages: UserProfile.defaultLanguages,
defaultSubtitleLanguage: 'eng',
subtitleLanguages: UserProfile.defaultLanguages,
modifiers: UserProfile.defaultModifiers,
),
UserProfile(
Expand All @@ -39,8 +41,10 @@ class UserProfilesNotifier extends ChangeNotifier {
'hearing_impaired',
'default',
],
defaultLanguage: 'eng',
languages: UserProfile.defaultLanguages,
defaultAudioLanguage: 'eng',
audioLanguages: UserProfile.defaultLanguages,
defaultSubtitleLanguage: 'eng',
subtitleLanguages: UserProfile.defaultLanguages,
modifiers: UserProfile.defaultModifiers,
),
];
Expand Down
5 changes: 3 additions & 2 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"videoTitleNote": "This will apply to the video file title and video track title",
"audioTitleNote": "This will apply to the audio track title",
"subtitleTitleNote": "This will apply to the subtitle track title",
"selectLanguageToInclude": "Select languages to include and choose to set as default",
"languages": "Languages",
"selectedLanguages": "Selected languages",
"selectTrackFlagsAndReorder": "Select track flags and reorder which will set the Default flag based on order fallbacks",
Expand Down Expand Up @@ -266,5 +265,7 @@
"addAttachments": "Add Attachments",
"resetAndUseDefault": "Reset and use the default tool path",
"sameAsOutput": "Same as output file title",
"textAlgorithmSubstitute": "Text matching substitute algorithm"
"textAlgorithmSubstitute": "Text matching substitute algorithm",
"selectSubtitleLanguageToInclude": "Select subtitle languages to include and choose to set as default",
"selectAudioLanguageToInclude": "Select audio languages to include and choose to set as default"
}
5 changes: 3 additions & 2 deletions lib/l10n/intl_fil.arb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"videoTitleNote": "Ito ay mag a-apply sa pangalan ng file ng bidyo at sa pangalan ng track nito",
"audioTitleNote": "Ito ay mag a-apply sa pangalan ng track ng audio",
"subtitleTitleNote": "Ito ay mag a-apply sa pangalan ng track ng subtitle",
"selectLanguageToInclude": "Piliin ang mga lengguwaheng isasama at pumili rin ng maaring gawing default",
"languages": "Mga lengguwahe",
"selectedLanguages": "Mga pinili",
"selectTrackFlagsAndReorder": "Pumili ng track flags at i-reorder, ito ay mag se-set ng Default flag base sa pagkakasunod ng fallbacks",
Expand Down Expand Up @@ -266,5 +265,7 @@
"addAttachments": "Mag-add ng Attachments",
"resetAndUseDefault": "I-reset at gamiting ang default path ng tool",
"sameAsOutput": "Kapareho sa pangalan ng output file",
"textAlgorithmSubstitute": "Kapalit na pantugmang algoritmo sa text"
"textAlgorithmSubstitute": "Kapalit na pantugmang algoritmo sa text",
"selectSubtitleLanguageToInclude": "Piliin ang mga lengguwahe ng subtitle na isasama at pumili ng itatakdang default",
"selectAudioLanguageToInclude": "Piliin ang mga lengguwahe ng audio na isasama at pumili ng itatakdang default"
}
117 changes: 85 additions & 32 deletions lib/models/user_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
this.audioExtraOptions = '',
this.subtitleExtraOptions = '',
this.attachmentExtraOptions = '',
this.defaultLanguage = '',
this.languages = const [],
this.defaultAudioLanguage = '',
this.audioLanguages = const [],
this.defaultSubtitleLanguage = '',
this.subtitleLanguages = const [],
this.defaultFlagOrder = const [],
this.modifiers = const [],
this.useFolderName = true,
Expand Down Expand Up @@ -174,7 +176,7 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
'hin',
'spa',
'fre',
'arb',
'ara',
'ben',
'por',
'rus',
Expand All @@ -192,8 +194,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
String audioExtraOptions;
String subtitleExtraOptions;
String attachmentExtraOptions;
String defaultLanguage;
List<String> languages;
String defaultAudioLanguage;
List<String> audioLanguages;
String defaultSubtitleLanguage;
List<String> subtitleLanguages;
List<String> defaultFlagOrder;
List<TextModifier> modifiers;
bool useFolderName;
Expand All @@ -210,8 +214,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
audioExtraOptions: json['audioExtraOptions'],
subtitleExtraOptions: json['subtitleExtraOptions'],
attachmentExtraOptions: json['attachmentExtraOptions'],
defaultLanguage: json['defaultLanguage'],
languages: List<String>.from(json['languages']),
defaultAudioLanguage: json['defaultAudioLanguage'],
audioLanguages: List<String>.from(json['audioLanguages']),
defaultSubtitleLanguage: json['defaultSubtitleLanguage'],
subtitleLanguages: List<String>.from(json['subtitleLanguages']),
defaultFlagOrder: List<String>.from(json['defaultFlagOrder']),
modifiers: List<TextModifier>.from(
json['modifiers'].map((e) => TextModifier.fromJson(e))),
Expand All @@ -230,8 +236,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
'audioExtraOptions': audioExtraOptions,
'subtitleExtraOptions': subtitleExtraOptions,
'attachmentExtraOptions': attachmentExtraOptions,
'defaultLanguage': defaultLanguage,
'languages': languages,
'defaultAudioLanguage': defaultAudioLanguage,
'audioLanguages': audioLanguages,
'defaultSubtitleLanguage': defaultSubtitleLanguage,
'subtitleLanguages': subtitleLanguages,
'defaultFlagOrder': defaultFlagOrder,
'modifiers': modifiers,
'useFolderName': useFolderName,
Expand All @@ -248,8 +256,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
String? audioExtraOptions,
String? subtitleExtraOptions,
String? attachmentExtraOptions,
String? defaultLanguage,
List<String>? languages,
String? defaultAudioLanguage,
List<String>? audioLanguages,
String? defaultSubtitleLanguage,
List<String>? subtitleLanguages,
List<String>? defaultFlagOrder,
List<TextModifier>? modifiers,
bool? useFolderName,
Expand All @@ -266,8 +276,12 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
subtitleExtraOptions: subtitleExtraOptions ?? this.subtitleExtraOptions,
attachmentExtraOptions:
attachmentExtraOptions ?? this.attachmentExtraOptions,
defaultLanguage: defaultLanguage ?? this.defaultLanguage,
languages: languages ?? List.from(this.languages),
defaultAudioLanguage: defaultAudioLanguage ?? this.defaultAudioLanguage,
audioLanguages: audioLanguages ?? this.audioLanguages,
defaultSubtitleLanguage:
defaultSubtitleLanguage ?? this.defaultSubtitleLanguage,
subtitleLanguages:
subtitleLanguages ?? List.from(this.subtitleLanguages),
defaultFlagOrder: defaultFlagOrder ?? List.from(this.defaultFlagOrder),
modifiers: modifiers ?? List.from(this.modifiers),
useFolderName: useFolderName ?? this.useFolderName,
Expand All @@ -284,8 +298,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
String? audioExtraOptions,
String? subtitleExtraOptions,
String? attachmentExtraOptions,
String? defaultLanguage,
List<String>? languages,
String? defaultAudioLanguage,
List<String>? audioLanguages,
String? defaultSubtitleLanguage,
List<String>? subtitleLanguages,
List<String>? defaultFlagOrder,
List<TextModifier>? modifiers,
bool? useFolderName,
Expand All @@ -302,42 +318,77 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
subtitleExtraOptions ?? this.subtitleExtraOptions;
this.attachmentExtraOptions;
attachmentExtraOptions ?? this.attachmentExtraOptions;
this.defaultLanguage = defaultLanguage ?? this.defaultLanguage;
this.languages = languages ?? this.languages;
this.defaultAudioLanguage =
defaultAudioLanguage ?? this.defaultAudioLanguage;
this.audioLanguages = audioLanguages ?? this.audioLanguages;
this.defaultSubtitleLanguage =
defaultSubtitleLanguage ?? this.defaultSubtitleLanguage;
this.subtitleLanguages = subtitleLanguages ?? this.subtitleLanguages;
this.defaultFlagOrder = defaultFlagOrder ?? this.defaultFlagOrder;
this.modifiers = modifiers ?? this.modifiers;
this.useFolderName = useFolderName ?? this.useFolderName;
notifyListeners();
}

void updateDefaultLanguage(String language) {
if (defaultLanguage == language) {
defaultLanguage = '';
void updateDefaultAudioLanguage(String language) {
if (defaultAudioLanguage == language) {
defaultAudioLanguage = '';
} else {
defaultLanguage = language;
defaultAudioLanguage = language;
}
notifyListeners();
}

void updateLanguages(String iso6393, [bool add = true]) {
void updateAudioLanguages(String iso6393, [bool add = true]) {
if (add) {
if (!languages.contains(iso6393)) {
languages = List.from(languages)..add(iso6393);
if (!audioLanguages.contains(iso6393)) {
audioLanguages = List.from(audioLanguages)..add(iso6393);
}
} else {
if (iso6393 == defaultLanguage) {
updateDefaultLanguage(iso6393);
if (iso6393 == defaultAudioLanguage) {
updateDefaultAudioLanguage(iso6393);
}
languages = List.from(languages)..remove(iso6393);
audioLanguages = List.from(audioLanguages)..remove(iso6393);
}
notifyListeners();
}

void reorderLanguages(int oldIndex, int newIndex) {
void reorderAudioLanguages(int oldIndex, int newIndex) {
if (newIndex > oldIndex) newIndex -= 1;
final languagesCopy = List<String>.from(languages);
final languagesCopy = List<String>.from(audioLanguages);
final item = languagesCopy.removeAt(oldIndex);
languages = languagesCopy..insert(newIndex, item);
audioLanguages = languagesCopy..insert(newIndex, item);
notifyListeners();
}

void updateDefaultSubtitleLanguage(String language) {
if (defaultSubtitleLanguage == language) {
defaultSubtitleLanguage = '';
} else {
defaultSubtitleLanguage = language;
}
notifyListeners();
}

void updateSubtitleLanguages(String iso6393, [bool add = true]) {
if (add) {
if (!subtitleLanguages.contains(iso6393)) {
subtitleLanguages = List.from(subtitleLanguages)..add(iso6393);
}
} else {
if (iso6393 == defaultSubtitleLanguage) {
updateDefaultSubtitleLanguage(iso6393);
}
subtitleLanguages = List.from(subtitleLanguages)..remove(iso6393);
}
notifyListeners();
}

void reorderSubtitleLanguages(int oldIndex, int newIndex) {
if (newIndex > oldIndex) newIndex -= 1;
final languagesCopy = List<String>.from(subtitleLanguages);
final item = languagesCopy.removeAt(oldIndex);
subtitleLanguages = languagesCopy..insert(newIndex, item);
notifyListeners();
}

Expand Down Expand Up @@ -382,8 +433,10 @@ class UserProfile extends ChangeNotifier with EquatableMixin {
audioExtraOptions,
subtitleExtraOptions,
attachmentExtraOptions,
defaultLanguage,
languages,
defaultAudioLanguage,
audioLanguages,
defaultSubtitleLanguage,
subtitleLanguages,
defaultFlagOrder,
modifiers,
useFolderName,
Expand Down
Loading

0 comments on commit 0fe90ac

Please sign in to comment.