Skip to content

Commit

Permalink
[Infrastructure] Added a method to read backup file and restore it
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 23, 2023
1 parent 1e31f28 commit e7812b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
77 changes: 67 additions & 10 deletions lib/infrastructure/backup_restore_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ import 'package:shiori/domain/services/backup_restore_service.dart';
import 'package:shiori/domain/services/data_service.dart';
import 'package:shiori/domain/services/device_info_service.dart';
import 'package:shiori/domain/services/logging_service.dart';
import 'package:shiori/domain/services/notification_service.dart';
import 'package:shiori/domain/services/settings_service.dart';
import 'package:version/version.dart';

const _fileExtension = '.bk';

class BackupRestoreServiceImpl implements BackupRestoreService {
final LoggingService _loggingService;
final SettingsService _settingsService;
final DeviceInfoService _deviceInfoService;
final DataService _dataService;
final NotificationService _notificationService;

BackupRestoreServiceImpl(this._loggingService, this._settingsService, this._deviceInfoService, this._dataService);
BackupRestoreServiceImpl(this._loggingService, this._settingsService, this._deviceInfoService, this._dataService, this._notificationService);

@override
Future<CreateBackupResultModel> createBackup() async {
//TODO: CHANGE THE FILENAME TO .bk
final filename = 'shiori_backup_${DateTime.now().millisecondsSinceEpoch}.json';
final filename = path.join('shiori_backup_', '${DateTime.now().millisecondsSinceEpoch}', _fileExtension);
final dirPath = await _getBackupDir();
final filePath = path.join(dirPath, filename);
try {
Expand All @@ -46,7 +50,7 @@ class BackupRestoreServiceImpl implements BackupRestoreService {
notifications: notifications,
);

_loggingService.info(runtimeType, 'Creating json ...');
_loggingService.info(runtimeType, 'Creating json...');
final jsonMap = bk.toJson();

final file = File(filePath);
Expand All @@ -55,21 +59,74 @@ class BackupRestoreServiceImpl implements BackupRestoreService {
}
final jsonString = json.encode(jsonMap);

_loggingService.info(runtimeType, 'Creating file ...');
_loggingService.info(runtimeType, 'Saving file...');
await file.create();
await file.writeAsString(jsonString);

_loggingService.info(runtimeType, 'Bk = $filename was successfully created');
_loggingService.info(runtimeType, 'Bk = $filePath was successfully created');
return CreateBackupResultModel(name: filename, path: filePath, succeed: true);
} catch (e, s) {
_loggingService.error(runtimeType, 'Error creating bk = $filename', e, s);
_loggingService.error(runtimeType, 'Error creating bk = $filePath', e, s);
return CreateBackupResultModel(name: filename, path: filePath, succeed: false);
}
}

Future<void> restoreBackup(String filePath) async {
//TODO: CHECK FILE
//TODO: RETURN RESULT
@override
Future<BackupModel?> readBackup(String filePath) async {
_loggingService.info(runtimeType, 'readBackup: Trying to read file = $filePath');
final file = File(filePath);
if (!await file.exists()) {
_loggingService.warning(runtimeType, 'readBackup: File = $filePath does not exist');
return null;
}

try {
final jsonString = await file.readAsString();
final jsonMap = json.decode(jsonString) as Map<String, dynamic>;
return BackupModel.fromJson(jsonMap);
} catch (e, s) {
_loggingService.error(runtimeType, 'readBackup: Error reading file = $filePath', e, s);
return null;
}
}

@override
bool canBackupBeRestored(String bkAppVersion) {
try {
final bkVersion = Version.parse(bkAppVersion);
final appVersion = Version.parse(_deviceInfoService.version);
return bkVersion <= appVersion;
} catch (_) {
return false;
}
}

@override
Future<bool> restoreBackup(BackupModel bk) async {
await _notificationService.cancelAllNotifications();

if (!canBackupBeRestored(bk.appVersion)) {
return false;
}

try {
_settingsService.restoreFromBackup(bk.settings);

await _dataService.tierList.restoreFromBackup(bk.tierList);

await _dataService.notifications.restoreFromBackup(bk.notifications, bk.settings.serverResetTime);

await _dataService.customBuilds.restoreFromBackup(bk.customBuilds);

await _dataService.inventory.restoreFromBackup(bk.inventory);

await _dataService.calculator.restoreFromBackup(bk.calculatorAscMaterials);

return true;
} catch (e, s) {
_loggingService.error(runtimeType, 'restoreBackup: Error restoring bk', e, s);
return false;
}
}

//TODO: PERHAPS CREATE A BK FOLDER ?
Expand Down
15 changes: 15 additions & 0 deletions lib/infrastructure/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ class SettingsServiceImpl extends SettingsService {
_logger.info(runtimeType, 'Settings were initialized successfully');
}

@override
void restoreFromBackup(AppSettings settings) {
appTheme = settings.appTheme;
useDarkAmoledTheme = settings.useDarkAmoled;
accentColor = settings.accentColor;
language = settings.appLanguage;
showCharacterDetails = settings.showCharacterDetails;
showWeaponDetails = settings.showWeaponDetails;
isFirstInstall = settings.isFirstInstall;
serverResetTime = settings.serverResetTime;
doubleBackToClose = settings.doubleBackToClose;
useOfficialMap = settings.useOfficialMap;
useTwentyFourHoursFormat = settings.useTwentyFourHoursFormat;
}

Future<AppLanguageType> _getDefaultLangToUse() async {
try {
_logger.info(runtimeType, '_getDefaultLangToUse: Trying to retrieve device lang...');
Expand Down

0 comments on commit e7812b5

Please sign in to comment.