From c6b03c3aefec68eb06c789287247e54cf132cccf Mon Sep 17 00:00:00 2001 From: shilangyu Date: Wed, 16 Sep 2020 23:15:42 +0200 Subject: [PATCH] added hook for stores --- lib/hooks/stores.dart | 8 +++++ lib/main.dart | 54 ++++++++++++++++++---------------- lib/pages/communities_tab.dart | 17 ++++------- lib/pages/community.dart | 13 ++++---- lib/pages/profile_tab.dart | 17 +++++------ lib/pages/settings.dart | 31 ++++++++++--------- 6 files changed, 71 insertions(+), 69 deletions(-) create mode 100644 lib/hooks/stores.dart diff --git a/lib/hooks/stores.dart b/lib/hooks/stores.dart new file mode 100644 index 0000000..85b9287 --- /dev/null +++ b/lib/hooks/stores.dart @@ -0,0 +1,8 @@ +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:provider/provider.dart'; + +import '../stores/accounts_store.dart'; +import '../stores/config_store.dart'; + +AccountsStore useAccountsStore() => useContext().watch(); +ConfigStore useConfigStore() => useContext().watch(); diff --git a/lib/main.dart b/lib/main.dart index 03c5d86..87382d7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; +import 'package:lemmur/pages/profile_tab.dart'; import 'package:provider/provider.dart'; -import 'pages/instance.dart'; +import 'hooks/stores.dart'; import 'stores/accounts_store.dart'; import 'stores/config_store.dart'; @@ -32,32 +34,34 @@ Future main() async { ); } -class MyApp extends StatelessWidget { +class MyApp extends HookWidget { @override - Widget build(BuildContext context) => Observer( - builder: (ctx) { - var maybeAmoledColor = - ctx.watch().amoledDarkMode ? Colors.black : null; + Widget build(BuildContext context) { + final configStore = useConfigStore(); - return MaterialApp( - title: 'Flutter Demo', - themeMode: ctx.watch().theme, - darkTheme: ThemeData.dark().copyWith( - scaffoldBackgroundColor: maybeAmoledColor, - backgroundColor: maybeAmoledColor, - canvasColor: maybeAmoledColor, - cardColor: maybeAmoledColor, - splashColor: maybeAmoledColor, - ), - theme: ThemeData( - visualDensity: VisualDensity.adaptivePlatformDensity, - ), - home: InstancePage( - instanceUrl: 'dev.lemmy.ml', - ), - ); - }, - ); + return Observer( + builder: (ctx) { + final maybeAmoledColor = + configStore.amoledDarkMode ? Colors.black : null; + + return MaterialApp( + title: 'Flutter Demo', + themeMode: configStore.theme, + darkTheme: ThemeData.dark().copyWith( + scaffoldBackgroundColor: maybeAmoledColor, + backgroundColor: maybeAmoledColor, + canvasColor: maybeAmoledColor, + cardColor: maybeAmoledColor, + splashColor: maybeAmoledColor, + ), + theme: ThemeData( + visualDensity: VisualDensity.adaptivePlatformDensity, + ), + home: UserProfileTab(), + ); + }, + ); + } } class MyHomePage extends StatefulWidget { diff --git a/lib/pages/communities_tab.dart b/lib/pages/communities_tab.dart index 7db66fa..0712a28 100644 --- a/lib/pages/communities_tab.dart +++ b/lib/pages/communities_tab.dart @@ -4,11 +4,10 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:fuzzy/fuzzy.dart'; +import 'package:lemmur/hooks/stores.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart'; -import 'package:provider/provider.dart'; import '../hooks/delayed_loading.dart'; -import '../stores/accounts_store.dart'; import '../util/extensions/iterators.dart'; import '../util/text_color.dart'; @@ -20,9 +19,9 @@ class CommunitiesTab extends HookWidget { var theme = Theme.of(context); var filterController = useTextEditingController(); useValueListenable(filterController); - var amountOfDisplayInstances = useMemoized(() { - var accountsStore = context.watch(); + final accountsStore = useAccountsStore(); + var amountOfDisplayInstances = useMemoized(() { return accountsStore.users.keys .where((e) => !accountsStore.isAnonymousFor(e)) .length; @@ -31,8 +30,6 @@ class CommunitiesTab extends HookWidget { // TODO: use useMemoFuture var instancesFut = useMemoized(() { - var accountsStore = context.watch(); - var futures = accountsStore.users.keys .where((e) => !accountsStore.isAnonymousFor(e)) .map( @@ -44,8 +41,6 @@ class CommunitiesTab extends HookWidget { return Future.wait(futures); }); var communitiesFut = useMemoized(() { - var accountsStore = context.watch(); - var futures = accountsStore.users.keys .where((e) => !accountsStore.isAnonymousFor(e)) .map( @@ -239,6 +234,7 @@ class _CommunitySubscribeToggle extends HookWidget { var theme = Theme.of(context); var subbed = useState(true); var delayed = useDelayedLoading(const Duration(milliseconds: 500)); + final accountsStore = useAccountsStore(); handleTap() async { delayed.start(); @@ -247,10 +243,7 @@ class _CommunitySubscribeToggle extends HookWidget { await LemmyApi(instanceUrl).v1.followCommunity( communityId: communityId, follow: !subbed.value, - auth: context - .read() - .defaultTokenFor(instanceUrl) - .raw, + auth: accountsStore.defaultTokenFor(instanceUrl).raw, ); subbed.value = !subbed.value; } on Exception catch (err) { diff --git a/lib/pages/community.dart b/lib/pages/community.dart index b5fc289..0b3d660 100644 --- a/lib/pages/community.dart +++ b/lib/pages/community.dart @@ -6,12 +6,11 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:intl/intl.dart'; +import 'package:lemmur/hooks/stores.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart'; -import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart' as ul; import '../hooks/memo_future.dart'; -import '../stores/accounts_store.dart'; import '../util/api_extensions.dart'; import '../util/goto.dart'; import '../util/intl.dart'; @@ -49,8 +48,10 @@ class CommunityPage extends HookWidget { @override Widget build(BuildContext context) { final theme = Theme.of(context); + final accountsStore = useAccountsStore(); + var fullCommunitySnap = useMemoFuture(() { - final token = context.watch().defaultTokenFor(instanceUrl); + final token = accountsStore.defaultTokenFor(instanceUrl); if (communityId != null) { return LemmyApi(instanceUrl).v1.getCommunity( @@ -535,14 +536,14 @@ class _FollowButton extends HookWidget { final theme = Theme.of(context); final isSubbed = useState(community.subscribed ?? false); - final colorOnTopOfAccent = textColorBasedOnBackground(theme.accentColor); - final token = - context.watch().defaultTokenFor(community.instanceUrl); + final token = useAccountsStore().defaultTokenFor(community.instanceUrl); // TODO: use hook for handling spinner and pending final showSpinner = useState(false); final isPending = useState(false); + final colorOnTopOfAccent = textColorBasedOnBackground(theme.accentColor); + subscribe() async { if (token == null) { Scaffold.of(context).showSnackBar( diff --git a/lib/pages/profile_tab.dart b/lib/pages/profile_tab.dart index bdddc34..21af767 100644 --- a/lib/pages/profile_tab.dart +++ b/lib/pages/profile_tab.dart @@ -2,9 +2,8 @@ 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:provider/provider.dart'; +import 'package:lemmur/hooks/stores.dart'; -import '../stores/accounts_store.dart'; import '../util/api_extensions.dart'; import '../util/goto.dart'; import '../widgets/bottom_modal.dart'; @@ -17,10 +16,11 @@ class UserProfileTab extends HookWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); + final accountsStore = useAccountsStore(); return Observer( builder: (ctx) { - if (ctx.watch().hasNoAccount) { + if (accountsStore.hasNoAccount) { return Scaffold( body: Center( child: Column( @@ -40,7 +40,7 @@ class UserProfileTab extends HookWidget { ); } - var user = ctx.watch().defaultUser; + var user = accountsStore.defaultUser; return Scaffold( extendBodyBehindAppBar: true, @@ -71,10 +71,7 @@ class UserProfileTab extends HookWidget { builder: (_) { var userTags = []; - ctx - .read() - .users - .forEach((instanceUrl, value) { + accountsStore.users.forEach((instanceUrl, value) { value.forEach((username, _) { userTags.add('$username@$instanceUrl'); }); @@ -82,7 +79,7 @@ class UserProfileTab extends HookWidget { return Observer( builder: (ctx) { - var user = ctx.watch().defaultUser; + var user = accountsStore.defaultUser; var instanceUrl = user.instanceUrl; return BottomModal( @@ -96,7 +93,7 @@ class UserProfileTab extends HookWidget { groupValue: '${user.name}@$instanceUrl', onChanged: (selected) { var userTag = selected.split('@'); - ctx.read().setDefaultAccount( + accountsStore.setDefaultAccount( userTag[1], userTag[0]); Navigator.of(ctx).pop(); }, diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index 673c1b9..a25386b 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -2,10 +2,8 @@ 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:provider/provider.dart'; +import 'package:lemmur/hooks/stores.dart'; -import '../stores/accounts_store.dart'; -import '../stores/config_store.dart'; import '../util/goto.dart'; class SettingsPage extends StatelessWidget { @@ -49,6 +47,7 @@ class AppearanceConfigPage extends StatelessWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); + final configStore = useConfigStore(); return Scaffold( appBar: AppBar( @@ -66,16 +65,16 @@ class AppearanceConfigPage extends StatelessWidget { RadioListTile( value: theme, title: Text(theme.toString().split('.')[1]), - groupValue: ctx.watch().theme, + groupValue: configStore.theme, onChanged: (selected) { - ctx.read().theme = selected; + configStore.theme = selected; }, ), SwitchListTile( title: Text('AMOLED dark mode'), - value: ctx.watch().amoledDarkMode, + value: configStore.amoledDarkMode, onChanged: (checked) { - ctx.read().amoledDarkMode = checked; + configStore.amoledDarkMode = checked; }) ], ), @@ -90,6 +89,7 @@ class AccountsConfigPage extends HookWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); + final accountsStore = useAccountsStore(); return Scaffold( key: _scaffoldKey, @@ -112,7 +112,6 @@ class AccountsConfigPage extends HookWidget { ), body: Observer( builder: (ctx) { - var accountsStore = ctx.watch(); var theme = Theme.of(context); return ListView( @@ -168,15 +167,14 @@ class _AccountsConfigAddInstanceDialog extends HookWidget { Widget build(BuildContext context) { var instanceController = useTextEditingController(); useValueListenable(instanceController); + final accountsStore = useAccountsStore(); var loading = useState(false); handleOnAdd() async { try { loading.value = true; - await context - .read() - .addInstance(instanceController.text); + await accountsStore.addInstance(instanceController.text); scaffoldKey.currentState.hideCurrentSnackBar(); } on Exception catch (err) { scaffoldKey.currentState.showSnackBar(SnackBar( @@ -226,17 +224,18 @@ class _AccountsConfigAddAccountDialog extends HookWidget { var passwordController = useTextEditingController(); useValueListenable(usernameController); useValueListenable(passwordController); + final accountsStore = useAccountsStore(); var loading = useState(false); handleOnAdd() async { try { loading.value = true; - await context.read().addAccount( - instanceUrl, - usernameController.text, - passwordController.text, - ); + await accountsStore.addAccount( + instanceUrl, + usernameController.text, + passwordController.text, + ); } on Exception catch (err) { scaffoldKey.currentState.showSnackBar(SnackBar( content: Text(err.toString()),