private async Task RestoreData() { Enums.BackupRestoreStatus status = Enums.BackupRestoreStatus.Processing; var setting = await SettingService.GetSettingValue <bool>(Constants.RESTORE_SETTING_KEY); if (setting) { throw new Exception("Data already restored!"); } var restoreDataCheck = await BindingPage.DisplayAlert("Are you sure you want to restore data?", "WARNING: Restoring wrongly may cause duplicate entries", "Yes, I am Sure", "Cancel"); await RunAsync(async (ct) => { if (restoreDataCheck) { status = await BackupService.RestoreData(); } }, true, (ex) => { status = Enums.BackupRestoreStatus.Failed; }); switch (status) { case Enums.BackupRestoreStatus.Sucess: LoggerService.LogInformation("Data Restored Successfully"); break; case Enums.BackupRestoreStatus.Failed: LoggerService.LogInformation("Restore Failed"); break; case Enums.BackupRestoreStatus.Processing: break; } }
public async static Task <Enums.BackupRestoreStatus> RestoreData() { var fileName = Path.Combine(DependencyService.Get <IFileAccessService>().GetFileLocation(), Constants.BACKUP_FILE_NAME); var restoreSetting = await SettingService.GetSettingValue <bool>(Constants.RESTORE_SETTING_KEY); if (!restoreSetting && File.Exists(fileName)) { var data = File.ReadAllText(fileName); if (data.Length > 0) { var tableBackups = JsonConvert.DeserializeObject <List <BackupModel> >(data); if (tableBackups.Any()) { foreach (var backupData in tableBackups) { var isInserted = await App.DbService.BulkInsertAsync(backupData.TableType, backupData.BackupData); if (isInserted > 0) { var setting = await SettingService.GetSetting(Constants.RESTORE_SETTING_KEY); if (setting != null) { setting.SettingValue = "true"; await App.DbService.UpdateAsync(setting); return(Enums.BackupRestoreStatus.Sucess); } } } } } } return(Enums.BackupRestoreStatus.Failed); }