-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring Moving feed status from ViewModel to State objects
- Loading branch information
1 parent
3af2cb5
commit c32dc61
Showing
50 changed files
with
986 additions
and
626 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
52 changes: 52 additions & 0 deletions
52
amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/AccountFeedContentStates.kt
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,52 @@ | ||
/** | ||
* Copyright (c) 2024 Vitor Pamplona | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
* Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.vitorpamplona.amethyst.ui.feeds | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.vitorpamplona.amethyst.ui.dal.ChatroomListKnownFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.ChatroomListNewFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.DiscoverChatFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.DiscoverCommunityFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.DiscoverLiveFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.DiscoverMarketplaceFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.DiscoverNIP89FeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.HomeConversationsFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.HomeNewThreadFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.VideoFeedFilter | ||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel | ||
|
||
class AccountFeedContentStates( | ||
val accountViewModel: AccountViewModel, | ||
) { | ||
val homeNewThreads = FeedContentState(HomeNewThreadFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val homeReplies = FeedContentState(HomeConversationsFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
|
||
val dmKnown = FeedContentState(ChatroomListKnownFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val dmNew = FeedContentState(ChatroomListNewFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
|
||
val videoFeed = FeedContentState(VideoFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
|
||
val discoverMarketplace = FeedContentState(DiscoverMarketplaceFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val discoverDVMs = FeedContentState(DiscoverNIP89FeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val discoverLive = FeedContentState(DiscoverLiveFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val discoverCommunities = FeedContentState(DiscoverCommunityFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
val discoverPublicChats = FeedContentState(DiscoverChatFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) | ||
} |
182 changes: 182 additions & 0 deletions
182
amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt
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,182 @@ | ||
/** | ||
* Copyright (c) 2024 Vitor Pamplona | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
* Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.vitorpamplona.amethyst.ui.feeds | ||
|
||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.mutableStateOf | ||
import com.vitorpamplona.amethyst.model.Note | ||
import com.vitorpamplona.amethyst.service.checkNotInMainThread | ||
import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter | ||
import com.vitorpamplona.amethyst.ui.dal.FeedFilter | ||
import com.vitorpamplona.amethyst.ui.screen.equalImmutableLists | ||
import com.vitorpamplona.ammolite.relays.BundledInsert | ||
import com.vitorpamplona.ammolite.relays.BundledUpdate | ||
import com.vitorpamplona.quartz.events.DeletionEvent | ||
import kotlinx.collections.immutable.ImmutableList | ||
import kotlinx.collections.immutable.toImmutableList | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
@Stable | ||
class FeedContentState( | ||
val localFilter: FeedFilter<Note>, | ||
val viewModelScope: CoroutineScope, | ||
) : InvalidatableContent { | ||
private val _feedContent = MutableStateFlow<FeedState>(FeedState.Loading) | ||
val feedContent = _feedContent.asStateFlow() | ||
|
||
// Simple counter that changes when it needs to invalidate everything | ||
private val _scrollToTop = MutableStateFlow<Int>(0) | ||
val scrollToTop = _scrollToTop.asStateFlow() | ||
var scrolltoTopPending = false | ||
|
||
private var lastFeedKey: String? = null | ||
|
||
fun sendToTop() { | ||
if (scrolltoTopPending) return | ||
|
||
scrolltoTopPending = true | ||
viewModelScope.launch(Dispatchers.IO) { _scrollToTop.emit(_scrollToTop.value + 1) } | ||
} | ||
|
||
suspend fun sentToTop() { | ||
scrolltoTopPending = false | ||
} | ||
|
||
private fun refresh() { | ||
viewModelScope.launch(Dispatchers.Default) { refreshSuspended() } | ||
} | ||
|
||
fun refreshSuspended() { | ||
checkNotInMainThread() | ||
|
||
lastFeedKey = localFilter.feedKey() | ||
val notes = localFilter.loadTop().distinctBy { it.idHex }.toImmutableList() | ||
|
||
val oldNotesState = _feedContent.value | ||
if (oldNotesState is FeedState.Loaded) { | ||
if (!equalImmutableLists(notes, oldNotesState.feed.value)) { | ||
updateFeed(notes) | ||
} | ||
} else { | ||
updateFeed(notes) | ||
} | ||
} | ||
|
||
private fun updateFeed(notes: ImmutableList<Note>) { | ||
viewModelScope.launch(Dispatchers.Main) { | ||
val currentState = _feedContent.value | ||
if (notes.isEmpty()) { | ||
_feedContent.update { FeedState.Empty } | ||
} else if (currentState is FeedState.Loaded) { | ||
// updates the current list | ||
if (currentState.showHidden.value != localFilter.showHiddenKey()) { | ||
currentState.showHidden.value = localFilter.showHiddenKey() | ||
} | ||
currentState.feed.value = notes | ||
} else { | ||
_feedContent.update { | ||
FeedState.Loaded(mutableStateOf(notes), mutableStateOf(localFilter.showHiddenKey())) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun refreshFromOldState(newItems: Set<Note>) { | ||
val oldNotesState = _feedContent.value | ||
if (localFilter is AdditiveFeedFilter && lastFeedKey == localFilter.feedKey()) { | ||
if (oldNotesState is FeedState.Loaded) { | ||
val deletionEvents: List<DeletionEvent> = | ||
newItems.mapNotNull { | ||
val noteEvent = it.event | ||
if (noteEvent is DeletionEvent) noteEvent else null | ||
} | ||
|
||
val oldList = | ||
if (deletionEvents.isEmpty()) { | ||
oldNotesState.feed.value | ||
} else { | ||
val deletedEventIds = deletionEvents.flatMapTo(HashSet()) { it.deleteEvents() } | ||
val deletedEventAddresses = deletionEvents.flatMapTo(HashSet()) { it.deleteAddresses() } | ||
oldNotesState.feed.value | ||
.filter { !it.wasOrShouldBeDeletedBy(deletedEventIds, deletedEventAddresses) } | ||
.toImmutableList() | ||
} | ||
|
||
val newList = | ||
localFilter | ||
.updateListWith(oldList, newItems) | ||
.distinctBy { it.idHex } | ||
.toImmutableList() | ||
if (!equalImmutableLists(newList, oldNotesState.feed.value)) { | ||
updateFeed(newList) | ||
} | ||
} else if (oldNotesState is FeedState.Empty) { | ||
val newList = | ||
localFilter | ||
.updateListWith(emptyList(), newItems) | ||
.distinctBy { it.idHex } | ||
.toImmutableList() | ||
if (newList.isNotEmpty()) { | ||
updateFeed(newList) | ||
} | ||
} else { | ||
// Refresh Everything | ||
refreshSuspended() | ||
} | ||
} else { | ||
// Refresh Everything | ||
refreshSuspended() | ||
} | ||
} | ||
|
||
private val bundler = BundledUpdate(250, Dispatchers.IO) | ||
private val bundlerInsert = BundledInsert<Set<Note>>(250, Dispatchers.IO) | ||
|
||
override fun invalidateData(ignoreIfDoing: Boolean) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
bundler.invalidate(ignoreIfDoing) { | ||
// adds the time to perform the refresh into this delay | ||
// holding off new updates in case of heavy refresh routines. | ||
refreshSuspended() | ||
} | ||
} | ||
} | ||
|
||
fun checkKeysInvalidateDataAndSendToTop() { | ||
if (lastFeedKey != localFilter.feedKey()) { | ||
bundler.invalidate(false) { | ||
// adds the time to perform the refresh into this delay | ||
// holding off new updates in case of heavy refresh routines. | ||
refreshSuspended() | ||
sendToTop() | ||
} | ||
} | ||
} | ||
|
||
fun invalidateInsertData(newItems: Set<Note>) { | ||
bundlerInsert.invalidateList(newItems) { refreshFromOldState(it.flatten().toSet()) } | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentStateView.kt
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,143 @@ | ||
/** | ||
* Copyright (c) 2024 Vitor Pamplona | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
* Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.vitorpamplona.amethyst.ui.feeds | ||
|
||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.lazy.LazyListState | ||
import androidx.compose.foundation.lazy.grid.LazyGridState | ||
import androidx.compose.foundation.lazy.grid.rememberLazyGridState | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled | ||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel | ||
|
||
@Composable | ||
fun RefresheableFeedContentStateView( | ||
feedContentState: FeedContentState, | ||
routeForLastRead: String?, | ||
enablePullRefresh: Boolean = true, | ||
scrollStateKey: String? = null, | ||
accountViewModel: AccountViewModel, | ||
nav: (String) -> Unit, | ||
) { | ||
RefresheableBox(feedContentState, enablePullRefresh) { | ||
SaveableFeedContentState(feedContentState, scrollStateKey) { listState -> | ||
RenderFeedContentState(feedContentState, accountViewModel, listState, nav, routeForLastRead) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun SaveableFeedContentState( | ||
feedContentState: FeedContentState, | ||
scrollStateKey: String? = null, | ||
content: @Composable (LazyListState) -> Unit, | ||
) { | ||
val listState = | ||
if (scrollStateKey != null) { | ||
rememberForeverLazyListState(scrollStateKey) | ||
} else { | ||
rememberLazyListState() | ||
} | ||
|
||
WatchScrollToTopFeedContentState(feedContentState, listState) | ||
|
||
content(listState) | ||
} | ||
|
||
@Composable | ||
fun SaveableGridFeedContentState( | ||
feedContentState: FeedContentState, | ||
scrollStateKey: String? = null, | ||
content: @Composable (LazyGridState) -> Unit, | ||
) { | ||
val gridState = | ||
if (scrollStateKey != null) { | ||
rememberForeverLazyGridState(scrollStateKey) | ||
} else { | ||
rememberLazyGridState() | ||
} | ||
|
||
WatchScrollToTopFeedContentState(feedContentState, gridState) | ||
|
||
content(gridState) | ||
} | ||
|
||
@Composable | ||
fun RenderFeedContentState( | ||
feedContentState: FeedContentState, | ||
accountViewModel: AccountViewModel, | ||
listState: LazyListState, | ||
nav: (String) -> Unit, | ||
routeForLastRead: String?, | ||
onLoaded: @Composable (FeedState.Loaded) -> Unit = { FeedLoaded(it, listState, routeForLastRead, accountViewModel, nav) }, | ||
onEmpty: @Composable () -> Unit = { FeedEmpty(feedContentState::invalidateData) }, | ||
onError: @Composable (String) -> Unit = { FeedError(it, feedContentState::invalidateData) }, | ||
onLoading: @Composable () -> Unit = { LoadingFeed() }, | ||
) { | ||
val feedState by feedContentState.feedContent.collectAsStateWithLifecycle() | ||
|
||
CrossfadeIfEnabled( | ||
targetState = feedState, | ||
animationSpec = tween(durationMillis = 100), | ||
accountViewModel = accountViewModel, | ||
) { state -> | ||
when (state) { | ||
is FeedState.Empty -> onEmpty() | ||
is FeedState.FeedError -> onError(state.errorMessage) | ||
is FeedState.Loaded -> onLoaded(state) | ||
is FeedState.Loading -> onLoading() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun WatchScrollToTopFeedContentState( | ||
feedContentState: FeedContentState, | ||
listState: LazyListState, | ||
) { | ||
val scrollToTop by feedContentState.scrollToTop.collectAsStateWithLifecycle() | ||
|
||
LaunchedEffect(scrollToTop) { | ||
if (scrollToTop > 0 && feedContentState.scrolltoTopPending) { | ||
listState.scrollToItem(index = 0) | ||
feedContentState.sentToTop() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun WatchScrollToTopFeedContentState( | ||
feedContentState: FeedContentState, | ||
listState: LazyGridState, | ||
) { | ||
val scrollToTop by feedContentState.scrollToTop.collectAsStateWithLifecycle() | ||
|
||
LaunchedEffect(scrollToTop) { | ||
if (scrollToTop > 0 && feedContentState.scrolltoTopPending) { | ||
listState.scrollToItem(index = 0) | ||
feedContentState.sentToTop() | ||
} | ||
} | ||
} |
Oops, something went wrong.