From 0d3c60b9c29f8c600c2f8443cc3ba555fbbd37ec Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 22 Aug 2025 16:11:30 -0400 Subject: [PATCH] Fixes the disappearance of drafts. --- .../loggedIn/AccountFeedContentStates.kt | 5 ++ .../screen/loggedIn/drafts/DraftListScreen.kt | 56 ++++--------------- .../drafts/dal/DraftEventsFeedViewModel.kt | 39 ------------- 3 files changed, 17 insertions(+), 83 deletions(-) delete mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/dal/DraftEventsFeedViewModel.kt diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountFeedContentStates.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountFeedContentStates.kt index 2d9bbd153..962a1009d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountFeedContentStates.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountFeedContentStates.kt @@ -35,6 +35,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.discover.nip53LiveActivitie import com.vitorpamplona.amethyst.ui.screen.loggedIn.discover.nip72Communities.DiscoverCommunityFeedFilter import com.vitorpamplona.amethyst.ui.screen.loggedIn.discover.nip90DVMs.DiscoverNIP89FeedFilter import com.vitorpamplona.amethyst.ui.screen.loggedIn.discover.nip99Classifieds.DiscoverMarketplaceFeedFilter +import com.vitorpamplona.amethyst.ui.screen.loggedIn.drafts.dal.DraftEventsFeedFilter import com.vitorpamplona.amethyst.ui.screen.loggedIn.home.dal.HomeConversationsFeedFilter import com.vitorpamplona.amethyst.ui.screen.loggedIn.home.dal.HomeLiveFilter import com.vitorpamplona.amethyst.ui.screen.loggedIn.home.dal.HomeNewThreadFeedFilter @@ -68,6 +69,8 @@ class AccountFeedContentStates( val feedListOptions = FollowListState(accountViewModel.account, accountViewModel.viewModelScope) + val drafts = FeedContentState(DraftEventsFeedFilter(accountViewModel.account), accountViewModel.viewModelScope) + suspend fun init() { notificationSummary.initializeSuspend() feedListOptions.initializeSuspend() @@ -97,6 +100,8 @@ class AccountFeedContentStates( notificationSummary.invalidateInsertData(newNotes) feedListOptions.updateFeedWith(newNotes) + + drafts.updateFeedWith(newNotes) } fun destroy() { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/DraftListScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/DraftListScreen.kt index 88bfc3763..95179f429 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/DraftListScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/DraftListScreen.kt @@ -38,32 +38,27 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.res.stringResource -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.compose.collectAsStateWithLifecycle -import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.ui.components.SwipeToDeleteContainer +import com.vitorpamplona.amethyst.ui.feeds.FeedContentState import com.vitorpamplona.amethyst.ui.feeds.FeedState import com.vitorpamplona.amethyst.ui.feeds.RefresheableBox +import com.vitorpamplona.amethyst.ui.feeds.RenderFeedContentState import com.vitorpamplona.amethyst.ui.feeds.ScrollStateKeys.DRAFTS +import com.vitorpamplona.amethyst.ui.feeds.WatchLifecycleAndUpdateModel import com.vitorpamplona.amethyst.ui.layouts.DisappearingScaffold import com.vitorpamplona.amethyst.ui.navigation.navs.INav import com.vitorpamplona.amethyst.ui.navigation.topbars.TopBarWithBackButton import com.vitorpamplona.amethyst.ui.note.NoteCompose -import com.vitorpamplona.amethyst.ui.screen.RenderFeedState import com.vitorpamplona.amethyst.ui.screen.SaveableFeedState import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel -import com.vitorpamplona.amethyst.ui.screen.loggedIn.drafts.dal.DraftEventsFeedViewModel import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.amethyst.ui.theme.DividerThickness import com.vitorpamplona.amethyst.ui.theme.FeedPadding @@ -74,42 +69,16 @@ fun DraftListScreen( accountViewModel: AccountViewModel, nav: INav, ) { - val draftFeedViewModel: DraftEventsFeedViewModel = - viewModel( - key = "NostrDraftEventsFeedViewModel", - factory = DraftEventsFeedViewModel.Factory(accountViewModel.account), - ) - - RenderDraftListScreen(draftFeedViewModel, accountViewModel, nav) + RenderDraftListScreen(accountViewModel.feedStates.drafts, accountViewModel, nav) } @Composable private fun RenderDraftListScreen( - feedViewModel: DraftEventsFeedViewModel, + feedState: FeedContentState, accountViewModel: AccountViewModel, nav: INav, ) { - val lifeCycleOwner = LocalLifecycleOwner.current - - LaunchedEffect(feedViewModel) { - feedViewModel.invalidateData() - } - - DisposableEffect(lifeCycleOwner) { - val observer = - LifecycleEventObserver { _, event -> - if (event == Lifecycle.Event.ON_RESUME) { - println("DraftList Start") - feedViewModel.invalidateData() - } - if (event == Lifecycle.Event.ON_PAUSE) { - println("DraftList Stop") - } - } - - lifeCycleOwner.lifecycle.addObserver(observer) - onDispose { lifeCycleOwner.lifecycle.removeObserver(observer) } - } + WatchLifecycleAndUpdateModel(feedState) DisappearingScaffold( isInvertedLayout = false, @@ -119,15 +88,15 @@ private fun RenderDraftListScreen( accountViewModel = accountViewModel, ) { Column(Modifier.padding(it).fillMaxHeight()) { - RefresheableBox(feedViewModel) { - SaveableFeedState(feedViewModel.feedState, DRAFTS) { listState -> - RenderFeedState( - viewModel = feedViewModel, + RefresheableBox(feedState) { + SaveableFeedState(feedState, DRAFTS) { listState -> + RenderFeedContentState( + feedContentState = feedState, accountViewModel = accountViewModel, listState = listState, nav = nav, routeForLastRead = null, - onLoaded = { DraftFeedLoaded(it, listState, null, accountViewModel, nav) }, + onLoaded = { DraftFeedLoaded(it, listState, accountViewModel, nav) }, ) } } @@ -140,7 +109,6 @@ private fun RenderDraftListScreen( private fun DraftFeedLoaded( loaded: FeedState.Loaded, listState: LazyListState, - routeForLastRead: String?, accountViewModel: AccountViewModel, nav: INav, ) { @@ -207,7 +175,7 @@ private fun DraftFeedLoaded( NoteCompose( item, modifier = MaterialTheme.colorScheme.maxWidthWithBackground, - routeForLastRead = routeForLastRead, + routeForLastRead = null, isBoostedNote = false, isHiddenFeed = items.showHidden, quotesLeft = 3, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/dal/DraftEventsFeedViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/dal/DraftEventsFeedViewModel.kt deleted file mode 100644 index a0c753bfb..000000000 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/drafts/dal/DraftEventsFeedViewModel.kt +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2025 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.screen.loggedIn.drafts.dal - -import androidx.compose.runtime.Stable -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.vitorpamplona.amethyst.model.Account -import com.vitorpamplona.amethyst.ui.screen.FeedViewModel - -@Stable -class DraftEventsFeedViewModel( - val account: Account, -) : FeedViewModel(DraftEventsFeedFilter(account)) { - class Factory( - val account: Account, - ) : ViewModelProvider.Factory { - @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T = DraftEventsFeedViewModel(account) as T - } -}