Externalized API and models as own package

This commit is contained in:
Anton Stubenbord
2022-12-02 01:48:13 +01:00
parent 60d1a2e62a
commit ec7707e4a4
143 changed files with 1496 additions and 1339 deletions

View File

@@ -0,0 +1,85 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
class AddSavedViewPage extends StatefulWidget {
final DocumentFilter currentFilter;
const AddSavedViewPage({super.key, required this.currentFilter});
@override
State<AddSavedViewPage> createState() => _AddSavedViewPageState();
}
class _AddSavedViewPageState extends State<AddSavedViewPage> {
static const fkName = 'name';
static const fkShowOnDashboard = 'show_on_dashboard';
static const fkShowInSidebar = 'show_in_sidebar';
final GlobalKey<FormBuilderState> _formKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).savedViewCreateNewLabel),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Tooltip(
child: const Icon(Icons.info_outline),
message: S.of(context).savedViewCreateTooltipText,
),
),
],
),
floatingActionButton: FloatingActionButton.extended(
icon: const Icon(Icons.add),
onPressed: () => _onCreate(context),
label: Text(S.of(context).genericActionCreateLabel),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: FormBuilder(
key: _formKey,
child: ListView(
children: [
FormBuilderTextField(
name: fkName,
validator: FormBuilderValidators.required(),
decoration: InputDecoration(
label: Text(S.of(context).savedViewNameLabel),
),
),
FormBuilderCheckbox(
name: fkShowOnDashboard,
initialValue: false,
title: Text(S.of(context).savedViewShowOnDashboardLabel),
),
FormBuilderCheckbox(
name: fkShowInSidebar,
initialValue: false,
title: Text(S.of(context).savedViewShowInSidebarLabel),
),
],
),
),
),
);
}
void _onCreate(BuildContext context) {
if (_formKey.currentState?.saveAndValidate() ?? false) {
Navigator.pop(
context,
SavedView.fromDocumentFilter(
widget.currentFilter,
name: _formKey.currentState?.value[fkName] as String,
showOnDashboard:
_formKey.currentState?.value[fkShowOnDashboard] as bool,
showInSidebar: _formKey.currentState?.value[fkShowInSidebar] as bool,
),
);
}
}
}

View File

@@ -0,0 +1,127 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/di_initializer.dart';
import 'package:paperless_mobile/features/documents/bloc/documents_cubit.dart';
import 'package:paperless_mobile/features/saved_view/view/add_saved_view_page.dart';
import 'package:paperless_mobile/features/documents/view/widgets/selection/confirm_delete_saved_view_dialog.dart';
import 'package:paperless_mobile/features/saved_view/bloc/saved_view_cubit.dart';
import 'package:paperless_mobile/features/saved_view/bloc/saved_view_state.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:paperless_mobile/util.dart';
class SavedViewSelectionWidget extends StatelessWidget {
const SavedViewSelectionWidget({
Key? key,
required this.height,
required this.enabled,
}) : super(key: key);
final double height;
final bool enabled;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BlocBuilder<SavedViewCubit, SavedViewState>(
builder: (context, state) {
if (state.value.isEmpty) {
return Text(S.of(context).savedViewsEmptyStateText);
}
return SizedBox(
height: height,
child: ListView.separated(
itemCount: state.value.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
final view = state.value.values.elementAt(index);
return GestureDetector(
onLongPress: () => _onDelete(context, view),
child: FilterChip(
label: Text(state.value.values.toList()[index].name),
selected: view.id == state.selectedSavedViewId,
onSelected: enabled
? (isSelected) =>
_onSelected(isSelected, context, view)
: null,
),
);
},
separatorBuilder: (context, index) => const SizedBox(
width: 8.0,
),
),
);
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
S.of(context).savedViewsLabel,
style: Theme.of(context).textTheme.titleSmall,
),
TextButton.icon(
icon: const Icon(Icons.add),
onPressed: enabled ? () => _onCreatePressed(context) : null,
label: Text(S.of(context).savedViewCreateNewLabel),
),
],
),
],
);
}
void _onCreatePressed(BuildContext context) async {
final newView = await Navigator.of(context).push<SavedView?>(
MaterialPageRoute(
builder: (context) => AddSavedViewPage(
currentFilter: getIt<DocumentsCubit>().state.filter,
),
),
);
if (newView != null) {
try {
await BlocProvider.of<SavedViewCubit>(context).add(newView);
} on PaperlessServerException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
}
}
}
void _onSelected(
bool isSelected, BuildContext context, SavedView view) async {
try {
if (isSelected) {
BlocProvider.of<DocumentsCubit>(context)
.updateFilter(filter: view.toDocumentFilter());
BlocProvider.of<SavedViewCubit>(context).selectView(view);
} else {
BlocProvider.of<DocumentsCubit>(context).updateFilter();
BlocProvider.of<SavedViewCubit>(context).selectView(null);
}
} on PaperlessServerException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
}
}
void _onDelete(BuildContext context, SavedView view) async {
{
final delete = await showDialog<bool>(
context: context,
builder: (context) => ConfirmDeleteSavedViewDialog(view: view),
) ??
false;
if (delete) {
try {
BlocProvider.of<SavedViewCubit>(context).remove(view);
} on PaperlessServerException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
}
}
}
}
}