Adds requests for total flight time and flight in between timestamps
This commit is contained in:
@ -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<String, dynamic> json) =>
|
||||
_$PersonFromJson(json);
|
||||
_$RTimeConfigFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$PersonToJson(this);
|
||||
Map<String, dynamic> toJson() => _$RTimeConfigToJson(this);
|
||||
|
||||
static Future<RTimeConfig?> 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()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,6 +160,34 @@ class DbHelper {
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<List<Flight>> 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<int?> 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.");
|
||||
|
||||
Reference in New Issue
Block a user