javier-nogales / cyclingtrainer

Cycling Trainer App
0 stars 0 forks source link

Link Device #4

Open javier-nogales opened 4 years ago

javier-nogales commented 4 years ago

Save device in storage with sqlite. On save, bloc should emits state to warn other blocs

class DBProvider {

static Database _database;

static final DBProvider db = DBProvider._();

DBProvider._();

Future get database async {

if (_database != null) return _database;

_database = await initDB();

//deleteAll();

return _database;

}

initDB() async {

String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'BikeTrainerDB.db');

_onCreate(Database db, int version) async {
  // Database is created, create the table
  await db.execute(
          'CREATE TABLE Devices ('
          ' id TEXT PRIMARY KEY, '
          ' type TEXT, '
          ' name TEXT '
          ')'
        );
}

_onUpgrade(Database db, int oldVersion, int newVersion) async {
  // Database version is updated, alter the table

}

_onOpen(Database db) async {

}

return await openDatabase(
  path,
  version: 1,
  onOpen: _onOpen,
  onCreate: _onCreate,
  onUpgrade: _onUpgrade,
  onDowngrade: onDatabaseDowngradeDelete,
);

}

// CRUD createDeviceRAW(Device device) async { final db = await database; final result = await db.rawInsert( "INSERT Into Devices (id, type, name) " "VALUES ( '${device.id}', '${device.type}', '${device.name}' )" ); return result; }

// Create Future createDevice(Device device) async { final db = await database; final result = db.insert('Devices', device.toJson()); return result; }

// Read Future getDeviceById(String id) async { final db = await database; final result = await db.query('Devices', where: 'id = ?', whereArgs: [id]); return result.isNotEmpty ? Device.fromJson(result.first) : null; }

Future<List> getDevicesByType(DeviceType type) async { final db = await database; final result = await db.query('Devices', where: 'type = ?', whereArgs: [type]); List list = result.isNotEmpty ? result.map((deviceMap) => Device.fromJson(deviceMap)).toList() : []; return list; }

Future<List> getAllDevices() async { final db = await database; final result = await db.query('Devices'); List list = result.isNotEmpty ? result.map((deviceMap) => Device.fromJson(deviceMap)).toList() : []; return list; }

// Update Future updateDevice(Device device) async { final db = await database; final result = await db.update('Devices', device.toJson(), where: 'id = ?', whereArgs: [device.id]); return result; }

// Delete Future deleteDevice(Device device) async { final db = await database; final result = await db.delete('Devices', where: 'id = ?', whereArgs: [device.id]); return result; }

Future deleteAll() async { final db = await database; final result = await db.delete('Devices'); return result; } }

javier-nogales commented 4 years ago

Device table: [id][name][type][class]

'CREATE TABLE Devices ('
              ' id TEXT PRIMARY KEY, '
              ' name TEXT '
              ' type TEXT, '
              ' deviceClass TEXT '
              ')'