NoteFontSize: Simplify the code

This commit is contained in:
Vishesh Handa
2019-10-08 13:55:21 +02:00
parent 276a3bf976
commit e65053e437
3 changed files with 84 additions and 114 deletions

View File

@ -135,7 +135,7 @@ class NoteViewer extends StatelessWidget {
theme = theme.copyWith( theme = theme.copyWith(
textTheme: theme.textTheme.copyWith( textTheme: theme.textTheme.copyWith(
body1: theme.textTheme.body1 body1: theme.textTheme.body1
.copyWith(fontSize: Settings.instance.getNoteViewerFontSize()), .copyWith(fontSize: Settings.instance.noteFontSize.toDouble()),
), ),
); );

View File

@ -37,6 +37,8 @@ class SettingsListState extends State<SettingsList> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var settings = Settings.instance;
var saveGitAuthor = (String gitAuthor) { var saveGitAuthor = (String gitAuthor) {
Settings.instance.gitAuthor = gitAuthor; Settings.instance.gitAuthor = gitAuthor;
Settings.instance.save(); Settings.instance.save();
@ -130,18 +132,18 @@ class SettingsListState extends State<SettingsList> {
), ),
ListTile( ListTile(
title: Text("Font Size"), title: Text("Font Size"),
subtitle: Text( subtitle: Text(settings.noteFontSize.toPublicString()),
Settings.noteViewerFontSizeToString(
Settings.instance.noteViewerFontSize),
),
onTap: () async { onTap: () async {
String result = await showDialog<String>( var fontSize = await showDialog<NoteFontSize>(
context: context, builder: (context) => FontSizeSettingsDialog()); context: context,
builder: (context) => FontSizeSettingsDialog(),
);
var fontSize = Settings.noteViewerFontSizeFromString(result); if (fontSize != null) {
Settings.instance.noteViewerFontSize = fontSize; Settings.instance.noteFontSize = fontSize;
Settings.instance.save(); Settings.instance.save();
setState(() {}); setState(() {});
}
}, },
), ),
SettingsHeader("Git Author Settings"), SettingsHeader("Git Author Settings"),
@ -269,54 +271,11 @@ class FontSizeSettingsDialog extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var style = Theme.of(context).textTheme.body1; var sizes = <Widget>[];
var sizes = <Widget>[ for (var fontSize in NoteFontSize.options) {
ListTile( var tile = _constructTile(context, fontSize);
title: Text("Extra Small", sizes.add(tile);
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");
},
),
];
return AlertDialog( return AlertDialog(
title: Text(title), 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);
},
);
}
} }

View File

@ -1,6 +1,5 @@
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
enum NoteViewerFontSize { Normal, Small, ExtraSmall, Large, ExtraLarge }
enum NoteFileNameFormat { enum NoteFileNameFormat {
Iso8601, Iso8601,
Iso8601WithTimeZone, Iso8601WithTimeZone,
@ -22,7 +21,7 @@ class Settings {
NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat noteFileNameFormat =
NoteFileNameFormat.Iso8601WithTimeZone; NoteFileNameFormat.Iso8601WithTimeZone;
NoteViewerFontSize noteViewerFontSize = NoteViewerFontSize.Normal; NoteFontSize noteFontSize;
bool collectUsageStatistics = true; bool collectUsageStatistics = true;
bool collectCrashReports = true; bool collectCrashReports = true;
@ -31,11 +30,10 @@ class Settings {
gitAuthor = pref.getString("gitAuthor") ?? gitAuthor; gitAuthor = pref.getString("gitAuthor") ?? gitAuthor;
gitAuthorEmail = pref.getString("gitAuthorEmail") ?? gitAuthorEmail; gitAuthorEmail = pref.getString("gitAuthorEmail") ?? gitAuthorEmail;
String str; noteFontSize =
str = pref.getString("noteViewerFontSize") ?? noteViewerFontSize.toString(); NoteFontSize.fromInternalString(pref.getString("noteFontSize"));
noteViewerFontSize =
NoteViewerFontSize.values.firstWhere((e) => e.toString() == str);
String str;
str = pref.getString("noteFileNameFormat") ?? noteFileNameFormat.toString(); str = pref.getString("noteFileNameFormat") ?? noteFileNameFormat.toString();
noteFileNameFormat = noteFileNameFormat =
NoteFileNameFormat.values.firstWhere((e) => e.toString() == str); NoteFileNameFormat.values.firstWhere((e) => e.toString() == str);
@ -50,7 +48,7 @@ class Settings {
var pref = await SharedPreferences.getInstance(); var pref = await SharedPreferences.getInstance();
pref.setString("gitAuthor", gitAuthor); pref.setString("gitAuthor", gitAuthor);
pref.setString("gitAuthorEmail", gitAuthorEmail); pref.setString("gitAuthorEmail", gitAuthorEmail);
pref.setString("noteViewerFontSize", noteViewerFontSize.toString()); pref.setString("noteFontSize", noteFontSize.toInternalString());
pref.setString("noteFileNameFormat", noteFileNameFormat.toString()); pref.setString("noteFileNameFormat", noteFileNameFormat.toString());
pref.setBool("collectUsageStatistics", collectUsageStatistics); pref.setBool("collectUsageStatistics", collectUsageStatistics);
pref.setBool("collectCrashReports", collectCrashReports); pref.setBool("collectCrashReports", collectCrashReports);
@ -60,61 +58,62 @@ class Settings {
f(); f();
} }
} }
}
double getNoteViewerFontSize() { class NoteFontSize {
return noteViewerFontSizeToDouble(noteViewerFontSize); 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 = <NoteFontSize>[
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) { static NoteFontSize fromPublicString(String str) {
switch (size) { for (var opt in options) {
case NoteViewerFontSize.Normal: if (opt.toPublicString() == str) {
return 18.0; return opt;
case NoteViewerFontSize.Small: }
return 15.0; }
case NoteViewerFontSize.ExtraSmall: return Normal;
return 12.0;
case NoteViewerFontSize.Large:
return 22.0;
case NoteViewerFontSize.ExtraLarge:
return 26.0;
} }
assert(false, "noteViewerFontSizeToDouble: We should never be here"); final String _str;
return 50000.0; final String _publicStr;
final double _value;
const NoteFontSize(this._str, this._publicStr, this._value);
String toInternalString() {
return _str;
} }
static String noteViewerFontSizeToString(NoteViewerFontSize size) { String toPublicString() {
switch (size) { return _publicStr;
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";
} }
assert(false, "noteViewerFontSizeToString: We should never be here"); double toDouble() {
return _value;
}
@override
String toString() {
assert(false, "NoteFontSize toString should never be called");
return ""; 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;
}
}
} }