Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
share121 committed Sep 17, 2024
1 parent 88fe719 commit 3f4b617
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 223 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.16.9+36

1. 显示文章更新提示

## 2.16.8+35

1. 优化提示
Expand Down
5 changes: 3 additions & 2 deletions lib/api_root/get_pinned_discussions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'api_root.dart';

Future<Nodes<HData>> getPinnedDiscussions(String? after) async {
final res = await graphql(
'{ repository(owner: "$owner", name: "$repo") { pinnedDiscussions(first: 20, after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { discussion { number } } } } } }');
'{ repository(owner: "$owner", name: "$repo") { pinnedDiscussions(first: 20, after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { discussion { number updatedAt } } } } } }');
if (res.data
case {
'data': {
Expand All @@ -24,9 +24,10 @@ Future<Nodes<HData>> getPinnedDiscussions(String? after) async {
case {
'discussion': {
'number': final int number,
'updatedAt': final String updatedAt,
}
}) {
return HData(number, isPin: true);
return HData(number, DateTime.parse(updatedAt), isPin: true);
}
})
.whereType<HData>()
Expand Down
5 changes: 3 additions & 2 deletions lib/api_root/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'api_root.dart';

Future<Nodes<HData>> search(String query, String? after) async {
final res = await graphql(
'{ search(first: 20, type: DISCUSSION, query: "repo:$owner/$repo ${encode(query)}", after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { ... on Discussion { number } } } }');
'{ search(first: 20, type: DISCUSSION, query: "repo:$owner/$repo ${encode(query)}", after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { ... on Discussion { number updatedAt } } } }');
if (res.data
case {
'data': {
Expand All @@ -21,8 +21,9 @@ Future<Nodes<HData>> search(String query, String? after) async {
if (e
case {
'number': final int number,
'updatedAt': final String updatedAt,
}) {
return HData(number);
return HData(number, DateTime.parse(updatedAt));
}
})
.whereType<HData>()
Expand Down
5 changes: 3 additions & 2 deletions lib/api_user/get_pinned_discussions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'api_user.dart';

Future<Nodes<HData>> getPinnedDiscussions(String? after) async {
final res = await graphql(
'{ repository(owner: "$owner", name: "$repo") { pinnedDiscussions(first: 100, after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { discussion { number } } } } }');
'{ repository(owner: "$owner", name: "$repo") { pinnedDiscussions(first: 100, after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { discussion { number updatedAt } } } } }');
if (res.data
case {
'data': {
Expand All @@ -24,9 +24,10 @@ Future<Nodes<HData>> getPinnedDiscussions(String? after) async {
case {
'discussion': {
'number': final int number,
'updatedAt': final String updatedAt,
}
}) {
return HData(number, isPin: true);
return HData(number, DateTime.parse(updatedAt), isPin: true);
}
})
.whereType<HData>()
Expand Down
5 changes: 3 additions & 2 deletions lib/api_user/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'api_user.dart';

Future<Nodes<HData>> search(String query, String? after) async {
final res = await graphql(
'{ search(first: 100, type: DISCUSSION, query: "repo:$owner/$repo ${encode(query)}", after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { ... on Discussion { number } } } }');
'{ search(first: 100, type: DISCUSSION, query: "repo:$owner/$repo ${encode(query)}", after: ${after == null ? null : '"$after"'}) { pageInfo { endCursor hasNextPage } nodes { ... on Discussion { number updatedAt } } } }');
if (res.data
case {
'data': {
Expand All @@ -21,8 +21,9 @@ Future<Nodes<HData>> search(String query, String? after) async {
if (e
case {
'number': final int number,
'updatedAt': final String updatedAt,
}) {
return HData(number);
return HData(number, DateTime.parse(updatedAt));
}
})
.whereType<HData>()
Expand Down
5 changes: 3 additions & 2 deletions lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ class Controller extends GetxController {
history.addAll(pref.getStringList('history')?.map(HData.fromStr) ?? []);
ever(bookmarks, (v) {
pref.setStringList(
'bookmarks', v.map((e) => e.number.toString()).toList());
'bookmarks', v.map((e) => '${e.number},${e.updatedAt}').toList());
});
ever(history, (v) {
pref.setStringList('history', v.map((e) => e.number.toString()).toList());
pref.setStringList(
'history', v.map((e) => '${e.number},${e.updatedAt}').toList());
});
if (c.isLogin()) {
api_user.getAllReports(reportDiscussionNumber).then(report.call);
Expand Down
36 changes: 25 additions & 11 deletions lib/interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ class Reply {
int get hashCode => Object.hash(author, bodyHTML, createdAt, lastEditedAt);
}

final authorInfoCache = <String, Future<({String? name, int? totalCount})>>{};

class Author {
final String login;
final String avatar;
late final name = login.obs;
final level = 0.obs;

static final authorInfoCache =
<String, Future<({String? name, int? totalCount})>>{};

late final url = 'https://github.com/$login';

Author({required this.login, required this.avatar}) {
Expand All @@ -201,26 +202,39 @@ class Author {
int get hashCode => login.hashCode;
}

final hDataCache = <int, Future<Discussion?>>{};

class HData {
final int number;
final bool isPin;
late final int number;
late final bool isPin;
late final DateTime updatedAt;

static final hDataCache = <String, Future<Discussion?>>{};

Future<Discussion?> get discussion {
var t = hDataCache[number];
var t = hDataCache['$number,$updatedAt'];
if (t != null) return t;
t = c.isLogin()
? api_user.getDiscussion(number)
: api_root.getDiscussion(number);
hDataCache[number] = t;
hDataCache['$number,$updatedAt'] = t;
return t;
}

late final url = 'https://github.com/$owner/$repo/discussions/$number';

HData(this.number, {this.isPin = false});
HData.fromStr(String number) : this(int.parse(number));
HData.fromDiscussion(Discussion discussion) : this(discussion.number);
HData(this.number, this.updatedAt, {this.isPin = false});
HData.fromStr(String data) {
final s = data.split(',');
if (s.length == 2) {
number = int.parse(s[0]);
updatedAt = DateTime.parse(s[1]);
} else {
number = int.parse(s[0]);
updatedAt = DateTime.fromMillisecondsSinceEpoch(0);
}
isPin = false;
}
HData.fromDiscussion(Discussion discussion, DateTime updatedAt)
: this(discussion.number, updatedAt);

@override
operator ==(Object other) => other is HData && other.number == number;
Expand Down
174 changes: 100 additions & 74 deletions lib/pages/discussion_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import '../widget/my_chip.dart';
import '../widget/report_discussion_comment.dart';

class DiscussionPage extends StatefulWidget {
const DiscussionPage(
{super.key, required this.discussion, required this.isPin});
const DiscussionPage({
super.key,
required this.discussion,
required this.hData,
});

final Discussion discussion;
final bool isPin;
final HData hData;

@override
State<DiscussionPage> createState() => _DiscussionPageState();
Expand All @@ -36,7 +39,7 @@ class _DiscussionPageState extends State<DiscussionPage> {
void initState() {
super.initState();
Future(() {
c.history({HData.fromDiscussion(widget.discussion), ...c.history});
c.history({widget.hData, ...c.history});
});
scrollController.addListener(() {
final maxScroll = scrollController.position.maxScrollExtent;
Expand Down Expand Up @@ -193,7 +196,10 @@ class _DiscussionPageState extends State<DiscussionPage> {
width: double.infinity,
child: Cover(discussion: widget.discussion),
),
RightBox(discussion: widget.discussion),
RightBox(
discussion: widget.discussion,
hData: widget.hData,
),
],
);
}
Expand Down Expand Up @@ -238,7 +244,9 @@ class _DiscussionPageState extends State<DiscussionPage> {
child: SingleChildScrollView(
controller: scrollController,
child: RightBox(
discussion: widget.discussion),
discussion: widget.discussion,
hData: widget.hData,
),
),
),
)
Expand All @@ -248,53 +256,6 @@ class _DiscussionPageState extends State<DiscussionPage> {
),
],
),
floatingActionButton: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (canReport(widget.discussion, widget.isPin)) ...[
const SizedBox(height: 8),
FloatingActionButton(
heroTag: null,
onPressed: () {
Future.delayed(3.s).then((_) => launchUrlString(
'https://github.com/share121/inter-knot/discussions/$reportDiscussionNumber#new_comment_form'));
copyText(
'违规讨论:#${widget.discussion.number}\n举报原因:',
title: 'Report template copied'.tr,
msg: 'Jump to the report page after 3 seconds'
.tr,
);
},
tooltip: 'Report'.tr,
child: const Icon(Icons.report_outlined),
),
],
const SizedBox(height: 8),
Obx(() {
final isLiked = c.bookmarks
.map((e) => e.number)
.contains(widget.discussion.number);
return FloatingActionButton(
heroTag: null,
onPressed: () {
if (isLiked) {
c.bookmarks.removeWhere((e) =>
e.number == widget.discussion.number);
} else {
c.bookmarks({
HData.fromDiscussion(widget.discussion),
...c.bookmarks
});
}
},
tooltip: isLiked ? 'Dislike'.tr : 'Like'.tr,
child: Icon(isLiked
? Icons.favorite
: Icons.favorite_outline),
);
}),
],
),
),
),
),
Expand All @@ -307,9 +268,10 @@ class _DiscussionPageState extends State<DiscussionPage> {
}

class RightBox extends StatelessWidget {
const RightBox({super.key, required this.discussion});
const RightBox({super.key, required this.discussion, required this.hData});

final Discussion discussion;
final HData hData;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -344,28 +306,92 @@ class RightBox extends StatelessWidget {
],
),
const SizedBox(height: 16),
ClickRegion(
onTap: () => launchUrlString('${discussion.url}#new_comment_form'),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xff222222),
borderRadius: BorderRadius.circular(maxRadius),
border: Border.all(color: const Color(0xff2D2D2D), width: 4),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.add_comment_outlined),
const SizedBox(width: 8),
Text(
'Write a review'.tr,
style: const TextStyle(fontSize: 16),
Row(
children: [
Expanded(
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xff222222),
borderRadius: BorderRadius.circular(maxRadius),
border:
Border.all(color: const Color(0xff2D2D2D), width: 4),
),
],
child: ClickRegion(
onTap: () =>
launchUrlString('${discussion.url}#new_comment_form'),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.add_comment_outlined),
const SizedBox(width: 8),
Text(
'Write a review'.tr,
style: const TextStyle(fontSize: 16),
),
],
),
),
),
),
),
if (canReport(discussion, hData.isPin)) ...[
const SizedBox(width: 8),
Tooltip(
message: 'Report'.tr,
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xff222222),
borderRadius: BorderRadius.circular(maxRadius),
border:
Border.all(color: const Color(0xff2D2D2D), width: 4),
),
child: ClickRegion(
onTap: () {
Future.delayed(3.s).then((_) => launchUrlString(
'https://github.com/share121/inter-knot/discussions/$reportDiscussionNumber#new_comment_form'));
copyText(
'违规讨论:#${discussion.number}\n举报原因:',
title: 'Report template copied'.tr,
msg: 'Jump to the report page after 3 seconds'.tr,
);
},
child: const Icon(Icons.report_outlined),
),
),
),
],
const SizedBox(width: 8),
Obx(() {
final isLiked = c.bookmarks
.map((e) => e.number)
.contains(discussion.number);
return Tooltip(
message: isLiked ? 'Dislike'.tr : 'Like'.tr,
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: const Color(0xff222222),
borderRadius: BorderRadius.circular(maxRadius),
border:
Border.all(color: const Color(0xff2D2D2D), width: 4),
),
child: ClickRegion(
onTap: () {
if (isLiked) {
c.bookmarks.removeWhere(
(e) => e.number == discussion.number);
} else {
c.bookmarks({hData, ...c.bookmarks});
}
},
child: Icon(
isLiked ? Icons.favorite : Icons.favorite_outline),
),
),
);
}),
],
),
const SizedBox(height: 16),
const Divider(),
Expand Down
Loading

0 comments on commit 3f4b617

Please sign in to comment.