summaryrefslogtreecommitdiff
path: root/lib/services
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-02-08 20:08:23 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2023-02-08 20:08:23 +0100
commitfcbf592d10199dbac80198dd8c2efb181f95165e (patch)
tree4fd50f1bf1c65c7180f1f3b7d64fc1603bf9be73 /lib/services
parente37d74527e03b6a804a35cbfb0956d4e0100889f (diff)
blacklist
Diffstat (limited to 'lib/services')
-rw-r--r--lib/services/iblacklist_provider_service.dart31
-rw-r--r--lib/services/local_blacklist_provider_service.dart57
-rw-r--r--lib/services/storegear_api_service.dart2
3 files changed, 88 insertions, 2 deletions
diff --git a/lib/services/iblacklist_provider_service.dart b/lib/services/iblacklist_provider_service.dart
new file mode 100644
index 0000000..c66a909
--- /dev/null
+++ b/lib/services/iblacklist_provider_service.dart
@@ -0,0 +1,31 @@
+import 'dart:async';
+
+class BlacklistEntry {
+ String postalcodeNumeric;
+ String postalcodeAplha;
+ int houseNumber;
+ String houseNumberExtra;
+
+ BlacklistEntry(this.postalcodeNumeric, this.postalcodeAplha, this.houseNumber,
+ this.houseNumberExtra);
+
+ BlacklistEntry.fromJson(Map<String, dynamic> json)
+ : postalcodeNumeric = json['postalcodeNumeric'],
+ postalcodeAplha = json['postalcodeAplha'],
+ houseNumber = json['houseNumber'],
+ houseNumberExtra = json['houseNumberExtra'];
+
+ Map<String, dynamic> toJson() {
+ return {
+ 'postalcodeNumeric': postalcodeNumeric,
+ 'postalcodeAplha': postalcodeAplha,
+ 'houseNumber': houseNumber,
+ 'houseNumberExtra': houseNumberExtra,
+ };
+ }
+}
+
+abstract class IBlacklistProviderService {
+ Future<List<BlacklistEntry>> getBlacklist();
+ Future<void> addToBlacklist(BlacklistEntry data);
+}
diff --git a/lib/services/local_blacklist_provider_service.dart b/lib/services/local_blacklist_provider_service.dart
new file mode 100644
index 0000000..cb3840e
--- /dev/null
+++ b/lib/services/local_blacklist_provider_service.dart
@@ -0,0 +1,57 @@
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:path_provider/path_provider.dart';
+import 'package:training_planner/pages/logbook_page.dart';
+import 'package:training_planner/services/iblacklist_provider_service.dart';
+import 'package:training_planner/services/log_service.dart';
+
+class LocalBlacklistProviderService extends IBlacklistProviderService {
+ Future<Directory> get _localDir async {
+ final directory = await getApplicationDocumentsDirectory();
+ return directory;
+ }
+
+ Future<String> get _localPath async {
+ final directory = await getApplicationDocumentsDirectory();
+ return directory.path;
+ }
+
+ Future<File> _localFile() async {
+ final path = await _localPath;
+ String fullPath = '$path/blacklist.json';
+ File file = File(fullPath);
+
+ bool exists = await file.exists();
+ if (!exists) {
+ LogService.log('creating ' + fullPath);
+ await file.create();
+ await file.writeAsString(jsonEncode([]));
+ }
+
+ return File(fullPath);
+ }
+
+ @override
+ Future<List<BlacklistEntry>> getBlacklist() async {
+ var file = await _localFile();
+ var data = await file.readAsString();
+ final Iterable iterable = await jsonDecode(data);
+ List<BlacklistEntry> parsedData = List<BlacklistEntry>.from(
+ iterable.map((model) => BlacklistEntry.fromJson(model)));
+ LogService.log('read ' + data);
+ return parsedData;
+ }
+
+ @override
+ Future<void> addToBlacklist(BlacklistEntry data) async {
+ var file = await _localFile();
+
+ List<BlacklistEntry> dataToStore = await getBlacklist();
+ dataToStore.add(BlacklistEntry(data.postalcodeNumeric, data.postalcodeAplha,
+ data.houseNumber, data.houseNumberExtra));
+
+ LogService.log('writing ' + jsonEncode(dataToStore));
+ file.writeAsString(jsonEncode(dataToStore));
+ }
+}
diff --git a/lib/services/storegear_api_service.dart b/lib/services/storegear_api_service.dart
index f4d96b1..7b3657f 100644
--- a/lib/services/storegear_api_service.dart
+++ b/lib/services/storegear_api_service.dart
@@ -56,8 +56,6 @@ class StoregearApiService extends IStoregearApiService {
// If the server did return a 200 OK response,
// then parse the JSON.
-
-
var content = jsonDecode(response.body);
if (content["message"] != null) {
return RouteList(routes: []);