2020-03-24 19:59:36 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2020-03-27 16:07:55 +00:00
|
|
|
import 'package:myapp/models/photo.dart';
|
2020-03-24 19:59:36 +00:00
|
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
|
|
class DatabaseHelper {
|
2020-03-26 14:39:31 +00:00
|
|
|
static final _databaseName = "orma.db";
|
2020-03-24 19:59:36 +00:00
|
|
|
static final _databaseVersion = 1;
|
|
|
|
|
2020-03-27 16:07:55 +00:00
|
|
|
static final table = 'photos';
|
2020-03-26 14:39:31 +00:00
|
|
|
|
2020-04-12 12:38:49 +00:00
|
|
|
static final columnUploadedFileId = 'uploaded_file_id';
|
2020-04-11 21:29:07 +00:00
|
|
|
static final columnLocalId = 'local_id';
|
2020-03-27 16:07:55 +00:00
|
|
|
static final columnLocalPath = 'local_path';
|
2020-03-30 10:57:04 +00:00
|
|
|
static final columnThumbnailPath = 'thumbnail_path';
|
2020-04-09 19:03:11 +00:00
|
|
|
static final columnPath = 'path';
|
2020-03-26 14:39:31 +00:00
|
|
|
static final columnHash = 'hash';
|
2020-04-12 12:38:49 +00:00
|
|
|
static final columnIsDeleted = 'is_deleted';
|
2020-03-27 16:07:55 +00:00
|
|
|
static final columnSyncTimestamp = 'sync_timestamp';
|
2020-03-24 19:59:36 +00:00
|
|
|
|
|
|
|
// make this a singleton class
|
|
|
|
DatabaseHelper._privateConstructor();
|
|
|
|
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
|
|
|
|
|
|
|
|
// only have a single app-wide reference to the database
|
|
|
|
static Database _database;
|
|
|
|
Future<Database> get database async {
|
|
|
|
if (_database != null) return _database;
|
|
|
|
// lazily instantiate the db the first time it is accessed
|
|
|
|
_database = await _initDatabase();
|
|
|
|
return _database;
|
|
|
|
}
|
2020-03-26 14:39:31 +00:00
|
|
|
|
2020-03-24 19:59:36 +00:00
|
|
|
// this opens the database (and creates it if it doesn't exist)
|
|
|
|
_initDatabase() async {
|
|
|
|
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
|
|
|
String path = join(documentsDirectory.path, _databaseName);
|
|
|
|
return await openDatabase(path,
|
2020-03-26 14:39:31 +00:00
|
|
|
version: _databaseVersion, onCreate: _onCreate);
|
2020-03-24 19:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SQL code to create the database table
|
|
|
|
Future _onCreate(Database db, int version) async {
|
|
|
|
await db.execute('''
|
|
|
|
CREATE TABLE $table (
|
2020-04-11 21:29:07 +00:00
|
|
|
$columnLocalId TEXT,
|
2020-04-12 12:38:49 +00:00
|
|
|
$columnUploadedFileId INTEGER NOT NULL,
|
2020-03-27 16:07:55 +00:00
|
|
|
$columnLocalPath TEXT NOT NULL,
|
2020-03-30 10:57:04 +00:00
|
|
|
$columnThumbnailPath TEXT NOT NULL,
|
2020-04-09 19:03:11 +00:00
|
|
|
$columnPath TEXT,
|
2020-03-26 14:39:31 +00:00
|
|
|
$columnHash TEXT NOT NULL,
|
2020-04-12 12:38:49 +00:00
|
|
|
$columnIsDeleted INTEGER DEFAULT 0,
|
2020-03-28 13:56:06 +00:00
|
|
|
$columnSyncTimestamp TEXT
|
2020-03-24 19:59:36 +00:00
|
|
|
)
|
|
|
|
''');
|
|
|
|
}
|
2020-03-26 14:39:31 +00:00
|
|
|
|
2020-03-27 16:07:55 +00:00
|
|
|
Future<int> insertPhoto(Photo photo) async {
|
2020-03-26 14:39:31 +00:00
|
|
|
Database db = await instance.database;
|
2020-03-30 14:28:46 +00:00
|
|
|
return await db.insert(table, _getRowForPhoto(photo));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<dynamic>> insertPhotos(List<Photo> photos) async {
|
|
|
|
Database db = await instance.database;
|
|
|
|
var batch = db.batch();
|
|
|
|
int batchCounter = 0;
|
|
|
|
for (Photo photo in photos) {
|
|
|
|
if (batchCounter == 400) {
|
|
|
|
await batch.commit();
|
|
|
|
batch = db.batch();
|
|
|
|
}
|
|
|
|
batch.insert(table, _getRowForPhoto(photo));
|
|
|
|
batchCounter++;
|
|
|
|
}
|
|
|
|
return await batch.commit();
|
2020-03-26 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 16:07:55 +00:00
|
|
|
Future<List<Photo>> getAllPhotos() async {
|
|
|
|
Database db = await instance.database;
|
2020-04-12 12:38:49 +00:00
|
|
|
var results = await db.query(table, where: '$columnIsDeleted = 0');
|
|
|
|
return _convertToPhotos(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Photo>> getAllDeletedPhotos() async {
|
|
|
|
Database db = await instance.database;
|
|
|
|
var results = await db.query(table, where: '$columnIsDeleted = 1');
|
2020-03-28 13:56:06 +00:00
|
|
|
return _convertToPhotos(results);
|
2020-03-27 16:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:56:06 +00:00
|
|
|
Future<List<Photo>> getPhotosToBeUploaded() async {
|
2020-03-24 19:59:36 +00:00
|
|
|
Database db = await instance.database;
|
2020-04-12 12:38:49 +00:00
|
|
|
var results = await db.query(table, where: '$columnUploadedFileId = -1');
|
2020-03-28 13:56:06 +00:00
|
|
|
return _convertToPhotos(results);
|
2020-03-24 19:59:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 12:38:49 +00:00
|
|
|
Future<int> updatePhoto(Photo photo) async {
|
2020-03-24 19:59:36 +00:00
|
|
|
Database db = await instance.database;
|
2020-04-12 12:38:49 +00:00
|
|
|
return await db.update(table, _getRowForPhoto(photo),
|
|
|
|
where: '$columnHash = ? AND $columnPath = ?',
|
|
|
|
whereArgs: [photo.hash, photo.localPath]);
|
2020-03-24 19:59:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:03:11 +00:00
|
|
|
Future<Photo> getPhotoByPath(String path) async {
|
2020-03-24 19:59:36 +00:00
|
|
|
Database db = await instance.database;
|
2020-04-09 19:03:11 +00:00
|
|
|
var rows =
|
|
|
|
await db.query(table, where: '$columnPath =?', whereArgs: [path]);
|
2020-04-05 14:45:04 +00:00
|
|
|
if (rows.length > 0) {
|
2020-04-12 12:38:49 +00:00
|
|
|
return _getPhotofromRow(rows[0]);
|
2020-04-05 14:45:04 +00:00
|
|
|
} else {
|
|
|
|
throw ("No cached photo");
|
|
|
|
}
|
2020-03-24 19:59:36 +00:00
|
|
|
}
|
2020-03-26 14:39:31 +00:00
|
|
|
|
2020-03-28 13:56:06 +00:00
|
|
|
Future<bool> containsPhotoHash(String hash) async {
|
2020-03-26 14:39:31 +00:00
|
|
|
Database db = await instance.database;
|
2020-03-28 13:56:06 +00:00
|
|
|
return (await db.query(table, where: '$columnHash =?', whereArgs: [hash]))
|
2020-03-26 14:39:31 +00:00
|
|
|
.length >
|
|
|
|
0;
|
|
|
|
}
|
2020-03-28 13:56:06 +00:00
|
|
|
|
2020-04-12 12:38:49 +00:00
|
|
|
Future<int> markPhotoAsDeleted(Photo photo) async {
|
|
|
|
Database db = await instance.database;
|
|
|
|
var values = new Map<String, dynamic>();
|
|
|
|
values[columnIsDeleted] = 1;
|
|
|
|
return db.update(table, values,
|
|
|
|
where: '$columnHash =? AND $columnLocalPath =?',
|
|
|
|
whereArgs: [photo.hash, photo.localPath]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> deletePhoto(Photo photo) async {
|
|
|
|
Database db = await instance.database;
|
|
|
|
return db.delete(table,
|
|
|
|
where: '$columnHash =? AND $columnLocalPath =?',
|
|
|
|
whereArgs: [photo.hash, photo.localPath]);
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:56:06 +00:00
|
|
|
List<Photo> _convertToPhotos(List<Map<String, dynamic>> results) {
|
|
|
|
var photos = List<Photo>();
|
|
|
|
for (var result in results) {
|
2020-04-12 12:38:49 +00:00
|
|
|
photos.add(_getPhotofromRow(result));
|
2020-03-28 13:56:06 +00:00
|
|
|
}
|
|
|
|
return photos;
|
|
|
|
}
|
2020-03-30 14:28:46 +00:00
|
|
|
|
|
|
|
Map<String, dynamic> _getRowForPhoto(Photo photo) {
|
|
|
|
var row = new Map<String, dynamic>();
|
2020-04-11 21:29:07 +00:00
|
|
|
row[columnLocalId] = photo.localId;
|
2020-04-12 12:38:49 +00:00
|
|
|
row[columnUploadedFileId] =
|
|
|
|
photo.uploadedFileId == null ? -1 : photo.uploadedFileId;
|
2020-03-30 14:28:46 +00:00
|
|
|
row[columnLocalPath] = photo.localPath;
|
|
|
|
row[columnThumbnailPath] = photo.thumbnailPath;
|
2020-04-09 19:03:11 +00:00
|
|
|
row[columnPath] = photo.path;
|
2020-03-30 14:28:46 +00:00
|
|
|
row[columnHash] = photo.hash;
|
|
|
|
row[columnSyncTimestamp] = photo.syncTimestamp;
|
|
|
|
return row;
|
|
|
|
}
|
2020-04-12 12:38:49 +00:00
|
|
|
|
|
|
|
Photo _getPhotofromRow(Map<String, dynamic> row) {
|
|
|
|
Photo photo = Photo();
|
|
|
|
photo.localId = row[columnLocalId];
|
|
|
|
photo.uploadedFileId = row[columnUploadedFileId];
|
|
|
|
photo.localPath = row[columnLocalPath];
|
|
|
|
photo.thumbnailPath = row[columnThumbnailPath];
|
|
|
|
photo.path = row[columnPath];
|
|
|
|
photo.hash = row[columnHash];
|
|
|
|
photo.syncTimestamp = row[columnSyncTimestamp] == null
|
|
|
|
? -1
|
|
|
|
: int.parse(row[columnSyncTimestamp]);
|
|
|
|
return photo;
|
|
|
|
}
|
2020-03-26 14:39:31 +00:00
|
|
|
}
|