🐛 Fix draft box

This commit is contained in:
LittleSheep 2024-10-15 21:14:56 +08:00
parent df787f02a1
commit 1c26944a05
2 changed files with 73 additions and 100 deletions

View File

@ -1,15 +1,16 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:solian/models/pagination.dart'; import 'package:solian/models/pagination.dart';
import 'package:solian/models/post.dart'; import 'package:solian/models/post.dart';
import 'package:solian/providers/content/posts.dart'; import 'package:solian/providers/content/posts.dart';
import 'package:solian/theme.dart'; import 'package:solian/theme.dart';
import 'package:solian/widgets/app_bar_leading.dart'; import 'package:solian/widgets/app_bar_leading.dart';
import 'package:solian/widgets/app_bar_title.dart'; import 'package:solian/widgets/app_bar_title.dart';
import 'package:solian/widgets/loading_indicator.dart';
import 'package:solian/widgets/posts/post_action.dart'; import 'package:solian/widgets/posts/post_action.dart';
import 'package:solian/widgets/posts/post_owned_list.dart'; import 'package:solian/widgets/posts/post_item.dart';
import 'package:solian/widgets/root_container.dart'; import 'package:solian/widgets/root_container.dart';
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
class DraftBoxScreen extends StatefulWidget { class DraftBoxScreen extends StatefulWidget {
const DraftBoxScreen({super.key}); const DraftBoxScreen({super.key});
@ -19,38 +20,50 @@ class DraftBoxScreen extends StatefulWidget {
} }
class _DraftBoxScreenState extends State<DraftBoxScreen> { class _DraftBoxScreenState extends State<DraftBoxScreen> {
final PagingController<int, Post> _pagingController = bool _isBusy = true;
PagingController(firstPageKey: 0); int? _totalPosts;
final List<Post> _posts = List.empty(growable: true);
_getPosts(int pageKey) async { _getPosts() async {
final PostProvider provider = Get.find(); setState(() => _isBusy = true);
Response resp; final PostProvider posts = Get.find();
try { final resp = await posts.listDraft(_posts.length);
resp = await provider.listDraft(pageKey);
} catch (e) {
_pagingController.error = e;
return;
}
final PaginationResult result = PaginationResult.fromJson(resp.body); final PaginationResult result = PaginationResult.fromJson(resp.body);
if (result.count == 0) {
_pagingController.appendLastPage([]);
return;
}
final parsed = result.data?.map((e) => Post.fromJson(e)).toList(); final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
if (parsed != null && parsed.length >= 10) { _totalPosts = result.count;
_pagingController.appendPage(parsed, pageKey + parsed.length); _posts.addAll(parsed ?? List.empty());
} else if (parsed != null) {
_pagingController.appendLastPage(parsed); setState(() => _isBusy = false);
} }
Future<void> _openActions(Post item) async {
showModalBottomSheet(
useRootNavigator: true,
context: context,
builder: (context) => PostAction(
item: item,
noReact: true,
),
).then((value) {
if (value is Future) {
value.then((_) {
_posts.clear();
_getPosts();
});
} else if (value != null) {
_posts.clear();
_getPosts();
}
});
} }
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_pagingController.addPageRequestListener(_getPosts); _getPosts();
} }
@override @override
@ -68,47 +81,48 @@ class _DraftBoxScreenState extends State<DraftBoxScreen> {
), ),
], ],
), ),
body: RefreshIndicator( body: Column(
onRefresh: () => Future.sync(() => _pagingController.refresh()), children: [
child: PagedListView<int, Post>( LoadingIndicator(isActive: _isBusy),
pagingController: _pagingController, Expanded(
builderDelegate: PagedChildBuilderDelegate( child: RefreshIndicator(
itemBuilder: (context, item, index) { onRefresh: () {
return PostOwnedListEntry( _posts.clear();
item: item, return _getPosts();
isFullContent: true, },
backgroundColor: child: InfiniteList(
Theme.of(context).colorScheme.surfaceContainerLow, itemCount: _posts.length,
onTap: () async { hasReachedMax: _totalPosts == _posts.length,
showModalBottomSheet( isLoading: _isBusy,
useRootNavigator: true, onFetchData: () => _getPosts(),
context: context, itemBuilder: (context, index) {
builder: (context) => PostAction( final item = _posts[index];
item: item, return Card(
noReact: true, child: GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
PostItem(
key: Key('p${item.id}'),
item: item,
isShowEmbed: false,
isClickable: false,
isShowReply: false,
isReactable: false,
onTapMore: () => _openActions(item),
).paddingSymmetric(vertical: 8),
],
),
onTap: () => _openActions(item),
), ),
).then((value) { ).paddingOnly(left: 12, right: 12, bottom: 4);
if (value is Future) {
value.then((_) {
_pagingController.refresh();
});
} else if (value != null) {
_pagingController.refresh();
}
});
}, },
).paddingOnly(left: 12, right: 12, bottom: 4); ),
}, ),
), ),
), ],
), ),
), ),
); );
} }
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
} }

View File

@ -1,41 +0,0 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/post.dart';
import 'package:solian/widgets/posts/post_item.dart';
class PostOwnedListEntry extends StatelessWidget {
final Post item;
final Function onTap;
final bool isFullContent;
final Color? backgroundColor;
const PostOwnedListEntry({
super.key,
required this.item,
required this.onTap,
this.isFullContent = false,
this.backgroundColor,
});
@override
Widget build(BuildContext context) {
return Card(
child: GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
PostItem(
key: Key('p${item.id}'),
item: item,
isShowEmbed: false,
isClickable: false,
isShowReply: false,
isReactable: false,
).paddingSymmetric(vertical: 8),
],
),
onTap: () => onTap(),
),
);
}
}