From e65053e437b7a6c56cef3663a8bb0f74239d9289 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 8 Oct 2019 13:55:21 +0200 Subject: [PATCH] NoteFontSize: Simplify the code --- lib/screens/note_viewer.dart | 2 +- lib/screens/settings_screen.dart | 87 ++++++++---------------- lib/settings.dart | 109 +++++++++++++++---------------- 3 files changed, 84 insertions(+), 114 deletions(-) diff --git a/lib/screens/note_viewer.dart b/lib/screens/note_viewer.dart index 94c0b312..b753cc82 100644 --- a/lib/screens/note_viewer.dart +++ b/lib/screens/note_viewer.dart @@ -135,7 +135,7 @@ class NoteViewer extends StatelessWidget { theme = theme.copyWith( textTheme: theme.textTheme.copyWith( body1: theme.textTheme.body1 - .copyWith(fontSize: Settings.instance.getNoteViewerFontSize()), + .copyWith(fontSize: Settings.instance.noteFontSize.toDouble()), ), ); diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 89da24a2..dc3e169e 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -37,6 +37,8 @@ class SettingsListState extends State { @override Widget build(BuildContext context) { + var settings = Settings.instance; + var saveGitAuthor = (String gitAuthor) { Settings.instance.gitAuthor = gitAuthor; Settings.instance.save(); @@ -130,18 +132,18 @@ class SettingsListState extends State { ), ListTile( title: Text("Font Size"), - subtitle: Text( - Settings.noteViewerFontSizeToString( - Settings.instance.noteViewerFontSize), - ), + subtitle: Text(settings.noteFontSize.toPublicString()), onTap: () async { - String result = await showDialog( - context: context, builder: (context) => FontSizeSettingsDialog()); + var fontSize = await showDialog( + context: context, + builder: (context) => FontSizeSettingsDialog(), + ); - var fontSize = Settings.noteViewerFontSizeFromString(result); - Settings.instance.noteViewerFontSize = fontSize; - Settings.instance.save(); - setState(() {}); + if (fontSize != null) { + Settings.instance.noteFontSize = fontSize; + Settings.instance.save(); + setState(() {}); + } }, ), SettingsHeader("Git Author Settings"), @@ -269,54 +271,11 @@ class FontSizeSettingsDialog extends StatelessWidget { @override Widget build(BuildContext context) { - var style = Theme.of(context).textTheme.body1; - var sizes = [ - ListTile( - title: Text("Extra Small", - style: style.copyWith( - fontSize: Settings.noteViewerFontSizeToDouble( - NoteViewerFontSize.ExtraSmall))), - onTap: () { - Navigator.of(context).pop("Extra Small"); - }, - ), - ListTile( - title: Text("Small", - style: style.copyWith( - fontSize: Settings.noteViewerFontSizeToDouble( - NoteViewerFontSize.Small))), - onTap: () { - Navigator.of(context).pop("Small"); - }, - ), - ListTile( - title: Text("Normal", - style: style.copyWith( - fontSize: Settings.noteViewerFontSizeToDouble( - NoteViewerFontSize.Normal))), - onTap: () { - Navigator.of(context).pop("Normal"); - }, - ), - ListTile( - title: Text("Large", - style: style.copyWith( - fontSize: Settings.noteViewerFontSizeToDouble( - NoteViewerFontSize.Large))), - onTap: () { - Navigator.of(context).pop("Large"); - }, - ), - ListTile( - title: Text("Extra Large", - style: style.copyWith( - fontSize: Settings.noteViewerFontSizeToDouble( - NoteViewerFontSize.ExtraLarge))), - onTap: () { - Navigator.of(context).pop("Extra Large"); - }, - ), - ]; + var sizes = []; + for (var fontSize in NoteFontSize.options) { + var tile = _constructTile(context, fontSize); + sizes.add(tile); + } return AlertDialog( title: Text(title), @@ -327,4 +286,16 @@ class FontSizeSettingsDialog extends StatelessWidget { ), ); } + + ListTile _constructTile(BuildContext context, NoteFontSize fontSize) { + var style = Theme.of(context).textTheme.body1; + style = style.copyWith(fontSize: fontSize.toDouble()); + + return ListTile( + title: Text(fontSize.toPublicString(), style: style), + onTap: () { + Navigator.of(context).pop(fontSize); + }, + ); + } } diff --git a/lib/settings.dart b/lib/settings.dart index 2f56fd64..2fd0b256 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -1,6 +1,5 @@ import 'package:shared_preferences/shared_preferences.dart'; -enum NoteViewerFontSize { Normal, Small, ExtraSmall, Large, ExtraLarge } enum NoteFileNameFormat { Iso8601, Iso8601WithTimeZone, @@ -22,7 +21,7 @@ class Settings { NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat.Iso8601WithTimeZone; - NoteViewerFontSize noteViewerFontSize = NoteViewerFontSize.Normal; + NoteFontSize noteFontSize; bool collectUsageStatistics = true; bool collectCrashReports = true; @@ -31,11 +30,10 @@ class Settings { gitAuthor = pref.getString("gitAuthor") ?? gitAuthor; gitAuthorEmail = pref.getString("gitAuthorEmail") ?? gitAuthorEmail; - String str; - str = pref.getString("noteViewerFontSize") ?? noteViewerFontSize.toString(); - noteViewerFontSize = - NoteViewerFontSize.values.firstWhere((e) => e.toString() == str); + noteFontSize = + NoteFontSize.fromInternalString(pref.getString("noteFontSize")); + String str; str = pref.getString("noteFileNameFormat") ?? noteFileNameFormat.toString(); noteFileNameFormat = NoteFileNameFormat.values.firstWhere((e) => e.toString() == str); @@ -50,7 +48,7 @@ class Settings { var pref = await SharedPreferences.getInstance(); pref.setString("gitAuthor", gitAuthor); pref.setString("gitAuthorEmail", gitAuthorEmail); - pref.setString("noteViewerFontSize", noteViewerFontSize.toString()); + pref.setString("noteFontSize", noteFontSize.toInternalString()); pref.setString("noteFileNameFormat", noteFileNameFormat.toString()); pref.setBool("collectUsageStatistics", collectUsageStatistics); pref.setBool("collectCrashReports", collectCrashReports); @@ -60,61 +58,62 @@ class Settings { f(); } } +} - double getNoteViewerFontSize() { - return noteViewerFontSizeToDouble(noteViewerFontSize); +class NoteFontSize { + static const ExtraSmall = NoteFontSize("ExtraSmall", "Small", 12.0); + static const Small = NoteFontSize("Small", "Small", 16.0); + static const Normal = NoteFontSize("Normal", "Normal", 18.0); + static const Large = NoteFontSize("Large", "Large", 22.0); + static const ExtraLarge = NoteFontSize("ExtraLarge", "Extra Large", 26.0); + + static const options = [ + ExtraSmall, + Small, + Normal, + Large, + ExtraLarge, + ]; + + static NoteFontSize fromInternalString(String str) { + for (var opt in options) { + if (opt.toInternalString() == str) { + return opt; + } + } + return Normal; } - static double noteViewerFontSizeToDouble(NoteViewerFontSize size) { - switch (size) { - case NoteViewerFontSize.Normal: - return 18.0; - case NoteViewerFontSize.Small: - return 15.0; - case NoteViewerFontSize.ExtraSmall: - return 12.0; - case NoteViewerFontSize.Large: - return 22.0; - case NoteViewerFontSize.ExtraLarge: - return 26.0; + static NoteFontSize fromPublicString(String str) { + for (var opt in options) { + if (opt.toPublicString() == str) { + return opt; + } } - - assert(false, "noteViewerFontSizeToDouble: We should never be here"); - return 50000.0; + return Normal; } - static String noteViewerFontSizeToString(NoteViewerFontSize size) { - switch (size) { - case NoteViewerFontSize.Normal: - return "Normal"; - case NoteViewerFontSize.Small: - return "Small"; - case NoteViewerFontSize.ExtraSmall: - return "Extra Small"; - case NoteViewerFontSize.Large: - return "Large"; - case NoteViewerFontSize.ExtraLarge: - return "Extra Large"; - } + final String _str; + final String _publicStr; + final double _value; - assert(false, "noteViewerFontSizeToString: We should never be here"); + const NoteFontSize(this._str, this._publicStr, this._value); + + String toInternalString() { + return _str; + } + + String toPublicString() { + return _publicStr; + } + + double toDouble() { + return _value; + } + + @override + String toString() { + assert(false, "NoteFontSize toString should never be called"); return ""; } - - static NoteViewerFontSize noteViewerFontSizeFromString(String val) { - switch (val) { - case "Extra Small": - return NoteViewerFontSize.ExtraSmall; - case "Small": - return NoteViewerFontSize.Small; - case "Normal": - return NoteViewerFontSize.Normal; - case "Large": - return NoteViewerFontSize.Large; - case "Extra Large": - return NoteViewerFontSize.ExtraLarge; - default: - return NoteViewerFontSize.Normal; - } - } }