Solian/lib/screens/dashboard.dart

528 lines
21 KiB
Dart
Raw Normal View History

2024-09-03 15:07:20 +00:00
import 'dart:developer';
import 'dart:math' hide log;
2024-09-01 09:20:26 +00:00
import 'package:collection/collection.dart';
import 'package:flutter/material.dart' hide Notification;
2024-09-03 15:07:20 +00:00
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:gap/gap.dart';
2024-09-01 09:20:26 +00:00
import 'package:get/get.dart';
2024-09-02 15:11:40 +00:00
import 'package:google_fonts/google_fonts.dart';
2024-09-01 09:20:26 +00:00
import 'package:intl/intl.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/exts.dart';
import 'package:solian/models/channel.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/models/daily_sign.dart';
2024-09-03 15:07:20 +00:00
import 'package:solian/models/event.dart';
import 'package:solian/models/notification.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/models/pagination.dart';
import 'package:solian/models/post.dart';
import 'package:solian/providers/auth.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/providers/content/posts.dart';
import 'package:solian/providers/daily_sign.dart';
2024-09-13 16:30:33 +00:00
import 'package:solian/providers/database/services/messages.dart';
2024-09-03 15:07:20 +00:00
import 'package:solian/providers/last_read.dart';
2024-09-01 09:20:26 +00:00
import 'package:solian/providers/websocket.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/router.dart';
2024-09-01 09:20:26 +00:00
import 'package:solian/screens/account/notification.dart';
2024-09-07 17:48:01 +00:00
import 'package:solian/theme.dart';
2024-09-03 15:07:20 +00:00
import 'package:solian/widgets/chat/chat_event.dart';
2024-09-07 16:23:59 +00:00
import 'package:solian/widgets/daily_sign/history_chart.dart';
2024-09-02 15:11:40 +00:00
import 'package:solian/widgets/posts/post_list.dart';
2024-09-01 09:20:26 +00:00
class DashboardScreen extends StatefulWidget {
const DashboardScreen({super.key});
@override
State<DashboardScreen> createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen> {
late final AuthProvider _auth = Get.find();
2024-09-03 15:07:20 +00:00
late final LastReadProvider _lastRead = Get.find();
2024-09-01 09:20:26 +00:00
late final WebSocketProvider _ws = Get.find();
2024-09-02 15:11:40 +00:00
late final PostProvider _posts = Get.find();
late final DailySignProvider _dailySign = Get.find();
2024-09-02 15:15:24 +00:00
Color get _unFocusColor =>
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
List<Notification> get _pendingNotifications =>
List<Notification>.from(_ws.notifications)
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
2024-09-02 15:11:40 +00:00
List<Post>? _currentPosts;
2024-09-03 15:07:20 +00:00
int? _currentPostsCount;
2024-09-02 15:11:40 +00:00
Future<void> _pullPosts() async {
2024-09-03 15:07:20 +00:00
if (_lastRead.feedLastReadAt == null) return;
log('[Dashboard] Pulling posts with pivot: ${_lastRead.feedLastReadAt}');
final resp = await _posts.seeWhatsNew(_lastRead.feedLastReadAt!);
2024-09-02 15:11:40 +00:00
final result = PaginationResult.fromJson(resp.body);
2024-09-03 15:07:20 +00:00
setState(() {
_currentPostsCount = result.count;
_currentPosts = result.data?.map((e) => Post.fromJson(e)).toList();
});
}
List<Event>? _currentMessages;
int? _currentMessagesCount;
Map<Channel, List<Event>>? get _currentGroupedMessages =>
_currentMessages?.groupListsBy((x) => x.channel!);
2024-09-03 15:07:20 +00:00
Future<void> _pullMessages() async {
if (_lastRead.messagesLastReadAt == null) return;
log('[Dashboard] Pulling messages with pivot: ${_lastRead.messagesLastReadAt}');
2024-09-13 16:30:33 +00:00
final src = Get.find<MessagesFetchingProvider>();
final out = await src.getWhatsNewEvents(_lastRead.messagesLastReadAt!);
2024-09-03 15:07:20 +00:00
if (out == null) return;
setState(() {
_currentMessages = out.$1;
_currentMessagesCount = out.$2;
});
2024-09-02 15:11:40 +00:00
}
bool _signingDaily = true;
DailySignRecord? _signRecord;
2024-09-07 16:23:59 +00:00
List<DailySignRecord>? _signRecordHistory;
2024-09-02 15:11:40 +00:00
Future<void> _pullDaily() async {
try {
_signRecord = await _dailySign.getToday();
_dailySign.listLastRecord(14).then((value) {
2024-09-07 16:23:59 +00:00
setState(() => _signRecordHistory = value);
});
2024-09-02 15:11:40 +00:00
} catch (e) {
context.showErrorDialog(e);
}
setState(() => _signingDaily = false);
}
Future<void> _signDaily() async {
setState(() => _signingDaily = true);
try {
_signRecord = await _dailySign.signToday();
_dailySign.listLastRecord(14).then((value) {
2024-09-11 15:40:23 +00:00
setState(() => _signRecordHistory = value);
});
2024-09-02 15:11:40 +00:00
} catch (e) {
context.showErrorDialog(e);
}
setState(() => _signingDaily = false);
}
Future<void> _pullData() async {
if (!_auth.isAuthorized.value) return;
await Future.wait([
_pullPosts(),
_pullMessages(),
_pullDaily(),
]);
}
2024-09-02 15:11:40 +00:00
@override
void initState() {
super.initState();
_pullData();
2024-09-02 15:11:40 +00:00
}
2024-09-01 09:20:26 +00:00
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return RefreshIndicator(
onRefresh: _pullData,
child: ListView(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
2024-09-08 04:32:21 +00:00
DateTime.now().day == DateTime.now().toUtc().day
? 'today'.tr
: 'yesterday'.tr,
style: Theme.of(context).textTheme.headlineSmall,
),
2024-09-08 04:32:21 +00:00
Text(DateFormat('yyyy/MM/dd').format(DateTime.now().toUtc())),
],
).paddingOnly(top: 8, left: 18, right: 18, bottom: 12),
Card(
2024-09-07 16:23:59 +00:00
child: Column(
children: [
ListTile(
leading: AnimatedSwitcher(
switchInCurve: Curves.fastOutSlowIn,
switchOutCurve: Curves.fastOutSlowIn,
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
return ScaleTransition(
scale: animation,
child: child,
);
},
child: _signRecord == null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
DateFormat('dd').format(DateTime.now()),
style: GoogleFonts.robotoMono(
fontSize: 22, height: 1.2),
),
Text(
DateFormat('yy/MM').format(DateTime.now()),
style: GoogleFonts.robotoMono(fontSize: 12),
),
],
)
: Text(
_signRecord!.symbol,
style: GoogleFonts.notoSerifHk(
fontSize: 20, height: 1),
).paddingSymmetric(horizontal: 9),
).paddingOnly(left: 4),
title: _signRecord == null
? Text('dailySign'.tr)
: Text(_signRecord!.overviewSuggestion),
subtitle: _signRecord == null
? Text('dailySignNone'.tr)
: Text('+${_signRecord!.resultExperience} EXP'),
trailing: AnimatedSwitcher(
switchInCurve: Curves.fastOutSlowIn,
switchOutCurve: Curves.fastOutSlowIn,
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
return ScaleTransition(
scale: animation,
child: child,
);
},
child: _signRecord == null
? IconButton(
tooltip: 'dailySignAction'.tr,
2024-09-07 16:23:59 +00:00
icon: const Icon(Icons.local_fire_department),
onPressed: _signingDaily ? null : _signDaily,
)
2024-09-11 15:40:23 +00:00
: (_signRecordHistory?.isEmpty ?? true)
? const SizedBox.shrink()
: IconButton(
tooltip: 'dailySignHistoryAction'.tr,
icon: const Icon(Icons.history),
onPressed: () {
showDialog(
context: context,
useRootNavigator: true,
builder: (context) =>
DailySignHistoryChartDialog(
data: _signRecordHistory,
),
);
},
),
2024-09-07 16:23:59 +00:00
),
),
],
),
).paddingSymmetric(horizontal: 8),
const Divider(thickness: 0.3).paddingSymmetric(vertical: 8),
/// Unread notifications
Obx(
() => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-09-02 15:11:40 +00:00
children: [
Text(
'notification'.tr,
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 18),
2024-09-02 15:11:40 +00:00
),
Text(
'notificationUnreadCount'.trParams({
'count': _ws.notifications.length.toString(),
}),
2024-09-02 15:11:40 +00:00
),
],
),
IconButton(
icon: const Icon(Icons.more_horiz),
onPressed: () {
showModalBottomSheet(
useRootNavigator: true,
isScrollControlled: true,
context: context,
builder: (context) => const NotificationScreen(),
).then((_) => _ws.notificationUnread.value = 0);
},
),
],
).paddingOnly(left: 18, right: 18, bottom: 8),
if (_ws.notifications.isNotEmpty)
SizedBox(
height: 76,
child: ListView.separated(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 8),
itemCount: min(_pendingNotifications.length, 10),
itemBuilder: (context, idx) {
final x = _pendingNotifications[idx];
return SizedBox(
width: min(360, width),
child: Card(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 4,
),
title: Text(
x.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-09-05 12:28:22 +00:00
if (x.subtitle != null)
Text(
x.subtitle!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
else
Text(
x.body,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
),
);
},
separatorBuilder: (_, __) => const Gap(4),
),
)
else
Card(
child: ListTile(
contentPadding:
const EdgeInsets.only(left: 24, right: 32),
trailing: const Icon(Icons.inbox_outlined),
title: Text('notifyEmpty'.tr),
subtitle: Text('notifyEmptyCaption'.tr),
),
).paddingSymmetric(horizontal: 8),
],
).paddingOnly(bottom: 12),
2024-09-02 15:11:40 +00:00
),
/// Unread friends / followed people posts
if (_currentPosts?.isNotEmpty ?? false)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'feed'.tr,
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 18),
),
Text(
'feedUnreadCount'.trParams({
'count': (_currentPostsCount ?? 0).toString(),
}),
),
],
),
IconButton(
icon: const Icon(Icons.arrow_forward),
onPressed: () {
2024-10-06 09:31:44 +00:00
AppRouter.instance.goNamed('explore');
},
),
],
).paddingOnly(left: 18, right: 18, bottom: 8),
2024-09-01 09:20:26 +00:00
SizedBox(
height: 360,
2024-09-01 09:20:26 +00:00
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _currentPosts!.length,
2024-09-01 09:20:26 +00:00
itemBuilder: (context, idx) {
final item = _currentPosts![idx];
2024-09-01 09:20:26 +00:00
return SizedBox(
width: min(480, width),
2024-09-01 09:20:26 +00:00
child: Card(
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
child: SingleChildScrollView(
child: PostListEntryWidget(
item: item,
isClickable: true,
isShowEmbed: true,
isNestedClickable: true,
2024-09-16 15:35:44 +00:00
showFeaturedReply: true,
onUpdate: (_) {
_pullPosts();
},
backgroundColor: Theme.of(context)
.colorScheme
.surfaceContainerLow,
),
2024-09-05 12:28:22 +00:00
),
2024-09-01 09:20:26 +00:00
),
).paddingSymmetric(horizontal: 8),
);
},
),
)
],
).paddingOnly(bottom: 12),
/// Unread messages part
if (_currentMessages?.isNotEmpty ?? false)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'messages'.tr,
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontSize: 18),
),
Text(
'messagesUnreadCount'.trParams({
'count': (_currentMessagesCount ?? 0).toString(),
}),
),
],
),
IconButton(
icon: const Icon(Icons.arrow_forward),
onPressed: () {
AppRouter.instance.goNamed('chat');
},
),
],
).paddingOnly(left: 18, right: 18, bottom: 8),
SizedBox(
height: 360,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _currentGroupedMessages!.length,
itemBuilder: (context, idx) {
final channel =
_currentGroupedMessages!.keys.elementAt(idx);
final itemList =
_currentGroupedMessages!.values.elementAt(idx);
return SizedBox(
width: min(520, width),
child: Card(
child: Column(
children: [
ListTile(
tileColor: Theme.of(context)
.colorScheme
.surfaceContainerHigh,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
topRight: Radius.circular(8),
)),
leading: CircleAvatar(
backgroundColor: channel.realmId == null
? Theme.of(context).colorScheme.primary
: Colors.transparent,
radius: 20,
child: FaIcon(
FontAwesomeIcons.hashtag,
color: channel.realmId == null
? Theme.of(context)
.colorScheme
.onPrimary
: Theme.of(context).colorScheme.primary,
size: 16,
),
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 16),
title: Text(channel.name),
subtitle: Text(channel.description),
onTap: () {
AppRouter.instance.pushNamed(
'channelChat',
pathParameters: {'alias': channel.alias},
queryParameters: {
if (channel.realmId != null)
'realm': channel.realm!.alias,
},
);
},
),
Expanded(
child: ListView.builder(
itemCount: itemList.length,
itemBuilder: (context, idx) {
final item = itemList[idx];
return ChatEvent(item: item).paddingOnly(
bottom: 8, top: 16, left: 8, right: 8);
},
),
),
],
),
).paddingSymmetric(horizontal: 8),
);
2024-09-02 15:11:40 +00:00
},
),
)
],
).paddingOnly(bottom: 12),
/// Footer
2024-09-03 15:07:20 +00:00
Column(
mainAxisAlignment: AppTheme.isLargeScreen(context)
2024-09-07 17:48:01 +00:00
? MainAxisAlignment.start
: MainAxisAlignment.center,
2024-09-03 15:07:20 +00:00
children: [
Text(
'Powered by Solar Network',
style: TextStyle(color: _unFocusColor, fontSize: 12),
),
Text(
'dashboardFooter'.tr,
2024-09-08 04:32:21 +00:00
style: TextStyle(color: _unFocusColor, fontSize: 12),
2024-09-02 15:11:40 +00:00
)
],
).paddingAll(8),
],
),
2024-09-01 09:20:26 +00:00
);
}
}