lemmur-app-android/lib/hooks/logged_in_action.dart

38 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
import '../pages/settings.dart';
import '../util/goto.dart';
import 'stores.dart';
/// If user has an account for the given instance the passed wrapper will call
/// the passed action with a Jwt token. Otherwise the action is ignored and a
/// Snackbar is rendered. If [any] is set to true, this check is performed for
/// all instances and if any of them have an account, the wrapped action will be
/// called with a null token.
2021-01-03 21:03:32 +01:00
VoidCallback Function(
void Function(Jwt token) action, [
String message,
]) useLoggedInAction(String instanceHost, {bool any = false}) {
final context = useContext();
final store = useAccountsStore();
2021-01-03 21:03:32 +01:00
return (action, [message]) {
if (any && store.hasNoAccount ||
!any && store.isAnonymousFor(instanceHost)) {
return () {
Scaffold.of(context).showSnackBar(SnackBar(
2020-09-18 21:24:33 +02:00
content: Text(message ?? 'you have to be logged in to do that'),
action: SnackBarAction(
label: 'log in',
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
));
};
}
final token = store.defaultTokenFor(instanceHost);
return () => action(token);
};
}