Adds db helper requsts for flights pertaining to drones/batteries, and config class with generated json serializer
This commit is contained in:
16
lib/config.dart
Normal file
16
lib/config.dart
Normal file
@ -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<String, dynamic> json) =>
|
||||
_$PersonFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$PersonToJson(this);
|
||||
}
|
||||
@ -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<Database> get database async
|
||||
{
|
||||
if (_database != null) return _database!;
|
||||
await _initDb();
|
||||
return _database!;
|
||||
}
|
||||
Future<Database> 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<Drone> insertDrone(Drone drone) async
|
||||
{
|
||||
final db = await database;
|
||||
drone.id = await db.insert("drones", drone.toMap());
|
||||
return drone;
|
||||
}
|
||||
// Drone related helpers
|
||||
Future<Drone> insertDrone(Drone drone) async {
|
||||
final db = await database;
|
||||
drone.id = await db.insert("drones", drone.toMap());
|
||||
return drone;
|
||||
}
|
||||
|
||||
Future<List<Drone>> getDrones() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("drones");
|
||||
return maps.map((e) => Drone.fromMap(e)).toList();
|
||||
}
|
||||
Future<List<Drone>> getDrones() async {
|
||||
final db = await database;
|
||||
final maps = await db.query("drones");
|
||||
return maps.map((e) => Drone.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteDrone(int droneId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [droneId]);
|
||||
}
|
||||
Future<int> deleteDrone(int droneId) async {
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [droneId]);
|
||||
}
|
||||
|
||||
// Battery related helpers
|
||||
Future<Battery> insertBattery(Battery battery) async
|
||||
{
|
||||
final db = await database;
|
||||
battery.id = await db.insert("batteries", battery.toMap());
|
||||
return battery;
|
||||
}
|
||||
// Battery related helpers
|
||||
Future<Battery> insertBattery(Battery battery) async {
|
||||
final db = await database;
|
||||
battery.id = await db.insert("batteries", battery.toMap());
|
||||
return battery;
|
||||
}
|
||||
|
||||
Future<List<Battery>> getBatteries() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("batteries");
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
Future<List<Battery>> getBatteries() async {
|
||||
final db = await database;
|
||||
final maps = await db.query("batteries");
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteBattery(int batteryId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("batteries", where: "id = ?", whereArgs: [batteryId]);
|
||||
}
|
||||
Future<int> deleteBattery(int batteryId) async {
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete(
|
||||
"batteries",
|
||||
where: "id = ?",
|
||||
whereArgs: [batteryId],
|
||||
);
|
||||
}
|
||||
|
||||
// Flight related helpers
|
||||
Future<Flight> insertFlight(Flight flight) async
|
||||
{
|
||||
final db = await database;
|
||||
flight.id = await db.insert("flights", flight.toMap());
|
||||
return flight;
|
||||
}
|
||||
Future<List<Battery>> 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<List<Flight>> getFlights() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("flights");
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
// Flight related helpers
|
||||
Future<Flight> insertFlight(Flight flight) async {
|
||||
final db = await database;
|
||||
flight.id = await db.insert("flights", flight.toMap());
|
||||
return flight;
|
||||
}
|
||||
|
||||
Future<int> deleteFlight(int flightId) async
|
||||
{
|
||||
final db = await database;
|
||||
return await db.delete("flights", where: "id = ?", whereArgs: [flightId]);
|
||||
}
|
||||
Future<List<Flight>> 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<int> deleteFlight(int flightId) async {
|
||||
final db = await database;
|
||||
return await db.delete("flights", where: "id = ?", whereArgs: [flightId]);
|
||||
}
|
||||
|
||||
// Complex requests
|
||||
Future<List<Flight>> 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<List<Flight>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
13
lib/generated/config.g.dart
Normal file
13
lib/generated/config.g.dart
Normal file
@ -0,0 +1,13 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of '../config.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
RTimeConfig _$RTimeConfigFromJson(Map<String, dynamic> json) =>
|
||||
RTimeConfig(language: json['language'] as String);
|
||||
|
||||
Map<String, dynamic> _$RTimeConfigToJson(RTimeConfig instance) =>
|
||||
<String, dynamic>{'language': instance.language};
|
||||
@ -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<MyHomePage> {
|
||||
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<MyHomePage> {
|
||||
// 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 =
|
||||
<Widget>[
|
||||
const Text('You have pushed the button this many times:'),
|
||||
Text(
|
||||
'$_counter',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
FutureBuilder(
|
||||
future: Future<Widget>(() async
|
||||
{
|
||||
final drones = await DbHelper.instance.getDrones();
|
||||
var children = <Widget>[
|
||||
const Text('You have pushed the button this many times:'),
|
||||
Text('$_counter', style: Theme.of(context).textTheme.headlineMedium),
|
||||
FutureBuilder(
|
||||
future: Future<Widget>(() 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<Widget> img)
|
||||
{
|
||||
if(!img.hasData)
|
||||
{
|
||||
return Center(child: CircularProgressIndicator(),);
|
||||
}
|
||||
|
||||
return img.data!;
|
||||
}
|
||||
),
|
||||
Icon(Icons.build)];
|
||||
return image;
|
||||
}),
|
||||
builder: (BuildContext ctx, AsyncSnapshot<Widget> 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<MyHomePage> {
|
||||
// wireframe for each widget.
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: children,
|
||||
),
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _incrementCounter,
|
||||
@ -182,4 +174,3 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user