Skip to content

Commit

Permalink
Refactor getCurrentProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
phileastv committed Dec 26, 2023
1 parent a953d27 commit 19922ff
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 44 deletions.
2 changes: 2 additions & 0 deletions lib/models/profile.dart
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion lib/pages/home/calendar_editor/calendar_editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _CalendarEditorPageState extends State<CalendarEditorPage> {
}

void setMediaStorePath() {
final currentProfile = Utils.getCurrentProfileString();
final currentProfile = Utils.getCurrentProfile().storageString;
if (currentProfile.isEmpty || currentProfile == 'Default') {
MediaStore.appFolder = 'OneSecondDiary';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class _VideoSubtitlesEditorPageState extends State<VideoSubtitlesEditorPage> {
maxHeight: MediaQuery.of(context).size.height * 0.7
),
child: AspectRatio(
aspectRatio: _videoController!.value.aspectRatio,
aspectRatio: _videoController.value.aspectRatio,
child: Stack(
children: [
VideoPlayer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class _CreateMovieButtonState extends State<CreateMovieButton> {
ScaffoldMessenger.of(context).showSnackBar(snackBar);

// Get current profile
final currentProfileName = Utils.getCurrentProfileString();
final currentProfileName = Utils.getCurrentProfile().storageString;

// Videos folder
String videosFolder = SharedPrefsUtil.getString('appPath');
Expand Down
19 changes: 10 additions & 9 deletions lib/pages/home/profiles/profiles_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class _ProfilesPageState extends State<ProfilesPage> {
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();
}
Expand Down Expand Up @@ -206,6 +206,8 @@ class _ProfilesPageState extends State<ProfilesPage> {
profiles.length,
Profile(
label: _profileNameController.text.trim(),
storageString: _verticalModeSwitch? '${_profileNameController.text.trim()}_vertical'
: _profileNameController.text.trim(),
isVertical: _verticalModeSwitch),
);
_profileNameController.clear();
Expand All @@ -214,8 +216,7 @@ class _ProfilesPageState extends State<ProfilesPage> {
// 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);

Expand Down Expand Up @@ -269,11 +270,11 @@ class _ProfilesPageState extends State<ProfilesPage> {
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
Expand Down Expand Up @@ -426,7 +427,7 @@ class _ProfilesPageState extends State<ProfilesPage> {

// 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';
Expand Down
13 changes: 10 additions & 3 deletions lib/pages/save_video/save_video_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class _SaveVideoPageState extends State<SaveVideoPage> {
bool _isLocationProcessing = false;

late final bool isDarkTheme = ThemeService().isDarkTheme();
Profile selectedProfile = Utils.getCurrentProfileObject();
Profile selectedProfile = Utils.getCurrentProfile();

void _initCorrectDates() {
final DateTime selectedDate = routeArguments['currentDate'];
Expand Down Expand Up @@ -604,13 +604,20 @@ class _SaveVideoPageState extends State<SaveVideoPage> {
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(
Expand All @@ -625,7 +632,7 @@ class _SaveVideoPageState extends State<SaveVideoPage> {
onPressed: () {
Get.to(const ProfilesPage())?.then(
(_) => setState(() {
selectedProfile = Utils.getCurrentProfileObject();
selectedProfile = Utils.getCurrentProfile();
}),
);
},
Expand Down
43 changes: 14 additions & 29 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -327,7 +312,7 @@ class Utils {
static List<String> 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'));
Expand Down

0 comments on commit 19922ff

Please sign in to comment.