init method
Initialize the persistence layer and the database.
Implementation
Future<void> init() async {
info('Initializing $runtimeType...');
_databasePath ??= await getDatabasesPath();
// open the database - make sure to use the same database across app (re)start
_database = await openDatabase(
databaseName,
version: DATABASE_VERSION,
singleInstance: true,
onCreate: (Database db, int version) async {
// when creating the database, create the tables
await _createStudyTable(db);
await _createTaskQueueTable(db);
debug('$runtimeType - $databaseName DB created');
},
onUpgrade: (Database db, int oldVersion, int newVersion) async {
if (oldVersion < 2) await _migrateToV2(db);
},
);
// Listen to changes to studies in the client repository so we can save them.
SmartphoneClientRepository().studyStatusEvents.listen(
(event) => updateStudy(event.study),
);
// Listen to changes to the app task queue so we can save them.
AppTaskController().userTaskEvents.listen((task) => saveUserTask(task));
info('$runtimeType - SQLite DB initialized - file: $databaseName');
}