Skip to content

Commit

Permalink
Fixing auto backup for ios and android
Browse files Browse the repository at this point in the history
  • Loading branch information
graphefruit committed Dec 16, 2024
1 parent 4db898c commit 36251a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import { UIStorage } from '../services/uiStorage';
import { MeticulousHelpPopoverComponent } from '../popover/meticulous-help-popover/meticulous-help-popover.component';
import { SplashScreen } from '@capacitor/splash-screen';
import { BrewInstanceHelper } from '../classes/brew/brew';
import { AndroidPlatformService } from '../services/androidPlatform/android-platform.service';
import { IosPlatformService } from '../services/iosPlatform/ios-platform.service';

declare var window;

Expand Down Expand Up @@ -236,8 +238,10 @@ export class AppComponent implements AfterViewInit {
private readonly uiExportImportHelper: UIExportImportHelper,
private readonly uiGraphStorage: UIGraphStorage,
private readonly uiStorage: UIStorage,
private readonly androidPlatformService: AndroidPlatformService,
private readonly iosPlatformService: IosPlatformService,
) {
// Dont remove androidPlatformService, we need to initialize it via constructor
// Dont remove androidPlatformService && iosPlatformservice, we need to initialize it via constructor
try {
// Touch DB Factory to make sure, it is properly initialized even on iOS 14.6
const _db = window.indexedDB;
Expand Down
64 changes: 32 additions & 32 deletions src/services/uiExportImportHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class UIExportImportHelper {
private readonly uiAlert: UIAlert,
private readonly uiSettingsStorage: UISettingsStorage,
private readonly uiBrewStorage: UIBrewStorage,
private readonly modalController: ModalController
private readonly modalController: ModalController,
) {}

public async buildExportZIP(): Promise<Blob> {
Expand Down Expand Up @@ -99,13 +99,13 @@ export class UIExportImportHelper {

await zipWriter.add(
EXPORT_MAIN_FILE_NAME,
new TextReader(JSON.stringify(mainJsonData))
new TextReader(JSON.stringify(mainJsonData)),
);

for (const chunk of chunkedData) {
await zipWriter.add(
chunk.fileName,
new TextReader(JSON.stringify(chunk.content))
new TextReader(JSON.stringify(chunk.content)),
);
}

Expand All @@ -114,7 +114,7 @@ export class UIExportImportHelper {
}

public async getJSONFromZIPArrayBufferContent(
_arrayBuffer: Uint8Array | ArrayBuffer
_arrayBuffer: Uint8Array | ArrayBuffer,
): Promise<any> {
const readBlob = new Blob([_arrayBuffer], {
type: 'application/zip',
Expand All @@ -127,12 +127,12 @@ export class UIExportImportHelper {
throw new Error('Attempted to import empty zip file');
}
const foundGeneralEntry = entries.find(
(e) => e.filename === EXPORT_MAIN_FILE_NAME
(e) => e.filename === EXPORT_MAIN_FILE_NAME,
);
if (foundGeneralEntry === undefined) {
await zipReader.close();
throw new Error(
`ZIP file does not contain a ${EXPORT_MAIN_FILE_NAME} file`
`ZIP file does not contain a ${EXPORT_MAIN_FILE_NAME} file`,
);
}

Expand All @@ -147,7 +147,7 @@ export class UIExportImportHelper {
let entry: zip.Entry;
while (
(entry = entries.find(
(e) => e.filename === chunkFileName(c.fileName, i)
(e) => e.filename === chunkFileName(c.fileName, i),
)) !== undefined
) {
this.uiLog.log(`Found ${entry.filename} - Import`);
Expand All @@ -166,7 +166,7 @@ export class UIExportImportHelper {
private async checkBackupAndSeeIfDataAreCorrupted(_actualUIStorageDataObj) {
try {
this.uiLog.log(
'checkBackupAndSeeIfDataAreCorrupted - Check if we got a deep corruption'
'checkBackupAndSeeIfDataAreCorrupted - Check if we got a deep corruption',
);
const dataObj = _actualUIStorageDataObj.DATA;
const parsedJSON: any = await this.readBackupZIPFile();
Expand All @@ -184,37 +184,37 @@ export class UIExportImportHelper {

this.uiLog.log(
'checkBackupAndSeeIfDataAreCorrupted- Check over - if we got a deep corruption - Result: ' +
somethingCorrupted
somethingCorrupted,
);
if (somethingCorrupted) {
const importBackup = await this.showDataCorruptionPopover(
dataObj,
parsedJSON
parsedJSON,
);
if (importBackup) {
await this.importBackupJSON(parsedJSON);
}
} else {
this.uiLog.log(
"checkBackupAndSeeIfDataAreCorrupted - Check over - we didn't find any corrupted data"
"checkBackupAndSeeIfDataAreCorrupted - Check over - we didn't find any corrupted data",
);
}
} else {
this.uiLog.log(
"checkBackupAndSeeIfDataAreCorrupted - We didn't find any json backup data so we can't do any checks"
"checkBackupAndSeeIfDataAreCorrupted - We didn't find any json backup data so we can't do any checks",
);
}
} catch (ex) {
this.uiLog.log(
'Check over - if we got a deep corruption - Result exception: ' +
JSON.stringify(ex)
JSON.stringify(ex),
);
}
}

public async showDataCorruptionPopover(
_actualUIStorageDataObj: any,
_backupDataObj: any
_backupDataObj: any,
) {
const modal = await this.modalController.create({
component: DataCorruptionFoundComponent,
Expand All @@ -227,7 +227,7 @@ export class UIExportImportHelper {
await modal.present();
const returnData = await modal.onWillDismiss();
this.uiLog.log(
'Data corruption, choose to import: ' + returnData?.data?.import
'Data corruption, choose to import: ' + returnData?.data?.import,
);
if (returnData?.data?.import) {
//User choose to import backup, go
Expand All @@ -254,11 +254,11 @@ export class UIExportImportHelper {
if (!hasData || actualUIStorageDataObj.CORRUPTED) {
if (!hasData) {
this.uiLog.log(
'Check Backup - We did not find any data inside the app, so try to find a backup and import it'
'Check Backup - We did not find any data inside the app, so try to find a backup and import it',
);
} else {
this.uiLog.log(
'Check Backup - We found data but they where corrupted, so try to import a backup'
'Check Backup - We found data but they where corrupted, so try to import a backup',
);
}

Expand Down Expand Up @@ -294,7 +294,7 @@ export class UIExportImportHelper {
this.uiAlert.hideLoadingSpinner();
}, 150);
resolve(null);
}
},
);
});
return promise;
Expand All @@ -307,17 +307,16 @@ export class UIExportImportHelper {
async (_arrayBuffer) => {
try {
this.uiLog.log('Read ZIP-File, we found an zip-file');
const parsedJSON = await this.getJSONFromZIPArrayBufferContent(
_arrayBuffer
);
const parsedJSON =
await this.getJSONFromZIPArrayBufferContent(_arrayBuffer);
resolve(parsedJSON);
} catch (ex) {
resolve(null);
}
},
() => {
this.uiLog.log(
`Read ZIP-FILE failed, try to read an old ${EXPORT_MAIN_FILE_NAME}`
`Read ZIP-FILE failed, try to read an old ${EXPORT_MAIN_FILE_NAME}`,
);
this.uiFileHelper.readInternalJSONFile(EXPORT_MAIN_FILE_NAME).then(
async (_json) => {
Expand All @@ -326,12 +325,12 @@ export class UIExportImportHelper {
},
() => {
this.uiLog.log(
'Check Backup - We couldnt retrieve any JSON file'
'Check Backup - We couldnt retrieve any JSON file',
);
resolve(null);
}
},
);
}
},
);
});
return promise;
Expand All @@ -348,6 +347,7 @@ export class UIExportImportHelper {
this.buildExportZIP().then(
async (_blob) => {
try {
this.uiLog.debug('Start writing automatic backups');
this.__saveInternalBeanconquerorDump(_blob);
this.__saveAutomaticBeanconquerorDump(_blob);
} catch (ex) {
Expand All @@ -358,7 +358,7 @@ export class UIExportImportHelper {
'ZIP_BACKUP_FILE_COULD_NOT_BE_BUILD',
'CARE',
'OK',
true
true,
);
}
}
Expand All @@ -371,17 +371,17 @@ export class UIExportImportHelper {
'ZIP_BACKUP_FILE_COULD_NOT_BE_BUILD',
'CARE',
'OK',
true
true,
);
}
}
},
);
}
private async __saveInternalBeanconquerorDump(_blob: Blob) {
try {
await this.uiFileHelper.writeInternalFileFromBlob(
_blob,
'Beanconqueror.zip'
'Beanconqueror.zip',
);
} catch (ex) {
this.uiLog.error('Could not to export normal ZIP file');
Expand All @@ -391,7 +391,7 @@ export class UIExportImportHelper {
'INTERNAL_BACKUP_DID_FAIL',
'CARE',
'OK',
true
true,
);
}
}
Expand All @@ -408,7 +408,7 @@ export class UIExportImportHelper {
'Download/Beanconqueror_export/Beanconqueror_automatic_export_' +
this.getAutomatedBackupFilename() +
'.zip',
Directory.External
Directory.External,
);
} catch (ex) {
this.uiLog.error('Could not to export automatic ZIP file');
Expand All @@ -417,7 +417,7 @@ export class UIExportImportHelper {
'AUTOMATIC_BACKUP_DID_FAIL',
'CARE',
'OK',
true
true,
);
}
}
Expand Down

0 comments on commit 36251a9

Please sign in to comment.