Refactored DI, serialization, added feedback to document download

This commit is contained in:
Anton Stubenbord
2022-12-06 00:39:18 +01:00
parent d79682a011
commit 75fa2f7713
51 changed files with 711 additions and 366 deletions

View File

@@ -1,12 +1,14 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/labels/label_model.dart';
import 'package:paperless_api/src/models/labels/matching_algorithm.dart';
part 'correspondent_model.g.dart';
@JsonSerializable(includeIfNull: false, fieldRename: FieldRename.snake)
class Correspondent extends Label {
static const lastCorrespondenceKey = 'last_correspondence';
final DateTime? lastCorrespondence;
late DateTime? lastCorrespondence;
Correspondent({
const Correspondent({
required super.id,
required super.name,
super.slug,
@@ -17,24 +19,16 @@ class Correspondent extends Label {
this.lastCorrespondence,
});
Correspondent.fromJson(Map<String, dynamic> json)
: lastCorrespondence =
DateTime.tryParse(json[lastCorrespondenceKey] ?? ''),
super.fromJson(json);
factory Correspondent.fromJson(Map<String, dynamic> json) =>
_$CorrespondentFromJson(json);
Map<String, dynamic> toJson() => _$CorrespondentToJson(this);
@override
String toString() {
return name;
}
@override
void addSpecificFieldsToJson(Map<String, dynamic> json) {
if (lastCorrespondence != null) {
json.putIfAbsent(
lastCorrespondenceKey, () => lastCorrespondence!.toIso8601String());
}
}
@override
Correspondent copyWith({
int? id,
@@ -51,13 +45,25 @@ class Correspondent extends Label {
name: name ?? this.name,
documentCount: documentCount ?? documentCount,
isInsensitive: isInsensitive ?? isInsensitive,
lastCorrespondence: lastCorrespondence ?? this.lastCorrespondence,
match: match ?? this.match,
matchingAlgorithm: matchingAlgorithm ?? this.matchingAlgorithm,
slug: slug ?? this.slug,
lastCorrespondence: lastCorrespondence ?? this.lastCorrespondence,
);
}
@override
String get queryEndpoint => 'correspondents';
@override
List<Object?> get props => [
id,
name,
slug,
isInsensitive,
documentCount,
lastCorrespondence,
matchingAlgorithm,
match,
];
}

View File

@@ -0,0 +1,53 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'correspondent_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Correspondent _$CorrespondentFromJson(Map<String, dynamic> json) =>
Correspondent(
id: json['id'] as int?,
name: json['name'] as String,
slug: json['slug'] as String?,
match: json['match'] as String?,
matchingAlgorithm: $enumDecodeNullable(
_$MatchingAlgorithmEnumMap, json['matching_algorithm']),
isInsensitive: json['is_insensitive'] as bool?,
documentCount: json['document_count'] as int?,
lastCorrespondence: json['last_correspondence'] == null
? null
: DateTime.parse(json['last_correspondence'] as String),
);
Map<String, dynamic> _$CorrespondentToJson(Correspondent instance) {
final val = <String, dynamic>{};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('id', instance.id);
val['name'] = instance.name;
writeNotNull('slug', instance.slug);
writeNotNull('match', instance.match);
writeNotNull('matching_algorithm',
_$MatchingAlgorithmEnumMap[instance.matchingAlgorithm]);
writeNotNull('is_insensitive', instance.isInsensitive);
writeNotNull('document_count', instance.documentCount);
writeNotNull(
'last_correspondence', instance.lastCorrespondence?.toIso8601String());
return val;
}
const _$MatchingAlgorithmEnumMap = {
MatchingAlgorithm.anyWord: 1,
MatchingAlgorithm.allWords: 2,
MatchingAlgorithm.exactMatch: 3,
MatchingAlgorithm.regex: 4,
MatchingAlgorithm.similarWord: 5,
MatchingAlgorithm.auto: 6,
};

View File

@@ -1,8 +1,11 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/labels/label_model.dart';
import 'package:paperless_api/src/models/labels/matching_algorithm.dart';
part 'document_type_model.g.dart';
@JsonSerializable(includeIfNull: false, fieldRename: FieldRename.snake)
class DocumentType extends Label {
DocumentType({
const DocumentType({
required super.id,
required super.name,
super.slug,
@@ -12,10 +15,8 @@ class DocumentType extends Label {
super.documentCount,
});
DocumentType.fromJson(Map<String, dynamic> json) : super.fromJson(json);
@override
void addSpecificFieldsToJson(Map<String, dynamic> json) {}
factory DocumentType.fromJson(Map<String, dynamic> json) =>
_$DocumentTypeFromJson(json);
@override
String get queryEndpoint => 'document_types';
@@ -40,4 +41,18 @@ class DocumentType extends Label {
slug: slug ?? this.slug,
);
}
@override
Map<String, dynamic> toJson() => _$DocumentTypeToJson(this);
@override
List<Object?> get props => [
id,
name,
slug,
isInsensitive,
documentCount,
matchingAlgorithm,
match,
];
}

View File

@@ -0,0 +1,47 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'document_type_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
DocumentType _$DocumentTypeFromJson(Map<String, dynamic> json) => DocumentType(
id: json['id'] as int?,
name: json['name'] as String,
slug: json['slug'] as String?,
match: json['match'] as String?,
matchingAlgorithm: $enumDecodeNullable(
_$MatchingAlgorithmEnumMap, json['matching_algorithm']),
isInsensitive: json['is_insensitive'] as bool?,
documentCount: json['document_count'] as int?,
);
Map<String, dynamic> _$DocumentTypeToJson(DocumentType instance) {
final val = <String, dynamic>{};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('id', instance.id);
val['name'] = instance.name;
writeNotNull('slug', instance.slug);
writeNotNull('match', instance.match);
writeNotNull('matching_algorithm',
_$MatchingAlgorithmEnumMap[instance.matchingAlgorithm]);
writeNotNull('is_insensitive', instance.isInsensitive);
writeNotNull('document_count', instance.documentCount);
return val;
}
const _$MatchingAlgorithmEnumMap = {
MatchingAlgorithm.anyWord: 1,
MatchingAlgorithm.allWords: 2,
MatchingAlgorithm.exactMatch: 3,
MatchingAlgorithm.regex: 4,
MatchingAlgorithm.similarWord: 5,
MatchingAlgorithm.auto: 6,
};

View File

@@ -1,7 +1,8 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/labels/matching_algorithm.dart';
abstract class Label with EquatableMixin implements Comparable {
abstract class Label extends Equatable implements Comparable {
static const idKey = "id";
static const nameKey = "name";
static const slugKey = "slug";
@@ -11,13 +12,19 @@ abstract class Label with EquatableMixin implements Comparable {
static const documentCountKey = "document_count";
String get queryEndpoint;
@JsonKey()
final int? id;
@JsonKey()
final String name;
@JsonKey()
final String? slug;
@JsonKey()
final String? match;
@JsonKey()
final MatchingAlgorithm? matchingAlgorithm;
@JsonKey()
final bool? isInsensitive;
@JsonKey()
final int? documentCount;
const Label({
@@ -30,31 +37,6 @@ abstract class Label with EquatableMixin implements Comparable {
this.slug,
});
Label.fromJson(Map<String, dynamic> json)
: id = json[idKey],
name = json[nameKey],
slug = json[slugKey],
match = json[matchKey],
matchingAlgorithm =
MatchingAlgorithm.fromInt(json[matchingAlgorithmKey]),
isInsensitive = json[isInsensitiveKey],
documentCount = json[documentCountKey];
Map<String, dynamic> toJson() {
Map<String, dynamic> json = {};
json.putIfAbsent(idKey, () => id);
json.putIfAbsent(nameKey, () => name);
json.putIfAbsent(slugKey, () => slug);
json.putIfAbsent(matchKey, () => match);
json.putIfAbsent(matchingAlgorithmKey, () => matchingAlgorithm?.value);
json.putIfAbsent(isInsensitiveKey, () => isInsensitive);
json.putIfAbsent(documentCountKey, () => documentCount);
addSpecificFieldsToJson(json);
return json;
}
void addSpecificFieldsToJson(Map<String, dynamic> json);
Label copyWith({
int? id,
String? name,
@@ -75,6 +57,5 @@ abstract class Label with EquatableMixin implements Comparable {
return toString().toLowerCase().compareTo(other.toString().toLowerCase());
}
@override
List<Object?> get props => [id];
Map<String, dynamic> toJson();
}

View File

@@ -1,3 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
@JsonEnum(valueField: 'value')
enum MatchingAlgorithm {
anyWord(1, "Any: Match one of the following words"),
allWords(2, "All: Match all of the following words"),

View File

@@ -1,9 +1,11 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/labels/label_model.dart';
import 'package:paperless_api/src/models/labels/matching_algorithm.dart';
part 'storage_path_model.g.dart';
@JsonSerializable(includeIfNull: false, fieldRename: FieldRename.snake)
class StoragePath extends Label {
static const pathKey = 'path';
late String? path;
StoragePath({
@@ -17,23 +19,14 @@ class StoragePath extends Label {
required this.path,
});
StoragePath.fromJson(Map<String, dynamic> json)
: path = json[pathKey],
super.fromJson(json);
factory StoragePath.fromJson(Map<String, dynamic> json) =>
_$StoragePathFromJson(json);
@override
String toString() {
return name;
}
@override
void addSpecificFieldsToJson(Map<String, dynamic> json) {
json.putIfAbsent(
pathKey,
() => path,
);
}
@override
StoragePath copyWith({
int? id,
@@ -59,4 +52,19 @@ class StoragePath extends Label {
@override
String get queryEndpoint => 'storage_paths';
@override
List<Object?> get props => [
id,
name,
slug,
isInsensitive,
documentCount,
path,
matchingAlgorithm,
match,
];
@override
Map<String, dynamic> toJson() => _$StoragePathToJson(this);
}

View File

@@ -0,0 +1,49 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'storage_path_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
StoragePath _$StoragePathFromJson(Map<String, dynamic> json) => StoragePath(
id: json['id'] as int?,
name: json['name'] as String,
slug: json['slug'] as String?,
match: json['match'] as String?,
matchingAlgorithm: $enumDecodeNullable(
_$MatchingAlgorithmEnumMap, json['matching_algorithm']),
isInsensitive: json['is_insensitive'] as bool?,
documentCount: json['document_count'] as int?,
path: json['path'] as String?,
);
Map<String, dynamic> _$StoragePathToJson(StoragePath instance) {
final val = <String, dynamic>{};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('id', instance.id);
val['name'] = instance.name;
writeNotNull('slug', instance.slug);
writeNotNull('match', instance.match);
writeNotNull('matching_algorithm',
_$MatchingAlgorithmEnumMap[instance.matchingAlgorithm]);
writeNotNull('is_insensitive', instance.isInsensitive);
writeNotNull('document_count', instance.documentCount);
writeNotNull('path', instance.path);
return val;
}
const _$MatchingAlgorithmEnumMap = {
MatchingAlgorithm.anyWord: 1,
MatchingAlgorithm.allWords: 2,
MatchingAlgorithm.exactMatch: 3,
MatchingAlgorithm.regex: 4,
MatchingAlgorithm.similarWord: 5,
MatchingAlgorithm.auto: 6,
};

View File

@@ -1,6 +1,7 @@
import 'dart:developer';
import 'dart:ui';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_api/src/models/labels/label_model.dart';
import 'package:paperless_api/src/models/labels/matching_algorithm.dart';
@@ -10,11 +11,17 @@ class Tag extends Label {
static const textColorKey = 'text_color';
static const legacyColourKey = 'colour';
final Color? color;
final Color? _apiV2color;
final Color? _apiV1color;
final Color? textColor;
final bool? isInboxTag;
Tag({
Color? get color => _apiV2color ?? _apiV1color;
const Tag({
required super.id,
required super.name,
super.documentCount,
@@ -22,42 +29,17 @@ class Tag extends Label {
super.match,
super.matchingAlgorithm,
super.slug,
this.color,
Color? color,
this.textColor,
this.isInboxTag,
});
Tag.fromJson(Map<String, dynamic> json)
: isInboxTag = json[isInboxTagKey],
textColor = Color(_colorStringToInt(json[textColorKey]) ?? 0),
color = _parseColorFromJson(json),
super.fromJson(json);
///
/// The `color` field of the json object can either be of type [Color] or a hex [String].
/// Since API version 2, the old attribute `colour` has been replaced with `color`.
///
static Color _parseColorFromJson(Map<String, dynamic> json) {
if (json.containsKey(legacyColourKey)) {
return Color(_colorStringToInt(json[legacyColourKey]) ?? 0);
}
if (json[colorKey] is Color) {
return json[colorKey];
}
return Color(_colorStringToInt(json[colorKey]) ?? 0);
}
}) : _apiV1color = color,
_apiV2color = color;
@override
String toString() {
return name;
}
@override
void addSpecificFieldsToJson(Map<String, dynamic> json) {
json.putIfAbsent(colorKey, () => _toHex(color));
json.putIfAbsent(isInboxTagKey, () => isInboxTag);
}
@override
Tag copyWith({
int? id,
@@ -87,22 +69,103 @@ class Tag extends Label {
@override
String get queryEndpoint => 'tags';
}
///
/// Taken from [FormBuilderColorPicker].
///
String? _toHex(Color? color) {
if (color == null) {
@override
List<Object?> get props => [
id,
name,
slug,
isInsensitive,
documentCount,
matchingAlgorithm,
color,
textColor,
isInboxTag,
match,
];
factory Tag.fromJson(Map<String, dynamic> json) {
const $MatchingAlgorithmEnumMap = {
MatchingAlgorithm.anyWord: 1,
MatchingAlgorithm.allWords: 2,
MatchingAlgorithm.exactMatch: 3,
MatchingAlgorithm.regex: 4,
MatchingAlgorithm.similarWord: 5,
MatchingAlgorithm.auto: 6,
};
return Tag(
id: json['id'] as int?,
name: json['name'] as String,
documentCount: json['document_count'] as int?,
isInsensitive: json['is_insensitive'] as bool?,
match: json['match'] as String?,
matchingAlgorithm: $enumDecodeNullable(
$MatchingAlgorithmEnumMap, json['matching_algorithm']),
slug: json['slug'] as String?,
textColor: _colorFromJson(json['text_color']),
isInboxTag: json['is_inbox_tag'] as bool?,
color: _colorFromJson(json['color']) ?? _colorFromJson(json['colour']),
);
}
@override
Map<String, dynamic> toJson() {
final val = <String, dynamic>{};
const $MatchingAlgorithmEnumMap = {
MatchingAlgorithm.anyWord: 1,
MatchingAlgorithm.allWords: 2,
MatchingAlgorithm.exactMatch: 3,
MatchingAlgorithm.regex: 4,
MatchingAlgorithm.similarWord: 5,
MatchingAlgorithm.auto: 6,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('id', id);
val['name'] = name;
writeNotNull('slug', slug);
writeNotNull('match', match);
writeNotNull(
'matching_algorithm', $MatchingAlgorithmEnumMap[matchingAlgorithm]);
writeNotNull('is_insensitive', isInsensitive);
writeNotNull('document_count', documentCount);
writeNotNull('color', _toHex(_apiV2color));
writeNotNull('colour', _toHex(_apiV1color));
writeNotNull('text_color', _toHex(textColor));
writeNotNull('is_inbox_tag', isInboxTag);
return val;
}
static Color? _colorFromJson(dynamic color) {
if (color is Color) {
return color;
}
if (color is String) {
final decoded = int.tryParse(color.replaceAll("#", "ff"), radix: 16);
if (decoded == null) {
return null;
}
return Color(decoded);
}
return null;
}
String val =
'#${(color.value & 0xFFFFFF).toRadixString(16).padLeft(6, '0').toLowerCase()}';
log("Color in Tag#_toHex is $val");
return val;
}
int? _colorStringToInt(String? color) {
if (color == null) return null;
return int.tryParse(color.replaceAll("#", "ff"), radix: 16);
///
/// Taken from [FormBuilderColorPicker].
///
static String? _toHex(Color? color) {
if (color == null) {
return null;
}
String val =
'#${(color.value & 0xFFFFFF).toRadixString(16).padLeft(6, '0').toLowerCase()}';
return val;
}
}

View File

@@ -2,6 +2,5 @@ abstract class PaperlessAuthenticationApi {
Future<String> login({
required String username,
required String password,
required String serverUrl,
});
}

View File

@@ -14,13 +14,15 @@ class PaperlessAuthenticationApiImpl implements PaperlessAuthenticationApi {
Future<String> login({
required String username,
required String password,
required String serverUrl,
}) async {
late Response response;
try {
response = await client.post(
Uri.parse("/api/token/"),
body: {"username": username, "password": password},
body: {
"username": username,
"password": password,
},
);
} on FormatException catch (e) {
final source = e.source;