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. 10
      packages/neon/neon_notes/lib/widgets/categories_view.dart
  7. 9
      packages/neon/neon_notes/lib/widgets/notes_floating_action_button.dart
  8. 15
      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,
), ),
); );

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

@ -2,14 +2,14 @@ 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) {
final bloc = Provider.of<NotesBloc>(context, listen: false);
return ResultBuilder<List<NextcloudNotesNote>>(
stream: bloc.notes, stream: bloc.notes,
builder: (final context, final notes) => SortBoxBuilder<CategoriesSortProperty, NoteCategory>( builder: (final context, final notes) => SortBoxBuilder<CategoriesSortProperty, NoteCategory>(
sortBox: categoriesSortBox, sortBox: categoriesSortBox,
@ -35,6 +35,7 @@ class NotesCategoriesView extends StatelessWidget {
), ),
), ),
); );
}
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,
), ),
), ),

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

@ -2,21 +2,21 @@ 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) {
final bloc = Provider.of<NotesBloc>(context, listen: false);
return FloatingActionButton(
onPressed: () async { onPressed: () async {
final result = await showDialog<List>( final result = await showDialog<List>(
context: context, context: context,
builder: (final context) => NotesCreateNoteDialog( builder: (final context) => NotesCreateNoteDialog(
bloc: bloc,
category: category, category: category,
), ),
); );
@ -30,4 +30,5 @@ class NotesFloatingActionButton extends StatelessWidget {
tooltip: AppLocalizations.of(context).noteCreate, tooltip: AppLocalizations.of(context).noteCreate,
child: const Icon(Icons.add), child: const Icon(Icons.add),
); );
}
} }

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

@ -2,16 +2,16 @@ 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) {
final bloc = Provider.of<NotesBloc>(context, listen: false);
return ResultBuilder<List<NextcloudNotesNote>>(
stream: bloc.notes, stream: bloc.notes,
builder: (final context, final notes) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>( builder: (final context, final notes) => SortBoxBuilder<NotesSortProperty, NextcloudNotesNote>(
sortBox: notesSortBox, sortBox: notesSortBox,
@ -42,12 +42,15 @@ class NotesView extends StatelessWidget {
), ),
), ),
); );
}
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);
return ListTile(
title: Text(note.title), title: Text(note.title),
subtitle: Row( subtitle: Row(
children: [ children: [
@ -94,7 +97,6 @@ class NotesView extends StatelessWidget {
bloc, bloc,
note, note,
), ),
notesBloc: bloc,
), ),
), ),
); );
@ -109,4 +111,5 @@ class NotesView extends StatelessWidget {
} }
}, },
); );
}
} }

Loading…
Cancel
Save