Skip to content

Commit

Permalink
final ! :(
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmdrz committed Oct 13, 2018
1 parent 91589ee commit b6ee8e2
Show file tree
Hide file tree
Showing 36 changed files with 1,174 additions and 140 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Expand Down
40 changes: 21 additions & 19 deletions lib/config/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,43 @@ import 'package:sqflite/sqflite.dart';
import '../models/Category.dart';
import '../models/CaptureModel.dart';
import '../models/Series.dart';
import '../models/Settings.dart';

class Application {
static Router router;
static Database db;
static bool _dbIsOpened = false;
static String _databaseName = 'cantonfair.db';
static Map<String, dynamic> cache;
static Map<String, dynamic> cache;
static String databasePath = "";
static String appDir;
static String mainDir;

static String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();

static Future backupDatabase() async {
await closeDatabase();
File f = new File(join(databasePath, _databaseName));
String newPath = join(databasePath, '${timestamp()}.db');
var backup = join(appDir, "Backup");
String newPath = join(backup, '${timestamp()}.db');
await Directory(backup).create(recursive: true);
print("Coping to $newPath");
await f.copy(newPath);
await openDB();
}

static Future initDatabase() async {
Directory extPath = await getExternalStorageDirectory();
appDir = join(extPath.path, "CantonFair");
static Future forceDelete() async {
await closeDatabase();
File f = new File(join(databasePath, _databaseName));
if (await f.exists()) await f.delete();
await openDB();
}

static Future initDatabase() async {
Directory extDir = await getExternalStorageDirectory();
mainDir = extDir.path;
appDir = join(mainDir, "CantonFair");
await Directory(appDir).create(recursive: true);
databasePath = join(appDir, "Database");
await openDB();
}
Expand All @@ -47,15 +60,9 @@ class Application {
print("Database opened !");
_dbIsOpened = true;

// only on development
// print("Droping ${Category.tableName} ...");
// await db.execute("DROP TABLE ${Category.tableName};");

// print("Droping ${Series.tableName} ...");
// await db.execute("DROP TABLE ${Series.tableName};");

// print("Droping ${CaptureModel.tableName} ...");
// await db.execute("DROP TABLE ${CaptureModel.tableName};");
print("Creating ${Settings.tableName} ...");
await db.execute(Settings.dbOnCreate);
Settings.db = db;

print("Creating ${CaptureModel.tableName} ...");
await db.execute(CaptureModel.dbOnCreate);
Expand All @@ -71,11 +78,6 @@ class Application {
},
onCreate: (Database db, int version) async {
// only on production
print("Creating ${Category.tableName} ...");
await db.execute(Category.dbOnCreate);

print("Creating ${Series.tableName} ...");
await db.execute(Series.dbOnCreate);
},
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/models/Series.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Series {
static Future<Category> getCategoryOfSeriesUUID(uuid,
{inv: 'desc', order: 'created_at'}) async {
var series = await getSelectedSeriesByUUID(uuid);
if (series == null) return null;
var result = await db.rawQuery(
'SELECT * FROM ${Category.tableName} WHERE ${Category.dbUUID} = "${series.categoryUUID}" ORDER BY "$order $inv";');
List<Category> categories = [];
Expand Down
53 changes: 53 additions & 0 deletions lib/models/Settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'dart:async';
import 'package:sqflite/sqflite.dart';

class Settings {
static Database db;

static final String dbKey = "key";
static final String dbValue = "value";

static final String tableName = "settings";
static final String dbOnCreate = "CREATE TABLE IF NOT EXISTS $tableName ("
"${Settings.dbKey} STRING PRIMARY KEY,"
"${Settings.dbValue} Text"
")";

String key;
String value;

Settings({this.key, this.value});

Settings.fromMap(Map<String, dynamic> map) {
this.key = map[dbKey];
this.value = map[dbValue];
}

Map<String, dynamic> toMap() {
return {
dbValue: value,
dbKey: key,
};
}

static Future<Settings> fetch(String key) async {
var result = await db.rawQuery(
'SELECT * FROM $tableName WHERE $dbKey = "$key";');
List<Settings> items = [];
for (Map<String, dynamic> item in result) {
items.add(new Settings.fromMap(item));
}
return items.length > 0 ? items[0] : null;
}

static Future save(Settings item) async {
await db.rawInsert(
'INSERT OR REPLACE INTO '
'$tableName(${Settings.dbKey}, ${Settings.dbValue})'
' VALUES(?, ?)',
[
item.key,
item.value,
]);
}
}
Loading

0 comments on commit b6ee8e2

Please sign in to comment.