nc-photos/app/lib/use_case/scan_dir_offline.dart
Ming Ming 7563fa2ad6 Show first 100 photos on startup
This is to create an illusion that the app loads quickly when it's not ;)
2022-07-24 22:01:50 +08:00

110 lines
3.3 KiB
Dart

import 'package:drift/drift.dart' as sql;
import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/sqlite_table_converter.dart';
import 'package:nc_photos/entity/sqlite_table_extension.dart' as sql;
import 'package:nc_photos/object_extension.dart';
class ScanDirOffline {
ScanDirOffline(this._c) : assert(require(_c));
static bool require(DiContainer c) => DiContainer.has(c, DiType.sqliteDb);
Future<List<File>> call(
Account account,
File root, {
bool isOnlySupportedFormat = true,
}) async {
return await _c.sqliteDb.isolate({
"account": account,
"root": root,
"isOnlySupportedFormat": isOnlySupportedFormat,
}, (db, Map args) async {
final Account account = args["account"];
final File root = args["root"];
final bool isOnlySupportedFormat = args["isOnlySupportedFormat"];
final dbFiles = await db.useInIsolate((db) async {
final query = db.queryFiles().run((q) {
q
..setQueryMode(sql.FilesQueryMode.completeFile)
..setAppAccount(account);
root.strippedPathWithEmpty.run((p) {
if (p.isNotEmpty) {
q.byRelativePathPattern("$p/%");
}
});
if (isOnlySupportedFormat) {
q
..byMimePattern("image/%")
..byMimePattern("video/%");
}
return q.build();
});
return await query
.map((r) => sql.CompleteFile(
r.readTable(db.files),
r.readTable(db.accountFiles),
r.readTableOrNull(db.images),
r.readTableOrNull(db.trashes),
))
.get();
});
return dbFiles
.map((f) => SqliteFileConverter.fromSql(account.userId.toString(), f))
.toList();
});
}
final DiContainer _c;
}
class ScanDirOfflineMini {
ScanDirOfflineMini(this._c) : assert(require(_c));
static bool require(DiContainer c) => DiContainer.has(c, DiType.sqliteDb);
Future<List<File>> call(
Account account,
Iterable<File> roots,
int limit, {
bool isOnlySupportedFormat = true,
}) async {
final dbFiles = await _c.sqliteDb.use((db) async {
final query = db.queryFiles().run((q) {
q
..setQueryMode(sql.FilesQueryMode.completeFile)
..setAppAccount(account);
for (final r in roots) {
final path = r.strippedPathWithEmpty;
if (path.isEmpty) {
break;
}
q.byRelativePathPattern("$path/%");
}
if (isOnlySupportedFormat) {
q
..byMimePattern("image/%")
..byMimePattern("video/%");
}
return q.build();
});
query
..orderBy([sql.OrderingTerm.desc(db.accountFiles.bestDateTime)])
..limit(limit);
return await query
.map((r) => sql.CompleteFile(
r.readTable(db.files),
r.readTable(db.accountFiles),
r.readTableOrNull(db.images),
r.readTableOrNull(db.trashes),
))
.get();
});
return dbFiles
.map((f) => SqliteFileConverter.fromSql(account.userId.toString(), f))
.toList();
}
final DiContainer _c;
}