Skip to content

Commit

Permalink
Add migration scripts, remove dependencies to codegen, and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
astubenbord committed Oct 6, 2023
1 parent 29b7c8b commit 5dcae3f
Show file tree
Hide file tree
Showing 32 changed files with 734 additions and 579 deletions.
8 changes: 8 additions & 0 deletions lib/core/config/hive/hive_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class HiveBoxes {
static const localUserAccount = 'localUserAccount';
static const localUserAppState = 'localUserAppState';
static const hosts = 'hosts';

static List<String> get all => [
globalSettings,
localUserCredentials,
localUserAccount,
localUserAppState,
hosts,
];
}

class HiveTypeIds {
Expand Down
24 changes: 9 additions & 15 deletions lib/core/repository/label_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,13 @@ class LabelRepository extends PersistentRepository<LabelRepositoryState> {
return updated;
}

@override
Future<void> clear() async {
await super.clear();
emit(const LabelRepositoryState());
}

@override
LabelRepositoryState? fromJson(Map<String, dynamic> json) {
return LabelRepositoryState.fromJson(json);
}

@override
Map<String, dynamic>? toJson(LabelRepositoryState state) {
return state.toJson();
}
// @override
// LabelRepositoryState? fromJson(Map<String, dynamic> json) {
// return LabelRepositoryState.fromJson(json);
// }

// @override
// Map<String, dynamic>? toJson(LabelRepositoryState state) {
// return state.toJson();
// }
}
2 changes: 1 addition & 1 deletion lib/core/repository/persistent_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:hydrated_bloc/hydrated_bloc.dart';

abstract class PersistentRepository<T> extends HydratedCubit<T> {
abstract class PersistentRepository<T> extends Cubit<T> {
final Map<Object, StreamSubscription> _subscribers = {};

PersistentRepository(T initialState) : super(initialState);
Expand Down
28 changes: 14 additions & 14 deletions lib/core/repository/saved_view_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ class SavedViewRepository
return found;
}

@override
Future<void> clear() async {
await _initialized.future;
await super.clear();
emit(const SavedViewRepositoryState.initial());
}
// @override
// Future<void> clear() async {
// await _initialized.future;
// await super.clear();
// emit(const SavedViewRepositoryState.initial());
// }

@override
SavedViewRepositoryState? fromJson(Map<String, dynamic> json) {
return SavedViewRepositoryState.fromJson(json);
}
// @override
// SavedViewRepositoryState? fromJson(Map<String, dynamic> json) {
// return SavedViewRepositoryState.fromJson(json);
// }

@override
Map<String, dynamic>? toJson(SavedViewRepositoryState state) {
return state.toJson();
}
// @override
// Map<String, dynamic>? toJson(SavedViewRepositoryState state) {
// return state.toJson();
// }
}
2 changes: 2 additions & 0 deletions lib/core/repository/saved_view_repository_state.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
part of 'saved_view_repository.dart';



@freezed
class SavedViewRepositoryState with _$SavedViewRepositoryState {
const factory SavedViewRepositoryState.initial({
Expand Down
19 changes: 9 additions & 10 deletions lib/core/repository/user_repository.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:equatable/equatable.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/repository/persistent_repository.dart';

part 'user_repository_state.dart';
part 'user_repository.freezed.dart';
part 'user_repository.g.dart';

/// Repository for new users (API v3, server version 1.14.2+)
class UserRepository extends PersistentRepository<UserRepositoryState> {
Expand All @@ -28,13 +27,13 @@ class UserRepository extends PersistentRepository<UserRepositoryState> {
return user;
}

@override
UserRepositoryState? fromJson(Map<String, dynamic> json) {
return UserRepositoryState.fromJson(json);
}
// @override
// UserRepositoryState? fromJson(Map<String, dynamic> json) {
// return UserRepositoryState.fromJson(json);
// }

@override
Map<String, dynamic>? toJson(UserRepositoryState state) {
return state.toJson();
}
// @override
// Map<String, dynamic>? toJson(UserRepositoryState state) {
// return state.toJson();
// }
}
22 changes: 15 additions & 7 deletions lib/core/repository/user_repository_state.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
part of 'user_repository.dart';

@freezed
class UserRepositoryState with _$UserRepositoryState {
const factory UserRepositoryState({
@Default({}) Map<int, UserModel> users,
}) = _UserRepositoryState;
class UserRepositoryState with EquatableMixin {
final Map<int, UserModel> users;
const UserRepositoryState({
this.users = const {},
});

factory UserRepositoryState.fromJson(Map<String, dynamic> json) =>
_$UserRepositoryStateFromJson(json);
UserRepositoryState copyWith({
Map<int, UserModel>? users,
}) {
return UserRepositoryState(
users: users ?? this.users,
);
}

@override
List<Object?> get props => [users];
}
86 changes: 54 additions & 32 deletions lib/features/document_edit/view/document_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
.watch<DocumentEditCubit>()
.state
.correspondents,
initialValue:
state.document.correspondent != null
? IdQueryParameter.fromId(
state.document.correspondent!)
: const IdQueryParameter.unset(),
initialValue: state
.document.correspondent !=
null
? SetIdQueryParameter(
id: state.document.correspondent!)
: const UnsetIdQueryParameter(),
name: fkCorrespondent,
prefixIcon:
const Icon(Icons.person_outlined),
Expand All @@ -135,7 +136,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
_formKey.currentState
?.fields[fkCorrespondent]
?.didChange(
IdQueryParameter.fromId(itemData),
SetIdQueryParameter(id: itemData),
);
},
),
Expand All @@ -161,11 +162,11 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
addLabelText:
S.of(context)!.addDocumentType,
labelText: S.of(context)!.documentType,
initialValue:
state.document.documentType != null
? IdQueryParameter.fromId(
state.document.documentType!)
: const IdQueryParameter.unset(),
initialValue: state.document.documentType !=
null
? SetIdQueryParameter(
id: state.document.documentType!)
: const UnsetIdQueryParameter(),
options: state.documentTypes,
name: _DocumentEditPageState.fkDocumentType,
prefixIcon:
Expand All @@ -185,7 +186,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
onPressed: () => _formKey.currentState
?.fields[fkDocumentType]
?.didChange(
IdQueryParameter.fromId(itemData),
SetIdQueryParameter(id: itemData),
),
),
),
Expand All @@ -211,9 +212,9 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
options: state.storagePaths,
initialValue:
state.document.storagePath != null
? IdQueryParameter.fromId(
state.document.storagePath!)
: const IdQueryParameter.unset(),
? SetIdQueryParameter(
id: state.document.storagePath!)
: const UnsetIdQueryParameter(),
name: fkStoragePath,
prefixIcon:
const Icon(Icons.folder_outlined),
Expand All @@ -229,7 +230,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
allowOnlySelection: true,
allowCreation: true,
allowExclude: false,
initialValue: TagsQuery.ids(
initialValue: IdsTagsQuery(
include: state.document.tags.toList(),
),
).padded(),
Expand All @@ -254,15 +255,17 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
?.fields[fkTags]?.value as TagsQuery;
_formKey.currentState?.fields[fkTags]
?.didChange(
currentTags.maybeWhen(
ids: (include, exclude) =>
TagsQuery.ids(include: [
...include,
itemData
], exclude: exclude),
orElse: () => TagsQuery.ids(
include: [itemData]),
),
switch (currentTags) {
IdsTagsQuery(
include: var i,
exclude: var e
) =>
IdsTagsQuery(
include: [...i, itemData],
exclude: e,
),
_ => IdsTagsQuery(include: [itemData])
},
);
},
);
Expand Down Expand Up @@ -301,16 +304,35 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
Future<void> _onSubmit(DocumentModel document) async {
if (_formKey.currentState?.saveAndValidate() ?? false) {
final values = _formKey.currentState!.value;

final correspondentParam = values[fkCorrespondent] as IdQueryParameter?;
final documentTypeParam = values[fkDocumentType] as IdQueryParameter?;
final storagePathParam = values[fkStoragePath] as IdQueryParameter?;
final tagsParam = values[fkTags] as TagsQuery?;

final correspondent = switch (correspondentParam) {
SetIdQueryParameter(id: var id) => id,
_ => null,
};
final documentType = switch (documentTypeParam) {
SetIdQueryParameter(id: var id) => id,
_ => null,
};
final storagePath = switch (storagePathParam) {
SetIdQueryParameter(id: var id) => id,
_ => null,
};
final tags = switch (tagsParam) {
IdsTagsQuery(include: var i) => i,
_ => null,
};
var mergedDocument = document.copyWith(
title: values[fkTitle],
created: values[fkCreatedDate],
documentType: () => (values[fkDocumentType] as IdQueryParameter?)
?.whenOrNull(fromId: (id) => id),
correspondent: () => (values[fkCorrespondent] as IdQueryParameter?)
?.whenOrNull(fromId: (id) => id),
storagePath: () => (values[fkStoragePath] as IdQueryParameter?)
?.whenOrNull(fromId: (id) => id),
tags: (values[fkTags] as IdsTagsQuery?)?.include,
correspondent: () => correspondent,
documentType: () => documentType,
storagePath: () => storagePath,
tags: tags,
content: values[fkContent],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,29 @@ class _DocumentUploadPreparationPageState
final cubit = context.read<DocumentUploadCubit>();
try {
setState(() => _isUploadLoading = true);
final formValues = _formKey.currentState!.value;

final fv = _formKey.currentState!.value;
final correspondentParam =
formValues[DocumentModel.correspondentKey] as IdQueryParameter?;
final docTypeParam =
formValues[DocumentModel.documentTypeKey] as IdQueryParameter?;
final tagsParam = formValues[DocumentModel.tagsKey] as TagsQuery?;
final createdAt = formValues[DocumentModel.createdKey] as DateTime?;
final title = formValues[DocumentModel.titleKey] as String;
final correspondent = switch (correspondentParam) {
SetIdQueryParameter(id: var id) => id,
_ => null,
};
final docType = switch (docTypeParam) {
SetIdQueryParameter(id: var id) => id,
_ => null,
};
final tags = switch (tagsParam) {
IdsTagsQuery(include: var ids) => ids,
_ => const <int>[],
};

final createdAt = fv[DocumentModel.createdKey] as DateTime?;
final title = fv[DocumentModel.titleKey] as String;
final docType = (fv[DocumentModel.documentTypeKey] as IdQueryParameter?)
?.whenOrNull(fromId: (id) => id);
final tags = (fv[DocumentModel.tagsKey] as TagsQuery?)
?.whenOrNull(ids: (include, exclude) => include) ??
[];
final correspondent =
(fv[DocumentModel.correspondentKey] as IdQueryParameter?)
?.whenOrNull(fromId: (id) => id);
final asn = fv[DocumentModel.asnKey] as int?;
final asn = formValues[DocumentModel.asnKey] as int?;
final taskId = await cubit.upload(
await widget.fileBytes,
filename: _padWithExtension(
Expand Down
19 changes: 9 additions & 10 deletions lib/features/documents/cubit/documents_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import 'package:paperless_mobile/features/paged_document_view/cubit/document_pag
import 'package:paperless_mobile/features/paged_document_view/cubit/paged_documents_state.dart';
import 'package:paperless_mobile/features/settings/model/view_type.dart';

part 'documents_cubit.g.dart';
part 'documents_state.dart';

class DocumentsCubit extends HydratedCubit<DocumentsState>
class DocumentsCubit extends Cubit<DocumentsState>
with DocumentPagingBlocMixin {
@override
final PaperlessDocumentsApi api;
Expand Down Expand Up @@ -135,13 +134,13 @@ class DocumentsCubit extends HydratedCubit<DocumentsState>
await _userState.save();
}

@override
DocumentsState? fromJson(Map<String, dynamic> json) {
return DocumentsState.fromJson(json);
}
// @override
// DocumentsState? fromJson(Map<String, dynamic> json) {
// return DocumentsState.fromJson(json);
// }

@override
Map<String, dynamic>? toJson(DocumentsState state) {
return state.toJson();
}
// @override
// Map<String, dynamic>? toJson(DocumentsState state) {
// return state.toJson();
// }
}
Loading

0 comments on commit 5dcae3f

Please sign in to comment.