converted config store to change notifier

This commit is contained in:
shilangyu 2020-10-25 21:33:44 +00:00
parent 8224f6292e
commit 4917c84623
4 changed files with 129 additions and 205 deletions

View File

@ -3,7 +3,6 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';
import 'hooks/stores.dart';
@ -26,9 +25,8 @@ Future<void> main() async {
runApp(
MultiProvider(
providers: [
Provider<ConfigStore>(
create: (_) => configStore,
dispose: (_, store) => store.dispose(),
ChangeNotifierProvider.value(
value: configStore,
),
Provider<AccountsStore>(
create: (_) => accountsStore,
@ -44,28 +42,22 @@ class MyApp extends HookWidget {
@override
Widget build(BuildContext context) {
final configStore = useConfigStore();
final maybeAmoledColor = configStore.amoledDarkMode ? Colors.black : null;
return Observer(
builder: (ctx) {
final maybeAmoledColor =
configStore.amoledDarkMode ? Colors.black : null;
return MaterialApp(
title: 'Lemmur',
themeMode: configStore.theme,
darkTheme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: maybeAmoledColor,
backgroundColor: maybeAmoledColor,
canvasColor: maybeAmoledColor,
cardColor: maybeAmoledColor,
splashColor: maybeAmoledColor,
),
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
},
return MaterialApp(
title: 'Lemmur',
themeMode: configStore.theme,
darkTheme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: maybeAmoledColor,
backgroundColor: maybeAmoledColor,
canvasColor: maybeAmoledColor,
cardColor: maybeAmoledColor,
splashColor: maybeAmoledColor,
),
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}

View File

@ -1,7 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
@ -67,27 +66,25 @@ class AppearanceConfigPage extends HookWidget {
title: Text('Appearance', style: theme.textTheme.headline6),
centerTitle: true,
),
body: Observer(
builder: (ctx) => ListView(
children: [
_SectionHeading('Theme'),
for (final theme in ThemeMode.values)
RadioListTile<ThemeMode>(
value: theme,
title: Text(theme.toString().split('.')[1]),
groupValue: configStore.theme,
onChanged: (selected) {
configStore.theme = selected;
},
),
SwitchListTile(
title: Text('AMOLED dark mode'),
value: configStore.amoledDarkMode,
onChanged: (checked) {
configStore.amoledDarkMode = checked;
})
],
),
body: ListView(
children: [
_SectionHeading('Theme'),
for (final theme in ThemeMode.values)
RadioListTile<ThemeMode>(
value: theme,
title: Text(theme.toString().split('.')[1]),
groupValue: configStore.theme,
onChanged: (selected) {
configStore.theme = selected;
},
),
SwitchListTile(
title: Text('AMOLED dark mode'),
value: configStore.amoledDarkMode,
onChanged: (checked) {
configStore.amoledDarkMode = checked;
})
],
),
);
}
@ -185,100 +182,92 @@ class AccountsConfigPage extends HookWidget {
),
],
),
body: Observer(
builder: (ctx) {
final theme = Theme.of(context);
return ListView(
children: [
if (accountsStore.tokens.isEmpty)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 100),
child: FlatButton.icon(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
onPressed: () => showCupertinoModalPopup(
context: context,
builder: (_) => AddInstancePage(),
),
icon: Icon(Icons.add),
label: Text('Add instance')),
),
],
),
for (final entry in accountsStore.tokens.entries) ...[
SizedBox(height: 40),
Slidable(
actionPane: SlidableBehindActionPane(),
secondaryActions: [
IconSlideAction(
closeOnTap: true,
onTap: () => removeInstanceDialog(entry.key),
icon: Icons.delete_sweep,
color: Colors.red,
),
],
key: Key(entry.key),
child: Container(
color: theme.canvasColor,
child: ListTile(
dense: true,
contentPadding: EdgeInsets.only(left: 0, top: 0),
title: _SectionHeading(entry.key),
),
),
),
for (final username in entry.value.keys) ...[
Slidable(
actionPane: SlidableBehindActionPane(),
key: Key('$username@${entry.key}'),
secondaryActions: [
IconSlideAction(
closeOnTap: true,
onTap: () => removeUserDialog(entry.key, username),
icon: Icons.delete_sweep,
color: Colors.red,
body: ListView(
children: [
if (accountsStore.tokens.isEmpty)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 100),
child: FlatButton.icon(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
],
child: Container(
decoration: BoxDecoration(color: theme.canvasColor),
child: ListTile(
trailing: username ==
accountsStore.defaultUsernameFor(entry.key)
onPressed: () => showCupertinoModalPopup(
context: context,
builder: (_) => AddInstancePage(),
),
icon: Icon(Icons.add),
label: Text('Add instance')),
),
],
),
for (final entry in accountsStore.tokens.entries) ...[
SizedBox(height: 40),
Slidable(
actionPane: SlidableBehindActionPane(),
secondaryActions: [
IconSlideAction(
closeOnTap: true,
onTap: () => removeInstanceDialog(entry.key),
icon: Icons.delete_sweep,
color: Colors.red,
),
],
key: Key(entry.key),
child: Container(
color: theme.canvasColor,
child: ListTile(
dense: true,
contentPadding: EdgeInsets.only(left: 0, top: 0),
title: _SectionHeading(entry.key),
),
),
),
for (final username in entry.value.keys) ...[
Slidable(
actionPane: SlidableBehindActionPane(),
key: Key('$username@${entry.key}'),
secondaryActions: [
IconSlideAction(
closeOnTap: true,
onTap: () => removeUserDialog(entry.key, username),
icon: Icons.delete_sweep,
color: Colors.red,
),
],
child: Container(
decoration: BoxDecoration(color: theme.canvasColor),
child: ListTile(
trailing:
username == accountsStore.defaultUsernameFor(entry.key)
? Icon(
Icons.check_circle_outline,
color: theme.accentColor,
)
: null,
title: Text(username),
onLongPress: () {
accountsStore.setDefaultAccountFor(
entry.key, username);
},
onTap: () {}, // TODO: go to managing account
),
),
),
],
if (entry.value.keys.isEmpty)
ListTile(
leading: Icon(Icons.add),
title: Text('Add account'),
onTap: () {
showCupertinoModalPopup(
context: context,
builder: (_) =>
AddAccountPage(instanceUrl: entry.key));
title: Text(username),
onLongPress: () {
accountsStore.setDefaultAccountFor(entry.key, username);
},
onTap: () {}, // TODO: go to managing account
),
]
),
),
],
);
},
if (entry.value.keys.isEmpty)
ListTile(
leading: Icon(Icons.add),
title: Text('Add account'),
onTap: () {
showCupertinoModalPopup(
context: context,
builder: (_) => AddAccountPage(instanceUrl: entry.key));
},
),
]
],
),
);
}

View File

@ -1,46 +1,38 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';
part 'config_store.g.dart';
/// Store managing user-level configuration such as theme or language
class ConfigStore extends _ConfigStore with _$ConfigStore {}
abstract class _ConfigStore with Store {
ReactionDisposer _saveReactionDisposer;
_ConfigStore() {
// persitently save settings each time they are changed
_saveReactionDisposer = reaction((_) => [theme, amoledDarkMode], (_) {
save();
});
class ConfigStore extends ChangeNotifier {
ThemeMode _theme;
ThemeMode get theme => _theme;
set theme(ThemeMode theme) {
_theme = theme;
notifyListeners();
save();
}
void dispose() {
_saveReactionDisposer();
bool _amoledDarkMode;
bool get amoledDarkMode => _amoledDarkMode;
set amoledDarkMode(bool amoledDarkMode) {
_amoledDarkMode = amoledDarkMode;
notifyListeners();
save();
}
void load() async {
Future<void> load() async {
final prefs = await SharedPreferences.getInstance();
// load saved settings or create defaults
theme = _themeModeFromString(prefs.getString('theme') ?? 'system');
amoledDarkMode = prefs.getBool('amoledDarkMode') ?? false;
notifyListeners();
}
void save() async {
Future<void> save() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('theme', describeEnum(theme));
await prefs.setBool('amoledDarkMode', amoledDarkMode);
}
@observable
ThemeMode theme;
@observable
bool amoledDarkMode;
}
/// converts string to ThemeMode

View File

@ -1,49 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'config_store.dart';
// **************************************************************************
// StoreGenerator
// **************************************************************************
// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic
mixin _$ConfigStore on _ConfigStore, Store {
final _$themeAtom = Atom(name: '_ConfigStore.theme');
@override
ThemeMode get theme {
_$themeAtom.reportRead();
return super.theme;
}
@override
set theme(ThemeMode value) {
_$themeAtom.reportWrite(value, super.theme, () {
super.theme = value;
});
}
final _$amoledDarkModeAtom = Atom(name: '_ConfigStore.amoledDarkMode');
@override
bool get amoledDarkMode {
_$amoledDarkModeAtom.reportRead();
return super.amoledDarkMode;
}
@override
set amoledDarkMode(bool value) {
_$amoledDarkModeAtom.reportWrite(value, super.amoledDarkMode, () {
super.amoledDarkMode = value;
});
}
@override
String toString() {
return '''
theme: ${theme},
amoledDarkMode: ${amoledDarkMode}
''';
}
}