This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlogged_in_action.dart
58 lines (52 loc) · 1.84 KB
/
logged_in_action.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';
import '../pages/settings/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.
VoidCallback Function(
void Function(Jwt token) action, [
String? message,
]) useAnyLoggedInAction() {
final context = useContext();
final store = useAccountsStore();
return (action, [message]) {
if (store.hasNoAccount) {
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())),
));
};
}
return () => action(store.defaultUserData!.jwt);
};
}
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())),
));
};
}
final token = store.defaultUserDataFor(instanceHost)!.jwt;
return () => action(token);
};
}