Realm posts

This commit is contained in:
LittleSheep 2024-05-29 22:42:11 +08:00
parent 5f06fc4f9d
commit 6bb29dfbc0
12 changed files with 321 additions and 37 deletions

3
devtools_options.yaml Normal file
View File

@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:

View File

@ -7,8 +7,13 @@ class PostProvider extends GetConnect {
httpClient.baseUrl = ServiceFinder.services['interactive']; httpClient.baseUrl = ServiceFinder.services['interactive'];
} }
Future<Response> listPost(int page) async { Future<Response> listPost(int page, {int? realm}) async {
final resp = await get('/api/feed?take=${10}&offset=$page'); final queries = [
'take=${10}',
'offset=$page',
if (realm != null) 'realmId=$realm',
];
final resp = await get('/api/feed?${queries.join('&')}');
if (resp.statusCode != 200) { if (resp.statusCode != 200) {
throw Exception(resp.body); throw Exception(resp.body);
} }

View File

@ -3,6 +3,21 @@ import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart'; import 'package:solian/services.dart';
class RealmProvider extends GetxController { class RealmProvider extends GetxController {
Future<Response> getRealm(String alias) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = GetConnect(maxAuthRetries: 3);
client.httpClient.baseUrl = ServiceFinder.services['passport'];
final resp = await client.get('/api/realms/$alias');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
Future<Response> listAvailableRealm() async { Future<Response> listAvailableRealm() async {
final AuthProvider auth = Get.find(); final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized'); if (!await auth.isAuthorized) throw Exception('unauthorized');

View File

@ -1,5 +1,6 @@
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:solian/models/channel.dart'; import 'package:solian/models/channel.dart';
import 'package:solian/models/realm.dart';
import 'package:solian/screens/account.dart'; import 'package:solian/screens/account.dart';
import 'package:solian/screens/account/friend.dart'; import 'package:solian/screens/account/friend.dart';
import 'package:solian/screens/account/personalize.dart'; import 'package:solian/screens/account/personalize.dart';
@ -9,7 +10,9 @@ import 'package:solian/screens/channel/channel_organize.dart';
import 'package:solian/screens/contact.dart'; import 'package:solian/screens/contact.dart';
import 'package:solian/screens/posts/post_detail.dart'; import 'package:solian/screens/posts/post_detail.dart';
import 'package:solian/screens/realms.dart'; import 'package:solian/screens/realms.dart';
import 'package:solian/screens/realms/realm_detail.dart';
import 'package:solian/screens/realms/realm_organize.dart'; import 'package:solian/screens/realms/realm_organize.dart';
import 'package:solian/screens/realms/realm_view.dart';
import 'package:solian/screens/social.dart'; import 'package:solian/screens/social.dart';
import 'package:solian/screens/posts/post_publish.dart'; import 'package:solian/screens/posts/post_publish.dart';
import 'package:solian/shells/basic_shell.dart'; import 'package:solian/shells/basic_shell.dart';
@ -57,6 +60,19 @@ abstract class AppRouter {
), ),
], ],
), ),
GoRoute(
path: '/posts/publish',
name: 'postPublishing',
builder: (context, state) {
final arguments = state.extra as PostPublishingArguments?;
return PostPublishingScreen(
edit: arguments?.edit,
reply: arguments?.reply,
repost: arguments?.repost,
realm: arguments?.realm,
);
},
),
ShellRoute( ShellRoute(
builder: (context, state, child) => builder: (context, state, child) =>
BasicShell(state: state, child: child), BasicShell(state: state, child: child),
@ -87,19 +103,6 @@ abstract class AppRouter {
), ),
], ],
), ),
GoRoute(
path: '/posts/publish',
name: 'postPublishing',
builder: (context, state) {
final arguments = state.extra as PostPublishingArguments?;
return PostPublishingScreen(
edit: arguments?.edit,
reply: arguments?.reply,
repost: arguments?.repost,
realm: state.uri.queryParameters['realm'],
);
},
),
GoRoute( GoRoute(
path: '/chat/organize', path: '/chat/organize',
name: 'channelOrganizing', name: 'channelOrganizing',
@ -121,6 +124,20 @@ abstract class AppRouter {
); );
}, },
), ),
ShellRoute(
builder: (context, state, child) =>
BasicShell(state: state, child: child),
routes: [
GoRoute(
path: '/realms/:alias/detail',
name: 'realmDetail',
builder: (context, state) => RealmDetailScreen(
realm: state.extra as Realm,
alias: state.pathParameters['alias']!,
),
),
],
),
GoRoute( GoRoute(
path: '/realm/organize', path: '/realm/organize',
name: 'realmOrganizing', name: 'realmOrganizing',
@ -131,6 +148,15 @@ abstract class AppRouter {
); );
}, },
), ),
GoRoute(
path: '/realm/:alias',
name: 'realmView',
builder: (context, state) {
return RealmViewScreen(
alias: state.pathParameters['alias']!,
);
},
),
], ],
); );
} }

View File

@ -161,11 +161,6 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
), ),
), ),
onLongPress: () {}, onLongPress: () {},
).animate(key: Key('m${item.id}'), autoPlay: true).slideY(
curve: Curves.fastEaseInToSlowEaseOut,
duration: 350.ms,
begin: 0.25,
end: 0,
); );
} }

View File

@ -165,10 +165,10 @@ class _ContactScreenState extends State<ContactScreen> {
feColor: Colors.white, feColor: Colors.white,
), ),
contentPadding: const EdgeInsets.symmetric(horizontal: 24), contentPadding: const EdgeInsets.symmetric(horizontal: 24),
title: Text(otherside.account.name), title: Text(otherside.account.nick),
subtitle: Text( subtitle: Text(
'channelDirectDescription' 'channelDirectDescription'
.trParams({'username': otherside.account.name}), .trParams({'username': '@${otherside.account.name}'}),
), ),
onTap: () { onTap: () {
AppRouter.instance.pushNamed( AppRouter.instance.pushNamed(

View File

@ -4,6 +4,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/exts.dart'; import 'package:solian/exts.dart';
import 'package:solian/models/post.dart'; import 'package:solian/models/post.dart';
import 'package:solian/models/realm.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/services.dart'; import 'package:solian/services.dart';
@ -16,15 +17,16 @@ class PostPublishingArguments {
final Post? edit; final Post? edit;
final Post? reply; final Post? reply;
final Post? repost; final Post? repost;
final Realm? realm;
PostPublishingArguments({this.edit, this.reply, this.repost}); PostPublishingArguments({this.edit, this.reply, this.repost, this.realm});
} }
class PostPublishingScreen extends StatefulWidget { class PostPublishingScreen extends StatefulWidget {
final Post? edit; final Post? edit;
final Post? reply; final Post? reply;
final Post? repost; final Post? repost;
final String? realm; final Realm? realm;
const PostPublishingScreen({ const PostPublishingScreen({
super.key, super.key,
@ -73,6 +75,7 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
if (widget.edit != null) 'alias': widget.edit!.alias, if (widget.edit != null) 'alias': widget.edit!.alias,
if (widget.reply != null) 'reply_to': widget.reply!.id, if (widget.reply != null) 'reply_to': widget.reply!.id,
if (widget.repost != null) 'repost_to': widget.repost!.id, if (widget.repost != null) 'repost_to': widget.repost!.id,
if (widget.realm != null) 'realm': widget.realm!.alias,
}; };
Response resp; Response resp;
@ -226,7 +229,18 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
} }
}, },
), ),
const Divider(thickness: 0.3), if (widget.realm != null)
MaterialBanner(
leading: const Icon(Icons.group),
leadingPadding: const EdgeInsets.only(left: 10, right: 20),
dividerColor: Colors.transparent,
content: Text(
'postInRealmNotify'
.trParams({'realm': '#${widget.realm!.alias}'}),
),
actions: notifyBannerActions,
),
const Divider(thickness: 0.3, height: 0.3).paddingOnly(bottom: 8),
Expanded( Expanded(
child: Container( child: Container(
padding: padding:
@ -245,14 +259,9 @@ class _PostPublishingScreenState extends State<PostPublishingScreen> {
), ),
), ),
), ),
Container( const Divider(thickness: 0.3, height: 0.3),
constraints: const BoxConstraints(minHeight: 56), SizedBox(
decoration: BoxDecoration( height: 56,
border: Border(
top: BorderSide(
width: 0.3, color: Theme.of(context).dividerColor),
),
),
child: Row( child: Row(
children: [ children: [
TextButton( TextButton(

View File

@ -154,7 +154,11 @@ class _RealmListScreenState extends State<RealmListScreen> {
) )
], ],
), ),
onTap: () {}, onTap: () {
AppRouter.instance.pushNamed('realmView', pathParameters: {
'alias': element.alias,
});
},
), ),
), ),
).paddingOnly(left: 8, right: 8, bottom: 4); ).paddingOnly(left: 8, right: 8, bottom: 4);

View File

@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:solian/models/realm.dart';
class RealmDetailScreen extends StatelessWidget {
final String alias;
final Realm realm;
const RealmDetailScreen({
super.key,
required this.alias,
required this.realm,
});
@override
Widget build(BuildContext context) {
throw UnimplementedError();
}
}

View File

@ -0,0 +1,201 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/pagination.dart';
import 'package:solian/models/post.dart';
import 'package:solian/models/realm.dart';
import 'package:solian/providers/content/post.dart';
import 'package:solian/providers/content/realm.dart';
import 'package:solian/router.dart';
import 'package:solian/screens/posts/post_publish.dart';
import 'package:solian/theme.dart';
import 'package:solian/widgets/posts/post_list.dart';
class RealmViewScreen extends StatefulWidget {
final String alias;
const RealmViewScreen({super.key, required this.alias});
@override
State<RealmViewScreen> createState() => _RealmViewScreenState();
}
class _RealmViewScreenState extends State<RealmViewScreen> {
bool _isBusy = false;
String? _overrideAlias;
Realm? _realm;
getRealm({String? overrideAlias}) async {
final RealmProvider provider = Get.find();
setState(() => _isBusy = true);
if (overrideAlias != null) {
_overrideAlias = overrideAlias;
}
try {
final resp = await provider.getRealm(_overrideAlias ?? widget.alias);
setState(() => _realm = Realm.fromJson(resp.body));
} catch (e) {
context.showErrorDialog(e);
}
setState(() => _isBusy = false);
}
@override
void initState() {
super.initState();
getRealm();
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: DefaultTabController(
length: 2,
child: SafeArea(
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverOverlapAbsorber(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
title: Text(_realm?.name ?? 'loading'.tr),
centerTitle: false,
actions: [
IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {
AppRouter.instance
.pushNamed(
'realmDetail',
pathParameters: {'alias': widget.alias},
extra: _realm,
)
.then((value) {
if (value == false) AppRouter.instance.pop();
if (value != null) {
final resp =
Realm.fromJson(value as Map<String, dynamic>);
getRealm(overrideAlias: resp.alias);
}
});
},
),
SizedBox(
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
),
],
bottom: const TabBar(
isScrollable: true,
tabs: [
Tab(icon: Icon(Icons.feed)),
Tab(icon: Icon(Icons.chat)),
],
),
),
)
];
},
body: Builder(
builder: (context) {
if (_isBusy) {
return const Center(child: CircularProgressIndicator());
}
return TabBarView(
children: [
RealmPostListWidget(realm: _realm!),
Icon(Icons.directions_transit),
],
);
},
),
),
),
),
);
}
}
class RealmPostListWidget extends StatefulWidget {
final Realm realm;
const RealmPostListWidget({super.key, required this.realm});
@override
State<RealmPostListWidget> createState() => _RealmPostListWidgetState();
}
class _RealmPostListWidgetState extends State<RealmPostListWidget> {
final PagingController<int, Post> _pagingController =
PagingController(firstPageKey: 0);
getPosts(int pageKey) async {
final PostProvider provider = Get.find();
Response resp;
try {
resp = await provider.listPost(pageKey, realm: widget.realm.id);
} catch (e) {
_pagingController.error = e;
return;
}
final PaginationResult result = PaginationResult.fromJson(resp.body);
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
if (parsed != null && parsed.length >= 10) {
_pagingController.appendPage(parsed, pageKey + parsed.length);
} else if (parsed != null) {
_pagingController.appendLastPage(parsed);
}
}
@override
void initState() {
super.initState();
_pagingController.addPageRequestListener(getPosts);
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: () => Future.sync(() => _pagingController.refresh()),
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: ListTile(
leading: const Icon(Icons.post_add),
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
tileColor: Theme.of(context).colorScheme.surfaceContainer,
title: Text('postNew'.tr),
subtitle: Text(
'postNewInRealmHint'
.trParams({'realm': '#${widget.realm.alias}'}),
),
onTap: () {
AppRouter.instance
.pushNamed(
'postPublishing',
extra: PostPublishingArguments(realm: widget.realm),
)
.then((value) {
if (value != null) _pagingController.refresh();
});
},
),
),
PostListWidget(controller: _pagingController),
],
),
);
}
}

View File

@ -68,10 +68,12 @@ class SolianMessages extends Translations {
'notifyAllRead': 'Mark all as read', 'notifyAllRead': 'Mark all as read',
'notifyEmpty': 'All notifications read', 'notifyEmpty': 'All notifications read',
'notifyEmptyCaption': 'It seems like nothing happened recently', 'notifyEmptyCaption': 'It seems like nothing happened recently',
'postNew': 'Create a new post',
'postNewInRealmHint': 'Add post in realm @realm',
'postAction': 'Post', 'postAction': 'Post',
'postDetail': 'Post', 'postDetail': 'Post',
'postReplies': 'Replies', 'postReplies': 'Replies',
'postPublishing': 'Post a post', 'postPublishing': 'Post post',
'postIdentityNotify': 'You will post this post as', 'postIdentityNotify': 'You will post this post as',
'postContentPlaceholder': 'What\'s happened?!', 'postContentPlaceholder': 'What\'s happened?!',
'postReaction': 'Reactions of the Post', 'postReaction': 'Reactions of the Post',
@ -79,6 +81,7 @@ class SolianMessages extends Translations {
'postReplyAction': 'Make a reply', 'postReplyAction': 'Make a reply',
'postRepliedNotify': 'Replied a post from @username.', 'postRepliedNotify': 'Replied a post from @username.',
'postRepostedNotify': 'Reposted a post from @username.', 'postRepostedNotify': 'Reposted a post from @username.',
'postInRealmNotify': 'You\'re posting in realm @realm.',
'postEditingNotify': 'You\'re editing as post from you.', 'postEditingNotify': 'You\'re editing as post from you.',
'postReplyingNotify': 'You\'re replying a post from @username.', 'postReplyingNotify': 'You\'re replying a post from @username.',
'postRepostingNotify': 'You\'re reposting a post from @username.', 'postRepostingNotify': 'You\'re reposting a post from @username.',
@ -104,6 +107,7 @@ class SolianMessages extends Translations {
'realmDescription': 'Description', 'realmDescription': 'Description',
'realmPublic': 'Public Realm', 'realmPublic': 'Public Realm',
'realmCommunity': 'Community Realm', 'realmCommunity': 'Community Realm',
'realmDetail': 'Realm detail',
'channelOrganizing': 'Organize a channel', 'channelOrganizing': 'Organize a channel',
'channelOrganizeCommon': 'Create regular channel', 'channelOrganizeCommon': 'Create regular channel',
'channelOrganizeDirect': 'Create DM', 'channelOrganizeDirect': 'Create DM',
@ -190,6 +194,8 @@ class SolianMessages extends Translations {
'notifyAllRead': '已读所有通知', 'notifyAllRead': '已读所有通知',
'notifyEmpty': '通知箱为空', 'notifyEmpty': '通知箱为空',
'notifyEmptyCaption': '看起来最近没发生什么呢', 'notifyEmptyCaption': '看起来最近没发生什么呢',
'postNew': '创建新帖子',
'postNewInRealmHint': '在领域 @realm 里发表新帖子',
'postAction': '发表', 'postAction': '发表',
'postDetail': '帖子详情', 'postDetail': '帖子详情',
'postReplies': '帖子回复', 'postReplies': '帖子回复',
@ -201,6 +207,7 @@ class SolianMessages extends Translations {
'postReplyAction': '发表一则回复', 'postReplyAction': '发表一则回复',
'postRepliedNotify': '回了一个 @username 的帖子', 'postRepliedNotify': '回了一个 @username 的帖子',
'postRepostedNotify': '转了一个 @username 的帖子', 'postRepostedNotify': '转了一个 @username 的帖子',
'postInRealmNotify': '你正在领域 @realm 中发表帖子',
'postEditingNotify': '你正在编辑一个你发布的帖子', 'postEditingNotify': '你正在编辑一个你发布的帖子',
'postReplyingNotify': '你正在回一个来自 @username 的帖子', 'postReplyingNotify': '你正在回一个来自 @username 的帖子',
'postRepostingNotify': '你正在转一个来自 @username 的帖子', 'postRepostingNotify': '你正在转一个来自 @username 的帖子',
@ -225,6 +232,7 @@ class SolianMessages extends Translations {
'realmDescription': '领域简介', 'realmDescription': '领域简介',
'realmPublic': '公开领域', 'realmPublic': '公开领域',
'realmCommunity': '社区领域', 'realmCommunity': '社区领域',
'realmDetail': '领域详情',
'channelOrganizing': '组织频道', 'channelOrganizing': '组织频道',
'channelOrganizeCommon': '创建普通频道', 'channelOrganizeCommon': '创建普通频道',
'channelOrganizeDirect': '创建私信频道', 'channelOrganizeDirect': '创建私信频道',

View File

@ -48,7 +48,7 @@ class PostListWidget extends StatelessWidget {
context: context, context: context,
builder: (context) => PostAction(item: item), builder: (context) => PostAction(item: item),
).then((value) { ).then((value) {
if (value == true) controller.refresh(); if (value != null) controller.refresh();
}); });
}, },
); );