Skip to content

Commit

Permalink
feat: auto backups
Browse files Browse the repository at this point in the history
ref #69
  • Loading branch information
MSOB7YY committed Feb 8, 2024
1 parent 26224d7 commit 241dcfa
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/controller/backup_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,49 @@ class BackupController {
final RxBool isRestoringBackup = false.obs;

String get _backupDirectoryPath => settings.defaultBackupLocation.value;
int get _defaultAutoBackupInterval => settings.autoBackupIntervalDays.value;

Future<void> checkForAutoBackup() async {
final interval = _defaultAutoBackupInterval;
if (interval <= 0) return;

if (!await requestManageStoragePermission()) return;

final sortedBackupFiles = await _getBackupFilesSorted.thready(_backupDirectoryPath);
final latestBackup = sortedBackupFiles.firstOrNull;
if (latestBackup != null) {
final lastModified = await latestBackup.stat().then((value) => value.modified);
final diff = DateTime.now().difference(lastModified).abs().inDays;
if (diff > interval) {
final itemsToBackup = [
AppPaths.TRACKS,
AppPaths.TRACKS_STATS,
AppPaths.TOTAL_LISTEN_TIME,
AppPaths.VIDEOS_CACHE,
AppPaths.VIDEOS_LOCAL,
AppPaths.FAVOURITES_PLAYLIST,
AppPaths.SETTINGS,
AppPaths.LATEST_QUEUE,
AppPaths.YT_LIKES_PLAYLIST,
AppDirs.PLAYLISTS,
AppDirs.HISTORY_PLAYLIST,
AppDirs.QUEUES,
AppDirs.YT_DOWNLOAD_TASKS,
AppDirs.YT_STATS,
AppDirs.YT_PLAYLISTS,
AppDirs.YT_HISTORY_PLAYLIST,
];

await createBackupFile(itemsToBackup);
}
}
}

Future<void> createBackupFile(List<String> backupItemsPaths) async {
if (isCreatingBackup.value) return snackyy(title: lang.NOTE, message: lang.ANOTHER_PROCESS_IS_RUNNING);

if (!await requestManageStoragePermission()) {
return;
}
if (!await requestManageStoragePermission()) return;

isCreatingBackup.value = true;

// formats date
Expand Down
7 changes: 7 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class SettingsController {
final RxBool preventDuplicatedTracks = false.obs;
final RxBool respectNoMedia = false.obs;
final RxString defaultBackupLocation = AppDirs.BACKUPS.obs;
final RxInt autoBackupIntervalDays = 2.obs;
final RxString defaultFolderStartupLocation = kStoragePaths.first.obs;
final RxString ytDownloadLocation = AppDirs.YOUTUBE_DOWNLOADS_DEFAULT.obs;
final RxBool enableFoldersHierarchy = true.obs;
Expand Down Expand Up @@ -401,6 +402,7 @@ class SettingsController {
preventDuplicatedTracks.value = json['preventDuplicatedTracks'] ?? preventDuplicatedTracks.value;
respectNoMedia.value = json['respectNoMedia'] ?? respectNoMedia.value;
defaultBackupLocation.value = json['defaultBackupLocation'] ?? defaultBackupLocation.value;
autoBackupIntervalDays.value = json['autoBackupIntervalDays'] ?? autoBackupIntervalDays.value;
defaultFolderStartupLocation.value = json['defaultFolderStartupLocation'] ?? defaultFolderStartupLocation.value;
ytDownloadLocation.value = json['ytDownloadLocation'] ?? ytDownloadLocation.value;
enableFoldersHierarchy.value = json['enableFoldersHierarchy'] ?? enableFoldersHierarchy.value;
Expand Down Expand Up @@ -638,6 +640,7 @@ class SettingsController {
'preventDuplicatedTracks': preventDuplicatedTracks.value,
'respectNoMedia': respectNoMedia.value,
'defaultBackupLocation': defaultBackupLocation.value,
'autoBackupIntervalDays': autoBackupIntervalDays.value,
'defaultFolderStartupLocation': defaultFolderStartupLocation.value,
'ytDownloadLocation': ytDownloadLocation.value,
'enableFoldersHierarchy': enableFoldersHierarchy.value,
Expand Down Expand Up @@ -830,6 +833,7 @@ class SettingsController {
bool? preventDuplicatedTracks,
bool? respectNoMedia,
String? defaultBackupLocation,
int? autoBackupIntervalDays,
String? defaultFolderStartupLocation,
String? ytDownloadLocation,
bool? enableFoldersHierarchy,
Expand Down Expand Up @@ -1171,6 +1175,9 @@ class SettingsController {
if (defaultBackupLocation != null) {
this.defaultBackupLocation.value = defaultBackupLocation;
}
if (autoBackupIntervalDays != null) {
this.autoBackupIntervalDays.value = autoBackupIntervalDays;
}
if (defaultFolderStartupLocation != null) {
this.defaultFolderStartupLocation.value = defaultFolderStartupLocation;
}
Expand Down
1 change: 1 addition & 0 deletions lib/core/translations/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class LanguageKeys {
String get AUDIO => _getKey('AUDIO');
String get AUDIO_CACHE => _getKey('AUDIO_CACHE');
String get AUDIO_ONLY => _getKey('AUDIO_ONLY');
String get AUTO_BACKUP_INTERVAL => _getKey('AUTO_BACKUP_INTERVAL');
String get AUTO_COLORING_SUBTITLE => _getKey('AUTO_COLORING_SUBTITLE');
String get AUTO_COLORING => _getKey('AUTO_COLORING');
String get AUTO_EXTRACT_TAGS_FROM_FILENAME => _getKey('AUTO_EXTRACT_TAGS_FROM_FILENAME');
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:jiffy/jiffy.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

import 'package:namida/controller/backup_controller.dart';
import 'package:namida/controller/connectivity.dart';
import 'package:namida/controller/current_color.dart';
import 'package:namida/controller/folders_controller.dart';
Expand Down Expand Up @@ -124,6 +125,9 @@ void main() async {
if (!shouldShowOnBoarding && settings.refreshOnStartup.value) {
Indexer.inst.refreshLibraryAndCheckForDiff(allowDeletion: false, showFinishedSnackbar: false);
}
if (!shouldShowOnBoarding) {
BackupController.inst.checkForAutoBackup();
}

QueueController.inst.prepareAllQueuesFile();

Expand Down
27 changes: 27 additions & 0 deletions lib/ui/widgets/settings/backup_restore_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum _BackupAndRestoreKeys {
create,
restore,
defaultLocation,
autoBackupInterval,
importYT,
importLastfm,
}
Expand All @@ -41,6 +42,7 @@ class BackupAndRestore extends SettingSubpageProvider {
_BackupAndRestoreKeys.create: [lang.CREATE_BACKUP],
_BackupAndRestoreKeys.restore: [lang.RESTORE_BACKUP],
_BackupAndRestoreKeys.defaultLocation: [lang.DEFAULT_BACKUP_LOCATION],
_BackupAndRestoreKeys.autoBackupInterval: [lang.AUTO_BACKUP_INTERVAL],
_BackupAndRestoreKeys.importYT: [lang.IMPORT_YOUTUBE_HISTORY],
_BackupAndRestoreKeys.importLastfm: [lang.IMPORT_LAST_FM_HISTORY],
};
Expand Down Expand Up @@ -516,6 +518,31 @@ class BackupAndRestore extends SettingSubpageProvider {
// -- Default Backup Location
getDefaultBackupLocationWidget(),

// -- Auto backup interval
getItemWrapper(
key: _BackupAndRestoreKeys.autoBackupInterval,
child: CustomListTile(
bgColor: getBgColor(_BackupAndRestoreKeys.autoBackupInterval),
title: lang.AUTO_BACKUP_INTERVAL,
icon: Broken.timer,
trailing: Obx(
() {
final days = settings.autoBackupIntervalDays.value;
return NamidaWheelSlider<int>(
totalCount: 14,
squeeze: 1,
initValue: days,
itemSize: 5,
onValueChanged: (val) {
settings.save(autoBackupIntervalDays: val);
},
text: days == 0 ? lang.NONE : "$days ${days == 1 ? lang.DAY : lang.DAYS}",
);
},
),
),
),

// -- Import Youtube History
getItemWrapper(
key: _BackupAndRestoreKeys.importYT,
Expand Down

0 comments on commit 241dcfa

Please sign in to comment.