2020-09-18 15:48:37 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2021-04-05 20:14:39 +02:00
|
|
|
import 'package:lemmy_api_client/v3.dart';
|
2020-09-18 15:48:37 +02:00
|
|
|
|
2021-09-14 23:45:26 +02:00
|
|
|
import '../pages/settings/settings.dart';
|
2020-09-18 15:48:37 +02:00
|
|
|
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.
|
2021-01-03 21:03:32 +01:00
|
|
|
|
|
|
|
VoidCallback Function(
|
|
|
|
void Function(Jwt token) action, [
|
2021-04-09 00:11:44 +02:00
|
|
|
String? message,
|
|
|
|
]) useAnyLoggedInAction() {
|
2020-09-18 15:48:37 +02:00
|
|
|
final context = useContext();
|
|
|
|
final store = useAccountsStore();
|
|
|
|
|
2021-01-03 21:03:32 +01:00
|
|
|
return (action, [message]) {
|
2021-04-09 00:11:44 +02:00
|
|
|
if (store.hasNoAccount) {
|
2020-09-18 15:48:37 +02:00
|
|
|
return () {
|
2021-03-10 08:34:30 +01:00
|
|
|
ScaffoldMessenger.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())),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
}
|
2021-04-11 18:27:22 +02:00
|
|
|
return () => action(store.defaultUserData!.jwt);
|
2021-04-09 00:11:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
VoidCallback Function(
|
|
|
|
void Function(Jwt token) action, [
|
|
|
|
String? message,
|
|
|
|
]) useLoggedInAction(String instanceHost) {
|
|
|
|
final context = useContext();
|
|
|
|
final store = useAccountsStore();
|
|
|
|
|
|
|
|
return (action, [message]) {
|
|
|
|
if (store.isAnonymousFor(instanceHost)) {
|
|
|
|
return () {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(message ?? 'you have to be logged in to do that'),
|
|
|
|
action: SnackBarAction(
|
|
|
|
label: 'log in',
|
|
|
|
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
}
|
2021-04-11 18:27:22 +02:00
|
|
|
final token = store.defaultUserDataFor(instanceHost)!.jwt;
|
2020-09-18 15:48:37 +02:00
|
|
|
return () => action(token);
|
|
|
|
};
|
|
|
|
}
|