Solian/lib/screens/account/profile_page.dart

694 lines
28 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:fl_chart/fl_chart.dart';
2024-07-26 08:53:05 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-07-26 08:53:05 +00:00
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:intl/intl.dart';
2024-07-26 08:53:05 +00:00
import 'package:solian/controllers/post_list_controller.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/account.dart';
import 'package:solian/models/attachment.dart';
import 'package:solian/models/daily_sign.dart';
2024-07-26 08:53:05 +00:00
import 'package:solian/models/pagination.dart';
import 'package:solian/models/post.dart';
import 'package:solian/models/relations.dart';
2024-09-16 18:14:23 +00:00
import 'package:solian/models/subscription.dart';
2024-09-08 04:32:21 +00:00
import 'package:solian/providers/account_status.dart';
import 'package:solian/providers/relation.dart';
2024-09-16 18:14:23 +00:00
import 'package:solian/providers/subscription.dart';
2024-07-26 08:53:05 +00:00
import 'package:solian/services.dart';
import 'package:solian/theme.dart';
import 'package:solian/widgets/account/account_avatar.dart';
2024-09-08 04:32:21 +00:00
import 'package:solian/widgets/account/account_heading.dart';
2024-07-26 08:53:05 +00:00
import 'package:solian/widgets/app_bar_leading.dart';
import 'package:solian/widgets/attachments/attachment_list.dart';
import 'package:solian/widgets/daily_sign/history_chart.dart';
import 'package:solian/widgets/posts/post_list.dart';
2024-07-31 17:21:27 +00:00
import 'package:solian/widgets/posts/post_warped_list.dart';
import 'package:solian/widgets/reports/abuse_report.dart';
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
2024-07-26 08:53:05 +00:00
import 'package:solian/widgets/sized_container.dart';
class AccountProfilePage extends StatefulWidget {
final String name;
const AccountProfilePage({super.key, required this.name});
@override
State<AccountProfilePage> createState() => _AccountProfilePageState();
}
class _AccountProfilePageState extends State<AccountProfilePage> {
late final RelationshipProvider _relationshipProvider;
2024-07-26 08:53:05 +00:00
late final PostListController _postController;
final PagingController<int, Attachment> _albumPagingController =
PagingController(firstPageKey: 0);
bool _isBusy = true;
bool _isMakingFriend = false;
2024-09-16 18:14:23 +00:00
bool _isSubscribing = false;
2024-07-26 09:35:54 +00:00
bool _showMature = false;
2024-07-26 08:53:05 +00:00
Account? _userinfo;
2024-09-16 18:14:23 +00:00
Subscription? _subscription;
Relationship? _relationship;
List<Post> _pinnedPosts = List.empty();
List<DailySignRecord> _dailySignRecords = List.empty();
int _totalUpvote = 0, _totalDownvote = 0;
2024-07-26 08:53:05 +00:00
2024-09-16 18:14:23 +00:00
Future<void> _getSubscription() async {
setState(() => _isSubscribing = true);
_subscription = await Get.find<SubscriptionProvider>()
.getSubscriptionOnUser(_userinfo!.id);
setState(() => _isSubscribing = false);
}
Future<void> _getRelationship() async {
setState(() => _isBusy = true);
final relations = Get.find<RelationshipProvider>();
_relationship = await relations.getRelationship(_userinfo!.id);
setState(() => _isBusy = false);
}
2024-08-03 06:00:52 +00:00
Future<void> _getUserinfo() async {
2024-07-26 08:53:05 +00:00
setState(() => _isBusy = true);
var client = await ServiceFinder.configureClient('id');
var resp = await client.get('/users/${widget.name}');
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString).then((_) {
Navigator.pop(context);
});
} else {
2024-07-26 08:53:05 +00:00
_userinfo = Account.fromJson(resp.body);
}
client = await ServiceFinder.configureClient('co');
resp = await client.get('/users/${widget.name}');
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString).then((_) {
Navigator.pop(context);
});
2024-07-26 08:53:05 +00:00
} else {
_totalUpvote = resp.body['total_upvote'];
_totalDownvote = resp.body['total_downvote'];
}
setState(() => _isBusy = false);
}
2024-09-16 18:14:23 +00:00
Future<void> _getPinnedPosts() async {
final client = await ServiceFinder.configureClient('co');
final resp = await client.get('/users/${widget.name}/pin');
if (resp.statusCode != 200) {
2024-07-26 08:53:05 +00:00
context.showErrorDialog(resp.bodyString).then((_) {
Navigator.pop(context);
});
} else {
setState(() {
_pinnedPosts =
resp.body.map((x) => Post.fromJson(x)).toList().cast<Post>();
});
2024-07-26 08:53:05 +00:00
}
}
Future<void> _getDailySignRecords() async {
final client = await ServiceFinder.configureClient('id');
final resp = await client.get('/users/${widget.name}/daily?take=14');
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString).then((_) {
Navigator.pop(context);
});
} else {
final result = PaginationResult.fromJson(resp.body);
setState(() {
_dailySignRecords = List.from(
result.data?.map((x) => DailySignRecord.fromJson(x)) ?? [],
);
});
}
}
2024-09-26 16:02:08 +00:00
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 {
2024-09-07 17:48:01 +00:00
return _totalUpvote * 2 - _totalDownvote + _postController.postTotal.value;
2024-07-26 08:53:05 +00:00
}
@override
void initState() {
super.initState();
_relationshipProvider = Get.find();
2024-07-26 08:53:05 +00:00
_postController = PostListController(author: widget.name);
_albumPagingController.addPageRequestListener((pageKey) async {
2024-09-16 03:57:16 +00:00
final client = await ServiceFinder.configureClient('files');
2024-09-07 17:48:01 +00:00
final resp = await client.get(
'/attachments?take=10&offset=$pageKey&author=${widget.name}&original=true',
);
2024-07-26 08:53:05 +00:00
if (resp.statusCode == 200) {
final result = PaginationResult.fromJson(resp.body);
2024-07-26 09:35:54 +00:00
final out = result.data
?.map((e) => Attachment.fromJson(e))
.where((x) => x.mimetype.split('/').firstOrNull == 'image')
.toList();
if (out != null && result.data!.length >= 10) {
2024-07-26 08:53:05 +00:00
_albumPagingController.appendPage(out, pageKey + out.length);
} else if (out != null) {
_albumPagingController.appendLastPage(out);
}
} else {
_albumPagingController.error = resp.bodyString;
}
});
2024-09-16 18:14:23 +00:00
_getUserinfo().then((_) {
_getRelationship();
2024-09-16 18:14:23 +00:00
_getSubscription();
_getPinnedPosts();
_getDailySignRecords();
2024-09-16 18:14:23 +00:00
});
}
2024-07-26 08:53:05 +00:00
@override
Widget build(BuildContext context) {
if (_isBusy || _userinfo == null) {
2024-10-06 17:47:34 +00:00
return RootContainer(
child: const Center(
child: CircularProgressIndicator(),
),
);
2024-07-26 08:53:05 +00:00
}
2024-10-06 09:37:07 +00:00
return RootContainer(
2024-07-26 08:53:05 +00:00
child: DefaultTabController(
2024-09-08 04:32:21 +00:00
length: 3,
2024-07-26 08:53:05 +00:00
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
centerTitle: false,
floating: true,
toolbarHeight: AppTheme.toolbarHeight(context),
2024-07-26 08:53:05 +00:00
leadingWidth: 24,
2024-07-26 09:35:54 +00:00
automaticallyImplyLeading: false,
2024-09-22 14:56:28 +00:00
flexibleSpace: SizedBox(
height: 56,
child: Row(
children: [
2024-10-06 17:47:34 +00:00
AppBarLeadingButton.adaptive(
context,
forceBack: true,
) ??
const Gap(8),
2024-09-22 14:56:28 +00:00
const Gap(8),
if (_userinfo != null)
AccountAvatar(content: _userinfo!.avatar, radius: 16),
const Gap(12),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (_userinfo != null)
Text(
_userinfo!.nick,
style: Theme.of(context).textTheme.bodyLarge,
),
if (_userinfo != null)
Text(
'@${_userinfo!.name}',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
2024-07-26 08:53:05 +00:00
),
2024-09-22 14:56:28 +00:00
if (_userinfo != null && _subscription == null)
IconButton(
2024-09-22 14:56:28 +00:00
style: const ButtonStyle(
visualDensity:
VisualDensity(horizontal: -4, vertical: -2),
),
2024-09-26 16:02:08 +00:00
onPressed: _isSubscribing ? null : _subscribeToUser,
icon: const Icon(Icons.add_circle_outline),
tooltip: 'subscribe'.tr,
2024-09-22 14:56:28 +00:00
)
else if (_userinfo != null)
IconButton(
2024-09-22 14:56:28 +00:00
style: const ButtonStyle(
visualDensity:
VisualDensity(horizontal: -4, vertical: -2),
),
2024-09-26 16:02:08 +00:00
onPressed:
_isSubscribing ? null : _unsubscribeFromUser,
icon: const Icon(Icons.remove_circle_outline),
tooltip: 'unsubscribe'.tr,
2024-09-16 18:14:23 +00:00
),
if (_userinfo != null && _relationship == null)
2024-09-22 14:56:28 +00:00
IconButton(
icon: const Icon(Icons.person_add),
2024-09-26 16:02:08 +00:00
onPressed: _isMakingFriend ? null : _makeFriend,
tooltip: 'friendAdd'.tr,
2024-09-22 14:56:28 +00:00
)
else
const IconButton(
icon: Icon(Icons.handshake),
onPressed: null,
2024-09-16 18:14:23 +00:00
),
2024-09-22 14:56:28 +00:00
SizedBox(
width: AppTheme.isLargeScreen(context) ? 8 : 16,
2024-09-16 18:14:23 +00:00
),
2024-09-22 14:56:28 +00:00
],
),
).paddingOnly(top: MediaQuery.of(context).padding.top),
2024-07-26 08:53:05 +00:00
bottom: TabBar(
tabs: [
2024-09-08 04:32:21 +00:00
Tab(text: 'profilePage'.tr),
2024-07-26 08:53:05 +00:00
Tab(text: 'profilePosts'.tr),
Tab(text: 'profileAlbum'.tr),
],
),
)
];
},
body: TabBarView(
physics: const NeverScrollableScrollPhysics(),
children: [
2024-09-22 14:56:28 +00:00
ListView(
2024-09-26 16:02:08 +00:00
padding: const EdgeInsets.only(top: 16, bottom: 16),
2024-09-08 04:32:21 +00:00
children: [
2024-09-22 14:56:28 +00:00
CenteredContainer(
child: AccountHeadingWidget(
name: _userinfo!.name,
nick: _userinfo!.nick,
desc: _userinfo!.description,
badges: _userinfo!.badges,
banner: _userinfo!.banner,
avatar: _userinfo!.avatar,
status: Get.find<StatusProvider>()
.getSomeoneStatus(_userinfo!.name),
detail: _userinfo,
profile: _userinfo!.profile,
extraWidgets: [
if (_dailySignRecords.isNotEmpty)
Card(
child: SizedBox(
height: 180,
width:
max(640, MediaQuery.of(context).size.width),
child: LineChart(
LineChartData(
lineBarsData: [
LineChartBarData(
isCurved: true,
isStrokeCapRound: true,
isStrokeJoinRound: true,
color:
Theme.of(context).colorScheme.primary,
belowBarData: BarAreaData(
show: true,
gradient: LinearGradient(
colors: List.filled(
_dailySignRecords.length,
Theme.of(context)
.colorScheme
.primary
.withOpacity(0.3),
).toList(),
),
),
2024-09-22 14:56:28 +00:00
spots: _dailySignRecords
.map(
(x) => FlSpot(
x.createdAt
.copyWith(
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
)
.millisecondsSinceEpoch
.toDouble(),
x.resultTier.toDouble(),
),
)
.toList(),
)
],
lineTouchData: LineTouchData(
touchTooltipData: LineTouchTooltipData(
getTooltipItems: (spots) => spots
.map((spot) => LineTooltipItem(
'${DailySignHistoryChartDialog.signSymbols[spot.y.toInt()]}\n${DateFormat('MM/dd').format(DateTime.fromMillisecondsSinceEpoch(spot.x.toInt()))}',
TextStyle(
color: Theme.of(context)
.colorScheme
.onSurface,
),
))
.toList(),
getTooltipColor: (_) => Theme.of(context)
.colorScheme
.surfaceContainerHigh,
),
),
2024-09-22 14:56:28 +00:00
titlesData: FlTitlesData(
topTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
rightTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 40,
interval: 1,
getTitlesWidget: (value, _) => Align(
alignment: Alignment.centerRight,
child: Text(
DailySignHistoryChartDialog
.signSymbols[value.toInt()],
textAlign: TextAlign.right,
).paddingOnly(right: 8),
),
),
),
2024-09-22 14:56:28 +00:00
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 28,
interval: 86400000,
getTitlesWidget: (value, _) => Text(
DateFormat('dd').format(
DateTime.fromMillisecondsSinceEpoch(
value.toInt(),
),
),
2024-09-22 14:56:28 +00:00
textAlign: TextAlign.center,
).paddingOnly(top: 8),
),
),
),
2024-09-22 14:56:28 +00:00
gridData: const FlGridData(show: false),
borderData: FlBorderData(show: false),
),
),
2024-09-22 14:56:28 +00:00
).marginOnly(
right: 24,
left: 12,
bottom: 8,
top: 24,
),
2024-09-22 14:56:28 +00:00
)
],
appendWidgets: [
Card(
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 8,
),
width: double.maxFinite,
child: Wrap(
alignment: WrapAlignment.spaceAround,
children: [
TextButton.icon(
style: const ButtonStyle(
visualDensity: VisualDensity(
horizontal: -4,
vertical: -2,
),
),
onPressed: () {
showDialog(
context: context,
builder: (context) => AbuseReportDialog(
resourceId: 'user:${_userinfo!.id}',
),
);
},
icon: const Icon(
Icons.flag,
size: 16,
),
label: Text('reportAbuse'.tr),
),
if (_relationship?.status != 2)
TextButton.icon(
style: const ButtonStyle(
visualDensity: VisualDensity(
horizontal: -4,
vertical: -2,
),
),
2024-09-26 16:02:08 +00:00
onPressed:
_isMakingFriend ? null : _blockUser,
icon: const Icon(
Icons.block,
size: 16,
),
label: Text('blockUser'.tr),
)
else
TextButton.icon(
style: const ButtonStyle(
visualDensity: VisualDensity(
horizontal: -4,
vertical: -2,
),
),
2024-09-26 16:02:08 +00:00
onPressed:
_isMakingFriend ? null : _unblockUser,
icon: const Icon(
Icons.add_circle_outline,
size: 16,
),
label: Text('unblockUser'.tr),
),
],
),
),
),
],
2024-09-22 14:56:28 +00:00
),
2024-09-08 04:32:21 +00:00
),
],
),
2024-07-26 08:53:05 +00:00
RefreshIndicator(
onRefresh: () => Future.wait([
_postController.reloadAllOver(),
2024-09-16 18:14:23 +00:00
_getPinnedPosts(),
]),
2024-07-26 08:53:05 +00:00
child: CustomScrollView(slivers: [
SliverToBoxAdapter(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
2024-09-26 16:02:08 +00:00
_StatsWidget(
'totalSocialCreditPoints'.tr,
_userinfo != null
? _userSocialCreditPoints.toString()
: 0.toString(),
),
],
),
const Gap(16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Obx(
2024-09-26 16:02:08 +00:00
() => _StatsWidget(
'totalPostCount'.tr,
_postController.postTotal.value.toString(),
),
),
2024-09-26 16:02:08 +00:00
_StatsWidget(
'totalUpvote'.tr,
_totalUpvote.toString(),
),
2024-09-26 16:02:08 +00:00
_StatsWidget(
'totalDownvote'.tr,
_totalDownvote.toString(),
),
],
),
],
).paddingOnly(top: 16, bottom: 12),
),
const SliverToBoxAdapter(
child: Divider(thickness: 0.3, height: 0.3),
),
SliverList.separated(
itemCount: _pinnedPosts.length,
itemBuilder: (context, idx) {
final element = _pinnedPosts[idx];
return Material(
color:
Theme.of(context).colorScheme.surfaceContainerLow,
child: PostListEntryWidget(
2024-08-03 06:00:52 +00:00
backgroundColor:
Theme.of(context).colorScheme.surfaceContainerLow,
item: element,
isClickable: true,
isNestedClickable: true,
isShowEmbed: true,
2024-09-16 15:35:44 +00:00
showFeaturedReply: true,
onUpdate: () {
_postController.reloadAllOver();
},
),
);
},
separatorBuilder: (context, idx) =>
const Divider(thickness: 0.3, height: 0.3),
),
2024-07-26 08:53:05 +00:00
if (_userinfo == null)
const SliverFillRemaining(
child: Center(child: CircularProgressIndicator()),
),
if (_userinfo != null)
2024-07-31 17:21:27 +00:00
PostWarpedListWidget(
isPinned: false,
2024-07-26 08:53:05 +00:00
controller: _postController.pagingController,
2024-08-04 10:13:59 +00:00
onUpdate: () => _postController.reloadAllOver(),
2024-07-26 08:53:05 +00:00
),
]),
),
CenteredContainer(
child: RefreshIndicator(
onRefresh: () => Future.sync(
() => _albumPagingController.refresh(),
),
2024-07-26 08:53:05 +00:00
child: PagedGridView<int, Attachment>(
2024-07-26 09:35:54 +00:00
padding: EdgeInsets.zero,
2024-07-26 08:53:05 +00:00
pagingController: _albumPagingController,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
),
builderDelegate: PagedChildBuilderDelegate<Attachment>(
itemBuilder: (BuildContext context, item, int index) {
const radius = BorderRadius.all(Radius.circular(8));
return Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).dividerColor,
width: 0.3,
),
borderRadius: radius,
),
child: ClipRRect(
borderRadius: radius,
2024-07-26 09:35:54 +00:00
child: AttachmentListEntry(
2024-07-26 08:53:05 +00:00
item: item,
2024-07-27 12:27:29 +00:00
isDense: true,
2024-09-17 07:59:17 +00:00
parentId: 'album-$index',
2024-07-26 09:35:54 +00:00
showMature: _showMature,
onReveal: (value) {
setState(() => _showMature = value);
},
2024-07-26 08:53:05 +00:00
),
),
);
},
),
2024-07-26 09:35:54 +00:00
).paddingAll(16),
2024-07-26 08:53:05 +00:00
),
),
],
),
),
),
);
}
}
2024-09-26 16:02:08 +00:00
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,
),
],
),
);
}
}