Browse Source

neon_notes: get blocs via context

pull/377/head
Nikolas Rimikis 2 years ago
parent
commit
b003ab9df5
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 15
      packages/neon/neon_notes/lib/dialogs/create_note.dart
  2. 14
      packages/neon/neon_notes/lib/dialogs/select_category.dart
  3. 4
      packages/neon/neon_notes/lib/pages/category.dart
  4. 14
      packages/neon/neon_notes/lib/pages/main.dart
  5. 6
      packages/neon/neon_notes/lib/pages/note.dart
  6. 58
      packages/neon/neon_notes/lib/widgets/categories_view.dart
  7. 41
      packages/neon/neon_notes/lib/widgets/notes_floating_action_button.dart
  8. 173
      packages/neon/neon_notes/lib/widgets/notes_view.dart

15
packages/neon/neon_notes/lib/dialogs/create_note.dart

@ -2,12 +2,10 @@ part of '../neon_notes.dart';
class NotesCreateNoteDialog extends StatefulWidget { class NotesCreateNoteDialog extends StatefulWidget {
const NotesCreateNoteDialog({ const NotesCreateNoteDialog({
required this.bloc,
this.category, this.category,
super.key, super.key,
}); });
final NotesBloc bloc;
final String? category; final String? category;
@override @override
@ -17,8 +15,17 @@ class NotesCreateNoteDialog extends StatefulWidget {
class _NotesCreateNoteDialogState extends State<NotesCreateNoteDialog> { class _NotesCreateNoteDialogState extends State<NotesCreateNoteDialog> {
final formKey = GlobalKey<FormState>(); final formKey = GlobalKey<FormState>();
final controller = TextEditingController(); final controller = TextEditingController();
late NotesBloc bloc;
String? selectedCategory; String? selectedCategory;
@override
void initState() {
bloc = Provider.of<NotesBloc>(context, listen: false);
super.initState();
}
void submit() { void submit() {
if (formKey.currentState!.validate()) { if (formKey.currentState!.validate()) {
Navigator.of(context).pop([controller.text, widget.category ?? selectedCategory]); Navigator.of(context).pop([controller.text, widget.category ?? selectedCategory]);
@ -27,7 +34,7 @@ class _NotesCreateNoteDialogState extends State<NotesCreateNoteDialog> {
@override @override
Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>( Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>(
stream: widget.bloc.notes, stream: bloc.notes,
builder: (final context, final notes) => NeonDialog( builder: (final context, final notes) => NeonDialog(
title: Text(AppLocalizations.of(context).noteCreate), title: Text(AppLocalizations.of(context).noteCreate),
children: [ children: [
@ -51,7 +58,7 @@ class _NotesCreateNoteDialogState extends State<NotesCreateNoteDialog> {
Center( Center(
child: NeonException( child: NeonException(
notes.error, notes.error,
onRetry: widget.bloc.refresh, onRetry: bloc.refresh,
), ),
), ),
Center( Center(

14
packages/neon/neon_notes/lib/dialogs/select_category.dart

@ -2,12 +2,10 @@ part of '../neon_notes.dart';
class NotesSelectCategoryDialog extends StatefulWidget { class NotesSelectCategoryDialog extends StatefulWidget {
const NotesSelectCategoryDialog({ const NotesSelectCategoryDialog({
required this.bloc,
this.initialCategory, this.initialCategory,
super.key, super.key,
}); });
final NotesBloc bloc;
final String? initialCategory; final String? initialCategory;
@override @override
@ -16,9 +14,17 @@ class NotesSelectCategoryDialog extends StatefulWidget {
class _NotesSelectCategoryDialogState extends State<NotesSelectCategoryDialog> { class _NotesSelectCategoryDialogState extends State<NotesSelectCategoryDialog> {
final formKey = GlobalKey<FormState>(); final formKey = GlobalKey<FormState>();
late NotesBloc bloc;
String? selectedCategory; String? selectedCategory;
@override
void initState() {
bloc = Provider.of<NotesBloc>(context, listen: false);
super.initState();
}
void submit() { void submit() {
if (formKey.currentState!.validate()) { if (formKey.currentState!.validate()) {
Navigator.of(context).pop(selectedCategory); Navigator.of(context).pop(selectedCategory);
@ -27,7 +33,7 @@ class _NotesSelectCategoryDialogState extends State<NotesSelectCategoryDialog> {
@override @override
Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>( Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>(
stream: widget.bloc.notes, stream: bloc.notes,
builder: (final context, final notes) => NeonDialog( builder: (final context, final notes) => NeonDialog(
title: Text(AppLocalizations.of(context).category), title: Text(AppLocalizations.of(context).category),
children: [ children: [
@ -39,7 +45,7 @@ class _NotesSelectCategoryDialogState extends State<NotesSelectCategoryDialog> {
Center( Center(
child: NeonException( child: NeonException(
notes.error, notes.error,
onRetry: widget.bloc.refresh, onRetry: bloc.refresh,
), ),
), ),
Center( Center(

4
packages/neon/neon_notes/lib/pages/category.dart

@ -2,12 +2,10 @@ part of '../neon_notes.dart';
class NotesCategoryPage extends StatelessWidget { class NotesCategoryPage extends StatelessWidget {
const NotesCategoryPage({ const NotesCategoryPage({
required this.bloc,
required this.category, required this.category,
super.key, super.key,
}); });
final NotesBloc bloc;
final NoteCategory category; final NoteCategory category;
@override @override
@ -17,11 +15,9 @@ class NotesCategoryPage extends StatelessWidget {
title: Text(category.name != '' ? category.name : AppLocalizations.of(context).categoryUncategorized), title: Text(category.name != '' ? category.name : AppLocalizations.of(context).categoryUncategorized),
), ),
body: NotesView( body: NotesView(
bloc: bloc,
category: category.name, category: category.name,
), ),
floatingActionButton: NotesFloatingActionButton( floatingActionButton: NotesFloatingActionButton(
bloc: bloc,
category: category.name, category: category.name,
), ),
); );

14
packages/neon/neon_notes/lib/pages/main.dart

@ -26,17 +26,13 @@ class _NotesMainPageState extends State<NotesMainPage> {
@override @override
Widget build(final BuildContext context) { Widget build(final BuildContext context) {
final views = [ const views = [
NotesView( NotesView(),
bloc: bloc, NotesCategoriesView(),
),
NotesCategoriesView(
bloc: bloc,
),
]; ];
final floatingActionButtons = [ const floatingActionButtons = [
NotesFloatingActionButton(bloc: bloc), NotesFloatingActionButton(),
null, null,
]; ];

6
packages/neon/neon_notes/lib/pages/note.dart

@ -3,18 +3,17 @@ part of '../neon_notes.dart';
class NotesNotePage extends StatefulWidget { class NotesNotePage extends StatefulWidget {
const NotesNotePage({ const NotesNotePage({
required this.bloc, required this.bloc,
required this.notesBloc,
super.key, super.key,
}); });
final NotesNoteBloc bloc; final NotesNoteBloc bloc;
final NotesBloc notesBloc;
@override @override
State<NotesNotePage> createState() => _NotesNotePageState(); State<NotesNotePage> createState() => _NotesNotePageState();
} }
class _NotesNotePageState extends State<NotesNotePage> { class _NotesNotePageState extends State<NotesNotePage> {
late NotesBloc notesBloc;
late final _contentController = TextEditingController()..text = widget.bloc.initialContent; late final _contentController = TextEditingController()..text = widget.bloc.initialContent;
late final _titleController = TextEditingController()..text = widget.bloc.initialTitle; late final _titleController = TextEditingController()..text = widget.bloc.initialTitle;
final _contentFocusNode = FocusNode(); final _contentFocusNode = FocusNode();
@ -32,6 +31,8 @@ class _NotesNotePageState extends State<NotesNotePage> {
void initState() { void initState() {
super.initState(); super.initState();
notesBloc = Provider.of<NotesBloc>(context, listen: false);
widget.bloc.errors.listen((final error) { widget.bloc.errors.listen((final error) {
handleNotesException(context, error); handleNotesException(context, error);
}); });
@ -118,7 +119,6 @@ class _NotesNotePageState extends State<NotesNotePage> {
final result = await showDialog<String>( final result = await showDialog<String>(
context: context, context: context,
builder: (final context) => NotesSelectCategoryDialog( builder: (final context) => NotesSelectCategoryDialog(
bloc: widget.notesBloc,
initialCategory: category, initialCategory: category,
), ),
); );

58
packages/neon/neon_notes/lib/widgets/categories_view.dart

@ -2,39 +2,40 @@ part of '../neon_notes.dart';
class NotesCategoriesView extends StatelessWidget { class NotesCategoriesView extends StatelessWidget {
const NotesCategoriesView({ const NotesCategoriesView({
required this.bloc,
super.key, super.key,
}); });
final NotesBloc bloc;
@override @override
Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>( Widget build(final BuildContext context) {
stream: bloc.notes, final bloc = Provider.of<NotesBloc>(context, listen: false);
builder: (final context, final notes) => SortBoxBuilder<CategoriesSortProperty, NoteCategory>(
sortBox: categoriesSortBox, return ResultBuilder<List<NextcloudNotesNote>>(
sortPropertyOption: bloc.options.categoriesSortPropertyOption, stream: bloc.notes,
sortBoxOrderOption: bloc.options.categoriesSortBoxOrderOption, builder: (final context, final notes) => SortBoxBuilder<CategoriesSortProperty, NoteCategory>(
input: notes.data sortBox: categoriesSortBox,
?.map((final note) => note.category) sortPropertyOption: bloc.options.categoriesSortPropertyOption,
.toSet() sortBoxOrderOption: bloc.options.categoriesSortBoxOrderOption,
.map( input: notes.data
(final category) => NoteCategory( ?.map((final note) => note.category)
category, .toSet()
notes.data!.where((final note) => note.category == category).length, .map(
), (final category) => NoteCategory(
) category,
.toList(), notes.data!.where((final note) => note.category == category).length,
builder: (final context, final sorted) => NeonListView<NoteCategory>( ),
scrollKey: 'notes-categories', )
items: sorted, .toList(),
isLoading: notes.loading, builder: (final context, final sorted) => NeonListView<NoteCategory>(
error: notes.error, scrollKey: 'notes-categories',
onRefresh: bloc.refresh, items: sorted,
builder: _buildCategory, isLoading: notes.loading,
), error: notes.error,
onRefresh: bloc.refresh,
builder: _buildCategory,
), ),
); ),
);
}
Widget _buildCategory( Widget _buildCategory(
final BuildContext context, final BuildContext context,
@ -54,7 +55,6 @@ class NotesCategoriesView extends StatelessWidget {
await Navigator.of(context).push( await Navigator.of(context).push(
MaterialPageRoute( MaterialPageRoute(
builder: (final context) => NotesCategoryPage( builder: (final context) => NotesCategoryPage(
bloc: bloc,
category: category, category: category,
), ),
), ),

41
packages/neon/neon_notes/lib/widgets/notes_floating_action_button.dart

@ -2,32 +2,33 @@ part of '../neon_notes.dart';
class NotesFloatingActionButton extends StatelessWidget { class NotesFloatingActionButton extends StatelessWidget {
const NotesFloatingActionButton({ const NotesFloatingActionButton({
required this.bloc,
this.category, this.category,
super.key, super.key,
}); });
final NotesBloc bloc;
final String? category; final String? category;
@override @override
Widget build(final BuildContext context) => FloatingActionButton( Widget build(final BuildContext context) {
onPressed: () async { final bloc = Provider.of<NotesBloc>(context, listen: false);
final result = await showDialog<List>(
context: context, return FloatingActionButton(
builder: (final context) => NotesCreateNoteDialog( onPressed: () async {
bloc: bloc, final result = await showDialog<List>(
category: category, context: context,
), builder: (final context) => NotesCreateNoteDialog(
category: category,
),
);
if (result != null) {
bloc.createNote(
title: result[0] as String,
category: result[1] as String? ?? '',
); );
if (result != null) { }
bloc.createNote( },
title: result[0] as String, tooltip: AppLocalizations.of(context).noteCreate,
category: result[1] as String? ?? '', child: const Icon(Icons.add),
); );
} }
},
tooltip: AppLocalizations.of(context).noteCreate,
child: const Icon(Icons.add),
);
} }

173
packages/neon/neon_notes/lib/widgets/notes_view.dart

@ -2,111 +2,114 @@ part of '../neon_notes.dart';
class NotesView extends StatelessWidget { class NotesView extends StatelessWidget {
const NotesView({ const NotesView({
required this.bloc,
this.category, this.category,
super.key, super.key,
}); });
final NotesBloc bloc;
final String? category; final String? category;
@override @override
Widget build(final BuildContext context) => ResultBuilder<List<NextcloudNotesNote>>( Widget build(final BuildContext context) {
stream: bloc.notes, final bloc = Provider.of<NotesBloc>(context, listen: false);
builder: (final context, final notes) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>( return ResultBuilder<List<NextcloudNotesNote>>(
stream: bloc.notes,
builder: (final context, final notes) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>(
sortBox: notesSortBox,
sortPropertyOption: bloc.options.notesSortPropertyOption,
sortBoxOrderOption: bloc.options.notesSortBoxOrderOption,
input: category != null
? notes.data?.where((final note) => note.favorite && note.category == category).toList()
: notes.data?.where((final note) => note.favorite).toList(),
builder: (final context, final sortedFavorites) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>(
sortBox: notesSortBox, sortBox: notesSortBox,
sortPropertyOption: bloc.options.notesSortPropertyOption, sortPropertyOption: bloc.options.notesSortPropertyOption,
sortBoxOrderOption: bloc.options.notesSortBoxOrderOption, sortBoxOrderOption: bloc.options.notesSortBoxOrderOption,
input: category != null input: category != null
? notes.data?.where((final note) => note.favorite && note.category == category).toList() ? notes.data?.where((final note) => !note.favorite && note.category == category).toList()
: notes.data?.where((final note) => note.favorite).toList(), : notes.data?.where((final note) => !note.favorite).toList(),
builder: (final context, final sortedFavorites) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>( builder: (final context, final sortedNonFavorites) => NeonListView<NextcloudNotesNote>(
sortBox: notesSortBox, scrollKey: 'notes-notes',
sortPropertyOption: bloc.options.notesSortPropertyOption, withFloatingActionButton: true,
sortBoxOrderOption: bloc.options.notesSortBoxOrderOption, items: [
input: category != null ...?sortedFavorites,
? notes.data?.where((final note) => !note.favorite && note.category == category).toList() ...?sortedNonFavorites,
: notes.data?.where((final note) => !note.favorite).toList(), ],
builder: (final context, final sortedNonFavorites) => NeonListView<NextcloudNotesNote>( isLoading: notes.loading,
scrollKey: 'notes-notes', error: notes.error,
withFloatingActionButton: true, onRefresh: bloc.refresh,
items: [ builder: _buildNote,
...?sortedFavorites,
...?sortedNonFavorites,
],
isLoading: notes.loading,
error: notes.error,
onRefresh: bloc.refresh,
builder: _buildNote,
),
), ),
), ),
); ),
);
}
Widget _buildNote( Widget _buildNote(
final BuildContext context, final BuildContext context,
final NextcloudNotesNote note, final NextcloudNotesNote note,
) => ) {
ListTile( final bloc = Provider.of<NotesBloc>(context, listen: false);
title: Text(note.title),
subtitle: Row( return ListTile(
children: [ title: Text(note.title),
RelativeTime( subtitle: Row(
date: DateTime.fromMillisecondsSinceEpoch(note.modified * 1000), children: [
RelativeTime(
date: DateTime.fromMillisecondsSinceEpoch(note.modified * 1000),
),
if (note.category != '') ...[
const SizedBox(
width: 8,
), ),
if (note.category != '') ...[ Icon(
const SizedBox( MdiIcons.tag,
width: 8, size: 14,
), color: NotesCategoryColor.compute(note.category),
Icon( ),
MdiIcons.tag, const SizedBox(
size: 14, width: 2,
color: NotesCategoryColor.compute(note.category), ),
), Text(note.category),
const SizedBox(
width: 2,
),
Text(note.category),
],
], ],
],
),
trailing: IconButton(
onPressed: () {
bloc.updateNote(
note.id,
note.etag,
favorite: !note.favorite,
);
},
tooltip: note.favorite ? AppLocalizations.of(context).noteUnstar : AppLocalizations.of(context).noteStar,
icon: Icon(
note.favorite ? Icons.star : Icons.star_outline,
color: Theme.of(context).colorScheme.primary,
), ),
trailing: IconButton( ),
onPressed: () { onTap: () async {
bloc.updateNote( await Navigator.of(context).push(
note.id, MaterialPageRoute(
note.etag, builder: (final context) => NotesNotePage(
favorite: !note.favorite, bloc: NotesNoteBloc(
); bloc.options,
}, Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!.client,
tooltip: note.favorite ? AppLocalizations.of(context).noteUnstar : AppLocalizations.of(context).noteStar, bloc,
icon: Icon( note,
note.favorite ? Icons.star : Icons.star_outline,
color: Theme.of(context).colorScheme.primary,
),
),
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (final context) => NotesNotePage(
bloc: NotesNoteBloc(
bloc.options,
Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!.client,
bloc,
note,
),
notesBloc: bloc,
), ),
), ),
); ),
}, );
onLongPress: () async { },
final result = await showConfirmationDialog( onLongPress: () async {
context, final result = await showConfirmationDialog(
AppLocalizations.of(context).noteDeleteConfirm(note.title), context,
); AppLocalizations.of(context).noteDeleteConfirm(note.title),
if (result) { );
bloc.deleteNote(note.id); if (result) {
} bloc.deleteNote(note.id);
}, }
); },
);
}
} }

Loading…
Cancel
Save