From fcbf592d10199dbac80198dd8c2efb181f95165e Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Wed, 8 Feb 2023 20:08:23 +0100 Subject: blacklist --- lib/services/iblacklist_provider_service.dart | 31 ++++++++++++ lib/services/local_blacklist_provider_service.dart | 57 ++++++++++++++++++++++ lib/services/storegear_api_service.dart | 2 - 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 lib/services/iblacklist_provider_service.dart create mode 100644 lib/services/local_blacklist_provider_service.dart (limited to 'lib/services') 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 json) + : postalcodeNumeric = json['postalcodeNumeric'], + postalcodeAplha = json['postalcodeAplha'], + houseNumber = json['houseNumber'], + houseNumberExtra = json['houseNumberExtra']; + + Map toJson() { + return { + 'postalcodeNumeric': postalcodeNumeric, + 'postalcodeAplha': postalcodeAplha, + 'houseNumber': houseNumber, + 'houseNumberExtra': houseNumberExtra, + }; + } +} + +abstract class IBlacklistProviderService { + Future> getBlacklist(); + Future 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 get _localDir async { + final directory = await getApplicationDocumentsDirectory(); + return directory; + } + + Future get _localPath async { + final directory = await getApplicationDocumentsDirectory(); + return directory.path; + } + + Future _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> getBlacklist() async { + var file = await _localFile(); + var data = await file.readAsString(); + final Iterable iterable = await jsonDecode(data); + List parsedData = List.from( + iterable.map((model) => BlacklistEntry.fromJson(model))); + LogService.log('read ' + data); + return parsedData; + } + + @override + Future addToBlacklist(BlacklistEntry data) async { + var file = await _localFile(); + + List 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: []); -- cgit v1.2.3-70-g09d2