diff --git a/build.yaml b/build.yaml new file mode 100644 index 0000000..598007c --- /dev/null +++ b/build.yaml @@ -0,0 +1,7 @@ +targets: + $default: + builders: + source_gen|combining_builder: + options: + build_extensions: + '^lib/{{}}.dart': 'lib/generated/{{}}.g.dart' diff --git a/lib/config.dart b/lib/config.dart new file mode 100644 index 0000000..4a25717 --- /dev/null +++ b/lib/config.dart @@ -0,0 +1,16 @@ +// This file holds a class to represent the configuration/settings of the app + +import 'package:json_annotation/json_annotation.dart'; +part 'generated/config.g.dart'; + +@JsonSerializable() +class RTimeConfig { + final String language; + + RTimeConfig({required this.language}); + + factory RTimeConfig.fromJson(Map json) => + _$PersonFromJson(json); + + Map toJson() => _$PersonToJson(this); +} diff --git a/lib/db/db_helper.dart b/lib/db/db_helper.dart index c8a3224..3ca7aac 100644 --- a/lib/db/db_helper.dart +++ b/lib/db/db_helper.dart @@ -7,46 +7,37 @@ import 'package:rtime/models/battery.dart'; import 'package:rtime/models/flight.dart'; import '../models/drone.dart'; - import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; -class DbHelper -{ - static final DbHelper instance = DbHelper._internal(); - static final log = Logger("DbHelper"); - static Database? _database; +class DbHelper { + static final DbHelper instance = DbHelper._internal(); + static final log = Logger("DbHelper"); + static Database? _database; - DbHelper._internal(); + DbHelper._internal(); - Future get database async - { - if (_database != null) return _database!; - await _initDb(); - return _database!; - } + Future get database async { + if (_database != null) return _database!; + await _initDb(); + return _database!; + } - Future _initDb() async - { - final directory = await getApplicationDocumentsDirectory(); - final db_name = "rtime.db"; - final path = join(directory.path, db_name); - log.info("Opeing database file at $path"); + Future _initDb() async { + final directory = await getApplicationDocumentsDirectory(); + final db_name = "rtime.db"; + final path = join(directory.path, db_name); + log.info("Opeing database file at $path"); - _database = await openDatabase( - path, - version: 1, - onCreate: _onCreate, - ); + _database = await openDatabase(path, version: 1, onCreate: _onCreate); - log.info("Database opened successfully !"); - log.info("${(await getDrones()).length} drones in DB."); - } + log.info("Database opened successfully !"); + log.info("${(await getDrones()).length} drones in DB."); + } - Future _onCreate(Database db, int version) async - { - log.info("Database does not exist, creating a new one."); - await db.execute(''' + Future _onCreate(Database db, int version) async { + log.info("Database does not exist, creating a new one."); + await db.execute(''' CREATE TABLE drones( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, @@ -54,7 +45,7 @@ class DbHelper ) '''); - await db.execute(''' + await db.execute(''' CREATE TABLE batteries( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, @@ -64,7 +55,7 @@ class DbHelper ) '''); - await db.execute(''' + await db.execute(''' CREATE TABLE flights( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, @@ -78,78 +69,101 @@ class DbHelper FOREIGN KEY(battery_id) REFERENCES batteries(id) ) '''); - } + } - // Drone related helpers - Future insertDrone(Drone drone) async - { - final db = await database; - drone.id = await db.insert("drones", drone.toMap()); - return drone; - } + // Drone related helpers + Future insertDrone(Drone drone) async { + final db = await database; + drone.id = await db.insert("drones", drone.toMap()); + return drone; + } - Future> getDrones() async - { - final db = await database; - final maps = await db.query("drones"); - return maps.map((e) => Drone.fromMap(e)).toList(); - } + Future> getDrones() async { + final db = await database; + final maps = await db.query("drones"); + return maps.map((e) => Drone.fromMap(e)).toList(); + } - Future deleteDrone(int droneId) async - { - // TODO: Delete image - final db = await database; - return await db.delete("drones", where: "id = ?", whereArgs: [droneId]); - } + Future deleteDrone(int droneId) async { + // TODO: Delete image + final db = await database; + return await db.delete("drones", where: "id = ?", whereArgs: [droneId]); + } - // Battery related helpers - Future insertBattery(Battery battery) async - { - final db = await database; - battery.id = await db.insert("batteries", battery.toMap()); - return battery; - } + // Battery related helpers + Future insertBattery(Battery battery) async { + final db = await database; + battery.id = await db.insert("batteries", battery.toMap()); + return battery; + } - Future> getBatteries() async - { - final db = await database; - final maps = await db.query("batteries"); - return maps.map((e) => Battery.fromMap(e)).toList(); - } + Future> getBatteries() async { + final db = await database; + final maps = await db.query("batteries"); + return maps.map((e) => Battery.fromMap(e)).toList(); + } - Future deleteBattery(int batteryId) async - { - // TODO: Delete image - final db = await database; - return await db.delete("batteries", where: "id = ?", whereArgs: [batteryId]); - } + Future deleteBattery(int batteryId) async { + // TODO: Delete image + final db = await database; + return await db.delete( + "batteries", + where: "id = ?", + whereArgs: [batteryId], + ); + } - // Flight related helpers - Future insertFlight(Flight flight) async - { - final db = await database; - flight.id = await db.insert("flights", flight.toMap()); - return flight; - } + Future> getBatteriesByType(String type) async { + final db = await database; + final maps = await db.query( + "batteries", + where: "type = ?", + whereArgs: [type], + ); + return maps.map((e) => Battery.fromMap(e)).toList(); + } - Future> getFlights() async - { - final db = await database; - final maps = await db.query("flights"); - return maps.map((e) => Flight.fromMap(e)).toList(); - } + // Flight related helpers + Future insertFlight(Flight flight) async { + final db = await database; + flight.id = await db.insert("flights", flight.toMap()); + return flight; + } - Future deleteFlight(int flightId) async - { - final db = await database; - return await db.delete("flights", where: "id = ?", whereArgs: [flightId]); - } + Future> getFlights() async { + final db = await database; + final maps = await db.query("flights"); + return maps.map((e) => Flight.fromMap(e)).toList(); + } - Future closeDb() async - { - final db = await database; - log.info("Closing database."); - db.close(); - _database = null; - } + Future deleteFlight(int flightId) async { + final db = await database; + return await db.delete("flights", where: "id = ?", whereArgs: [flightId]); + } + + // Complex requests + Future> getDroneFlights(int droneId) async { + final db = await database; + final maps = await db.rawQuery(''' + SELECT * FROM flights AS F + JOIN drones AS D ON F.drone_id = D.id + '''); + return maps.map((e) => Flight.fromMap(e)).toList(); + } + + Future> getBatteryFlights(int batteryId) async { + final db = await database; + final maps = await db.rawQuery(''' + SELECT * FROM flights AS F + JOIN batteries AS B ON F.battery_id = B.id + '''); + return maps.map((e) => Flight.fromMap(e)).toList(); + } + + Future closeDb() async { + final db = await database; + log.info("Closing database."); + db.close(); + _database = null; + } } diff --git a/lib/generated/config.g.dart b/lib/generated/config.g.dart new file mode 100644 index 0000000..38436f0 --- /dev/null +++ b/lib/generated/config.g.dart @@ -0,0 +1,13 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of '../config.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +RTimeConfig _$RTimeConfigFromJson(Map json) => + RTimeConfig(language: json['language'] as String); + +Map _$RTimeConfigToJson(RTimeConfig instance) => + {'language': instance.language}; diff --git a/lib/main.dart b/lib/main.dart index c70bee2..0cebe54 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,19 +11,19 @@ import 'package:image_picker/image_picker.dart'; import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart'; void main() { - if(Platform.isWindows || Platform.isLinux) - { - sqfliteFfiInit(); - databaseFactory = databaseFactoryFfi; - } + if (Platform.isWindows || Platform.isLinux) { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + } - Logger.root.level = Level.ALL; - Logger.root.onRecord.listen((record) => - print('${record.level.name}: ${record.time}: ${record.message}') - ); + Logger.root.level = Level.ALL; + Logger.root.onRecord.listen( + (record) => + print('${record.level.name}: ${record.time}: ${record.message}'), + ); runApp(const MyApp()); - //DbHelper.instance.closeDb(); + //DbHelper.instance.closeDb(); } class MyApp extends StatelessWidget { @@ -79,14 +79,12 @@ class _MyHomePageState extends State { int _counter = 0; Future _incrementCounter() async { - - DbHelper.instance.insertDrone(Drone - ( - name: "Image test", - imageUuid: await ImagesManager.instance.createImage(ImageSource.camera) - )); - - + DbHelper.instance.insertDrone( + Drone( + name: "Image test", + imageUuid: await ImagesManager.instance.createImage(ImageSource.camera), + ), + ); setState(() { // This call to setState tells the Flutter framework that something has @@ -106,42 +104,36 @@ class _MyHomePageState extends State { // The Flutter framework has been optimized to make rerunning build methods // fast, so that you can just rebuild anything that needs updating rather // than having to individually change instances of widgets. - var children = - [ - const Text('You have pushed the button this many times:'), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - FutureBuilder( - future: Future(() async - { - final drones = await DbHelper.instance.getDrones(); + var children = [ + const Text('You have pushed the button this many times:'), + Text('$_counter', style: Theme.of(context).textTheme.headlineMedium), + FutureBuilder( + future: Future(() async { + final drones = await DbHelper.instance.getDrones(); - if(drones.isEmpty) - { - return Icon(Icons.question_mark); - } + if (drones.isEmpty) { + return Icon(Icons.question_mark); + } - final image = await ImagesManager.instance.loadImage(drones.first.imageUuid!); - if(image == null) - { - return Icon(Icons.error); - } + final image = await ImagesManager.instance.loadImage( + drones.first.imageUuid!, + ); + if (image == null) { + return Icon(Icons.error); + } - return image; - }), - builder: (BuildContext ctx, AsyncSnapshot img) - { - if(!img.hasData) - { - return Center(child: CircularProgressIndicator(),); - } - - return img.data!; - } - ), - Icon(Icons.build)]; + return image; + }), + builder: (BuildContext ctx, AsyncSnapshot img) { + if (!img.hasData) { + return Center(child: CircularProgressIndicator()); + } + + return img.data!; + }, + ), + Icon(Icons.build), + ]; return Scaffold( appBar: AppBar( @@ -172,7 +164,7 @@ class _MyHomePageState extends State { // wireframe for each widget. mainAxisAlignment: MainAxisAlignment.center, children: children, - ), + ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, @@ -182,4 +174,3 @@ class _MyHomePageState extends State { ); } } - diff --git a/pubspec.lock b/pubspec.lock index 8ab4f87..74272e3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,22 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + sha256: da0d9209ca76bde579f2da330aeb9df62b6319c834fa7baae052021b0462401f + url: "https://pub.dev" + source: hosted + version: "85.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + sha256: "01949bf52ad33f0e0f74f881fbaac4f348c556531951d92c8d16f1262aa19ff8" + url: "https://pub.dev" + source: hosted + version: "7.5.4" archive: dependency: transitive description: @@ -9,6 +25,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.7" + args: + dependency: transitive + description: + name: args + sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04 + url: "https://pub.dev" + source: hosted + version: "2.7.0" async: dependency: transitive description: @@ -25,6 +49,70 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + build: + dependency: transitive + description: + name: build + sha256: "51dc711996cbf609b90cbe5b335bbce83143875a9d58e4b5c6d3c4f684d3dda7" + url: "https://pub.dev" + source: hosted + version: "2.5.4" + build_config: + dependency: transitive + description: + name: build_config + sha256: "4ae2de3e1e67ea270081eaee972e1bd8f027d459f249e0f1186730784c2e7e33" + url: "https://pub.dev" + source: hosted + version: "1.1.2" + build_daemon: + dependency: transitive + description: + name: build_daemon + sha256: "8e928697a82be082206edb0b9c99c5a4ad6bc31c9e9b8b2f291ae65cd4a25daa" + url: "https://pub.dev" + source: hosted + version: "4.0.4" + build_resolvers: + dependency: transitive + description: + name: build_resolvers + sha256: ee4257b3f20c0c90e72ed2b57ad637f694ccba48839a821e87db762548c22a62 + url: "https://pub.dev" + source: hosted + version: "2.5.4" + build_runner: + dependency: transitive + description: + name: build_runner + sha256: "382a4d649addbfb7ba71a3631df0ec6a45d5ab9b098638144faf27f02778eb53" + url: "https://pub.dev" + source: hosted + version: "2.5.4" + build_runner_core: + dependency: transitive + description: + name: build_runner_core + sha256: "85fbbb1036d576d966332a3f5ce83f2ce66a40bea1a94ad2d5fc29a19a0d3792" + url: "https://pub.dev" + source: hosted + version: "9.1.2" + built_collection: + dependency: transitive + description: + name: built_collection + sha256: "376e3dd27b51ea877c28d525560790aee2e6fbb5f20e2f85d5081027d94e2100" + url: "https://pub.dev" + source: hosted + version: "5.1.1" + built_value: + dependency: transitive + description: + name: built_value + sha256: "082001b5c3dc495d4a42f1d5789990505df20d8547d42507c29050af6933ee27" + url: "https://pub.dev" + source: hosted + version: "8.10.1" characters: dependency: transitive description: @@ -33,6 +121,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.4.0" + checked_yaml: + dependency: transitive + description: + name: checked_yaml + sha256: "959525d3162f249993882720d52b7e0c833978df229be20702b33d48d91de70f" + url: "https://pub.dev" + source: hosted + version: "2.0.4" clock: dependency: transitive description: @@ -41,6 +137,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.2" + code_builder: + dependency: transitive + description: + name: code_builder + sha256: "0ec10bf4a89e4c613960bf1e8b42c64127021740fb21640c29c909826a5eea3e" + url: "https://pub.dev" + source: hosted + version: "4.10.1" collection: dependency: transitive description: @@ -49,6 +153,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.19.1" + convert: + dependency: transitive + description: + name: convert + sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68 + url: "https://pub.dev" + source: hosted + version: "3.1.2" cross_file: dependency: transitive description: @@ -73,6 +185,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.8" + dart_style: + dependency: transitive + description: + name: dart_style + sha256: "5b236382b47ee411741447c1f1e111459c941ea1b3f2b540dde54c210a3662af" + url: "https://pub.dev" + source: hosted + version: "3.1.0" fake_async: dependency: transitive description: @@ -89,6 +209,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + file: + dependency: transitive + description: + name: file + sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4 + url: "https://pub.dev" + source: hosted + version: "7.0.1" file_selector_linux: dependency: transitive description: @@ -160,6 +288,30 @@ packages: description: flutter source: sdk version: "0.0.0" + frontend_server_client: + dependency: transitive + description: + name: frontend_server_client + sha256: f64a0333a82f30b0cca061bc3d143813a486dc086b574bfb233b7c1372427694 + url: "https://pub.dev" + source: hosted + version: "4.0.0" + glob: + dependency: transitive + description: + name: glob + sha256: c3f1ee72c96f8f78935e18aa8cecced9ab132419e8625dc187e1c2408efc20de + url: "https://pub.dev" + source: hosted + version: "2.1.3" + graphs: + dependency: transitive + description: + name: graphs + sha256: "741bbf84165310a68ff28fe9e727332eef1407342fca52759cb21ad8177bb8d0" + url: "https://pub.dev" + source: hosted + version: "2.3.2" http: dependency: transitive description: @@ -168,6 +320,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.4.0" + http_multi_server: + dependency: transitive + description: + name: http_multi_server + sha256: aa6199f908078bb1c5efb8d8638d4ae191aac11b311132c3ef48ce352fb52ef8 + url: "https://pub.dev" + source: hosted + version: "3.2.2" http_parser: dependency: transitive description: @@ -248,6 +408,38 @@ packages: url: "https://pub.dev" source: hosted version: "0.2.1+1" + io: + dependency: transitive + description: + name: io + sha256: dfd5a80599cf0165756e3181807ed3e77daf6dd4137caaad72d0b7931597650b + url: "https://pub.dev" + source: hosted + version: "1.0.5" + js: + dependency: transitive + description: + name: js + sha256: "53385261521cc4a0c4658fd0ad07a7d14591cf8fc33abbceae306ddb974888dc" + url: "https://pub.dev" + source: hosted + version: "0.7.2" + json_annotation: + dependency: transitive + description: + name: json_annotation + sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1" + url: "https://pub.dev" + source: hosted + version: "4.9.0" + json_serializable: + dependency: "direct main" + description: + name: json_serializable + sha256: c50ef5fc083d5b5e12eef489503ba3bf5ccc899e487d691584699b4bdefeea8c + url: "https://pub.dev" + source: hosted + version: "6.9.5" leak_tracker: dependency: transitive description: @@ -320,6 +512,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + package_config: + dependency: transitive + description: + name: package_config + sha256: f096c55ebb7deb7e384101542bfba8c52696c1b56fca2eb62827989ef2353bbc + url: "https://pub.dev" + source: hosted + version: "2.2.0" path: dependency: "direct main" description: @@ -400,6 +600,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + pool: + dependency: transitive + description: + name: pool + sha256: "20fe868b6314b322ea036ba325e6fc0711a22948856475e2c2b6306e8ab39c2a" + url: "https://pub.dev" + source: hosted + version: "1.5.1" posix: dependency: transitive description: @@ -408,11 +616,59 @@ packages: url: "https://pub.dev" source: hosted version: "6.0.3" + pub_semver: + dependency: transitive + description: + name: pub_semver + sha256: "5bfcf68ca79ef689f8990d1160781b4bad40a3bd5e5218ad4076ddb7f4081585" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + pubspec_parse: + dependency: transitive + description: + name: pubspec_parse + sha256: "0560ba233314abbed0a48a2956f7f022cce7c3e1e73df540277da7544cad4082" + url: "https://pub.dev" + source: hosted + version: "1.5.0" + shelf: + dependency: transitive + description: + name: shelf + sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12 + url: "https://pub.dev" + source: hosted + version: "1.4.2" + shelf_web_socket: + dependency: transitive + description: + name: shelf_web_socket + sha256: "3632775c8e90d6c9712f883e633716432a27758216dfb61bd86a8321c0580925" + url: "https://pub.dev" + source: hosted + version: "3.0.0" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.0" + source_gen: + dependency: transitive + description: + name: source_gen + sha256: "35c8150ece9e8c8d263337a265153c3329667640850b9304861faea59fc98f6b" + url: "https://pub.dev" + source: hosted + version: "2.0.0" + source_helper: + dependency: transitive + description: + name: source_helper + sha256: "86d247119aedce8e63f4751bd9626fc9613255935558447569ad42f9f5b48b3c" + url: "https://pub.dev" + source: hosted + version: "1.3.5" source_span: dependency: transitive description: @@ -509,6 +765,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + stream_transform: + dependency: transitive + description: + name: stream_transform + sha256: ad47125e588cfd37a9a7f86c7d6356dde8dfe89d071d293f80ca9e9273a33871 + url: "https://pub.dev" + source: hosted + version: "2.1.1" string_scanner: dependency: transitive description: @@ -541,6 +805,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.4" + timing: + dependency: transitive + description: + name: timing + sha256: "62ee18aca144e4a9f29d212f5a4c6a053be252b895ab14b5821996cff4ed90fe" + url: "https://pub.dev" + source: hosted + version: "1.0.2" typed_data: dependency: transitive description: @@ -573,6 +845,14 @@ packages: url: "https://pub.dev" source: hosted version: "15.0.0" + watcher: + dependency: transitive + description: + name: watcher + sha256: "0b7fd4a0bbc4b92641dbf20adfd7e3fd1398fe17102d94b674234563e110088a" + url: "https://pub.dev" + source: hosted + version: "1.1.2" web: dependency: transitive description: @@ -581,6 +861,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" + web_socket: + dependency: transitive + description: + name: web_socket + sha256: "34d64019aa8e36bf9842ac014bb5d2f5586ca73df5e4d9bf5c936975cae6982c" + url: "https://pub.dev" + source: hosted + version: "1.0.1" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + sha256: d645757fb0f4773d602444000a8131ff5d48c9e47adfe9772652dd1a4f2d45c8 + url: "https://pub.dev" + source: hosted + version: "3.0.3" xdg_directories: dependency: transitive description: @@ -597,6 +893,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.5.0" + yaml: + dependency: transitive + description: + name: yaml + sha256: b9da305ac7c39faa3f030eccd175340f968459dae4af175130b3fc47e40d76ce + url: "https://pub.dev" + source: hosted + version: "3.1.3" sdks: dart: ">=3.8.1 <4.0.0" flutter: ">=3.27.0" diff --git a/pubspec.yaml b/pubspec.yaml index 8d589cc..c677b89 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,7 @@ dependencies: image: ^4.5.4 image_picker: ^1.1.2 uuid: ^4.5.1 + json_serializable: ^6.9.5 dev_dependencies: flutter_test: