Browse Source

Merge pull request #279 from provokateurin/fix/files-display-old-directory

Display empty directory instead of last directory if nothing is cached
pull/280/head
Kate 2 years ago committed by GitHub
parent
commit
aa8f7dba99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 83
      packages/neon/neon/lib/src/utils/request_manager.dart
  2. 1
      packages/neon/neon_files/lib/blocs/browser.dart

83
packages/neon/neon/lib/src/utils/request_manager.dart

@ -14,6 +14,7 @@ class RequestManager {
final Future<R> Function() call, final Future<R> Function() call,
final T Function(R) unwrap, { final T Function(R) unwrap, {
final bool disableTimeout = false, final bool disableTimeout = false,
final bool emitEmptyCache = false,
}) async => }) async =>
_wrap<T, R>( _wrap<T, R>(
clientID, clientID,
@ -24,6 +25,7 @@ class RequestManager {
(final data) => json.encode(serializeNextcloud<R>(data)), (final data) => json.encode(serializeNextcloud<R>(data)),
(final data) => deserializeNextcloud<R>(json.decode(data)), (final data) => deserializeNextcloud<R>(json.decode(data)),
disableTimeout, disableTimeout,
emitEmptyCache,
0, 0,
); );
@ -34,6 +36,7 @@ class RequestManager {
final Future<WebDavMultistatus> Function() call, final Future<WebDavMultistatus> Function() call,
final T Function(WebDavMultistatus) unwrap, { final T Function(WebDavMultistatus) unwrap, {
final bool disableTimeout = false, final bool disableTimeout = false,
final bool emitEmptyCache = false,
}) async => }) async =>
_wrap<T, WebDavMultistatus>( _wrap<T, WebDavMultistatus>(
clientID, clientID,
@ -44,6 +47,7 @@ class RequestManager {
(final data) => data.toXmlElement(namespaces: namespaces).toXmlString(), (final data) => data.toXmlElement(namespaces: namespaces).toXmlString(),
(final data) => WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(data).rootElement), (final data) => WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(data).rootElement),
disableTimeout, disableTimeout,
emitEmptyCache,
0, 0,
); );
@ -56,6 +60,7 @@ class RequestManager {
final String Function(R) serialize, final String Function(R) serialize,
final R Function(String) deserialize, final R Function(String) deserialize,
final bool disableTimeout, final bool disableTimeout,
final bool emitEmptyCache,
final int retries, final int retries,
) async { ) async {
if (subject.valueOrNull?.data != null) { if (subject.valueOrNull?.data != null) {
@ -73,21 +78,14 @@ class RequestManager {
final key = '$clientID-$k'; final key = '$clientID-$k';
if (cache != null && await cache!.has(key)) { await _emitCached(
try { key,
subject.add( subject,
Result( unwrap,
unwrap(await compute(deserialize, (await cache!.get(key))!)), deserialize,
null, emitEmptyCache,
loading: true, true,
cached: true, );
),
);
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
}
}
try { try {
final response = await (disableTimeout ? call() : timeout(call)); final response = await (disableTimeout ? call() : timeout(call));
@ -107,28 +105,53 @@ class RequestManager {
serialize, serialize,
deserialize, deserialize,
disableTimeout, disableTimeout,
emitEmptyCache,
retries + 1, retries + 1,
); );
return; return;
} }
if (!(await _emitCached(
key,
subject,
unwrap,
deserialize,
emitEmptyCache,
false,
))) {
subject.add(Result.error(e));
}
}
}
Future<bool> _emitCached<T, R>(
final String key,
final BehaviorSubject<Result<T>> subject,
final T Function(R) unwrap,
final R Function(String) deserialize,
final bool emitEmptyCache,
final bool loading,
) async {
T? cached;
try {
if (cache != null && await cache!.has(key)) { if (cache != null && await cache!.has(key)) {
try { cached = unwrap(await compute(deserialize, (await cache!.get(key))!));
subject.add(
Result(
unwrap(await compute(deserialize, (await cache!.get(key))!)),
null,
loading: false,
cached: true,
),
);
return;
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
}
} }
subject.add(Result.error(e)); } catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
}
if (cached != null || emitEmptyCache) {
subject.add(
Result(
cached,
null,
loading: loading,
cached: true,
),
);
return true;
} }
return false;
} }
static Future<T> timeout<T>( static Future<T> timeout<T>(

1
packages/neon/neon_files/lib/blocs/browser.dart

@ -56,6 +56,7 @@ class FilesBrowserBloc extends InteractiveBloc implements FilesBrowserBlocEvents
), ),
), ),
(final response) => response.toWebDavFiles(client.webdav), (final response) => response.toWebDavFiles(client.webdav),
emitEmptyCache: true,
); );
} }

Loading…
Cancel
Save