-
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.
Merge pull request #34 from mario-bermonti:export-data
Export-data
- Loading branch information
Showing
7 changed files
with
106 additions
and
0 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
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,10 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:mdigit_span_tasks/src/export_data/view/export_button.dart'; | ||
|
||
final AppBar appBar = AppBar( | ||
title: const Text('mDigitSpanTask'), | ||
centerTitle: true, | ||
actions: [ | ||
ExportButton(), | ||
], | ||
); |
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,61 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:cognitive_data/errors.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
|
||
class DataExporter { | ||
final String dbName; | ||
late final File db; | ||
late final String dbPath; | ||
late final File destinationFile; | ||
|
||
DataExporter({required this.dbName}); | ||
|
||
/// Request storage permission | ||
Future<void> requestPermission() async { | ||
if (Platform.isAndroid) { | ||
bool granted = await Permission.storage.request().isGranted; | ||
if (!granted) { | ||
throw PermissionNotGrantedException(); | ||
} | ||
} | ||
} | ||
|
||
/// Get db from disk so it can be copied | ||
/// It assumes the db is in the [getApplicationDocumentsDirectory] dir | ||
/// with the name [dbName]. This method does not modify the original db. | ||
Future<void> getDB() async { | ||
final Directory dir = await getApplicationDocumentsDirectory(); | ||
dbPath = "${dir.path}/$dbName"; | ||
db = File(dbPath); | ||
} | ||
|
||
/// Get destination file on which to save the db copy. | ||
/// The destination file will be in the `Download` folder and will have the | ||
/// same name as the dbs. | ||
/// Currently only android is supported. | ||
Future<void> initDestinationFile() async { | ||
Directory destinationDir = await _initDestinationDir(); | ||
final String destinationPath = '${destinationDir.path}/$dbName'; | ||
destinationFile = File(destinationPath); | ||
} | ||
|
||
/// Gets the directory to which the db will be exported. | ||
/// Currently only android is supported and the destination dir is a | ||
/// directory named `mDigitSpanTasks` in the android `Download` folder. | ||
/// It creates the directory with its parent dirs if these do not exist. | ||
Future<Directory> _initDestinationDir() async { | ||
Directory downloadsDir = | ||
Directory('/storage/emulated/0/Download/mDigitSpanTasks'); | ||
await downloadsDir.create(recursive: true); | ||
|
||
return downloadsDir; | ||
} | ||
|
||
/// Create copy of db in the path specified by [destinationFile]. | ||
/// Does not alter the original db. | ||
Future<void> copyDB() async { | ||
await db.copy(destinationFile.path); | ||
} | ||
} |
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,15 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:mdigit_span_tasks/src/services/services/export_dbs.dart'; | ||
|
||
/// [ExportButton] Is used by the user to export the data from the app | ||
class ExportButton extends StatelessWidget { | ||
const ExportButton({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return IconButton( | ||
icon: const Icon(Icons.send_to_mobile), | ||
onPressed: () async => await exportDBs(), | ||
); | ||
} | ||
} |
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,16 @@ | ||
import 'package:mdigit_span_tasks/src/export_data/controller/data_exporter.dart'; | ||
|
||
/// Export dbs to make them accessible to users | ||
Future<void> exportDBs() async { | ||
await _exportDB(dbName: 'ds_forward.sqlite3'); | ||
await _exportDB(dbName: 'ds_backwards.sqlite3'); | ||
} | ||
|
||
/// Export the db specified in [dbName] | ||
Future<void> _exportDB({required String dbName}) async { | ||
DataExporter dataExporter = DataExporter(dbName: dbName); | ||
await dataExporter.requestPermission(); | ||
await dataExporter.getDB(); | ||
await dataExporter.initDestinationFile(); | ||
await dataExporter.copyDB(); | ||
} |
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