Implement a basic Zen Mode

This can be enabled in all the editors.
This commit is contained in:
Vishesh Handa
2020-07-27 09:39:31 +02:00
parent 6d101ad5c4
commit e9cc050866
6 changed files with 111 additions and 19 deletions

View File

@ -63,6 +63,7 @@ editors:
addImage: Add Image from Gallery addImage: Add Image from Gallery
editFileName: Edit File Name editFileName: Edit File Name
tags: Edit Tags tags: Edit Tags
zen: Toggle Zen Mode
saveNoteFailed: saveNoteFailed:
title: Failed to Save Note 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. message: We're sorry, but we cannot seem to save the Note. It has been copied to the clipboard to avoid data loss.

View File

@ -53,6 +53,7 @@ class ChecklistEditor extends StatefulWidget implements Editor {
} }
class ChecklistEditorState extends State<ChecklistEditor> class ChecklistEditorState extends State<ChecklistEditor>
with ChangeNotifier
implements EditorState { implements EditorState {
Checklist checklist; Checklist checklist;
var focusNodes = <UniqueKey, FocusScopeNode>{}; var focusNodes = <UniqueKey, FocusScopeNode>{};
@ -199,6 +200,8 @@ class ChecklistEditorState extends State<ChecklistEditor>
} }
void _noteTextChanged() { void _noteTextChanged() {
notifyListeners();
if (_noteModified) return; if (_noteModified) return;
setState(() { setState(() {
_noteModified = true; _noteModified = true;

View File

@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.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:image_picker/image_picker.dart';
import 'package:share/share.dart'; import 'package:share/share.dart';
@ -25,7 +26,7 @@ abstract class Editor {
NoteCallback get discardChangesSelected; NoteCallback get discardChangesSelected;
} }
abstract class EditorState { abstract class EditorState with ChangeNotifier {
Note getNote(); Note getNote();
Future<void> addImage(File file); Future<void> addImage(File file);
@ -87,12 +88,14 @@ class EditorBottomBar extends StatelessWidget {
final EditorState editorState; final EditorState editorState;
final NotesFolderFS parentFolder; final NotesFolderFS parentFolder;
final bool allowEdits; final bool allowEdits;
final Func0<void> onZenModeChanged;
EditorBottomBar({ EditorBottomBar({
@required this.editor, @required this.editor,
@required this.editorState, @required this.editorState,
@required this.parentFolder, @required this.parentFolder,
@required this.allowEdits, @required this.allowEdits,
@required this.onZenModeChanged,
}); });
@override @override
@ -113,7 +116,8 @@ class EditorBottomBar extends StatelessWidget {
onPressed: () { onPressed: () {
showModalBottomSheet( showModalBottomSheet(
context: context, context: context,
builder: (c) => _buildBottomMenuSheet(c, editor, editorState), builder: (c) =>
_buildBottomMenuSheet(c, editor, editorState, onZenModeChanged),
elevation: 0, elevation: 0,
); );
}, },
@ -208,6 +212,7 @@ Widget _buildBottomMenuSheet(
BuildContext context, BuildContext context,
Editor editor, Editor editor,
EditorState editorState, EditorState editorState,
Func0<void> zenModeChanged,
) { ) {
return Container( return Container(
child: Column( child: Column(
@ -256,12 +261,22 @@ Widget _buildBottomMenuSheet(
editor.renameNoteSelected(note); 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 Editor editor;
final EditorState editorState; final EditorState editorState;
final bool noteModified; final bool noteModified;
@ -280,24 +295,82 @@ class EditorScaffold extends StatelessWidget {
this.allowEdits = true, this.allowEdits = true,
}); });
@override
_EditorScaffoldState createState() => _EditorScaffoldState();
}
class _EditorScaffoldState extends State<EditorScaffold> {
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: EditorAppBar(
editor: editor,
editorState: editorState,
noteModified: noteModified,
extraButton: extraButton,
),
body: Column( body: Column(
children: <Widget>[ children: <Widget>[
Expanded(child: body), AnimatedOpacity(
EditorBottomBar( duration: const Duration(milliseconds: 500),
editor: editor, opacity: hideUIElements ? 0.0 : 1.0,
editorState: editorState, child: EditorAppBar(
parentFolder: parentFolder, editor: widget.editor,
allowEdits: allowEdits, 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;
}
});
},
),
)
], ],
), ),
); );

View File

@ -48,7 +48,9 @@ class JournalEditor extends StatefulWidget implements Editor {
} }
} }
class JournalEditorState extends State<JournalEditor> implements EditorState { class JournalEditorState extends State<JournalEditor>
with ChangeNotifier
implements EditorState {
Note note; Note note;
TextEditingController _textController = TextEditingController(); TextEditingController _textController = TextEditingController();
bool _noteModified; bool _noteModified;
@ -110,7 +112,10 @@ class JournalEditorState extends State<JournalEditor> implements EditorState {
} }
void _noteTextChanged() { void _noteTextChanged() {
if (_noteModified && !widget.isNewNote) return; if (_noteModified && !widget.isNewNote) {
notifyListeners();
return;
}
var newState = !(widget.isNewNote && _textController.text.trim().isEmpty); var newState = !(widget.isNewNote && _textController.text.trim().isEmpty);
if (newState != _noteModified) { if (newState != _noteModified) {
@ -118,6 +123,8 @@ class JournalEditorState extends State<JournalEditor> implements EditorState {
_noteModified = newState; _noteModified = newState;
}); });
} }
notifyListeners();
} }
@override @override

View File

@ -53,7 +53,9 @@ class MarkdownEditor extends StatefulWidget implements Editor {
} }
} }
class MarkdownEditorState extends State<MarkdownEditor> implements EditorState { class MarkdownEditorState extends State<MarkdownEditor>
with ChangeNotifier
implements EditorState {
Note note; Note note;
TextEditingController _textController = TextEditingController(); TextEditingController _textController = TextEditingController();
TextEditingController _titleTextController = TextEditingController(); TextEditingController _titleTextController = TextEditingController();
@ -187,6 +189,8 @@ class MarkdownEditorState extends State<MarkdownEditor> implements EditorState {
_noteModified = newState; _noteModified = newState;
}); });
} }
notifyListeners();
} }
void _applyHeuristics() { void _applyHeuristics() {

View File

@ -48,7 +48,9 @@ class RawEditor extends StatefulWidget implements Editor {
} }
} }
class RawEditorState extends State<RawEditor> implements EditorState { class RawEditorState extends State<RawEditor>
with ChangeNotifier
implements EditorState {
Note note; Note note;
bool _noteModified; bool _noteModified;
TextEditingController _textController = TextEditingController(); TextEditingController _textController = TextEditingController();
@ -106,6 +108,8 @@ class RawEditorState extends State<RawEditor> implements EditorState {
} }
void _noteTextChanged() { void _noteTextChanged() {
notifyListeners();
if (_noteModified) return; if (_noteModified) return;
setState(() { setState(() {
_noteModified = true; _noteModified = true;