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

33 lines
912 B
Dart
Raw Normal View History

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-18 17:30:32 +02:00
Function(
Function(Jwt token) action, [
String message,
2020-09-18 17:30:32 +02:00
]) useLoggedInAction(
String instanceUrl,
) {
final context = useContext();
final store = useAccountsStore();
2020-09-18 17:30:32 +02:00
return (Function(Jwt token) action, [message]) {
if (store.isAnonymousFor(instanceUrl)) {
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(instanceUrl);
return () => action(token);
};
}