From e9cc050866cfec77121c0f48b51a64e7b03739d7 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 27 Jul 2020 09:39:31 +0200 Subject: [PATCH] Implement a basic Zen Mode This can be enabled in all the editors. --- assets/langs/en.yaml | 1 + lib/editors/checklist_editor.dart | 3 + lib/editors/common.dart | 103 +++++++++++++++++++++++++----- lib/editors/journal_editor.dart | 11 +++- lib/editors/markdown_editor.dart | 6 +- lib/editors/raw_editor.dart | 6 +- 6 files changed, 111 insertions(+), 19 deletions(-) diff --git a/assets/langs/en.yaml b/assets/langs/en.yaml index c246d1de..9798688c 100644 --- a/assets/langs/en.yaml +++ b/assets/langs/en.yaml @@ -63,6 +63,7 @@ editors: addImage: Add Image from Gallery editFileName: Edit File Name tags: Edit Tags + zen: Toggle Zen Mode saveNoteFailed: title: Failed to Save Note message: We're sorry, but we cannot seem to save the Note. It has been copied to the clipboard to avoid data loss. diff --git a/lib/editors/checklist_editor.dart b/lib/editors/checklist_editor.dart index 151ef465..27f2bc42 100644 --- a/lib/editors/checklist_editor.dart +++ b/lib/editors/checklist_editor.dart @@ -53,6 +53,7 @@ class ChecklistEditor extends StatefulWidget implements Editor { } class ChecklistEditorState extends State + with ChangeNotifier implements EditorState { Checklist checklist; var focusNodes = {}; @@ -199,6 +200,8 @@ class ChecklistEditorState extends State } void _noteTextChanged() { + notifyListeners(); + if (_noteModified) return; setState(() { _noteModified = true; diff --git a/lib/editors/common.dart b/lib/editors/common.dart index f867b021..7c4e4818 100644 --- a/lib/editors/common.dart +++ b/lib/editors/common.dart @@ -5,6 +5,7 @@ import 'package:flutter/services.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:function_types/function_types.dart'; import 'package:image_picker/image_picker.dart'; import 'package:share/share.dart'; @@ -25,7 +26,7 @@ abstract class Editor { NoteCallback get discardChangesSelected; } -abstract class EditorState { +abstract class EditorState with ChangeNotifier { Note getNote(); Future addImage(File file); @@ -87,12 +88,14 @@ class EditorBottomBar extends StatelessWidget { final EditorState editorState; final NotesFolderFS parentFolder; final bool allowEdits; + final Func0 onZenModeChanged; EditorBottomBar({ @required this.editor, @required this.editorState, @required this.parentFolder, @required this.allowEdits, + @required this.onZenModeChanged, }); @override @@ -113,7 +116,8 @@ class EditorBottomBar extends StatelessWidget { onPressed: () { showModalBottomSheet( context: context, - builder: (c) => _buildBottomMenuSheet(c, editor, editorState), + builder: (c) => + _buildBottomMenuSheet(c, editor, editorState, onZenModeChanged), elevation: 0, ); }, @@ -208,6 +212,7 @@ Widget _buildBottomMenuSheet( BuildContext context, Editor editor, EditorState editorState, + Func0 zenModeChanged, ) { return Container( child: Column( @@ -256,12 +261,22 @@ Widget _buildBottomMenuSheet( editor.renameNoteSelected(note); }, ), + ProOverlay( + child: ListTile( + leading: const FaIcon(FontAwesomeIcons.peace), + title: Text(tr('editors.common.zen')), + onTap: () { + zenModeChanged(); + Navigator.of(context).pop(); + }, + ), + ), ], ), ); } -class EditorScaffold extends StatelessWidget { +class EditorScaffold extends StatefulWidget { final Editor editor; final EditorState editorState; final bool noteModified; @@ -280,24 +295,82 @@ class EditorScaffold extends StatelessWidget { this.allowEdits = true, }); + @override + _EditorScaffoldState createState() => _EditorScaffoldState(); +} + +class _EditorScaffoldState extends State { + var zenMode = false; + var hideUIElements = false; + + @override + void initState() { + super.initState(); + + widget.editorState.addListener(_editorChanged); + } + + @override + void dispose() { + widget.editorState.removeListener(_editorChanged); + + super.dispose(); + } + + void _editorChanged() { + if (zenMode && !hideUIElements) { + setState(() { + hideUIElements = true; + }); + } + } + @override Widget build(BuildContext context) { return Scaffold( - appBar: EditorAppBar( - editor: editor, - editorState: editorState, - noteModified: noteModified, - extraButton: extraButton, - ), body: Column( children: [ - Expanded(child: body), - EditorBottomBar( - editor: editor, - editorState: editorState, - parentFolder: parentFolder, - allowEdits: allowEdits, + AnimatedOpacity( + duration: const Duration(milliseconds: 500), + opacity: hideUIElements ? 0.0 : 1.0, + child: EditorAppBar( + editor: widget.editor, + editorState: widget.editorState, + noteModified: widget.noteModified, + extraButton: widget.extraButton, + ), ), + Expanded( + child: GestureDetector( + child: widget.body, + onTap: () { + if (zenMode) { + setState(() { + hideUIElements = false; + }); + } + }, + behavior: HitTestBehavior.translucent, + ), + ), + AnimatedOpacity( + duration: const Duration(milliseconds: 500), + opacity: hideUIElements ? 0.0 : 1.0, + child: EditorBottomBar( + editor: widget.editor, + editorState: widget.editorState, + parentFolder: widget.parentFolder, + allowEdits: widget.allowEdits, + onZenModeChanged: () { + setState(() { + zenMode = !zenMode; + if (zenMode) { + hideUIElements = true; + } + }); + }, + ), + ) ], ), ); diff --git a/lib/editors/journal_editor.dart b/lib/editors/journal_editor.dart index 47f574ca..b40b5990 100644 --- a/lib/editors/journal_editor.dart +++ b/lib/editors/journal_editor.dart @@ -48,7 +48,9 @@ class JournalEditor extends StatefulWidget implements Editor { } } -class JournalEditorState extends State implements EditorState { +class JournalEditorState extends State + with ChangeNotifier + implements EditorState { Note note; TextEditingController _textController = TextEditingController(); bool _noteModified; @@ -110,7 +112,10 @@ class JournalEditorState extends State implements EditorState { } void _noteTextChanged() { - if (_noteModified && !widget.isNewNote) return; + if (_noteModified && !widget.isNewNote) { + notifyListeners(); + return; + } var newState = !(widget.isNewNote && _textController.text.trim().isEmpty); if (newState != _noteModified) { @@ -118,6 +123,8 @@ class JournalEditorState extends State implements EditorState { _noteModified = newState; }); } + + notifyListeners(); } @override diff --git a/lib/editors/markdown_editor.dart b/lib/editors/markdown_editor.dart index c752eb61..ae6d1432 100644 --- a/lib/editors/markdown_editor.dart +++ b/lib/editors/markdown_editor.dart @@ -53,7 +53,9 @@ class MarkdownEditor extends StatefulWidget implements Editor { } } -class MarkdownEditorState extends State implements EditorState { +class MarkdownEditorState extends State + with ChangeNotifier + implements EditorState { Note note; TextEditingController _textController = TextEditingController(); TextEditingController _titleTextController = TextEditingController(); @@ -187,6 +189,8 @@ class MarkdownEditorState extends State implements EditorState { _noteModified = newState; }); } + + notifyListeners(); } void _applyHeuristics() { diff --git a/lib/editors/raw_editor.dart b/lib/editors/raw_editor.dart index 5646593a..38c6d819 100644 --- a/lib/editors/raw_editor.dart +++ b/lib/editors/raw_editor.dart @@ -48,7 +48,9 @@ class RawEditor extends StatefulWidget implements Editor { } } -class RawEditorState extends State implements EditorState { +class RawEditorState extends State + with ChangeNotifier + implements EditorState { Note note; bool _noteModified; TextEditingController _textController = TextEditingController(); @@ -106,6 +108,8 @@ class RawEditorState extends State implements EditorState { } void _noteTextChanged() { + notifyListeners(); + if (_noteModified) return; setState(() { _noteModified = true;