Skip to content

Commit

Permalink
fix: notification tap handling during refresh
Browse files Browse the repository at this point in the history
- route navigation after restoring or deleting db
- db delete confirmation
  • Loading branch information
lucafluri committed Jul 16, 2020
1 parent 415d13a commit a7b4f79
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
10 changes: 4 additions & 6 deletions lib/screens/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ class SettingsScreenView extends WidgetView<SettingsScreen, Settings> {
title: 'Backup',
subtitle: 'Save Backup File',
leading: Icon(Icons.backup),
onTap: () async {
await BackupService.instance.backup();
state.showToast("Backup file saved");
onTap: () {
state.backup();
},
),
SettingsTile(
title: 'Restore',
subtitle: 'Restore from Backup File',
leading: Icon(Icons.cloud_download),
onTap: () async {
await BackupService.instance.restore();
state.showToast("Products added to database");
onTap: () {
state.restore();
},
),
],
Expand Down
38 changes: 35 additions & 3 deletions lib/screens/settings/settings_controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/widgets.dart';
import 'package:price_tracker/models/product.dart';
import 'package:price_tracker/screens/settings/settings.dart';
import 'package:price_tracker/services/backup.dart';
import 'package:price_tracker/services/database.dart';
import 'package:price_tracker/services/init.dart';
import 'package:price_tracker/services/product_utils.dart';
import 'package:toast/toast.dart';
import 'package:workmanager/workmanager.dart';
Expand All @@ -27,9 +30,38 @@ class Settings extends State<SettingsScreen> {
Toast.show(text, context, duration: sec);

clearDB() async {
DatabaseService _db = await DatabaseService.getInstance();
await _db.deleteAll();
showToast("Database cleared!");
OkCancelResult result = await showOkCancelAlertDialog(
context: context,
title: "Do you really want to empty the database?",
message: "All tracked products will be lost without a backup",
okLabel: "Clear DB",
barrierDismissible: false,
isDestructiveAction: true,
);
if (result == OkCancelResult.ok) {
DatabaseService _db = await DatabaseService.getInstance();
if (await _db.deleteAll() > 0) {
showToast("Database cleared!");
navigatorKey.currentState
.pushNamedAndRemoveUntil("/", (route) => false);
} else
showToast("Database already empty");
}
}

backup() async {
if (await BackupService.instance.backup())
showToast("Backup file saved");
else
showToast("Error saving backup file");
}

restore() async {
if (await BackupService.instance.restore()) {
showToast("Products loaded from backup");
navigatorKey.currentState.pushNamedAndRemoveUntil("/", (route) => false);
} else
showToast("Error reading backup file");
}

void testPriceFallNotification() {
Expand Down
17 changes: 9 additions & 8 deletions lib/services/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ class BackupService {
}
}

backup() async {
Future<bool> backup() async {
String json = await _buildJSON();
final file = await _localFile;
if (json == null) return;
if (json == null || file == null) return false;
file.writeAsString(json);

final params = SaveFileDialogParams(sourceFilePath: file.path);
final filePath = await FlutterFileDialog.saveFile(params: params);
print(filePath);
return filePath != null;
}

restore() async {
Future<bool> restore() async {
DatabaseService _db = await DatabaseService.getInstance();

final params = OpenFileDialogParams(
Expand All @@ -72,13 +72,13 @@ class BackupService {
final filePath = await FlutterFileDialog.pickFile(params: params);
if (filePath == null) {
debugPrint("Filepath Error");
return;
return false;
}
File file = File(filePath);

if (file == null) {
debugPrint("ERROR Getting File");
return;
return false;
}

String string;
Expand All @@ -87,18 +87,19 @@ class BackupService {
string = await file.readAsString();
} catch (e) {
debugPrint("Can't read file as string!");
return;
return false;
}

List<Product> products = _buildProducts(string);

if (products == null) {
debugPrint("Error building Products");
return;
return false;
}

for (Product p in products) {
await _db.insert(p);
}
return true;
}
}
2 changes: 1 addition & 1 deletion lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ class DatabaseService {

// Clears db
Future<int> deleteAll() async {
return await _database.delete(table);
return await _database.delete(table, where: '1');
}
}
2 changes: 1 addition & 1 deletion lib/services/product_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Future<void> notificationTapCallback(String payload) async {
// Clear payload variable
NotificationService.currentPayload = null;

if (product != null)
if (!refreshing && product != null)
navigatorKey.currentState.push(MaterialPageRoute(
builder: (context) => ProductDetail(
product: product,
Expand Down

0 comments on commit a7b4f79

Please sign in to comment.