locations_interest_database_handler.dart 2.82 KB
Newer Older
1 2 3 4 5

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:logair_application/logic/data_packet.dart';
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
6
import 'package:logair_application/utils/helpers/location_of_interest.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
import 'package:path/path.dart';
import 'package:rxdart/rxdart.dart';
import 'package:sqflite/sqflite.dart';

/// Handler that integrates a mysqli database into the application's workflow.
class LocationsOfInterestDatabaseHandler {
  factory LocationsOfInterestDatabaseHandler() => _singleton;

  LocationsOfInterestDatabaseHandler._internal();

  static final LocationsOfInterestDatabaseHandler _singleton = new LocationsOfInterestDatabaseHandler._internal();

  /// Reference to the internalized [Database] within this instance.
  Future<Database> _loidb;

  /// [Database] initialization string, to reduce repetition.
  final String _createLOIDBString = '''CREATE TABLE loi(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT, latitude REAL, longitude REAL
  )''';

  /// Retrieves the internal [Database] instance, and if not instantiated, instantiates it.
  Future<Database> get _loiDatabase async {
    if (_loidb == null) 
      this._loidb = openDatabase(
        join((await getDatabasesPath()), 'logair_loi.db'),
        version: 0,
        onCreate: (db, version) => db.execute(this._createLOIDBString),
      );
    return this._loidb;
  }

  /// Purges the [Database] of all internal data.
  Future<void> recreateDB() async {
    final Database database = await _loiDatabase;

    await database.rawQuery('DROP TABLE IF EXISTS loi');
    await database.rawQuery(this._createLOIDBString);
    this.onUpdate.value = 1;
  }

  /// Inserts data into the [Database].
  Future<void> insertData({@required String name, @required double latitude, @required double longitude}) async {
    final Database database = await this._loiDatabase;
    Map<String, dynamic> insert = {
      'name': name,
      'latitude': latitude,
      'longitude': longitude
    };
    await database.insert(
      'loi',
      insert,
      conflictAlgorithm: ConflictAlgorithm.replace
    );
    this.onUpdate.value = 1;
  }

  /// Retrieves all of the [DataPacket]s inserted into the [Database].
  Future<List<LocationOfInterest>> get data async {
    final Database database = await this._loiDatabase;

    final List<Map<String, dynamic>> data = await database.query('loi');

    return List.generate(data.length, (i) => LocationOfInterest(
      id: data[i]['id'],
      name: data[i]['name'],
      latitude: data[i]['latitude'],
      longitude: data[i]['longitude']
    ));
  }

  /// Closes the [Database] instance.
  Future<void> close() async => (await this._loiDatabase).close();

  /// Async capable variable used to trigger updates.
  BehaviorSubject<void> onUpdate = BehaviorSubject<void>();

  LocationsOfInterestDatabaseHandler.dispose() {
    onUpdate.close();
    this.close();
  }
}