LsSingleFile now takes repo as with other use cases

This commit is contained in:
Ming Ming 2021-11-21 21:34:31 +08:00
parent d9a6d04ae8
commit adf1968a40
5 changed files with 37 additions and 12 deletions

View file

@ -508,6 +508,10 @@ class FileRepo {
Future<List<File>> list(Account account, File root) =>
dataSrc.list(account, root);
/// See [FileDataSource.listSingle]
Future<File> listSingle(Account account, File root) =>
dataSrc.listSingle(account, root);
/// See [FileDataSource.remove]
Future<void> remove(Account account, File file) =>
dataSrc.remove(account, file);
@ -575,6 +579,9 @@ abstract class FileDataSource {
/// List all files under [f]
Future<List<File>> list(Account account, File f);
/// List a single file [f]
Future<File> listSingle(Account account, File f);
/// Remove file
Future<void> remove(Account account, File f);

View file

@ -16,7 +16,6 @@ import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
import 'package:nc_photos/string_extension.dart';
import 'package:nc_photos/touch_token_manager.dart';
import 'package:nc_photos/use_case/compat/v32.dart';
import 'package:nc_photos/use_case/ls_single_file.dart';
import 'package:path/path.dart' as path;
import 'package:quiver/iterables.dart';
import 'package:uuid/uuid.dart';
@ -78,6 +77,12 @@ class FileWebdavDataSource implements FileDataSource {
return files;
}
@override
listSingle(Account account, File f) async {
_log.info("[listSingle] ${f.path}");
return (await list(account, f, depth: 0)).first;
}
@override
remove(Account account, File f) async {
_log.info("[remove] ${f.path}");
@ -250,6 +255,12 @@ class FileAppDbDataSource implements FileDataSource {
});
}
@override
listSingle(Account account, File f) {
_log.info("[listSingle] ${f.path}");
throw UnimplementedError();
}
@override
remove(Account account, File f) {
_log.info("[remove] ${f.path}");
@ -440,6 +451,11 @@ class FileCachedDataSource implements FileDataSource {
}
}
@override
listSingle(Account account, File f) {
return _remoteSrc.listSingle(account, f);
}
@override
remove(Account account, File f) async {
await _appDbSrc.remove(account, f);
@ -656,7 +672,7 @@ class _CacheManager {
// compare the etag to see if the content has been updated
var remoteEtag = f.etag;
// if no etag supplied, we need to query it form remote
remoteEtag ??= (await LsSingleFile(remoteSrc)(account, f.path)).etag;
remoteEtag ??= (await remoteSrc.list(account, f, depth: 0)).first.etag;
if (cacheEtag == remoteEtag) {
_log.fine(
"[_listCache] etag matched for ${AppDbFileEntry.toPath(account, f)}");

View file

@ -1,15 +1,11 @@
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
class LsSingleFile {
LsSingleFile(this.dataSrc);
LsSingleFile(this.fileRepo);
Future<File> call(Account account, String path) async {
final files = await dataSrc.list(account, File(path: path), depth: 0);
assert(files.length == 1);
return files.first;
}
Future<File> call(Account account, String path) =>
fileRepo.listSingle(account, File(path: path));
final FileWebdavDataSource dataSrc;
final FileRepo fileRepo;
}

View file

@ -5,6 +5,7 @@ import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/app_db.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
@ -57,12 +58,12 @@ class _RootPickerState extends State<RootPicker> {
void _initAccount() async {
try {
const fileSrc = FileWebdavDataSource();
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
final files = <File>[];
for (final r in widget.account.roots) {
if (r.isNotEmpty) {
_ensureInitDialog();
files.add(await LsSingleFile(fileSrc)(
files.add(await LsSingleFile(fileRepo)(
widget.account, file_util.unstripPath(widget.account, r)));
}
}

View file

@ -142,6 +142,11 @@ class MockFileRepo implements FileRepo {
throw UnimplementedError();
}
@override
Future<File> listSingle(Account account, File root) async {
throw UnimplementedError();
}
@override
Future<void> move(Account account, File f, String destination,
{bool? shouldOverwrite}) {