Compare commits

...

2 Commits

Author SHA1 Message Date
04b9427cdf Recommendation posts 2024-12-10 00:18:39 +08:00
eab939928f Notification card
🐛 Fix post item truncate hint from overflowing
2024-12-09 23:45:10 +08:00
7 changed files with 177 additions and 72 deletions

View File

@ -274,6 +274,12 @@
"attachmentUnsetAsPostThumbnail": "Unset as post thumbnail",
"attachmentSetThumbnail": "Set thumbnail",
"attachmentUpload": "Upload",
"notification": "Notification",
"notificationUnreadCount": {
"zero": "All notifications read",
"one": "{} unread notification",
"other": "{} unread notifications"
},
"notificationUnread": "Unread",
"notificationRead": "Read",
"notificationMarkAllRead": "Mark all notifications as read",
@ -424,5 +430,6 @@
"unauthorizedDescription": "Login to explore the entire Solar Network.",
"serviceStatus": "Service Status",
"termRelated": "Related Terms",
"appDetails": "App Details"
"appDetails": "App Details",
"postRecommendation": "Highlight Posts"
}

View File

@ -35,7 +35,7 @@
"errorRequestForbidden": "被禁止的请求,您没有足够的权限去做那件事。",
"errorRequestNotFound": "您正查找的资源无法被找到。",
"errorRequestConnection": "网络连接错误,请检查您的网络状态或者检查我们的服务状态。",
"errorRequestUnknown": "位置请求错误,您可能想将此对话框截图并发送给我们。",
"errorRequestUnknown": "未知请求错误,您可能想将此对话框截图并发送给我们。",
"unknown": "未知",
"loading": "加载中…",
"prev": "上一步",
@ -272,6 +272,12 @@
"attachmentUnsetAsPostThumbnail": "取消设置为帖子缩略图",
"attachmentSetThumbnail": "设置缩略图",
"attachmentUpload": "上传",
"notification": "通知",
"notificationUnreadCount": {
"zero": "无未读通知",
"one": "有 {} 个未读通知",
"other": "有 {} 个未读通知"
},
"notificationUnread": "未读",
"notificationRead": "已读",
"notificationMarkAllRead": "已读所有通知",
@ -422,5 +428,6 @@
"unauthorizedDescription": "登陆以探索整个 Solar Network。",
"serviceStatus": "服务状态",
"termRelated": "相关条款",
"appDetails": "应用程序详情"
"appDetails": "应用程序详情",
"postRecommendation": "推荐帖子"
}

View File

@ -60,6 +60,14 @@ class SnPostContentProvider {
return out;
}
Future<List<SnPost>> listRecommendations() async {
final resp = await _sn.client.get('/cgi/co/recommendations');
final out = _preloadRelatedDataInBatch(
List.from(resp.data.map((ele) => SnPost.fromJson(ele))),
);
return out;
}
Future<(List<SnPost>, int)> listPosts({
int take = 10,
int offset = 0,

View File

@ -3,16 +3,20 @@ import 'dart:math' as math;
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:flutter/material.dart';
import 'package:surface/providers/post.dart';
import 'package:surface/providers/sn_network.dart';
import 'package:surface/providers/userinfo.dart';
import 'package:surface/types/check_in.dart';
import 'package:surface/types/post.dart';
import 'package:surface/widgets/app_bar_leading.dart';
import 'package:surface/widgets/dialog.dart';
import 'package:surface/widgets/post/post_item.dart';
class HomeScreenDashEntry {
final String name;
@ -36,10 +40,20 @@ class HomeScreen extends StatefulWidget {
class _HomeScreenState extends State<HomeScreen> {
static const List<HomeScreenDashEntry> kCards = [
HomeScreenDashEntry(
name: 'dashEntryRecommendation',
cols: 2,
rows: 2,
child: _HomeDashRecommendationPostWidget(),
),
HomeScreenDashEntry(
name: 'dashEntryCheckIn',
child: _HomeDashCheckInWidget(),
),
HomeScreenDashEntry(
name: 'dashEntryNotification',
child: _HomeDashNotificationWidget(),
),
];
@override
@ -52,16 +66,12 @@ class _HomeScreenState extends State<HomeScreen> {
body: LayoutBuilder(
builder: (context, constraints) {
return Align(
alignment: constraints.maxWidth > 640
? Alignment.center
: Alignment.topCenter,
alignment: constraints.maxWidth > 640 ? Alignment.center : Alignment.topCenter,
child: Container(
constraints: const BoxConstraints(maxWidth: 640),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: constraints.maxWidth > 640
? MainAxisAlignment.center
: MainAxisAlignment.start,
mainAxisAlignment: constraints.maxWidth > 640 ? MainAxisAlignment.center : MainAxisAlignment.start,
children: [
if (constraints.maxWidth <= 640) const Gap(8),
_HomeDashSpecialDayWidget().padding(top: 8, horizontal: 8),
@ -96,9 +106,7 @@ class _HomeDashSpecialDayWidget extends StatelessWidget {
final ua = context.watch<UserProvider>();
final today = DateTime.now();
final birthday = ua.user?.profile?.birthday?.toLocal();
final isBirthday = birthday != null &&
birthday.day == today.day &&
birthday.month == today.month;
final isBirthday = birthday != null && birthday.day == today.day && birthday.month == today.month;
return Column(
children: [
if (isBirthday)
@ -154,20 +162,15 @@ class _HomeDashCheckInWidgetState extends State<_HomeDashCheckInWidget> {
}
Widget _buildDetailChunk(int index, bool positive) {
final prefix =
positive ? 'dailyCheckPositiveHint' : 'dailyCheckNegativeHint';
final mod =
positive ? kSuggestionPositiveHintCount : kSuggestionNegativeHintCount;
final prefix = positive ? 'dailyCheckPositiveHint' : 'dailyCheckNegativeHint';
final mod = positive ? kSuggestionPositiveHintCount : kSuggestionNegativeHintCount;
final pos = math.max(1, _todayRecord!.resultModifiers[index] % mod);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
prefix.tr(args: ['$prefix$pos'.tr()]),
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
style: Theme.of(context).textTheme.titleMedium!.copyWith(fontWeight: FontWeight.bold),
).tr(),
Text(
'$prefix${pos}Description',
@ -202,10 +205,7 @@ class _HomeDashCheckInWidgetState extends State<_HomeDashCheckInWidget> {
else
Text(
'dailyCheckEverythingIsNegative',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
style: Theme.of(context).textTheme.titleMedium!.copyWith(fontWeight: FontWeight.bold),
).tr(),
const Gap(8),
if (_todayRecord?.resultTier != 4)
@ -221,10 +221,7 @@ class _HomeDashCheckInWidgetState extends State<_HomeDashCheckInWidget> {
else
Text(
'dailyCheckEverythingIsPositive',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
style: Theme.of(context).textTheme.titleMedium!.copyWith(fontWeight: FontWeight.bold),
).tr(),
],
),
@ -338,14 +335,28 @@ class _HomeDashCheckInWidgetState extends State<_HomeDashCheckInWidget> {
}
}
class _HomeDashLinkWidget extends StatelessWidget {
final String title;
final String subtitle;
const _HomeDashLinkWidget({
super.key,
required this.title,
required this.subtitle,
});
class _HomeDashNotificationWidget extends StatefulWidget {
const _HomeDashNotificationWidget({super.key});
@override
State<_HomeDashNotificationWidget> createState() => _HomeDashNotificationWidgetState();
}
class _HomeDashNotificationWidgetState extends State<_HomeDashNotificationWidget> {
int? _count;
Future<void> _fetchNotificationCount() async {
final sn = context.read<SnNetworkProvider>();
final resp = await sn.client.get('/cgi/id/notifications/count');
_count = resp.data['count'];
setState(() {});
}
@override
void initState() {
super.initState();
_fetchNotificationCount();
}
@override
Widget build(BuildContext context) {
@ -358,11 +369,11 @@ class _HomeDashLinkWidget extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
'notification',
style: Theme.of(context).textTheme.titleLarge,
),
).tr(),
Text(
subtitle,
_count == null ? 'loading'.tr() : 'notificationUnreadCount'.plural(_count ?? 0),
style: Theme.of(context).textTheme.bodyLarge,
),
],
@ -377,7 +388,9 @@ class _HomeDashLinkWidget extends StatelessWidget {
),
child: IconButton(
icon: const Icon(Symbols.arrow_right_alt),
onPressed: () {},
onPressed: () {
GoRouter.of(context).goNamed('notification');
},
),
),
)
@ -386,3 +399,75 @@ class _HomeDashLinkWidget extends StatelessWidget {
);
}
}
class _HomeDashRecommendationPostWidget extends StatefulWidget {
const _HomeDashRecommendationPostWidget({super.key});
@override
State<_HomeDashRecommendationPostWidget> createState() => _HomeDashRecommendationPostWidgetState();
}
class _HomeDashRecommendationPostWidgetState extends State<_HomeDashRecommendationPostWidget> {
bool _isBusy = false;
List<SnPost>? _posts;
Future<void> _fetchRecommendationPosts() async {
setState(() => _isBusy = true);
try {
final pt = context.read<SnPostContentProvider>();
_posts = await pt.listRecommendations();
} catch (err) {
if (!mounted) return;
context.showErrorDialog(err);
} finally {
setState(() => _isBusy = false);
}
}
@override
void initState() {
super.initState();
_fetchRecommendationPosts();
}
@override
Widget build(BuildContext context) {
if (_isBusy) {
return Card(
child: CircularProgressIndicator().center(),
);
}
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'postRecommendation',
style: Theme.of(context).textTheme.titleLarge,
).tr().padding(horizontal: 20, top: 16, bottom: 8),
Expanded(
child: PageView.builder(
itemCount: _posts?.length ?? 0,
itemBuilder: (context, index) {
return SingleChildScrollView(
child: GestureDetector(
child: PostItem(
data: _posts![index],
showMenu: false,
).padding(left: 8, right: 8, bottom: 8),
onTap: () {
GoRouter.of(context).pushNamed('postDetail', pathParameters: {
'slug': _posts![index].id.toString(),
});
},
),
);
},
),
),
],
),
);
}
}

View File

@ -1,8 +1,4 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
const kMaterialYouToggleStoreKey = 'app_theme_material_you';

View File

@ -60,8 +60,7 @@ class _AppNavigationDrawerState extends State<AppNavigationDrawer> {
],
).padding(
horizontal: 32,
top: MediaQuery.of(context).padding.top + 12,
bottom: 12,
vertical: 12,
),
...destinations.where((ele) => ele.isPinned).map((ele) {
return NavigationDrawerDestination(

View File

@ -720,32 +720,35 @@ class _PostTruncatedHint extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: [
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.timer, size: 20),
const Gap(4),
Text('postReadEstimate').tr(args: [
'${Duration(
seconds: (data.body['content_length'] as num).toDouble() * 60 ~/ kHumanReadSpeed,
).inSeconds}s',
]),
],
).padding(right: 8),
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.height, size: 20),
const Gap(4),
Text(
'postTotalLength'.plural(data.body['content_length']),
)
],
),
],
).opacity(0.75);
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.timer, size: 20),
const Gap(4),
Text('postReadEstimate').tr(args: [
'${Duration(
seconds: (data.body['content_length'] as num).toDouble() * 60 ~/ kHumanReadSpeed,
).inSeconds}s',
]),
],
).padding(right: 8),
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.height, size: 20),
const Gap(4),
Text(
'postTotalLength'.plural(data.body['content_length']),
)
],
),
],
).opacity(0.75),
);
}
}