From 20ebcd0dfa8873264063d55696d21303f2c88db8 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 30 Jan 2021 15:22:06 +0000 Subject: [PATCH] Add auth to user profile fetching, fixes #118 --- lib/widgets/user_profile.dart | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/widgets/user_profile.dart b/lib/widgets/user_profile.dart index 7b62dde..279e5fc 100644 --- a/lib/widgets/user_profile.dart +++ b/lib/widgets/user_profile.dart @@ -2,6 +2,7 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:intl/intl.dart'; +import 'package:lemmur/hooks/memo_future.dart'; import 'package:lemmy_api_client/v2.dart'; import 'package:timeago/timeago.dart' as timeago; @@ -17,21 +18,35 @@ import 'sortable_infinite_list.dart'; /// Shared widget of UserPage and ProfileTab class UserProfile extends HookWidget { - final Future _userDetails; final String instanceHost; + final int userId; - UserProfile({@required int userId, @required this.instanceHost}) - : _userDetails = LemmyApiV2(instanceHost).run(GetUserDetails( - userId: userId, savedOnly: false, sort: SortType.active)); + final FullUserView _fullUserView; - UserProfile.fromFullUserView(FullUserView fullUserView) - : _userDetails = Future.value(fullUserView), - instanceHost = fullUserView.instanceHost; + const UserProfile({@required this.userId, @required this.instanceHost}) + : assert(userId != null), + assert(instanceHost != null), + _fullUserView = null; + + UserProfile.fromFullUserView(this._fullUserView) + : assert(_fullUserView != null), + userId = _fullUserView.userView.user.id, + instanceHost = _fullUserView.instanceHost; @override Widget build(BuildContext context) { final theme = Theme.of(context); - final userDetailsSnap = useFuture(_userDetails, preserveState: false); + final accountsStore = useAccountsStore(); + final userDetailsSnap = useMemoFuture(() async { + if (_fullUserView != null) return _fullUserView; + + return await LemmyApiV2(instanceHost).run(GetUserDetails( + userId: userId, + savedOnly: false, + sort: SortType.active, + auth: accountsStore.defaultTokenFor(instanceHost)?.raw, + )); + }); if (!userDetailsSnap.hasData) { return const Center(child: CircularProgressIndicator()); @@ -85,6 +100,7 @@ class UserProfile extends HookWidget { sort: SortType.active, page: page, limit: batchSize, + auth: accountsStore.defaultTokenFor(instanceHost)?.raw, )) .then((val) => val.posts), ), @@ -96,6 +112,7 @@ class UserProfile extends HookWidget { sort: SortType.active, page: page, limit: batchSize, + auth: accountsStore.defaultTokenFor(instanceHost)?.raw, )) .then((val) => val.comments), ),