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

33 lines
912 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 have to be logged in to do that'),
action: SnackBarAction(
label: 'log in',
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
));
};
}
final token = store.defaultTokenFor(instanceUrl);
return () => action(token);
};
}