-
Notifications
You must be signed in to change notification settings - Fork 175
Adds option to enable/disable infinite scroll #2172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a7dbb94
3fe222a
18ee28e
adca5f5
974a481
f0d1f0f
80b0f5d
95e3c35
c75a1d5
588638d
7937f11
45fc3f5
a0d5033
2422c79
f7cae6e
0563a90
8723976
cb41470
00a691e
22752eb
369ae5c
f4f97ec
a6c2aa2
52ed542
916415e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
rodrigo-fm marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,36 @@ | ||
| package com.jerboa.feed | ||
|
|
||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableLongStateOf | ||
| import androidx.compose.runtime.setValue | ||
| import it.vercruysse.lemmyapi.datatypes.PaginationCursor | ||
| import java.util.Stack | ||
|
|
||
| class PaginationController { | ||
|
|
||
| val previousPageCursors = Stack<PaginationCursor?>() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
| var currentPageCursor: PaginationCursor? = null | ||
| var nextPageCursor: PaginationCursor? = null | ||
| var page by mutableLongStateOf(1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did this need to change. I thought |
||
|
|
||
| fun appendPage(nextPage: PaginationCursor?) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a dupe of |
||
| page++ | ||
| this.nextPageCursor = nextPage | ||
| } | ||
|
|
||
| class PaginationController( | ||
| var page: Long = 1, | ||
| var pageCursor: PaginationCursor? = null, | ||
| ) { | ||
| fun reset() { | ||
| page = 1 | ||
| pageCursor = null | ||
| currentPageCursor = null | ||
| nextPageCursor = null | ||
| previousPageCursors.clear() | ||
| } | ||
|
|
||
| fun nextPage(pageCursor: PaginationCursor?) { | ||
| fun nextPage(nextPage: PaginationCursor?) { | ||
| page++ | ||
| this.pageCursor = pageCursor | ||
| previousPageCursors.push(this.currentPageCursor) | ||
| this.currentPageCursor = this.nextPageCursor | ||
| this.nextPageCursor = nextPage | ||
| } | ||
| } | ||
|
|
||
| fun canMoveToPreviousPage(): Boolean = page > 1 && previousPageCursors.isNotEmpty() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ import com.jerboa.db.entity.AnonAccount | |
| import com.jerboa.db.repository.AccountRepository | ||
| import com.jerboa.feed.PaginationController | ||
| import com.jerboa.feed.PostController | ||
| import com.jerboa.findAndUpdatePostHidden | ||
| import com.jerboa.toEnumSafe | ||
| import it.vercruysse.lemmyapi.datatypes.CreatePostLike | ||
| import it.vercruysse.lemmyapi.datatypes.DeletePost | ||
|
|
@@ -29,6 +28,7 @@ import it.vercruysse.lemmyapi.datatypes.GetPosts | |
| import it.vercruysse.lemmyapi.datatypes.HidePost | ||
| import it.vercruysse.lemmyapi.datatypes.LockPost | ||
| import it.vercruysse.lemmyapi.datatypes.MarkPostAsRead | ||
| import it.vercruysse.lemmyapi.datatypes.PaginationCursor | ||
| import it.vercruysse.lemmyapi.datatypes.PersonView | ||
| import it.vercruysse.lemmyapi.datatypes.PostView | ||
| import it.vercruysse.lemmyapi.datatypes.SavePost | ||
|
|
@@ -51,6 +51,9 @@ open class PostsViewModel( | |
| private val pageController = PaginationController() | ||
| private val postController = PostController() | ||
|
|
||
| val currentPage: Long | ||
| get() = pageController.page | ||
|
Comment on lines
+54
to
+55
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why. Just use the pageController.page.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pageController is private and I was passing the currentPage as an argument to some screens (to display on the Paginator composable), but it might not be needed anymore after implementing all the changes suggested here. |
||
|
|
||
| protected fun init() { | ||
| viewModelScope.launch { | ||
| accountRepository.currentAccount | ||
|
|
@@ -68,12 +71,14 @@ open class PostsViewModel( | |
| private fun initPosts( | ||
| form: GetPosts, | ||
| state: ApiState<List<PostView>> = ApiState.Loading, | ||
| onSuccess: (PaginationCursor?) -> Unit = {}, | ||
| ) { | ||
| postController.clear() | ||
| viewModelScope.launch { | ||
| postsRes = state | ||
| postsRes = API.getInstance().getPosts(form).fold( | ||
| onSuccess = { | ||
| pageController.nextPage(it.next_page) | ||
| onSuccess(it.next_page) | ||
| postController.addAll(it.posts) | ||
| ApiState.Success(postController.feed) | ||
| }, | ||
|
|
@@ -94,7 +99,7 @@ open class PostsViewModel( | |
|
|
||
| when (val newRes = API.getInstance().getPosts(getForm()).toApiState()) { | ||
| is ApiState.Success -> { | ||
| pageController.nextPage(newRes.data.next_page) | ||
| pageController.appendPage(newRes.data.next_page) | ||
| postController.addAll(newRes.data.posts) | ||
| postsRes = ApiState.Success(oldRes.data) | ||
| } | ||
|
|
@@ -114,21 +119,49 @@ open class PostsViewModel( | |
| postController.findAndUpdatePostCreatorBannedFromCommunity(banData) | ||
| } | ||
|
|
||
| fun nextPage() { | ||
| initPosts( | ||
| getForm(), | ||
| ApiState.Loading, | ||
| onSuccess = pageController::nextPage | ||
| ) | ||
| } | ||
|
|
||
| fun previousPage() { | ||
| if (!pageController.canMoveToPreviousPage()) return | ||
|
|
||
| pageController.page-- | ||
| pageController.currentPageCursor = pageController.previousPageCursors.pop() | ||
|
Comment on lines
+133
to
+134
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add these as functions to pageController, just like with the others. |
||
|
|
||
| initPosts( | ||
| getPreviousPageForm(), | ||
| ApiState.Loading, | ||
| onSuccess = { pageController.nextPageCursor = it }, | ||
| ) | ||
| } | ||
|
|
||
| fun resetPosts(state: ApiState<List<PostView>> = ApiState.Loading) { | ||
| pageController.reset() | ||
| postController.clear() | ||
| initPosts( | ||
| getForm(), | ||
| state, | ||
| onSuccess = { pageController.nextPageCursor = it } | ||
| ) | ||
| } | ||
|
|
||
| fun refreshPosts() = resetPosts(ApiState.Refreshing) | ||
|
|
||
| protected open fun getPreviousPageForm() = GetPosts( | ||
| page = pageController.page, | ||
| page_cursor = pageController.currentPageCursor, | ||
|
Comment on lines
+154
to
+156
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like the current page, not the previous one. I don't think these extra form builders will be necessary in
|
||
| sort = sortType, | ||
| type_ = listingType, | ||
| ) | ||
|
|
||
| protected open fun getForm(): GetPosts = | ||
| GetPosts( | ||
| page = pageController.page, | ||
| page_cursor = pageController.pageCursor, | ||
| page_cursor = pageController.nextPageCursor, | ||
| sort = sortType, | ||
| type_ = listingType, | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.