2020-09-18 15:48:37 +02:00
|
|
|
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';
|
|
|
|
|
2020-09-22 23:17:42 +02:00
|
|
|
/// 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.
|
2020-09-18 17:30:32 +02:00
|
|
|
Function(
|
|
|
|
Function(Jwt token) action, [
|
2020-09-18 15:48:37 +02:00
|
|
|
String message,
|
2020-09-22 23:17:42 +02:00
|
|
|
]) useLoggedInAction(String instanceUrl, {bool any = false}) {
|
2020-09-18 15:48:37 +02:00
|
|
|
final context = useContext();
|
|
|
|
final store = useAccountsStore();
|
|
|
|
|
2020-09-18 17:30:32 +02:00
|
|
|
return (Function(Jwt token) action, [message]) {
|
2020-09-22 23:17:42 +02:00
|
|
|
if (any && store.hasNoAccount ||
|
|
|
|
!any && store.isAnonymousFor(instanceUrl)) {
|
2020-09-18 15:48:37 +02:00
|
|
|
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'),
|
2020-09-18 15:48:37 +02:00
|
|
|
action: SnackBarAction(
|
|
|
|
label: 'log in',
|
|
|
|
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
final token = store.defaultTokenFor(instanceUrl);
|
|
|
|
return () => action(token);
|
|
|
|
};
|
|
|
|
}
|