2024-11-08 16:09:46 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2024-11-09 04:04:03 +00:00
|
|
|
import 'package:surface/providers/sn_attachment.dart';
|
2024-11-08 16:09:46 +00:00
|
|
|
import 'package:surface/providers/sn_network.dart';
|
|
|
|
import 'package:surface/types/post.dart';
|
|
|
|
import 'package:surface/widgets/navigation/app_scaffold.dart';
|
2024-11-09 03:16:14 +00:00
|
|
|
import 'package:surface/widgets/post/post_item.dart';
|
2024-11-08 16:09:46 +00:00
|
|
|
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
|
|
|
|
|
|
|
|
class ExploreScreen extends StatefulWidget {
|
|
|
|
const ExploreScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ExploreScreen> createState() => _ExploreScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ExploreScreenState extends State<ExploreScreen> {
|
|
|
|
bool _isBusy = true;
|
|
|
|
|
|
|
|
final List<SnPost> _posts = List.empty(growable: true);
|
2024-11-09 03:16:14 +00:00
|
|
|
int? _postCount;
|
2024-11-08 16:09:46 +00:00
|
|
|
|
|
|
|
void _fetchPosts() async {
|
2024-11-09 03:16:14 +00:00
|
|
|
if (_postCount != null && _posts.length >= _postCount!) return;
|
|
|
|
|
2024-11-08 16:09:46 +00:00
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
final resp = await sn.client.get('/cgi/co/posts', queryParameters: {
|
|
|
|
'take': 10,
|
2024-11-09 03:16:14 +00:00
|
|
|
'offset': _posts.length,
|
2024-11-08 16:09:46 +00:00
|
|
|
});
|
2024-11-09 04:04:03 +00:00
|
|
|
final List<SnPost> out =
|
|
|
|
List.from(resp.data['data']?.map((e) => SnPost.fromJson(e)) ?? []);
|
|
|
|
|
|
|
|
Set<String> rids = {};
|
|
|
|
for (var i = 0; i < out.length; i++) {
|
|
|
|
rids.addAll(out[i].body['attachments']?.cast<String>() ?? []);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
final attach = context.read<SnAttachmentProvider>();
|
|
|
|
final attachments = await attach.getMultiple(rids.toList());
|
|
|
|
for (var i = 0; i < out.length; i++) {
|
|
|
|
out[i] = out[i].copyWith(
|
|
|
|
preload: SnPostPreload(
|
|
|
|
attachments: attachments
|
|
|
|
.where(
|
|
|
|
(ele) => out[i].body['attachments']?.contains(ele.rid) ?? false,
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-11-08 16:09:46 +00:00
|
|
|
|
|
|
|
_postCount = resp.data['count'];
|
2024-11-09 04:04:03 +00:00
|
|
|
_posts.addAll(out);
|
2024-11-08 16:09:46 +00:00
|
|
|
|
|
|
|
if (mounted) setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_fetchPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AppScaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text('screenExplore').tr(),
|
|
|
|
),
|
|
|
|
body: InfiniteList(
|
|
|
|
itemCount: _posts.length,
|
|
|
|
isLoading: _isBusy,
|
2024-11-09 03:16:14 +00:00
|
|
|
hasReachedMax: _postCount != null && _posts.length >= _postCount!,
|
2024-11-08 16:09:46 +00:00
|
|
|
onFetchData: _fetchPosts,
|
|
|
|
itemBuilder: (context, idx) {
|
2024-11-09 03:16:14 +00:00
|
|
|
return PostItem(data: _posts[idx]);
|
2024-11-08 16:09:46 +00:00
|
|
|
},
|
2024-11-09 03:16:14 +00:00
|
|
|
separatorBuilder: (context, index) => const Divider(),
|
2024-11-08 16:09:46 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|