feat: Rework error handling, upgrade dio, fixed bugs

- Fix grey screen bug when adding labels from documnet upload
- Add more permission checks to conditionally show widgets
This commit is contained in:
Anton Stubenbord
2023-07-22 14:17:48 +02:00
parent c4f2810974
commit 6566b2b8d7
70 changed files with 1446 additions and 1133 deletions

View File

@@ -2,9 +2,9 @@ import 'dart:convert';
import 'dart:math';
import 'package:flutter/services.dart';
import 'package:paperless_mobile/core/type/types.dart';
Future<T> loadOne<T>(String filePath, T Function(JSON) transformFn, int? id) async {
Future<T> loadOne<T>(String filePath,
T Function(Map<String, dynamic>) transformFn, int? id) async {
if (id != null) {
final coll = await loadCollection(filePath, transformFn);
return coll.firstWhere((dynamic element) => element.id == id);
@@ -13,22 +13,27 @@ Future<T> loadOne<T>(String filePath, T Function(JSON) transformFn, int? id) asy
return transformFn(jsonDecode(response));
}
Future<List<T>> loadCollection<T>(String filePath, T Function(JSON) transformFn,
Future<List<T>> loadCollection<T>(
String filePath, T Function(Map<String, dynamic>) transformFn,
{int? numItems, List<int>? ids}) async {
assert(((numItems != null) ^ (ids != null)) || (numItems == null && ids == null));
assert(((numItems != null) ^ (ids != null)) ||
(numItems == null && ids == null));
final String response = await rootBundle.loadString(filePath);
final lst = (jsonDecode(response) as List<dynamic>);
final res = (jsonDecode(response) as List<dynamic>).map((e) => transformFn(e)).toList();
final res = (jsonDecode(response) as List<dynamic>)
.map((e) => transformFn(e))
.toList();
if (ids != null) {
return res.where((dynamic element) => ids.contains(element.id)).toList();
}
if (numItems != null && lst.length < numItems) {
throw Exception("The requested collection contains only ${lst.length} items!");
throw Exception(
"The requested collection contains only ${lst.length} items!");
} else {
return res.sublist(0, numItems);
}
}
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
String getRandomString(int length) => String.fromCharCodes(
Iterable.generate(length, (_) => _chars.codeUnitAt(Random().nextInt(_chars.length))));
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(Random().nextInt(_chars.length))));