From 19922ff4ba5d77cb2f6b62a63287f1957efb554f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phil=C3=A9as?= Date: Tue, 26 Dec 2023 19:59:10 +0100 Subject: [PATCH] Refactor getCurrentProfile --- lib/models/profile.dart | 2 + .../calendar_editor/calendar_editor_page.dart | 2 +- .../video_subtitles_editor_page.dart | 2 +- .../widgets/create_movie_button.dart | 2 +- lib/pages/home/profiles/profiles_page.dart | 19 ++++---- lib/pages/save_video/save_video_page.dart | 13 ++++-- lib/utils/utils.dart | 43 ++++++------------- 7 files changed, 39 insertions(+), 44 deletions(-) diff --git a/lib/models/profile.dart b/lib/models/profile.dart index e99d7f8..809134a 100644 --- a/lib/models/profile.dart +++ b/lib/models/profile.dart @@ -1,11 +1,13 @@ class Profile { const Profile({ required this.label, + required this.storageString, this.isDefault = false, this.isVertical = false, }); final String label; + final String storageString; final bool isDefault; final bool isVertical; } diff --git a/lib/pages/home/calendar_editor/calendar_editor_page.dart b/lib/pages/home/calendar_editor/calendar_editor_page.dart index 87a9c54..1cf7d87 100644 --- a/lib/pages/home/calendar_editor/calendar_editor_page.dart +++ b/lib/pages/home/calendar_editor/calendar_editor_page.dart @@ -79,7 +79,7 @@ class _CalendarEditorPageState extends State { } void setMediaStorePath() { - final currentProfile = Utils.getCurrentProfileString(); + final currentProfile = Utils.getCurrentProfile().storageString; if (currentProfile.isEmpty || currentProfile == 'Default') { MediaStore.appFolder = 'OneSecondDiary'; } else { diff --git a/lib/pages/home/calendar_editor/video_subtitles_editor_page.dart b/lib/pages/home/calendar_editor/video_subtitles_editor_page.dart index b9e8438..9c3e5b2 100644 --- a/lib/pages/home/calendar_editor/video_subtitles_editor_page.dart +++ b/lib/pages/home/calendar_editor/video_subtitles_editor_page.dart @@ -176,7 +176,7 @@ class _VideoSubtitlesEditorPageState extends State { maxHeight: MediaQuery.of(context).size.height * 0.7 ), child: AspectRatio( - aspectRatio: _videoController!.value.aspectRatio, + aspectRatio: _videoController.value.aspectRatio, child: Stack( children: [ VideoPlayer( diff --git a/lib/pages/home/create_movie/widgets/create_movie_button.dart b/lib/pages/home/create_movie/widgets/create_movie_button.dart index 646646e..584ae4e 100644 --- a/lib/pages/home/create_movie/widgets/create_movie_button.dart +++ b/lib/pages/home/create_movie/widgets/create_movie_button.dart @@ -106,7 +106,7 @@ class _CreateMovieButtonState extends State { ScaffoldMessenger.of(context).showSnackBar(snackBar); // Get current profile - final currentProfileName = Utils.getCurrentProfileString(); + final currentProfileName = Utils.getCurrentProfile().storageString; // Videos folder String videosFolder = SharedPrefsUtil.getString('appPath'); diff --git a/lib/pages/home/profiles/profiles_page.dart b/lib/pages/home/profiles/profiles_page.dart index 7651b0d..509e93c 100644 --- a/lib/pages/home/profiles/profiles_page.dart +++ b/lib/pages/home/profiles/profiles_page.dart @@ -56,17 +56,17 @@ class _ProfilesPageState extends State { if (!storedProfiles.contains('Default')) { profiles.insert( 0, - const Profile(label: 'Default', isDefault: true, isVertical: false), + const Profile(label: 'Default', storageString: 'Default', isDefault: true, isVertical: false), ); } else { // Profiles strings ending with '_vertical' creates an Profile object with isVertical value true, as other not. profiles = storedProfiles.map( (e) { - if (e == 'Default') return Profile(label: e, isDefault: true, isVertical: false); + if (e == 'Default') return Profile(label: e, storageString: e, isDefault: true, isVertical: false); if (e.endsWith('_vertical')) - return Profile(label: e.replaceAll('_vertical', ''), isVertical: true); + return Profile(label: e.replaceAll('_vertical', ''), storageString: e, isVertical: true); else - return Profile(label: e, isVertical: false); + return Profile(label: e, storageString: e, isVertical: false); }, ).toList(); } @@ -206,6 +206,8 @@ class _ProfilesPageState extends State { profiles.length, Profile( label: _profileNameController.text.trim(), + storageString: _verticalModeSwitch? '${_profileNameController.text.trim()}_vertical' + : _profileNameController.text.trim(), isVertical: _verticalModeSwitch), ); _profileNameController.clear(); @@ -214,8 +216,7 @@ class _ProfilesPageState extends State { // Add the modified profile list to persistence // Adds the string '_vertical' at the end of vertical profiles to keep this parameter persistent. final profileNamesToStringList = profiles - .map((e) => e.isVertical ? '${e.label}_vertical' : e.label) - .toList(); + .map((e) => e.storageString).toList(); SharedPrefsUtil.putStringList('profiles', profileNamesToStringList); @@ -269,11 +270,11 @@ class _ProfilesPageState extends State { onPressed: () async { // Delete the profile directory for the specific profile await StorageUtils.deleteSpecificProfileFolder( - profiles[index].label, + profiles[index].storageString, ); Utils.logWarning( - '${logTag}Profile ${profiles[index].label} deleted!', + '${logTag}Profile ${profiles[index].storageString} deleted!', ); // Remove the profile from the list @@ -426,7 +427,7 @@ class _ProfilesPageState extends State { // Update daily entry final String today = DateFormatUtils.getToday(); - final String profile = Utils.getCurrentProfileObject().label; + final String profile = Utils.getCurrentProfile().label; String todaysVideoPath = SharedPrefsUtil.getString('appPath'); if (profile.isEmpty) { todaysVideoPath = '$todaysVideoPath$today.mp4'; diff --git a/lib/pages/save_video/save_video_page.dart b/lib/pages/save_video/save_video_page.dart index 3b5eab1..96edbc3 100644 --- a/lib/pages/save_video/save_video_page.dart +++ b/lib/pages/save_video/save_video_page.dart @@ -67,7 +67,7 @@ class _SaveVideoPageState extends State { bool _isLocationProcessing = false; late final bool isDarkTheme = ThemeService().isDarkTheme(); - Profile selectedProfile = Utils.getCurrentProfileObject(); + Profile selectedProfile = Utils.getCurrentProfile(); void _initCorrectDates() { final DateTime selectedDate = routeArguments['currentDate']; @@ -604,13 +604,20 @@ class _SaveVideoPageState extends State { const SizedBox(width: 8), Flexible( child: Text( - selectedProfile.label.isEmpty ? 'default'.tr : selectedProfile.label, + selectedProfile.label.isEmpty ? 'default'.tr : + selectedProfile.isVertical ? selectedProfile.label.replaceAll('_vertical', '') + : selectedProfile.label, style: TextStyle( fontSize: MediaQuery.of(context).size.height * 0.019, ), ), ), const SizedBox(width: 20), + Icon( + selectedProfile.isVertical? Icons.stay_current_portrait : + Icons.stay_current_landscape + ), + const SizedBox(width: 20), Flexible( child: TextButton( style: ButtonStyle( @@ -625,7 +632,7 @@ class _SaveVideoPageState extends State { onPressed: () { Get.to(const ProfilesPage())?.then( (_) => setState(() { - selectedProfile = Utils.getCurrentProfileObject(); + selectedProfile = Utils.getCurrentProfile(); }), ); }, diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index c4710b1..467d2b7 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -149,7 +149,7 @@ class Utils { logInfo('[Utils.writeTxt()] - Writing txt file to $txtPath'); // Get current profile - final currentProfileName = getCurrentProfileString(); + final currentProfileName = getCurrentProfile().storageString; // Default directory String videosFolderPath = SharedPrefsUtil.getString('appPath'); @@ -253,48 +253,33 @@ class Utils { return '$hoursString:$minutesString:$secondsString,$millisecondsString'; } - /// Get current profile name, empty string if Default - static String getCurrentProfileString() { + /// Get current profile object, empty string if Default. + /// As vertical profiles are saved with suffix '_vertical', + /// storageString = what's in storage, label = name without suffix. + static Profile getCurrentProfile() { // Get current profile - String currentProfileName = ''; - - final selectedProfileIndex = SharedPrefsUtil.getInt('selectedProfileIndex') ?? 0; - if (selectedProfileIndex != 0) { - final allProfiles = SharedPrefsUtil.getStringList('profiles'); - if (allProfiles != null) { - currentProfileName = allProfiles[selectedProfileIndex]; - } - } - - final profileLog = currentProfileName == '' ? 'Default' : currentProfileName; - logInfo('[Utils.getCurrentProfile()] - Selected profile: $profileLog'); - return currentProfileName; - } - - /// Get current profile object, with isVertical property - static Profile getCurrentProfileObject() { - // Get current profile - String currentProfileName = ''; + String currentProfileStorageString = ''; + String currentProfileLabel = ''; bool isVertical = false; final selectedProfileIndex = SharedPrefsUtil.getInt('selectedProfileIndex') ?? 0; if (selectedProfileIndex != 0) { final allProfiles = SharedPrefsUtil.getStringList('profiles'); if (allProfiles != null) { - currentProfileName = allProfiles[selectedProfileIndex]; + currentProfileStorageString = allProfiles[selectedProfileIndex]; - // Vertical profiles are stored with '_vertical' and the end, but shown without. - if(currentProfileName.endsWith('_vertical')) { + // Vertical profiles are stored with '_vertical' in storage, but shown without. + if(currentProfileStorageString.endsWith('_vertical')) { isVertical = true; - currentProfileName.replaceAll('_vertical', ''); + currentProfileLabel = currentProfileStorageString.replaceAll('_vertical', ''); } } } - final profileLog = currentProfileName == '' ? 'Default' : currentProfileName; + final profileLog = currentProfileStorageString == '' ? 'Default' : currentProfileStorageString; logInfo('[Utils.getCurrentProfile()] - Selected profile: $profileLog'); - return Profile(label: currentProfileName, isVertical: isVertical); + return Profile(storageString: currentProfileStorageString, label: currentProfileLabel, isVertical: isVertical); } /// Get all video files inside DCIM/OneSecondDiary/Movies folder @@ -327,7 +312,7 @@ class Utils { static List getAllVideos({bool fullPath = false}) { logInfo('[Utils.getAllVideos()] - Asked for full path: $fullPath'); // Get current profile - final currentProfileName = getCurrentProfileString(); + final currentProfileName = getCurrentProfile().storageString; // Default directory io.Directory directory = io.Directory(SharedPrefsUtil.getString('appPath'));