Changed file directories, added clear storage setting, updated readme w/ Google Play reference

This commit is contained in:
Anton Stubenbord
2022-11-12 20:48:09 +01:00
parent c4104e75a7
commit afbd4bddb4
21 changed files with 380 additions and 135 deletions

View File

@@ -25,7 +25,8 @@ class GlobalErrorCubit extends Cubit<GlobalErrorState> {
bool _canEmitNewError() {
if (state.errorTimestamp != null) {
return DateTime.now().difference(state.errorTimestamp!).inSeconds >= 5;
return DateTime.now().difference(state.errorTimestamp!) >=
_waitBeforeNextErrorDuration;
}
return true;
}

View File

@@ -35,8 +35,12 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
Future<Response> delete(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>()
@@ -50,7 +54,10 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> get(Uri url, {Map<String, String>? headers}) async {
Future<Response> get(
Uri url, {
Map<String, String>? headers,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>().get(url, headers: headers).timeout(
@@ -62,7 +69,10 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> head(Uri url, {Map<String, String>? headers}) async {
Future<Response> head(
Uri url, {
Map<String, String>? headers,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>().head(url, headers: headers).timeout(
@@ -74,8 +84,12 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
Future<Response> patch(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>()
@@ -89,8 +103,12 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
Future<Response> post(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>()
@@ -104,8 +122,12 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
Future<Response> put(
Uri url, {
Map<String, String>? headers,
Object? body,
Encoding? encoding,
}) async {
await _handleOfflineState();
return _handle400Error(
await getIt<BaseClient>()
@@ -119,7 +141,10 @@ class TimeoutClient implements BaseClient {
}
@override
Future<String> read(Uri url, {Map<String, String>? headers}) async {
Future<String> read(
Uri url, {
Map<String, String>? headers,
}) async {
await _handleOfflineState();
return getIt<BaseClient>().read(url, headers: headers).timeout(
requestTimeout,
@@ -129,7 +154,10 @@ class TimeoutClient implements BaseClient {
}
@override
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) async {
Future<Uint8List> readBytes(
Uri url, {
Map<String, String>? headers,
}) async {
await _handleOfflineState();
return getIt<BaseClient>().readBytes(url, headers: headers).timeout(
requestTimeout,

View File

@@ -1,9 +1,11 @@
class ErrorMessage implements Exception {
final ErrorCode code;
final String? details;
final StackTrace? stackTrace;
final int? httpStatusCode;
const ErrorMessage(this.code, {this.stackTrace, this.httpStatusCode});
const ErrorMessage(this.code,
{this.details, this.stackTrace, this.httpStatusCode});
factory ErrorMessage.unknown() {
return const ErrorMessage(ErrorCode.unknown);

View File

@@ -0,0 +1,95 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
class FileService {
static Future<File> saveToFile(
Uint8List bytes,
String filename,
) async {
final dir = await documentsDirectory;
if (dir == null) {
throw ErrorMessage.unknown(); //TODO: better handling
}
File file = File("${dir.path}/$filename");
return file..writeAsBytes(bytes);
}
static Future<Directory?> getDirectory(PaperlessDirectoryType type) {
switch (type) {
case PaperlessDirectoryType.documents:
return documentsDirectory;
case PaperlessDirectoryType.temporary:
return temporaryDirectory;
case PaperlessDirectoryType.scans:
return scanDirectory;
case PaperlessDirectoryType.download:
return downloadsDirectory;
}
}
static Future<File> allocateTemporaryFile(
PaperlessDirectoryType type, {
required String extension,
String? fileName,
}) async {
final dir = await getDirectory(type);
final _fileName = (fileName ?? const Uuid().v1()) + '.$extension';
return File('${dir?.path}/$_fileName');
}
static Future<Directory> get temporaryDirectory => getTemporaryDirectory();
static Future<Directory?> get documentsDirectory async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectories(
type: StorageDirectory.documents,
))!
.first;
} else if (Platform.isIOS) {
return getApplicationDocumentsDirectory();
} else {
throw UnsupportedError("Platform not supported.");
}
}
static Future<Directory?> get downloadsDirectory async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectories(
type: StorageDirectory.downloads))!
.first;
} else if (Platform.isIOS) {
return getApplicationDocumentsDirectory();
} else {
throw UnsupportedError("Platform not supported.");
}
}
static Future<Directory?> get scanDirectory async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectories(type: StorageDirectory.dcim))!
.first;
} else if (Platform.isIOS) {
return getApplicationDocumentsDirectory();
} else {
throw UnsupportedError("Platform not supported.");
}
}
static Future<void> clearUserData() async {
final scanDir = await scanDirectory;
final tempDir = await temporaryDirectory;
scanDir?.delete(recursive: true);
tempDir.delete(recursive: true);
}
}
enum PaperlessDirectoryType {
documents,
temporary,
scans,
download;
}

View File

@@ -54,16 +54,3 @@ Future<List<T>> getCollection<T>(
}
return Future.error(errorCode);
}
class FileUtils {
static Future<File> saveToFile(
Uint8List bytes,
String filename, {
StorageDirectory directoryType = StorageDirectory.documents,
}) async {
final dir = (await getExternalStorageDirectories(type: directoryType));
File file = File("$dir/$filename");
file.writeAsBytesSync(bytes);
return file;
}
}