2024-09-02 15:11:40 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2024-07-25 06:42:50 +00:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/models/post.dart';
|
2024-07-25 08:08:46 +00:00
|
|
|
import 'package:solian/providers/content/posts.dart';
|
2024-09-03 15:07:20 +00:00
|
|
|
import 'package:solian/providers/last_read.dart';
|
2024-07-25 06:42:50 +00:00
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
class PostListController extends GetxController {
|
2024-07-26 08:53:05 +00:00
|
|
|
String? author;
|
2024-09-12 16:17:56 +00:00
|
|
|
String? realm;
|
2024-07-26 08:53:05 +00:00
|
|
|
|
2024-07-25 06:42:50 +00:00
|
|
|
/// The polling source modifier.
|
|
|
|
/// - `0`: default recommendations
|
2024-08-18 16:33:03 +00:00
|
|
|
/// - `1`: friend mode
|
|
|
|
/// - `2`: shuffle mode
|
2024-07-25 06:42:50 +00:00
|
|
|
RxInt mode = 0.obs;
|
|
|
|
|
|
|
|
/// The paging controller for infinite loading.
|
2024-09-03 15:07:20 +00:00
|
|
|
/// Only available when mode is `0`, `1` or `2`.
|
2024-07-25 06:42:50 +00:00
|
|
|
PagingController<int, Post> pagingController =
|
2024-07-26 08:53:05 +00:00
|
|
|
PagingController(firstPageKey: 0);
|
2024-07-25 06:42:50 +00:00
|
|
|
|
2024-07-26 08:53:05 +00:00
|
|
|
PostListController({this.author}) {
|
2024-07-25 06:42:50 +00:00
|
|
|
_initPagingController();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize a compatibility layer to paging controller
|
|
|
|
void _initPagingController() {
|
|
|
|
pagingController.addPageRequestListener(_onPagingControllerRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onPagingControllerRequest(int pageKey) async {
|
|
|
|
try {
|
|
|
|
final result = await loadMore();
|
|
|
|
|
|
|
|
if (result != null && hasMore.value) {
|
|
|
|
pagingController.appendPage(result, nextPageKey.value);
|
|
|
|
} else if (result != null) {
|
|
|
|
pagingController.appendLastPage(result);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
pagingController.error = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _resetPagingController() {
|
|
|
|
pagingController.removePageRequestListener(_onPagingControllerRequest);
|
|
|
|
pagingController.nextPageKey = nextPageKey.value;
|
|
|
|
pagingController.itemList?.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
RxBool isBusy = false.obs;
|
2024-07-25 08:08:46 +00:00
|
|
|
RxBool isPreparing = false.obs;
|
|
|
|
|
|
|
|
RxInt focusCursor = 0.obs;
|
2024-07-26 08:53:05 +00:00
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
Post get focusPost => postList[focusCursor.value];
|
2024-07-25 06:42:50 +00:00
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
RxInt postTotal = 0.obs;
|
2024-07-25 06:42:50 +00:00
|
|
|
RxList<Post> postList = RxList.empty(growable: true);
|
2024-07-25 08:08:46 +00:00
|
|
|
|
2024-07-25 06:42:50 +00:00
|
|
|
RxInt nextPageKey = 0.obs;
|
|
|
|
RxBool hasMore = true.obs;
|
|
|
|
|
|
|
|
Future<void> reloadAllOver() async {
|
2024-07-25 08:08:46 +00:00
|
|
|
isPreparing.value = true;
|
|
|
|
|
|
|
|
focusCursor.value = 0;
|
2024-07-25 06:42:50 +00:00
|
|
|
nextPageKey.value = 0;
|
2024-07-25 08:08:46 +00:00
|
|
|
postList.clear();
|
2024-07-25 06:42:50 +00:00
|
|
|
hasMore.value = true;
|
2024-07-25 08:08:46 +00:00
|
|
|
|
2024-07-25 06:42:50 +00:00
|
|
|
_resetPagingController();
|
|
|
|
final result = await loadMore();
|
|
|
|
if (result != null && hasMore.value) {
|
|
|
|
pagingController.appendPage(result, nextPageKey.value);
|
|
|
|
} else if (result != null) {
|
|
|
|
pagingController.appendLastPage(result);
|
|
|
|
}
|
|
|
|
_initPagingController();
|
2024-07-25 08:08:46 +00:00
|
|
|
|
|
|
|
isPreparing.value = false;
|
2024-07-25 06:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Post>?> loadMore() async {
|
|
|
|
final result = await _loadPosts(nextPageKey.value);
|
|
|
|
|
|
|
|
if (result != null && result.length >= 10) {
|
2024-07-25 08:08:46 +00:00
|
|
|
postList.addAll(result);
|
|
|
|
nextPageKey.value += result.length;
|
2024-07-25 06:42:50 +00:00
|
|
|
hasMore.value = true;
|
|
|
|
} else if (result != null) {
|
2024-07-25 08:08:46 +00:00
|
|
|
postList.addAll(result);
|
|
|
|
nextPageKey.value += result.length;
|
2024-07-25 06:42:50 +00:00
|
|
|
hasMore.value = false;
|
|
|
|
}
|
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
final idx = <dynamic>{};
|
|
|
|
postList.retainWhere((x) => idx.add(x.id));
|
|
|
|
|
2024-09-12 16:17:56 +00:00
|
|
|
if (postList.isNotEmpty) {
|
|
|
|
var lastId = postList.map((x) => x.id).reduce(max);
|
|
|
|
Get.find<LastReadProvider>().feedLastReadAt = lastId;
|
|
|
|
}
|
2024-09-02 15:11:40 +00:00
|
|
|
|
2024-07-25 06:42:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Post>?> _loadPosts(int pageKey) async {
|
|
|
|
isBusy.value = true;
|
|
|
|
|
|
|
|
final PostProvider provider = Get.find();
|
|
|
|
|
|
|
|
Response resp;
|
|
|
|
try {
|
2024-07-26 08:53:05 +00:00
|
|
|
if (author != null) {
|
|
|
|
resp = await provider.listPost(
|
|
|
|
pageKey,
|
|
|
|
author: author,
|
|
|
|
);
|
|
|
|
} else {
|
2024-08-18 16:33:03 +00:00
|
|
|
switch (mode.value) {
|
|
|
|
case 2:
|
|
|
|
resp = await provider.listRecommendations(
|
|
|
|
pageKey,
|
|
|
|
channel: 'shuffle',
|
2024-09-12 16:17:56 +00:00
|
|
|
realm: realm,
|
2024-08-18 16:33:03 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
resp = await provider.listRecommendations(
|
|
|
|
pageKey,
|
|
|
|
channel: 'friends',
|
2024-09-12 16:17:56 +00:00
|
|
|
realm: realm,
|
2024-08-18 16:33:03 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
2024-09-12 16:17:56 +00:00
|
|
|
resp = await provider.listRecommendations(
|
|
|
|
pageKey,
|
|
|
|
realm: realm,
|
|
|
|
);
|
2024-08-18 16:33:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-07-26 08:53:05 +00:00
|
|
|
}
|
2024-07-25 06:42:50 +00:00
|
|
|
} catch (e) {
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
isBusy.value = false;
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:53:05 +00:00
|
|
|
final result = PaginationResult.fromJson(resp.body);
|
2024-07-25 06:42:50 +00:00
|
|
|
final out = result.data?.map((e) => Post.fromJson(e)).toList();
|
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
postTotal.value = result.count;
|
2024-07-25 06:42:50 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-07-25 08:08:46 +00:00
|
|
|
@override
|
2024-07-25 06:42:50 +00:00
|
|
|
void dispose() {
|
|
|
|
pagingController.dispose();
|
2024-07-25 08:08:46 +00:00
|
|
|
super.dispose();
|
2024-07-25 06:42:50 +00:00
|
|
|
}
|
|
|
|
}
|