fix posts on account adding many times

This commit is contained in:
zoe 2022-09-05 23:56:06 +02:00
parent 1f557d5734
commit cb064cb806
2 changed files with 27 additions and 18 deletions

View File

@ -212,14 +212,17 @@ Future<MapEntry<int, List<ThreadModel>?>> getPostsForAccount(
return MapEntry(r.statusCode, null);
}
final List<dynamic> rb = jsonDecode(r.body);
List<ThreadModel> threads = [];
List<dynamic> rb = jsonDecode(r.body);
List<PostModel> posts = [];
for (var element in rb) {
posts.add(PostModel.fromJson(element, model.identity));
}
posts.sort();
List<ThreadModel> threads = [];
for (var element in posts) {
threads.add(
await PostModel.fromJson(
element,
model.identity,
).getThread(),
await element.getThread(),
);
}
return MapEntry(r.statusCode, threads);

View File

@ -31,6 +31,7 @@ class _ProfileViewState extends State<ProfileView> {
List<ThreadModel> threads = [];
String? maxid;
final ScrollController _scrollController = ScrollController();
bool loadingPosts = false;
Future<void> addRelationship(AccountModel m) async {
final r = await getRelationship(m.identity, m.id);
@ -65,25 +66,30 @@ class _ProfileViewState extends State<ProfileView> {
}
void loadPosts() async {
if (threads.isNotEmpty) {
maxid = threads.last.posts.last.id;
}
if (!loadingPosts) {
loadingPosts = true;
if (threads.isNotEmpty) {
maxid = threads.last.posts.last.id;
}
final t = await getPostsForAccount(identities[activeIdentity]!, maxid);
if (t.value == null) {
return;
}
if (mounted) {
setState(() {
threads.addAll(t.value!);
});
final t = await getPostsForAccount(identities[activeIdentity]!, maxid);
if (t.value == null) {
return;
}
if (mounted) {
setState(() {
threads.addAll(t.value!);
});
}
loadingPosts = false;
}
}
void reloadPosts() async {
void reloadPosts() {
_scrollController.animateTo(0,
duration: const Duration(seconds: 1), curve: Curves.ease);
setState(() {
maxid = null;
threads = [];
});
loadPosts();