1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-15 10:04:13 +01:00
lemmur-app-android/lib/hooks/logged_in_action.dart
2020-09-18 17:30:32 +02:00

33 lines
913 B
Dart

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart';
import '../pages/settings.dart';
import '../util/goto.dart';
import 'stores.dart';
Function(
Function(Jwt token) action, [
String message,
]) useLoggedInAction(
String instanceUrl,
) {
final context = useContext();
final store = useAccountsStore();
return (Function(Jwt token) action, [message]) {
if (store.isAnonymousFor(instanceUrl)) {
return () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(message ?? 'you ought to be logged in to do that'),
action: SnackBarAction(
label: 'log in',
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
));
};
}
final token = store.defaultTokenFor(instanceUrl);
return () => action(token);
};
}