Skip to content

Commit

Permalink
support switching post time
Browse files Browse the repository at this point in the history
  • Loading branch information
orzogc committed Aug 4, 2023
1 parent 0f8c67f commit 65f6041
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 更新记录
## v0.5.1(未发布)
- 可以设置长按历史或订阅/标签按钮会在新标签页打开对应页面(高级设置)
- 长按串的时间会在相对时间和绝对时间之间切换
- 修复“修复字体显示”无效的问题

## v0.5.0
Expand Down
25 changes: 19 additions & 6 deletions lib/app/widgets/buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import '../utils/url.dart';
import 'dialog.dart';
import 'feed.dart';
import 'history.dart';
import 'listenable.dart';
import 'reference.dart';
import 'size.dart';

Expand Down Expand Up @@ -368,8 +369,12 @@ class HistoryButton extends StatelessWidget {
}

@override
Widget build(BuildContext context) =>
SettingsService.to.longPressButtonToOpenNewTab
Widget build(BuildContext context) {
final settings = SettingsService.to;

return ListenBuilder(
listenable: settings.longPressButtonToOpenNewTabListenable,
builder: (context, child) => settings.longPressButtonToOpenNewTab
? GestureDetector(
onLongPress: _onLongPress,
child: IconButton(
Expand All @@ -381,7 +386,9 @@ class HistoryButton extends StatelessWidget {
padding: iconPadding,
onPressed: _onTap,
tooltip: '历史',
icon: Icon(Icons.history, color: iconColor));
icon: Icon(Icons.history, color: iconColor)),
);
}
}

class FeedButton extends StatelessWidget {
Expand Down Expand Up @@ -422,8 +429,12 @@ class FeedButton extends StatelessWidget {
}

@override
Widget build(BuildContext context) =>
SettingsService.to.longPressButtonToOpenNewTab
Widget build(BuildContext context) {
final settings = SettingsService.to;

return ListenBuilder(
listenable: settings.longPressButtonToOpenNewTabListenable,
builder: (context, child) => settings.longPressButtonToOpenNewTab
? GestureDetector(
onLongPress: _onLongPress,
child: IconButton(
Expand All @@ -435,7 +446,9 @@ class FeedButton extends StatelessWidget {
padding: iconPadding,
onPressed: _onTap,
tooltip: '订阅/标签',
icon: Icon(Icons.rss_feed, color: iconColor));
icon: Icon(Icons.rss_feed, color: iconColor)),
);
}
}

class _SponsorDialog extends StatelessWidget {
Expand Down
36 changes: 18 additions & 18 deletions lib/app/widgets/feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,24 +302,24 @@ class _FeedItemState extends State<_FeedItem> {
replyTime != null)
? (textStyle) => Align(
alignment: Alignment.centerRight,
child: (settings.isShowLatestAbsolutePostTimeInFeed
? Text(
'最新回复 ${formatTime(replyTime)}',
style: textStyle ?? AppTheme.postHeaderTextStyle,
strutStyle: textStyle != null
? StrutStyle.fromTextStyle(textStyle)
: AppTheme.postHeaderStrutStyle,
)
: TimerRefresher(
builder: (context) => Text(
'最新回复 ${time.relativeTime(replyTime)}',
style:
textStyle ?? AppTheme.postHeaderTextStyle,
strutStyle: textStyle != null
? StrutStyle.fromTextStyle(textStyle)
: AppTheme.postHeaderStrutStyle,
),
)),
child: PostTime(
isShowRelativeTime:
settings.isShowLatestRelativePostTimeInFeed,
relativeTime: (context) => Text(
'最新回复 ${time.relativeTime(replyTime)}',
style: textStyle ?? AppTheme.postHeaderTextStyle,
strutStyle: textStyle != null
? StrutStyle.fromTextStyle(textStyle)
: AppTheme.postHeaderStrutStyle,
),
absoluteTime: Text(
'最新回复 ${formatTime(replyTime)}',
style: textStyle ?? AppTheme.postHeaderTextStyle,
strutStyle: textStyle != null
? StrutStyle.fromTextStyle(textStyle)
: AppTheme.postHeaderStrutStyle,
),
),
)
: null,
onTap: (post) =>
Expand Down
20 changes: 11 additions & 9 deletions lib/app/widgets/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,20 +460,22 @@ class _BrowseHistoryItemState extends State<_BrowseHistoryItem> {
showFullTime: false,
showReplyCount: false,
header: (textStyle) => PostHeader(children: [
if (settings.showRelativeTime)
TimerRefresher(
builder: (context) => Text(
ListenBuilder(
listenable: settings.showRelativeTimeListenable,
builder: (context, child) => PostTime(
isShowRelativeTime: settings.showRelativeTime,
relativeTime: (context) => Text(
'最后浏览时间:${time.relativeTime(_history.browseTime)}',
style: AppTheme.postHeaderTextStyle,
strutStyle: AppTheme.postHeaderStrutStyle,
),
)
else
Text(
'最后浏览时间:${fullFormatTime(_history.browseTime)}',
style: AppTheme.postHeaderTextStyle,
strutStyle: AppTheme.postHeaderStrutStyle,
absoluteTime: Text(
'最后浏览时间:${fullFormatTime(_history.browseTime)}',
style: AppTheme.postHeaderTextStyle,
strutStyle: AppTheme.postHeaderStrutStyle,
),
),
),
if (browsePage != null && browsePostId != null)
Text(
'浏览到:第$browsePage页 ${browsePostId.toPostNumber()}',
Expand Down
30 changes: 15 additions & 15 deletions lib/app/widgets/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,21 @@ class _PostTimeState extends State<_PostTime> {

return ListenBuilder(
listenable: settings.showRelativeTimeListenable,
builder: (context, child) => settings.showRelativeTime
? TimerRefresher(
builder: (context) => Text(
time.relativeTime(widget.postTime),
style: textStyle,
strutStyle: strutStyle,
),
)
: Text(
widget.showFullTime
? fullFormatTime(widget.postTime)
: formatTime(widget.postTime),
style: textStyle,
strutStyle: strutStyle,
),
builder: (context, child) => PostTime(
isShowRelativeTime: settings.showRelativeTime,
relativeTime: (context) => Text(
time.relativeTime(widget.postTime),
style: textStyle,
strutStyle: strutStyle,
),
absoluteTime: Text(
widget.showFullTime
? fullFormatTime(widget.postTime)
: formatTime(widget.postTime),
style: textStyle,
strutStyle: strutStyle,
),
),
);
}
}
Expand Down
50 changes: 48 additions & 2 deletions lib/app/widgets/time.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
import 'package:flutter/material.dart';
import 'package:timeago_flutter/timeago_flutter.dart';

class TimerRefresher extends TimerRefreshWidget {
class _TimerRefresher extends TimerRefreshWidget {
final WidgetBuilder builder;

const TimerRefresher({super.key, super.refreshRate, required this.builder});
// ignore: unused_element
const _TimerRefresher({super.key, super.refreshRate, required this.builder});

@override
Widget build(BuildContext context) => builder(context);
}

class PostTime extends StatefulWidget {
final bool isShowRelativeTime;

final WidgetBuilder relativeTime;

final Widget absoluteTime;

const PostTime(
{super.key,
required this.isShowRelativeTime,
required this.relativeTime,
required this.absoluteTime});

@override
State<PostTime> createState() => _PostTimeState();
}

class _PostTimeState extends State<PostTime> {
late bool _isShowRelativeTime;

@override
void initState() {
super.initState();

_isShowRelativeTime = widget.isShowRelativeTime;
}

@override
void didUpdateWidget(covariant PostTime oldWidget) {
super.didUpdateWidget(oldWidget);

if (widget.isShowRelativeTime != oldWidget.isShowRelativeTime) {
_isShowRelativeTime = widget.isShowRelativeTime;
}
}

@override
Widget build(BuildContext context) => GestureDetector(
onLongPress: () =>
setState(() => _isShowRelativeTime = !_isShowRelativeTime),
child: _isShowRelativeTime
? _TimerRefresher(builder: widget.relativeTime)
: widget.absoluteTime);
}

0 comments on commit 65f6041

Please sign in to comment.