From 5d461d5b0f0e016988d7eace8902e161a236480a Mon Sep 17 00:00:00 2001 From: octagonal Date: Sat, 5 Jul 2025 10:24:55 +0200 Subject: [PATCH] Adds requests for total flight time and flight in between timestamps --- lib/config.dart | 27 +++++++++++++++++++++++++-- lib/db/db_helper.dart | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/config.dart b/lib/config.dart index 4a25717..f7f714b 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -1,6 +1,12 @@ // This file holds a class to represent the configuration/settings of the app +import 'dart:io'; + import 'package:json_annotation/json_annotation.dart'; +import 'dart:convert'; +import 'package:path_provider/path_provider.dart'; +import 'package:path/path.dart' as path; + part 'generated/config.g.dart'; @JsonSerializable() @@ -10,7 +16,24 @@ class RTimeConfig { RTimeConfig({required this.language}); factory RTimeConfig.fromJson(Map json) => - _$PersonFromJson(json); + _$RTimeConfigFromJson(json); - Map toJson() => _$PersonToJson(this); + Map toJson() => _$RTimeConfigToJson(this); + + static Future readFromConfig() async { + final directory = await getApplicationDocumentsDirectory(); + final configName = "rtime_config.json"; + final configPath = path.join(directory.path, configName); + + final string = jsonDecode(await File(configPath).readAsString()); + return RTimeConfig.fromJson(string); + } + + void writeConfig() async { + final directory = await getApplicationDocumentsDirectory(); + final configName = "rtime_config.json"; + final configPath = path.join(directory.path, configName); + + await File(configPath).writeAsString(jsonEncode(toJson())); + } } diff --git a/lib/db/db_helper.dart b/lib/db/db_helper.dart index 3ca7aac..f6237fc 100644 --- a/lib/db/db_helper.dart +++ b/lib/db/db_helper.dart @@ -160,6 +160,34 @@ class DbHelper { return maps.map((e) => Flight.fromMap(e)).toList(); } + Future> getFlightsBetween( + int startTimestamp, + int endTimestamp, + ) async { + final db = await database; + final maps = await db.rawQuery(''' + SELECT * FROM flights AS F + WHERE F.start_timestamp >= $startTimestamp AND F.end_timestamp <= $endTimestamp + '''); + return maps.map((e) => Flight.fromMap(e)).toList(); + } + + Future getTotalFlightTime() async { + final db = await database; + final ft = await db.rawQuery(''' + SELECT SUM(F.end_timestamp - F.startTimestamp) FROM flights AS F + '''); + final mapping = ft.firstOrNull; + if (mapping == null) { + return null; + } + final val = mapping["SUM(F.end_timestamp - F.startTimestamp)"]; + if (val is! int) { + return null; + } + return val; + } + Future closeDb() async { final db = await database; log.info("Closing database.");