Skip to content

Feature/xdu planet new #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/model/xdu_planet/xdu_planet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ class Article {
final DateTime time;
final String content;
final String url;
String? author;

Article({
required this.title,
required this.time,
required this.content,
required this.url,
this.author,
});

factory Article.fromJson(Map<String, dynamic> json) =>
Expand Down
155 changes: 128 additions & 27 deletions lib/page/xdu_planet/xdu_planet_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import 'dart:math';

// import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:watermeter/model/xdu_planet/xdu_planet.dart';
import 'package:watermeter/page/public_widget/context_extension.dart';
import 'package:watermeter/page/public_widget/public_widget.dart';
import 'package:watermeter/page/xdu_planet/person_page.dart';
import 'package:watermeter/page/xdu_planet/content_page.dart';
import 'package:watermeter/repository/xdu_planet_session.dart';

class XDUPlanetPage extends StatefulWidget {
Expand All @@ -24,6 +25,7 @@ class XDUPlanetPage extends StatefulWidget {
class _XDUPlanetPageState extends State<XDUPlanetPage>
with AutomaticKeepAliveClientMixin {
late Future<XDUPlanetDatabase> repoList;
String selected = "全部";

@override
bool get wantKeepAlive => true;
Expand All @@ -47,9 +49,18 @@ class _XDUPlanetPageState extends State<XDUPlanetPage>
builder: (context) => AlertDialog(
title: const Text("XDU Planet 介绍"),
content: const Text(
"本服务提供商是西电开源社区的 Planet 服务,查看我们学校同学们的博客。",
"服务提供者是西电开源社区,用于查看我们学校同学们的博客。\n"
"觉得有趣/有用的话,欢迎点点star哦\n\n\n"
"<(=ω=)>",
),
actions: [
TextButton(
child: const Text("项目首页"),
onPressed: () => launchUrlString(
"https://github.com/xdlinux/planet",
mode: LaunchMode.externalApplication,
),
),
TextButton(
child: const Text("网页版"),
onPressed: () => launchUrlString(
Expand All @@ -69,17 +80,6 @@ class _XDUPlanetPageState extends State<XDUPlanetPage>
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
try {
// Map<String, Repo> data = snapshot.data!.repos;
// List<String> keys = data.keys.toList();
/*
Widget icon(int index) => CachedNetworkImage(
imageUrl: data[keys[index]]!.favicon,
errorWidget: (context, url, error) =>
const Icon(Icons.rss_feed),
width: 32,
height: 32,
);
*/
return Center(
child: ConstrainedBox(
constraints: BoxConstraints(
Expand All @@ -89,19 +89,112 @@ class _XDUPlanetPageState extends State<XDUPlanetPage>
sheetMaxWidth - 16,
),
),
child: ListView.builder(
itemCount: snapshot.data?.author.length ?? 0,
itemBuilder: (context, index) => ListTile(
title: Text(snapshot.data!.author[index].name),
onTap: () {
context.pushReplacement(
PersonalPage(
key: ValueKey(snapshot.data!.author[index].name),
person: snapshot.data!.author[index],
),
);
}),
),
child: () {
var articles = snapshot.data!.author
.where((e) => selected == "全部" || e.name == selected)
.map((e) => e.article
.map((f) => Article(
title: f.title,
time: f.time,
content: f.content,
url: f.url,
author: e.name))
.toList())
.reduce((a, b) => a + b);
articles.sort((a, b) => b.time.compareTo(a.time));
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 48,
child: ListView(
scrollDirection: Axis.horizontal,
children: () {
var res = snapshot.data!.author
.map((e) => Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
style: TextButton.styleFrom(
backgroundColor: selected == e.name
? Theme.of(context)
.colorScheme
.primaryContainer
: Theme.of(context)
.colorScheme
.secondaryContainer,
),
onPressed: () {
setState(() {
selected = e.name;
});
},
child: Text(
e.name,
style: TextStyle(
color: selected == e.name
? Theme.of(context)
.colorScheme
.onPrimaryContainer
: Theme.of(context)
.colorScheme
.onSecondaryContainer),
),
)))
.toList();
res.insert(
0,
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
style: TextButton.styleFrom(
backgroundColor: selected == "全部"
? Theme.of(context)
.colorScheme
.primary
: Theme.of(context)
.colorScheme
.secondaryContainer,
),
onPressed: () {
setState(() {
selected = "全部";
});
},
child: Text(
"全部",
style: TextStyle(
color: selected == "全部"
? Theme.of(context)
.colorScheme
.onPrimaryContainer
: Theme.of(context)
.colorScheme
.onSecondaryContainer),
),
)));
return res;
}(),
),
),
Expanded(
child: ListView.builder(
itemCount: articles.length ?? 0,
itemBuilder: (context, index) {
return ListTile(
title: Text(articles[index].title),
subtitle: Text(
"${articles[index].author} ${Jiffy.parseFromDateTime(
articles[index].time,
).format(pattern: "yyyy年MM月dd日")}"),
onTap: () {
context.pushReplacement(ContentPage(
article: articles[index],
author: articles[index].author!));
});
}))
],
);
}(),
),
);
} catch (e) {
Expand All @@ -114,7 +207,15 @@ class _XDUPlanetPageState extends State<XDUPlanetPage>
);
}
} else {
return const Center(child: CircularProgressIndicator());
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('加载中,请稍等 <(=ω=)>'),
],
));
}
},
),
Expand Down