♻️ Refactor profile page code

This commit is contained in:
LittleSheep 2024-09-27 00:02:08 +08:00
parent 2673c11046
commit bbee825cf4
2 changed files with 96 additions and 97 deletions

View File

@ -132,6 +132,63 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
} }
} }
Future<void> _subscribeToUser() async {
setState(() => _isSubscribing = true);
_subscription =
await Get.find<SubscriptionProvider>().subscribeToUser(_userinfo!.id);
setState(() => _isSubscribing = false);
}
Future<void> _unsubscribeFromUser() async {
setState(() => _isSubscribing = true);
await Get.find<SubscriptionProvider>().unsubscribeFromUser(_userinfo!.id);
_subscription = null;
setState(() => _isSubscribing = false);
}
Future<void> _makeFriend() async {
setState(() => _isMakingFriend = true);
try {
_relationship = await _relationshipProvider.makeFriend(widget.name);
context.showSnackbar(
'accountFriendRequestSent'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isMakingFriend = false);
}
}
Future<void> _blockUser() async {
setState(() => _isMakingFriend = true);
try {
_relationship = await _relationshipProvider.blockUser(widget.name);
context.showSnackbar(
'accountBlocked'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isMakingFriend = false);
}
}
Future<void> _unblockUser() async {
setState(() => _isMakingFriend = true);
try {
_relationship =
await _relationshipProvider.editRelation(_userinfo!.id, 1);
context.showSnackbar(
'accountUnblocked'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isMakingFriend = false);
}
}
int get _userSocialCreditPoints { int get _userSocialCreditPoints {
return _totalUpvote * 2 - _totalDownvote + _postController.postTotal.value; return _totalUpvote * 2 - _totalDownvote + _postController.postTotal.value;
} }
@ -170,23 +227,6 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
}); });
} }
Widget _buildStatisticsEntry(String label, String content) {
return Expanded(
child: Column(
children: [
Text(
label,
style: Theme.of(context).textTheme.bodySmall,
),
Text(
content,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_isBusy || _userinfo == null) { if (_isBusy || _userinfo == null) {
@ -239,15 +279,7 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
visualDensity: visualDensity:
VisualDensity(horizontal: -4, vertical: -2), VisualDensity(horizontal: -4, vertical: -2),
), ),
onPressed: _isSubscribing onPressed: _isSubscribing ? null : _subscribeToUser,
? null
: () async {
setState(() => _isSubscribing = true);
_subscription =
await Get.find<SubscriptionProvider>()
.subscribeToUser(_userinfo!.id);
setState(() => _isSubscribing = false);
},
icon: const Icon(Icons.add_circle_outline), icon: const Icon(Icons.add_circle_outline),
tooltip: 'subscribe'.tr, tooltip: 'subscribe'.tr,
) )
@ -257,37 +289,15 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
visualDensity: visualDensity:
VisualDensity(horizontal: -4, vertical: -2), VisualDensity(horizontal: -4, vertical: -2),
), ),
onPressed: _isSubscribing onPressed:
? null _isSubscribing ? null : _unsubscribeFromUser,
: () async {
setState(() => _isSubscribing = true);
await Get.find<SubscriptionProvider>()
.unsubscribeFromUser(_userinfo!.id);
_subscription = null;
setState(() => _isSubscribing = false);
},
icon: const Icon(Icons.remove_circle_outline), icon: const Icon(Icons.remove_circle_outline),
tooltip: 'unsubscribe'.tr, tooltip: 'unsubscribe'.tr,
), ),
if (_userinfo != null && _relationship == null) if (_userinfo != null && _relationship == null)
IconButton( IconButton(
icon: const Icon(Icons.person_add), icon: const Icon(Icons.person_add),
onPressed: _isMakingFriend onPressed: _isMakingFriend ? null : _makeFriend,
? null
: () async {
setState(() => _isMakingFriend = true);
try {
_relationship = await _relationshipProvider
.makeFriend(widget.name);
context.showSnackbar(
'accountFriendRequestSent'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isMakingFriend = false);
}
},
tooltip: 'friendAdd'.tr, tooltip: 'friendAdd'.tr,
) )
else else
@ -315,9 +325,8 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
children: [ children: [
ListView( ListView(
padding: EdgeInsets.zero, padding: const EdgeInsets.only(top: 16, bottom: 16),
children: [ children: [
const Gap(16),
CenteredContainer( CenteredContainer(
child: AccountHeadingWidget( child: AccountHeadingWidget(
name: _userinfo!.name, name: _userinfo!.name,
@ -484,25 +493,8 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
vertical: -2, vertical: -2,
), ),
), ),
onPressed: _isMakingFriend onPressed:
? null _isMakingFriend ? null : _blockUser,
: () async {
setState(
() => _isMakingFriend = true);
try {
_relationship =
await _relationshipProvider
.blockUser(widget.name);
context.showSnackbar(
'accountBlocked'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() =>
_isMakingFriend = false);
}
},
icon: const Icon( icon: const Icon(
Icons.block, Icons.block,
size: 16, size: 16,
@ -517,26 +509,8 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
vertical: -2, vertical: -2,
), ),
), ),
onPressed: _isMakingFriend onPressed:
? null _isMakingFriend ? null : _unblockUser,
: () async {
setState(
() => _isMakingFriend = true);
try {
_relationship =
await _relationshipProvider
.editRelation(
_userinfo!.id, 1);
context.showSnackbar(
'accountUnblocked'.tr,
);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() =>
_isMakingFriend = false);
}
},
icon: const Icon( icon: const Icon(
Icons.add_circle_outline, Icons.add_circle_outline,
size: 16, size: 16,
@ -564,7 +538,7 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceAround, mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [ children: [
_buildStatisticsEntry( _StatsWidget(
'totalSocialCreditPoints'.tr, 'totalSocialCreditPoints'.tr,
_userinfo != null _userinfo != null
? _userSocialCreditPoints.toString() ? _userSocialCreditPoints.toString()
@ -577,16 +551,16 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
mainAxisAlignment: MainAxisAlignment.spaceAround, mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [ children: [
Obx( Obx(
() => _buildStatisticsEntry( () => _StatsWidget(
'totalPostCount'.tr, 'totalPostCount'.tr,
_postController.postTotal.value.toString(), _postController.postTotal.value.toString(),
), ),
), ),
_buildStatisticsEntry( _StatsWidget(
'totalUpvote'.tr, 'totalUpvote'.tr,
_totalUpvote.toString(), _totalUpvote.toString(),
), ),
_buildStatisticsEntry( _StatsWidget(
'totalDownvote'.tr, 'totalDownvote'.tr,
_totalDownvote.toString(), _totalDownvote.toString(),
), ),
@ -684,3 +658,28 @@ class _AccountProfilePageState extends State<AccountProfilePage> {
); );
} }
} }
class _StatsWidget extends StatelessWidget {
final String label;
final String content;
const _StatsWidget(this.label, this.content);
@override
Widget build(BuildContext context) {
return Expanded(
child: Column(
children: [
Text(
label,
style: Theme.of(context).textTheme.bodySmall,
),
Text(
content,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
);
}
}

View File

@ -113,7 +113,7 @@ class _AccountProfilePopupState extends State<AccountProfilePopup> {
borderRadius: BorderRadius.all(Radius.circular(8)), borderRadius: BorderRadius.all(Radius.circular(8)),
), ),
title: Text('visitProfilePage'.tr), title: Text('visitProfilePage'.tr),
subtitle: Text('learMoreAboutPerson'.tr), subtitle: Text('learnMoreAboutPerson'.tr),
visualDensity: visualDensity:
const VisualDensity(horizontal: -4, vertical: -2), const VisualDensity(horizontal: -4, vertical: -2),
trailing: const Icon(Icons.chevron_right), trailing: const Icon(Icons.chevron_right),