Solian/lib/screens/posts/draft_box.dart

129 lines
3.9 KiB
Dart
Raw Normal View History

2024-07-09 13:23:38 +00:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
2024-07-09 14:39:44 +00:00
import 'package:solian/models/pagination.dart';
import 'package:solian/models/post.dart';
import 'package:solian/providers/content/posts.dart';
2024-07-09 13:23:38 +00:00
import 'package:solian/theme.dart';
import 'package:solian/widgets/app_bar_leading.dart';
2024-07-09 13:23:38 +00:00
import 'package:solian/widgets/app_bar_title.dart';
2024-10-15 13:14:56 +00:00
import 'package:solian/widgets/loading_indicator.dart';
2024-07-09 14:39:44 +00:00
import 'package:solian/widgets/posts/post_action.dart';
2024-10-15 13:14:56 +00:00
import 'package:solian/widgets/posts/post_item.dart';
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
2024-10-15 13:14:56 +00:00
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
2024-07-09 13:23:38 +00:00
2024-07-09 14:39:44 +00:00
class DraftBoxScreen extends StatefulWidget {
2024-07-09 13:23:38 +00:00
const DraftBoxScreen({super.key});
2024-07-09 14:39:44 +00:00
@override
State<DraftBoxScreen> createState() => _DraftBoxScreenState();
}
class _DraftBoxScreenState extends State<DraftBoxScreen> {
2024-10-15 13:14:56 +00:00
bool _isBusy = true;
int? _totalPosts;
final List<Post> _posts = List.empty(growable: true);
2024-07-09 14:39:44 +00:00
2024-10-15 13:14:56 +00:00
_getPosts() async {
setState(() => _isBusy = true);
2024-07-09 14:39:44 +00:00
2024-10-15 13:14:56 +00:00
final PostProvider posts = Get.find();
final resp = await posts.listDraft(_posts.length);
2024-07-09 14:39:44 +00:00
final PaginationResult result = PaginationResult.fromJson(resp.body);
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
2024-10-15 13:14:56 +00:00
_totalPosts = result.count;
_posts.addAll(parsed ?? List.empty());
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();
}
});
2024-07-09 14:39:44 +00:00
}
@override
void initState() {
super.initState();
2024-10-15 13:14:56 +00:00
_getPosts();
2024-07-09 14:39:44 +00:00
}
2024-07-09 13:23:38 +00:00
@override
Widget build(BuildContext context) {
2024-10-06 09:37:07 +00:00
return RootContainer(
2024-07-09 13:23:38 +00:00
child: Scaffold(
appBar: AppBar(
2024-07-12 14:37:58 +00:00
leading: AppBarLeadingButton.adaptive(context),
2024-07-09 13:23:38 +00:00
title: AppBarTitle('draftBox'.tr),
centerTitle: false,
toolbarHeight: AppTheme.toolbarHeight(context),
2024-07-09 13:23:38 +00:00
actions: [
SizedBox(
width: AppTheme.isLargeScreen(context) ? 8 : 16,
2024-07-09 13:23:38 +00:00
),
],
),
2024-10-15 13:14:56 +00:00
body: Column(
children: [
LoadingIndicator(isActive: _isBusy),
Expanded(
child: RefreshIndicator(
onRefresh: () {
_posts.clear();
return _getPosts();
},
child: InfiniteList(
itemCount: _posts.length,
hasReachedMax: _totalPosts == _posts.length,
isLoading: _isBusy,
onFetchData: () => _getPosts(),
itemBuilder: (context, index) {
final item = _posts[index];
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,
onTapMore: () => _openActions(item),
).paddingSymmetric(vertical: 8),
],
),
onTap: () => _openActions(item),
),
2024-10-15 13:14:56 +00:00
).paddingOnly(left: 12, right: 12, bottom: 4);
},
2024-10-15 13:14:56 +00:00
),
),
2024-07-09 14:39:44 +00:00
),
2024-10-15 13:14:56 +00:00
],
2024-07-09 14:39:44 +00:00
),
2024-07-09 13:23:38 +00:00
),
);
}
}