added hook for stores

This commit is contained in:
shilangyu 2020-09-16 23:15:42 +02:00
parent d3eee1e837
commit c6b03c3aef
6 changed files with 71 additions and 69 deletions

8
lib/hooks/stores.dart Normal file
View File

@ -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<AccountsStore>();
ConfigStore useConfigStore() => useContext().watch<ConfigStore>();

View File

@ -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<void> main() async {
);
}
class MyApp extends StatelessWidget {
class MyApp extends HookWidget {
@override
Widget build(BuildContext context) => Observer(
builder: (ctx) {
var maybeAmoledColor =
ctx.watch<ConfigStore>().amoledDarkMode ? Colors.black : null;
Widget build(BuildContext context) {
final configStore = useConfigStore();
return MaterialApp(
title: 'Flutter Demo',
themeMode: ctx.watch<ConfigStore>().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 {

View File

@ -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<AccountsStore>();
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<AccountsStore>();
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<AccountsStore>();
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<AccountsStore>()
.defaultTokenFor(instanceUrl)
.raw,
auth: accountsStore.defaultTokenFor(instanceUrl).raw,
);
subbed.value = !subbed.value;
} on Exception catch (err) {

View File

@ -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<AccountsStore>().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<AccountsStore>().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(

View File

@ -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<AccountsStore>().hasNoAccount) {
if (accountsStore.hasNoAccount) {
return Scaffold(
body: Center(
child: Column(
@ -40,7 +40,7 @@ class UserProfileTab extends HookWidget {
);
}
var user = ctx.watch<AccountsStore>().defaultUser;
var user = accountsStore.defaultUser;
return Scaffold(
extendBodyBehindAppBar: true,
@ -71,10 +71,7 @@ class UserProfileTab extends HookWidget {
builder: (_) {
var userTags = <String>[];
ctx
.read<AccountsStore>()
.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<AccountsStore>().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<AccountsStore>().setDefaultAccount(
accountsStore.setDefaultAccount(
userTag[1], userTag[0]);
Navigator.of(ctx).pop();
},

View File

@ -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<ThemeMode>(
value: theme,
title: Text(theme.toString().split('.')[1]),
groupValue: ctx.watch<ConfigStore>().theme,
groupValue: configStore.theme,
onChanged: (selected) {
ctx.read<ConfigStore>().theme = selected;
configStore.theme = selected;
},
),
SwitchListTile(
title: Text('AMOLED dark mode'),
value: ctx.watch<ConfigStore>().amoledDarkMode,
value: configStore.amoledDarkMode,
onChanged: (checked) {
ctx.read<ConfigStore>().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<AccountsStore>();
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<AccountsStore>()
.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<AccountsStore>().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()),