better loading indicators for timetables, talk and files
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
class ApiError {
|
||||
class ApiError implements Exception {
|
||||
String message;
|
||||
|
||||
ApiError(this.message);
|
||||
|
||||
@@ -20,8 +20,9 @@ class AutocompleteApi {
|
||||
var headers = <String, String>{};
|
||||
headers.putIfAbsent('Accept', () => 'application/json');
|
||||
headers.putIfAbsent('OCS-APIRequest', () => 'true');
|
||||
headers.putIfAbsent('Authorization', AccountData().getBasicAuthHeader);
|
||||
|
||||
var endpoint = Uri.https('${AccountData().buildHttpAuthString()}@${EndpointData().nextcloud().domain}', '${EndpointData().nextcloud().path}/ocs/v2.php/core/autocomplete/get', getParameters);
|
||||
var endpoint = Uri.https(EndpointData().nextcloud().domain, '${EndpointData().nextcloud().path}/ocs/v2.php/core/autocomplete/get', getParameters);
|
||||
|
||||
var response = await http.get(endpoint, headers: headers);
|
||||
if(response.statusCode != HttpStatus.ok) throw Exception('Api call failed with ${response.statusCode}: ${response.body}');
|
||||
|
||||
@@ -11,8 +11,9 @@ class FileSharingApi {
|
||||
var headers = <String, String>{};
|
||||
headers.putIfAbsent('Accept', () => 'application/json');
|
||||
headers.putIfAbsent('OCS-APIRequest', () => 'true');
|
||||
headers.putIfAbsent('Authorization', AccountData().getBasicAuthHeader);
|
||||
|
||||
var endpoint = Uri.https('${AccountData().buildHttpAuthString()}@${EndpointData().nextcloud().domain}', '${EndpointData().nextcloud().path}/ocs/v2.php/apps/files_sharing/api/v1/shares', query.toJson().map((key, value) => MapEntry(key, value.toString())));
|
||||
var endpoint = Uri.https(EndpointData().nextcloud().domain, '${EndpointData().nextcloud().path}/ocs/v2.php/apps/files_sharing/api/v1/shares', query.toJson().map((key, value) => MapEntry(key, value.toString())));
|
||||
var response = await http.post(endpoint, headers: headers);
|
||||
|
||||
if(response.statusCode != HttpStatus.ok) {
|
||||
|
||||
@@ -8,7 +8,15 @@ import 'getChatResponse.dart';
|
||||
class GetChatCache extends RequestCache<GetChatResponse> {
|
||||
String chatToken;
|
||||
|
||||
GetChatCache({required void Function(GetChatResponse) onUpdate, required this.chatToken}) : super(RequestCache.cacheNothing, onUpdate) {
|
||||
GetChatCache({
|
||||
required void Function(GetChatResponse) onUpdate,
|
||||
void Function(Exception)? onError,
|
||||
required this.chatToken,
|
||||
}) : super(
|
||||
RequestCache.cacheNothing,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
) {
|
||||
start('nc-chat-$chatToken');
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,16 @@ import 'getRoomParams.dart';
|
||||
import 'getRoomResponse.dart';
|
||||
|
||||
class GetRoomCache extends RequestCache<GetRoomResponse> {
|
||||
GetRoomCache({void Function(GetRoomResponse)? onUpdate, bool? renew}) : super(RequestCache.cacheMinute, onUpdate, renew: renew) {
|
||||
GetRoomCache({
|
||||
void Function(GetRoomResponse)? onUpdate,
|
||||
void Function(Exception)? onError,
|
||||
bool? renew,
|
||||
}) : super(
|
||||
RequestCache.cacheMinute,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
renew: renew,
|
||||
) {
|
||||
start('nc-rooms');
|
||||
}
|
||||
|
||||
|
||||
@@ -34,11 +34,12 @@ abstract class TalkApi<T extends ApiResponse?> extends ApiRequest {
|
||||
getParameters?.update(key, (value) => value.toString());
|
||||
});
|
||||
|
||||
var endpoint = Uri.https('${AccountData().buildHttpAuthString()}@${EndpointData().nextcloud().domain}', '${EndpointData().nextcloud().path}/ocs/v2.php/apps/spreed/api/$path', getParameters);
|
||||
var endpoint = Uri.https(EndpointData().nextcloud().domain, '${EndpointData().nextcloud().path}/ocs/v2.php/apps/spreed/api/$path', getParameters);
|
||||
|
||||
headers ??= {};
|
||||
headers?.putIfAbsent('Accept', () => 'application/json');
|
||||
headers?.putIfAbsent('OCS-APIRequest', () => 'true');
|
||||
headers?.putIfAbsent('Authorization', AccountData().getBasicAuthHeader);
|
||||
|
||||
http.Response? data;
|
||||
|
||||
|
||||
@@ -11,9 +11,22 @@ class ListFiles extends WebdavApi<ListFilesParams> {
|
||||
|
||||
ListFiles(this.params) : super(params);
|
||||
|
||||
// The Nextcloud root listing is significantly slower than subdirectories on
|
||||
// our instance, so it gets a much longer ceiling. Subfolders fall back to a
|
||||
// tighter timeout to keep the UI responsive.
|
||||
static const Duration _rootTimeout = Duration(minutes: 3);
|
||||
static const Duration _subfolderTimeout = Duration(seconds: 30);
|
||||
|
||||
bool get _isRoot {
|
||||
final p = params.path.replaceAll('/', '').trim();
|
||||
return p.isEmpty;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ListFilesResponse> run() async {
|
||||
var davFiles = (await (await WebdavApi.webdav).propfind(PathUri.parse(params.path))).toWebDavFiles();
|
||||
final webdav = await WebdavApi.webdav;
|
||||
final timeout = _isRoot ? _rootTimeout : _subfolderTimeout;
|
||||
final davFiles = (await webdav.propfind(PathUri.parse(params.path)).timeout(timeout)).toWebDavFiles();
|
||||
var files = davFiles.map(CacheableFile.fromDavFile).toSet();
|
||||
|
||||
// webdav handles subdirectories wrong, this is a fix
|
||||
|
||||
@@ -9,7 +9,19 @@ import 'listFilesResponse.dart';
|
||||
class ListFilesCache extends RequestCache<ListFilesResponse> {
|
||||
String path;
|
||||
|
||||
ListFilesCache({required void Function(ListFilesResponse) onUpdate, required this.path}) : super(RequestCache.cacheNothing, onUpdate) {
|
||||
ListFilesCache({
|
||||
required void Function(ListFilesResponse) onUpdate,
|
||||
void Function(ListFilesResponse)? onCacheData,
|
||||
void Function(ListFilesResponse)? onNetworkData,
|
||||
void Function(Exception)? onError,
|
||||
required this.path,
|
||||
}) : super(
|
||||
RequestCache.cacheNothing,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
onCacheData: onCacheData,
|
||||
onNetworkData: onNetworkData,
|
||||
) {
|
||||
var bytes = utf8.encode('MarianumMobile-$path');
|
||||
var cacheName = md5.convert(bytes).toString();
|
||||
start('wd-folder-$cacheName');
|
||||
|
||||
@@ -15,9 +15,11 @@ abstract class WebdavApi<T> extends ApiRequest {
|
||||
Future<ApiResponse> run();
|
||||
|
||||
static Future<WebDavClient> webdav = establishWebdavConnection();
|
||||
static Future<String> webdavConnectString = buildWebdavConnectString();
|
||||
|
||||
static Future<WebDavClient> establishWebdavConnection() async => NextcloudClient(Uri.parse('https://${EndpointData().nextcloud().full()}'), password: AccountData().getPassword(), loginName: AccountData().getUsername()).webdav;
|
||||
|
||||
static Future<String> buildWebdavConnectString() async => 'https://${AccountData().buildHttpAuthString()}@${EndpointData().nextcloud().full()}/remote.php/dav/files/${AccountData().getUsername()}/';
|
||||
/// Builds the WebDAV download URL without embedded credentials. Callers must
|
||||
/// authenticate via the [AccountData.authHeaders] header instead.
|
||||
static String buildWebdavUrl() =>
|
||||
'https://${EndpointData().nextcloud().full()}/remote.php/dav/files/${AccountData().getUsername()}/';
|
||||
}
|
||||
|
||||
@@ -15,6 +15,15 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
|
||||
int maxCacheTime;
|
||||
void Function(T)? onUpdate;
|
||||
|
||||
/// Called only when [start] finds a cached payload in localstore. Use this
|
||||
/// (instead of [onUpdate]) when callers need to distinguish stale-but-fast
|
||||
/// cache hits from authoritative network responses.
|
||||
void Function(T)? onCacheData;
|
||||
|
||||
/// Called only when [start] receives a fresh payload from the network.
|
||||
void Function(T)? onNetworkData;
|
||||
|
||||
void Function(Exception) onError;
|
||||
bool? renew;
|
||||
|
||||
@@ -26,7 +35,14 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
/// attempt have settled.
|
||||
Future<void> get ready => _ready.future;
|
||||
|
||||
RequestCache(this.maxCacheTime, this.onUpdate, {this.onError = ignore, this.renew = false});
|
||||
RequestCache(
|
||||
this.maxCacheTime,
|
||||
this.onUpdate, {
|
||||
this.onError = ignore,
|
||||
this.renew = false,
|
||||
this.onCacheData,
|
||||
this.onNetworkData,
|
||||
});
|
||||
|
||||
static void ignore(Exception e) {}
|
||||
|
||||
@@ -34,7 +50,9 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
try {
|
||||
final tableData = await Localstore.instance.collection(collection).doc(document).get();
|
||||
if (tableData != null) {
|
||||
onUpdate?.call(onLocalData(tableData['json']));
|
||||
final cached = onLocalData(tableData['json']);
|
||||
onUpdate?.call(cached);
|
||||
onCacheData?.call(cached);
|
||||
}
|
||||
|
||||
if (DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < (tableData?['lastupdate'] ?? 0)) {
|
||||
@@ -44,6 +62,7 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
try {
|
||||
final newValue = await onLoad();
|
||||
onUpdate?.call(newValue);
|
||||
onNetworkData?.call(newValue);
|
||||
Localstore.instance.collection(collection).doc(document).set({
|
||||
'json': jsonEncode(newValue),
|
||||
'lastupdate': DateTime.now().millisecondsSinceEpoch,
|
||||
|
||||
Reference in New Issue
Block a user