From 03bd15045beee64dab5795053b662d02f7049e31 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 13 Nov 2022 23:07:11 +0100 Subject: v0.8, make it comfy, add salary log --- lib/services/local_salary_provider_service.dart | 72 +++++++++++++++++++++++++ lib/services/storegear_api_service.dart | 2 + 2 files changed, 74 insertions(+) create mode 100644 lib/services/local_salary_provider_service.dart (limited to 'lib/services') diff --git a/lib/services/local_salary_provider_service.dart b/lib/services/local_salary_provider_service.dart new file mode 100644 index 0000000..5688ad3 --- /dev/null +++ b/lib/services/local_salary_provider_service.dart @@ -0,0 +1,72 @@ +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/log_service.dart'; + +class IncomeData { + final DateTime firstDayOfMonth; + final double income; + + IncomeData(this.firstDayOfMonth, this.income); + + IncomeData.fromJson(Map json) + : firstDayOfMonth = DateTime.parse(json['firstDayOfMonth']), + income = double.parse(json['income']); + + Map toJson() { + return { + 'firstDayOfMonth': firstDayOfMonth.toIso8601String(), + 'income': income.toStringAsFixed(2), + }; + } +} + +class LocalSalaryProviderService { + 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/income.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); + } + + Future> getSavedIncome() async { + var file = await _localFile(); + var data = await file.readAsString(); + final Iterable iterable = await jsonDecode(data); + List parsedData = List.from( + iterable.map((model) => IncomeData.fromJson(model))); + LogService.log('read ' + data); + return parsedData; + } + + Future writeSavedIncome(List data) async { + var file = await _localFile(); + + List dataToStore = []; + for (var item in data) { + dataToStore.add(IncomeData(item.firstDayOfMonth, item.actualSalary)); + } + 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 b26f77b..b40626d 100644 --- a/lib/services/storegear_api_service.dart +++ b/lib/services/storegear_api_service.dart @@ -14,6 +14,8 @@ class StoregearApiService extends IStoregearApiService { @override Future login(LoginRequest req) async { + //return LoginResponse(); + final response = await http.post( Uri.parse('http://dhlapis.com/delivery/v1/users/login?env_type=PROD'), body: jsonEncode(req)); -- cgit v1.2.3-70-g09d2