1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-12 00:26:23 +01:00
lemmur-app-android/lib/pages/user.dart
Marcin Wojnarowski c7f037de0f
Feature/routing refactor (#258)
* Use MaterialPageRoute

* Use T.route
2021-09-30 12:55:23 +02:00

82 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';
import '../hooks/logged_in_action.dart';
import '../util/share.dart';
import '../widgets/user_profile.dart';
import 'write_message.dart';
/// Page showing posts, comments, and general info about a user.
class UserPage extends HookWidget {
final int? userId;
final String instanceHost;
final Future<FullPersonView> _userDetails;
UserPage({required this.userId, required this.instanceHost})
: _userDetails = LemmyApiV3(instanceHost).run(GetPersonDetails(
personId: userId, savedOnly: true, sort: SortType.active));
UserPage.fromName({required this.instanceHost, required String username})
: userId = null,
_userDetails = LemmyApiV3(instanceHost).run(GetPersonDetails(
username: username, savedOnly: true, sort: SortType.active));
@override
Widget build(BuildContext context) {
final userDetailsSnap = useFuture(_userDetails);
final body = () {
if (userDetailsSnap.hasData) {
return UserProfile.fromFullPersonView(userDetailsSnap.data!);
} else if (userDetailsSnap.hasError) {
return const Center(child: Text('Could not find that user.'));
} else {
return const Center(child: CircularProgressIndicator());
}
}();
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
actions: [
if (userDetailsSnap.hasData) ...[
SendMessageButton(userDetailsSnap.data!.personView.person),
IconButton(
icon: const Icon(Icons.share),
onPressed: () => share(
userDetailsSnap.data!.personView.person.actorId,
context: context,
),
),
]
],
),
body: body,
);
}
}
class SendMessageButton extends HookWidget {
final PersonSafe 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) => Navigator.of(context).push(
WriteMessagePage.sendRoute(
instanceHost: user.instanceHost,
recipient: user,
),
),
),
);
}
}