mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-08 16:07:52 -06:00
Updated onboarding, reformatted files, improved referenced documents view, updated error handling
This commit is contained in:
@@ -3,7 +3,8 @@ import 'dart:typed_data';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:paperless_mobile/core/type/json.dart';
|
||||
import 'package:paperless_mobile/core/service/connectivity_status.service.dart';
|
||||
import 'package:paperless_mobile/core/type/types.dart';
|
||||
import 'package:paperless_mobile/di_initializer.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
@@ -14,13 +15,17 @@ import 'package:injectable/injectable.dart';
|
||||
@Injectable(as: BaseClient)
|
||||
@Named("timeoutClient")
|
||||
class TimeoutClient implements BaseClient {
|
||||
final ConnectivityStatusService connectivityStatusService;
|
||||
static const Duration requestTimeout = Duration(seconds: 25);
|
||||
|
||||
TimeoutClient(this.connectivityStatusService);
|
||||
|
||||
@override
|
||||
Future<StreamedResponse> send(BaseRequest request) async {
|
||||
return getIt<BaseClient>().send(request).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -32,32 +37,38 @@ class TimeoutClient implements BaseClient {
|
||||
@override
|
||||
Future<Response> delete(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>()
|
||||
.delete(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> get(Uri url, {Map<String, String>? headers}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().get(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> head(Uri url, {Map<String, String>? headers}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().head(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -65,12 +76,14 @@ class TimeoutClient implements BaseClient {
|
||||
@override
|
||||
Future<Response> patch(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>()
|
||||
.patch(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -78,10 +91,14 @@ class TimeoutClient implements BaseClient {
|
||||
@override
|
||||
Future<Response> post(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().post(url, headers: headers, body: body, encoding: encoding).timeout(
|
||||
await getIt<BaseClient>()
|
||||
.post(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -89,27 +106,35 @@ class TimeoutClient implements BaseClient {
|
||||
@override
|
||||
Future<Response> put(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
await _handleOfflineState();
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().put(url, headers: headers, body: body, encoding: encoding).timeout(
|
||||
await getIt<BaseClient>()
|
||||
.put(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> read(Uri url, {Map<String, String>? headers}) async {
|
||||
await _handleOfflineState();
|
||||
return getIt<BaseClient>().read(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) {
|
||||
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) async {
|
||||
await _handleOfflineState();
|
||||
return getIt<BaseClient>().readBytes(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
onTimeout: () =>
|
||||
Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -117,11 +142,12 @@ class TimeoutClient implements BaseClient {
|
||||
if (response.statusCode == 400) {
|
||||
// try to parse contained error message, otherwise return response
|
||||
final JSON json = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
final Map<String, String> errorMessages = {};
|
||||
final PaperlessValidationErrors errorMessages = {};
|
||||
//TODO: This could be simplified, look at error message format of paperless-ngx
|
||||
for (final entry in json.entries) {
|
||||
if (entry.value is List) {
|
||||
errorMessages.putIfAbsent(entry.key, () => (entry.value as List).cast<String>().first);
|
||||
errorMessages.putIfAbsent(
|
||||
entry.key, () => (entry.value as List).cast<String>().first);
|
||||
} else if (entry.value is String) {
|
||||
errorMessages.putIfAbsent(entry.key, () => entry.value);
|
||||
} else {
|
||||
@@ -132,4 +158,10 @@ class TimeoutClient implements BaseClient {
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<void> _handleOfflineState() async {
|
||||
if (!(await connectivityStatusService.isConnectedToInternet())) {
|
||||
throw const ErrorMessage(ErrorCode.deviceOffline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user