-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,70 @@ | ||
import 'dart:convert'; | ||
// ignore: avoid_web_libraries_in_flutter | ||
import 'dart:html' as html; | ||
|
||
import 'dart:html'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:logger/logger.dart'; | ||
import 'package:wealth_wave/api/db/app_database.dart'; | ||
|
||
class BackupApi { | ||
final AppDatabase _db; | ||
|
||
BackupApi({final AppDatabase? db}) : _db = db ?? AppDatabase.instance; | ||
|
||
Future<void> importDatabase() async { | ||
final file = await _pickFile(); | ||
if (file == null) { | ||
return Future.error('No file selected'); | ||
} | ||
|
||
final json = await _readJsonFromFile(file); | ||
|
||
if (json == null) { | ||
return Future.error('Not a json file'); | ||
} | ||
|
||
await _db.loadBackup(json); | ||
} | ||
|
||
Future<void> exportDatabase() async { | ||
final result = await _db.getBackup(); | ||
|
||
final jsonString = json.encode(result); | ||
|
||
final blob = html.Blob([Uint8List.fromList(utf8.encode(jsonString))]); | ||
final blob = Blob([Uint8List.fromList(utf8.encode(jsonString))]); | ||
|
||
final anchor = html.AnchorElement(href: html.Url.createObjectUrlFromBlob(blob)) | ||
final anchor = AnchorElement(href: Url.createObjectUrlFromBlob(blob)) | ||
..target = 'blank' | ||
..download = 'wealth_app_data.json'; | ||
|
||
// Trigger a click event to prompt the user to download the file | ||
html.document.body?.append(anchor); | ||
document.body?.append(anchor); | ||
anchor.click(); | ||
// Clean up | ||
html.Url.revokeObjectUrl(anchor.href!); | ||
Url.revokeObjectUrl(anchor.href!); | ||
} | ||
|
||
Future<File?> _pickFile() async { | ||
final input = FileUploadInputElement()..accept = 'application/json'; | ||
input.click(); | ||
|
||
await input.onChange.first; | ||
return input.files?.isNotEmpty == true ? input.files![0] : null; | ||
} | ||
|
||
Future<Map<String, List<Map<String, dynamic>>>?> _readJsonFromFile( | ||
File file) async { | ||
try { | ||
final reader = FileReader(); | ||
reader.readAsText(file); | ||
|
||
await reader.onLoad.first; | ||
|
||
final jsonString = reader.result as String; | ||
return json.decode(jsonString) as Map<String, List<Map<String, dynamic>>>; | ||
} catch (e) { | ||
Logger().e(e.toString()); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:wealth_wave/api/apis/backup_api.dart'; | ||
import 'package:wealth_wave/core/presenter.dart'; | ||
import 'package:wealth_wave/core/single_event.dart'; | ||
|
||
class MainPresenter extends Presenter<MainViewState> { | ||
final BackupApi _backupApi; | ||
|
||
MainPresenter({final BackupApi? backupApi}) | ||
: _backupApi = backupApi ?? BackupApi(), | ||
super(MainViewState()); | ||
|
||
void performImportFile() { | ||
_backupApi.importDatabase().then((value) => updateViewState((viewState) { | ||
viewState.onImportCompleted = SingleEvent(null); | ||
})); | ||
} | ||
|
||
void performBackup() { | ||
_backupApi.exportDatabase().then((value) => updateViewState((viewState) { | ||
viewState.onBackupCompleted = SingleEvent(null); | ||
})); | ||
} | ||
} | ||
|
||
class MainViewState { | ||
SingleEvent<void>? onBackupCompleted; | ||
SingleEvent<void>? onImportCompleted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters