forked from LemmyNet/jerboa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance refactors for Posts feed (LemmyNet#1527)
* Current WIP * PostsViewModel Changes * Fix linter * Fix linter * Fix collapsible header causing recompositions * Do not show empty on startup * Fix linter * Apply remarks * Fix master * Rebase main * Fix linter * Fix OoB case * Rebase main
- Loading branch information
Showing
20 changed files
with
503 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.jerboa.feed | ||
|
||
import android.util.Log | ||
import androidx.compose.runtime.mutableStateListOf | ||
|
||
open class FeedController<T> { | ||
protected val items = mutableStateListOf<T>() | ||
|
||
val feed: List<T> = items | ||
|
||
fun updateAll( | ||
selector: (List<T>) -> List<Int>, | ||
transformer: (T) -> T, | ||
) { | ||
selector(items).forEach { | ||
safeUpdate(it, transformer) | ||
} | ||
} | ||
|
||
fun safeUpdate( | ||
index: Int, | ||
transformer: (T) -> T, | ||
) { | ||
if (!isValidIndex(index)) { | ||
Log.d("FeedController", "OoB item not updated $index") | ||
return | ||
} | ||
|
||
safeUpdate(index, transformer(items[index])) | ||
} | ||
|
||
fun safeUpdate( | ||
selector: (List<T>) -> Int, | ||
transformer: (T) -> T, | ||
) { | ||
safeUpdate(selector(items), transformer) | ||
} | ||
|
||
/** | ||
* Update the item at the given index with the new item. | ||
* | ||
* If given -1 or an index that is out of bounds, the update will not be performed. | ||
* It assumes that the item couldn't be found because the list has changed. | ||
* Example: a network request to update an item succeeded after the list has changed. | ||
* So, we ignore it | ||
*/ | ||
fun safeUpdate( | ||
index: Int, | ||
new: T, | ||
) { | ||
if (isValidIndex(index)) { | ||
items[index] = new | ||
} else { | ||
Log.d("FeedController", "OoB item not updated $new") | ||
} | ||
} | ||
|
||
fun add(item: T) = items.add(item) | ||
|
||
fun remove(item: T) = items.remove(item) | ||
|
||
fun clear() = items.clear() | ||
|
||
fun addAll(newItems: List<T>) = items.addAll(newItems) | ||
|
||
protected inline fun <E> Iterable<E>.indexesOf(predicate: (E) -> Boolean) = | ||
mapIndexedNotNull { index, elem -> | ||
index.takeIf { | ||
predicate(elem) | ||
} | ||
} | ||
|
||
private fun isValidIndex(index: Int) = index >= 0 && index < items.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jerboa.feed | ||
|
||
import it.vercruysse.lemmyapi.v0x19.datatypes.PaginationCursor | ||
|
||
class PaginationController( | ||
var page: Long = 1, | ||
var pageCursor: PaginationCursor? = null, | ||
) { | ||
fun reset() { | ||
page = 1 | ||
pageCursor = null | ||
} | ||
|
||
fun nextPage(pageCursor: PaginationCursor?) { | ||
page++ | ||
this.pageCursor = pageCursor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.jerboa.feed | ||
|
||
import com.jerboa.datatypes.BanFromCommunityData | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.HidePost | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.Person | ||
import it.vercruysse.lemmyapi.v0x19.datatypes.PostView | ||
|
||
open class PostController : FeedController<PostView>() { | ||
fun findAndUpdatePost(updatedPostView: PostView) { | ||
safeUpdate({ posts -> | ||
posts.indexOfFirst { | ||
it.post.id == updatedPostView.post.id | ||
} | ||
}) { updatedPostView } | ||
} | ||
|
||
fun findAndUpdateCreator(person: Person) { | ||
updateAll( | ||
{ it.indexesOf { postView -> postView.creator.id == person.id } }, | ||
) { it.copy(creator = person) } | ||
} | ||
|
||
fun findAndUpdatePostCreatorBannedFromCommunity(banData: BanFromCommunityData) { | ||
updateAll( | ||
{ | ||
it.indexesOf { postView -> | ||
postView.creator.id == banData.person.id && postView.community.id == banData.community.id | ||
} | ||
}, | ||
) { it.copy(banned_from_community = banData.banned, creator_banned_from_community = banData.banned, creator = banData.person) } | ||
} | ||
|
||
fun findAndUpdatePostHidden(hidePost: HidePost) { | ||
updateAll( | ||
{ | ||
it.indexesOf { postView -> | ||
hidePost.post_ids.contains(postView.post.id) | ||
} | ||
}, | ||
) { it.copy(hidden = hidePost.hide) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.