dart format
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
import '../../../../api_response.dart';
|
||||
import '../../webdav_api.dart';
|
||||
import 'download_file_params.dart';
|
||||
|
||||
@@ -10,8 +10,13 @@ class DownloadFileParams extends ApiParams {
|
||||
String localTargetPath;
|
||||
String filename;
|
||||
|
||||
DownloadFileParams(this.webdavSourcePath, this.localTargetPath, this.filename);
|
||||
DownloadFileParams(
|
||||
this.webdavSourcePath,
|
||||
this.localTargetPath,
|
||||
this.filename,
|
||||
);
|
||||
|
||||
factory DownloadFileParams.fromJson(Map<String, dynamic> json) => _$DownloadFileParamsFromJson(json);
|
||||
factory DownloadFileParams.fromJson(Map<String, dynamic> json) =>
|
||||
_$DownloadFileParamsFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$DownloadFileParamsToJson(this);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'download_file_response.g.dart';
|
||||
@@ -9,6 +8,7 @@ class DownloadFileResponse {
|
||||
|
||||
DownloadFileResponse(this.path);
|
||||
|
||||
factory DownloadFileResponse.fromJson(Map<String, dynamic> json) => _$DownloadFileResponseFromJson(json);
|
||||
factory DownloadFileResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$DownloadFileResponseFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$DownloadFileResponseToJson(this);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,16 @@ class CacheableFile {
|
||||
DateTime? modifiedAt;
|
||||
String? sort;
|
||||
|
||||
CacheableFile({required this.path, required this.isDirectory, required this.name, this.mimeType, this.size, this.eTag, this.createdAt, this.modifiedAt});
|
||||
CacheableFile({
|
||||
required this.path,
|
||||
required this.isDirectory,
|
||||
required this.name,
|
||||
this.mimeType,
|
||||
this.size,
|
||||
this.eTag,
|
||||
this.createdAt,
|
||||
this.modifiedAt,
|
||||
});
|
||||
|
||||
CacheableFile.fromDavFile(WebDavFile file) {
|
||||
path = file.path.path;
|
||||
@@ -28,6 +37,7 @@ class CacheableFile {
|
||||
modifiedAt = file.lastModified;
|
||||
}
|
||||
|
||||
factory CacheableFile.fromJson(Map<String, dynamic> json) => _$CacheableFileFromJson(json);
|
||||
factory CacheableFile.fromJson(Map<String, dynamic> json) =>
|
||||
_$CacheableFileFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$CacheableFileToJson(this);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import 'package:nextcloud/nextcloud.dart';
|
||||
|
||||
import '../../webdav_api.dart';
|
||||
@@ -26,12 +25,15 @@ class ListFiles extends WebdavApi<ListFilesParams> {
|
||||
Future<ListFilesResponse> run() async {
|
||||
final webdav = await WebdavApi.webdav;
|
||||
final timeout = _isRoot ? _rootTimeout : _subfolderTimeout;
|
||||
final davFiles = (await webdav.propfind(PathUri.parse(params.path)).timeout(timeout)).toWebDavFiles();
|
||||
final davFiles =
|
||||
(await webdav.propfind(PathUri.parse(params.path)).timeout(timeout))
|
||||
.toWebDavFiles();
|
||||
final files = davFiles.map(CacheableFile.fromDavFile).toSet();
|
||||
|
||||
|
||||
// somehow the current working folder is also listed, it is filtered here.
|
||||
files.removeWhere((element) => element.path == '/${params.path}/' || element.path == '/');
|
||||
files.removeWhere(
|
||||
(element) => element.path == '/${params.path}/' || element.path == '/',
|
||||
);
|
||||
|
||||
return ListFilesResponse(files);
|
||||
}
|
||||
|
||||
@@ -17,16 +17,18 @@ class ListFilesCache extends SimpleCache<ListFilesResponse> {
|
||||
super.onError,
|
||||
required String path,
|
||||
}) : super(
|
||||
cacheTime: RequestCache.cacheNothing,
|
||||
loader: () => ListFiles(ListFilesParams(path)).run(),
|
||||
fromJson: ListFilesResponse.fromJson,
|
||||
onUpdate: onUpdate,
|
||||
) {
|
||||
cacheTime: RequestCache.cacheNothing,
|
||||
loader: () => ListFiles(ListFilesParams(path)).run(),
|
||||
fromJson: ListFilesResponse.fromJson,
|
||||
onUpdate: onUpdate,
|
||||
) {
|
||||
start(_documentId(path));
|
||||
}
|
||||
|
||||
static String _documentId(String path) {
|
||||
final cacheName = md5.convert(utf8.encode('MarianumMobile-$path')).toString();
|
||||
final cacheName = md5
|
||||
.convert(utf8.encode('MarianumMobile-$path'))
|
||||
.toString();
|
||||
return 'wd-folder-$cacheName';
|
||||
}
|
||||
|
||||
@@ -35,7 +37,10 @@ class ListFilesCache extends SimpleCache<ListFilesResponse> {
|
||||
/// `_FilesView` for that path via [CacheInvalidationBus] so it refetches
|
||||
/// even while it is sitting in the background of the navigation stack.
|
||||
static Future<void> invalidate(String path) async {
|
||||
await Localstore.instance.collection(RequestCache.collection).doc(_documentId(path)).delete();
|
||||
await Localstore.instance
|
||||
.collection(RequestCache.collection)
|
||||
.doc(_documentId(path))
|
||||
.delete();
|
||||
CacheInvalidationBus.notifyListFiles(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ class ListFilesParams extends ApiParams {
|
||||
|
||||
ListFilesParams(this.path);
|
||||
|
||||
factory ListFilesParams.fromJson(Map<String, dynamic> json) => _$ListFilesParamsFromJson(json);
|
||||
factory ListFilesParams.fromJson(Map<String, dynamic> json) =>
|
||||
_$ListFilesParamsFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$ListFilesParamsToJson(this);
|
||||
}
|
||||
|
||||
@@ -9,49 +9,73 @@ part 'list_files_response.g.dart';
|
||||
|
||||
@JsonSerializable(explicitToJson: true)
|
||||
class ListFilesResponse extends ApiResponse {
|
||||
Set<CacheableFile> files;
|
||||
Set<CacheableFile> files;
|
||||
|
||||
ListFilesResponse(this.files);
|
||||
ListFilesResponse(this.files);
|
||||
|
||||
factory ListFilesResponse.fromJson(Map<String, dynamic> json) => _$ListFilesResponseFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$ListFilesResponseToJson(this);
|
||||
factory ListFilesResponse.fromJson(Map<String, dynamic> json) =>
|
||||
_$ListFilesResponseFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$ListFilesResponseToJson(this);
|
||||
|
||||
List<CacheableFile> sortBy({bool foldersToTop = true, SortOption sortOption = SortOption.name, bool reversed = false}) {
|
||||
var list = List<CacheableFile>.empty(growable: true);
|
||||
List<CacheableFile> sortBy({
|
||||
bool foldersToTop = true,
|
||||
SortOption sortOption = SortOption.name,
|
||||
bool reversed = false,
|
||||
}) {
|
||||
var list = List<CacheableFile>.empty(growable: true);
|
||||
|
||||
if(foldersToTop) {
|
||||
list.addAll(_sort(files.where((element) => element.isDirectory).toSet(), reversed: reversed, sortOption: sortOption));
|
||||
list.addAll(_sort(files.where((element) => !element.isDirectory).toSet(), reversed: reversed, sortOption: sortOption));
|
||||
} else {
|
||||
list.addAll(_sort(files, reversed: reversed, sortOption: sortOption));
|
||||
}
|
||||
|
||||
return list;
|
||||
if (foldersToTop) {
|
||||
list.addAll(
|
||||
_sort(
|
||||
files.where((element) => element.isDirectory).toSet(),
|
||||
reversed: reversed,
|
||||
sortOption: sortOption,
|
||||
),
|
||||
);
|
||||
list.addAll(
|
||||
_sort(
|
||||
files.where((element) => !element.isDirectory).toSet(),
|
||||
reversed: reversed,
|
||||
sortOption: sortOption,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
list.addAll(_sort(files, reversed: reversed, sortOption: sortOption));
|
||||
}
|
||||
|
||||
List<CacheableFile> _sort(Set<CacheableFile> files, {SortOption sortOption = SortOption.name, bool reversed = false}) {
|
||||
for (var file in files) {
|
||||
final buffer = StringBuffer();
|
||||
return list;
|
||||
}
|
||||
|
||||
switch(sortOption) {
|
||||
case SortOption.date:
|
||||
buffer.write(Jiffy.parseFromMillisecondsSinceEpoch(file.modifiedAt?.millisecondsSinceEpoch ?? 0).format(pattern: 'yyyyMMddhhmmss'));
|
||||
break;
|
||||
List<CacheableFile> _sort(
|
||||
Set<CacheableFile> files, {
|
||||
SortOption sortOption = SortOption.name,
|
||||
bool reversed = false,
|
||||
}) {
|
||||
for (var file in files) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
case SortOption.name:
|
||||
buffer.write(file.name.toLowerCase());
|
||||
break;
|
||||
switch (sortOption) {
|
||||
case SortOption.date:
|
||||
buffer.write(
|
||||
Jiffy.parseFromMillisecondsSinceEpoch(
|
||||
file.modifiedAt?.millisecondsSinceEpoch ?? 0,
|
||||
).format(pattern: 'yyyyMMddhhmmss'),
|
||||
);
|
||||
break;
|
||||
|
||||
case SortOption.size:
|
||||
buffer.write(file.size);
|
||||
break;
|
||||
}
|
||||
case SortOption.name:
|
||||
buffer.write(file.name.toLowerCase());
|
||||
break;
|
||||
|
||||
file.sort = buffer.toString();
|
||||
}
|
||||
case SortOption.size:
|
||||
buffer.write(file.size);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
var list = files.toList()..sort((a, b) => b.sort!.compareTo(a.sort!));
|
||||
return reversed ? list.reversed.toList() : list;
|
||||
file.sort = buffer.toString();
|
||||
}
|
||||
|
||||
var list = files.toList()..sort((a, b) => b.sort!.compareTo(a.sort!));
|
||||
return reversed ? list.reversed.toList() : list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,12 @@ abstract class WebdavApi<T> extends ApiRequest {
|
||||
|
||||
static Future<WebDavClient> webdav = establishWebdavConnection();
|
||||
|
||||
static Future<WebDavClient> establishWebdavConnection() async => NextcloudClient(Uri.parse('https://${EndpointData().nextcloud().full()}'), password: AccountData().getPassword(), loginName: AccountData().getUsername()).webdav;
|
||||
static Future<WebDavClient> establishWebdavConnection() async =>
|
||||
NextcloudClient(
|
||||
Uri.parse('https://${EndpointData().nextcloud().full()}'),
|
||||
password: AccountData().getPassword(),
|
||||
loginName: AccountData().getUsername(),
|
||||
).webdav;
|
||||
|
||||
/// Builds the WebDAV download URL without embedded credentials. Callers must
|
||||
/// authenticate via the [AccountData.authHeaders] header instead.
|
||||
|
||||
Reference in New Issue
Block a user