diff options
| -rw-r--r-- | lib/main.dart | 2 | ||||
| -rw-r--r-- | lib/pages/developer_page.dart | 25 | ||||
| -rw-r--r-- | lib/services/backup_helper_service.dart | 30 | ||||
| -rw-r--r-- | lib/services/storegear_api_service.dart | 5 |
4 files changed, 42 insertions, 20 deletions
diff --git a/lib/main.dart b/lib/main.dart index 909b5f1..e967179 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:training_planner/services/backup_helper_service.dart'; import 'package:training_planner/services/iblacklist_provider_service.dart'; import 'package:training_planner/services/iroute_provider_service.dart'; import 'package:training_planner/services/ishift_provider_service.dart'; @@ -66,6 +67,7 @@ final IStoregearApiService apiService = StoregearApiService(); final LocalSalaryProviderService incomeProvider = LocalSalaryProviderService(); final IBlacklistProviderService blacklistProvider = LocalBlacklistProviderService(); +final BackupHelperService backupService = BackupHelperService(); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); diff --git a/lib/pages/developer_page.dart b/lib/pages/developer_page.dart index 2b1b54c..a75ca92 100644 --- a/lib/pages/developer_page.dart +++ b/lib/pages/developer_page.dart @@ -73,24 +73,6 @@ class _DeveloperPageState extends State<DeveloperPage> { }); } - Future<String?> getDownloadPath() async { - Directory? directory; - try { - if (Platform.isIOS) { - directory = await getApplicationDocumentsDirectory(); - } else { - directory = Directory('/storage/emulated/0/Download'); - // Put file in global download folder, if for an unknown reason it didn't exist, we fallback - // ignore: avoid_slow_async_io - if (!await directory.exists()) - directory = await getExternalStorageDirectory(); - } - } catch (err, stack) { - print("Cannot get download folder path"); - } - return directory?.path; - } - File _createZipFile(String fileName) { final zipFilePath = fileName; final zipFile = File(zipFilePath); @@ -113,9 +95,9 @@ class _DeveloperPageState extends State<DeveloperPage> { } if (await Permission.storage.request().isGranted) { - String? path = await getDownloadPath(); + String? path = await backupService.getDownloadPath(); if (path != null) { - path += '/backup.zip'; + path += 'backup.zip'; var zip = _createZipFile(path); int onProgressCallCount1 = 0; @@ -134,6 +116,9 @@ class _DeveloperPageState extends State<DeveloperPage> { return ZipFileOperation.includeItem; }, ); + + ScaffoldMessenger.of(context) + .showSnackBar(const SnackBar(content: Text('Backup created'))); } on PlatformException catch (e) { print(e); } diff --git a/lib/services/backup_helper_service.dart b/lib/services/backup_helper_service.dart new file mode 100644 index 0000000..7a4b7f0 --- /dev/null +++ b/lib/services/backup_helper_service.dart @@ -0,0 +1,30 @@ +import 'dart:io'; + +import 'package:path_provider/path_provider.dart'; + +class BackupHelperService { + Future<String?> getDownloadPath() async { + Directory? directory; + try { + if (Platform.isIOS) { + directory = await getApplicationDocumentsDirectory(); + } else { + directory = Directory('/storage/emulated/0/Download/'); + if (!await directory.exists()) + directory = await getExternalStorageDirectory(); + } + } catch (err, stack) { + print("Cannot get download folder path"); + } + return directory?.path; + } + + Future<void> writeStringToFile(String content, String filename) async { + String? fullpath = await getDownloadPath(); + if (fullpath != null) { + fullpath += filename; + File file = File(fullpath); + file.writeAsString(content); + } + } +} diff --git a/lib/services/storegear_api_service.dart b/lib/services/storegear_api_service.dart index 3063b26..0c2cbbc 100644 --- a/lib/services/storegear_api_service.dart +++ b/lib/services/storegear_api_service.dart @@ -2,10 +2,12 @@ import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; import 'package:training_planner/config/defaults.dart'; +import 'package:training_planner/main.dart'; import 'package:training_planner/models/login_request.dart'; import 'package:training_planner/models/login_response.dart'; import 'package:training_planner/models/route_list.dart'; import 'package:training_planner/route.dart'; +import 'package:training_planner/services/backup_helper_service.dart'; import 'package:training_planner/services/istoregear_api_service.dart'; import 'package:training_planner/route.dart' as DHLRoute; import 'package:training_planner/services/mock_route_provider_service.dart'; @@ -33,6 +35,7 @@ class StoregearApiService extends IStoregearApiService { return res; } else { print(response.body); + backupService.writeStringToFile(response.body, 'failed_login.txt'); // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed login'); @@ -65,6 +68,7 @@ class StoregearApiService extends IStoregearApiService { } return RouteList.fromJson(content); } else { + backupService.writeStringToFile(response.body, 'failed_routelist.txt'); // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to load routes'); @@ -92,6 +96,7 @@ class StoregearApiService extends IStoregearApiService { } return RouteInfo.fromJson(content).route; } else { + backupService.writeStringToFile(response.body, 'failed_route.txt'); // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to load route'); |
