lemmur-app-android/lib/pages/user.dart

83 lines
2.5 KiB
Dart
Raw Normal View History

2020-09-08 21:08:50 +02:00
import 'package:esys_flutter_share/esys_flutter_share.dart';
2021-02-24 20:52:18 +01:00
import 'package:flutter/cupertino.dart';
2020-09-08 21:08:50 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
2020-09-08 21:08:50 +02:00
2021-02-24 20:52:18 +01:00
import '../hooks/logged_in_action.dart';
2020-09-08 21:08:50 +02:00
import '../widgets/user_profile.dart';
2021-02-24 20:52:18 +01:00
import 'write_message.dart';
2020-09-08 21:08:50 +02:00
2020-09-30 19:05:00 +02:00
/// Page showing posts, comments, and general info about a user.
2020-09-08 21:08:50 +02:00
class UserPage extends HookWidget {
final int userId;
final String instanceHost;
2021-01-24 20:01:55 +01:00
final Future<FullUserView> _userDetails;
2020-09-08 21:08:50 +02:00
UserPage({@required this.userId, @required this.instanceHost})
2020-09-08 21:08:50 +02:00
: assert(userId != null),
assert(instanceHost != null),
2021-01-24 20:01:55 +01:00
_userDetails = LemmyApiV2(instanceHost).run(GetUserDetails(
userId: userId, savedOnly: true, sort: SortType.active));
2020-09-30 23:43:21 +02:00
UserPage.fromName({@required this.instanceHost, @required String username})
: assert(instanceHost != null),
assert(username != null),
userId = null,
2021-01-24 20:01:55 +01:00
_userDetails = LemmyApiV2(instanceHost).run(GetUserDetails(
username: username, savedOnly: true, sort: SortType.active));
2020-09-08 21:08:50 +02:00
@override
Widget build(BuildContext context) {
2020-09-30 23:43:21 +02:00
final userDetailsSnap = useFuture(_userDetails);
2020-09-08 21:08:50 +02:00
2020-09-16 23:22:04 +02:00
final body = () {
2020-09-30 23:43:21 +02:00
if (userDetailsSnap.hasData) {
2021-01-26 22:18:53 +01:00
return UserProfile.fromFullUserView(userDetailsSnap.data);
2020-09-30 23:43:21 +02:00
} else if (userDetailsSnap.hasError) {
2021-01-03 19:43:39 +01:00
return const Center(child: Text('Could not find that user.'));
2020-09-08 22:56:45 +02:00
} else {
2021-01-03 19:43:39 +01:00
return const Center(child: CircularProgressIndicator());
2020-09-08 22:56:45 +02:00
}
}();
2020-09-08 21:08:50 +02:00
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
actions: [
2020-09-30 23:43:21 +02:00
if (userDetailsSnap.hasData) ...[
2021-02-24 20:52:18 +01:00
SendMessageButton(userDetailsSnap.data.userView.user),
2020-09-08 21:08:50 +02:00
IconButton(
2021-01-03 19:43:39 +01:00
icon: const Icon(Icons.share),
2020-09-30 23:43:21 +02:00
onPressed: () => Share.text('Share user',
2021-01-24 20:01:55 +01:00
userDetailsSnap.data.userView.user.actorId, 'text/plain'),
2021-02-24 20:52:18 +01:00
),
2020-09-08 21:08:50 +02:00
]
],
),
2020-09-08 22:56:45 +02:00
body: body,
2020-09-08 21:08:50 +02:00
);
}
}
2021-02-24 20:52:18 +01:00
class SendMessageButton extends HookWidget {
final UserSafe user;
const SendMessageButton(this.user);
@override
Widget build(BuildContext context) {
final loggedInAction = useLoggedInAction(user.instanceHost);
return IconButton(
icon: const Icon(Icons.email),
onPressed: loggedInAction((token) => showCupertinoModalPopup(
context: context,
builder: (_) => WriteMessagePage.send(
instanceHost: user.instanceHost,
recipient: user,
))),
);
}
}