Make adjustments to UI code following the ViewModel method signatures.

This commit is contained in:
KotlinGeekDev
2025-05-30 20:26:42 +01:00
parent 69ae42d344
commit 63177b15a1
2 changed files with 61 additions and 40 deletions
@@ -61,7 +61,6 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.navigation.INav import com.vitorpamplona.amethyst.ui.navigation.INav
import com.vitorpamplona.amethyst.ui.navigation.TopBarWithBackButton import com.vitorpamplona.amethyst.ui.navigation.TopBarWithBackButton
import com.vitorpamplona.amethyst.ui.screen.NostrUserListFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.DisappearingScaffold import com.vitorpamplona.amethyst.ui.screen.loggedIn.DisappearingScaffold
import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.followsets.FollowSetScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.followsets.FollowSetScreen
@@ -69,6 +68,8 @@ import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.amethyst.ui.theme.TabRowHeight import com.vitorpamplona.amethyst.ui.theme.TabRowHeight
import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonColumn import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonColumn
import com.vitorpamplona.quartz.nip51Lists.PeopleListEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -77,10 +78,10 @@ fun ListsScreen(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: INav, nav: INav,
) { ) {
val followSetsViewModel: NostrUserListFeedViewModel = val followSetsViewModel: NostrListFeedViewModel =
viewModel( viewModel(
key = "NostrUserListFeedViewModel", key = "NostrUserListFeedViewModel",
factory = NostrUserListFeedViewModel.Factory(accountViewModel.account), factory = NostrListFeedViewModel.Factory(accountViewModel.account),
) )
val currentCoroutineScope = rememberCoroutineScope() val currentCoroutineScope = rememberCoroutineScope()
@@ -107,7 +108,7 @@ fun ListsScreen(
// TODO: Replace this with nav-based solution. // TODO: Replace this with nav-based solution.
val isFollowSetSelected = remember { mutableStateOf(false) } val isFollowSetSelected = remember { mutableStateOf(false) }
val selectedFollowListWithIndex = remember { mutableStateOf<Pair<Int, FollowSet>?>(null) } val selectedFollowList = remember { mutableStateOf<FollowSet?>(null) }
CustomListsScreen( CustomListsScreen(
followSets, followSets,
@@ -119,50 +120,67 @@ fun ListsScreen(
setName = title, setName = title,
setDescription = description, setDescription = description,
setType = listType, setType = listType,
account = accountViewModel.account,
) )
}, },
openItem = { openItem = {
val selectedFollowSet = followSetsViewModel.findFollowSet(identifier = it) // TODO: Same as the next TODO below, as they are related.
if (selectedFollowSet != null) { currentCoroutineScope.launch(Dispatchers.IO) {
selectedFollowListWithIndex.value = selectedFollowSet val selectedFollowSetNote =
followSetsViewModel.getFollowSetNote(
noteIdentifier = it,
account = accountViewModel.account,
)
if (selectedFollowSetNote != null) {
val event = selectedFollowSetNote.event as PeopleListEvent
println("Found list, with title: ${event.nameOrTitle()}")
val selectedFollowSet =
FollowSet.mapEventToSet(
event,
accountViewModel.account.signer,
)
selectedFollowList.value = selectedFollowSet
isFollowSetSelected.value = true isFollowSetSelected.value = true
} else { } else {
println("This list has not been found. Could it not have been added/deleted properly?") println("No corresponding note found for this list.")
}
} }
// currentCoroutineScope.launch(Dispatchers.IO) {
// val note = followSetsViewModel.getFollowSetAddressable(it, accountViewModel.account)
// if (note != null) {
// val event = note.event as PeopleListEvent
// println("Found list, with title: ${event.nameOrTitle()}")
// } else {
// println("No corresponding note found for this list.")
// }
// }
}, },
renameItem = { index, newValue -> renameItem = { followSet, newValue ->
followSetsViewModel.renameFollowSet(newName = newValue, setIndex = index) followSetsViewModel.renameFollowSet(
newName = newValue,
followSet = followSet,
account = accountViewModel.account,
)
}, },
deleteItem = { deleteItem = { identifier ->
followSetsViewModel.deleteFollowSet(it) followSetsViewModel.deleteFollowSet(
listIdentifier = identifier,
account = accountViewModel.account,
)
}, },
accountViewModel, accountViewModel,
nav, nav,
) )
// TODO: Replace this with nav-based solution. // TODO: Replace this with nav-based solution.
if (isFollowSetSelected.value && selectedFollowListWithIndex.value != null) { if (isFollowSetSelected.value && selectedFollowList.value != null) {
val leFollowSetIndex = selectedFollowListWithIndex.value!!.first val leFollowSet = selectedFollowList.value
FollowSetScreen( FollowSetScreen(
onClose = { onClose = {
isFollowSetSelected.value = false isFollowSetSelected.value = false
selectedFollowListWithIndex.value = null selectedFollowList.value = null
}, },
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
navigator = nav, navigator = nav,
selectedSet = selectedFollowListWithIndex.value!!.second, selectedSet = leFollowSet!!,
onProfileRemove = { onProfileRemove = {
val newSet = followSetsViewModel.removeUserFromSet(it, leFollowSetIndex) followSetsViewModel.removeUserFromSet(
selectedFollowListWithIndex.value = Pair(leFollowSetIndex, newSet) it,
leFollowSet.identifierTag,
accountViewModel.account,
)
}, },
onListSave = { onListSave = {
accountViewModel.toastManager.toast("List Changes", "Changes already saved.") accountViewModel.toastManager.toast("List Changes", "Changes already saved.")
@@ -172,7 +190,10 @@ fun ListsScreen(
}, },
onListDelete = { onListDelete = {
isFollowSetSelected.value = false isFollowSetSelected.value = false
followSetsViewModel.deleteFollowSet(leFollowSetIndex) followSetsViewModel.deleteFollowSet(
leFollowSet.identifierTag,
accountViewModel.account,
)
}, },
) )
} }
@@ -184,8 +205,8 @@ fun CustomListsScreen(
refresh: () -> Unit, refresh: () -> Unit,
addItem: (title: String, description: String?, listType: ListVisibility) -> Unit, addItem: (title: String, description: String?, listType: ListVisibility) -> Unit,
openItem: (identifier: String) -> Unit, openItem: (identifier: String) -> Unit,
renameItem: (Int, String) -> Unit, renameItem: (targetSet: FollowSet, newName: String) -> Unit,
deleteItem: (Int) -> Unit, deleteItem: (setIdentifier: String) -> Unit,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: INav, nav: INav,
) { ) {
@@ -47,8 +47,8 @@ fun FollowSetFeedView(
followSetState: FollowSetState, followSetState: FollowSetState,
onRefresh: () -> Unit = {}, onRefresh: () -> Unit = {},
onOpenItem: (String) -> Unit = {}, onOpenItem: (String) -> Unit = {},
onRenameItem: (Int, String) -> Unit, onRenameItem: (targetSet: FollowSet, newName: String) -> Unit,
onDeleteItem: (Int) -> Unit, onDeleteItem: (setIdentifier: String) -> Unit,
) { ) {
when (followSetState) { when (followSetState) {
FollowSetState.Loading -> LoadingFeed() FollowSetState.Loading -> LoadingFeed()
@@ -88,9 +88,9 @@ fun FollowListLoaded(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
loadedFeedState: List<FollowSet>, loadedFeedState: List<FollowSet>,
onRefresh: () -> Unit = {}, onRefresh: () -> Unit = {},
onItemClick: (String) -> Unit = {}, onItemClick: (itemIdentifier: String) -> Unit = {},
onItemRename: (index: Int, newName: String) -> Unit, onItemRename: (followSet: FollowSet, newName: String) -> Unit,
onItemDelete: (itemIndex: Int) -> Unit, onItemDelete: (itemIdentifier: String) -> Unit,
) { ) {
Log.d("FollowSetComposable", "FollowListLoaded: Follow Set size: ${loadedFeedState.size}") Log.d("FollowSetComposable", "FollowListLoaded: Follow Set size: ${loadedFeedState.size}")
@@ -102,7 +102,7 @@ fun FollowListLoaded(
state = listState, state = listState,
contentPadding = FeedPadding, contentPadding = FeedPadding,
) { ) {
itemsIndexed(loadedFeedState, key = { _, item -> item.identifierTag }) { index, set -> itemsIndexed(loadedFeedState, key = { _, item -> item.identifierTag }) { _, set ->
CustomListItem( CustomListItem(
modifier = Modifier.animateItem(), modifier = Modifier.animateItem(),
followSet = set, followSet = set,
@@ -110,10 +110,10 @@ fun FollowListLoaded(
onItemClick(set.identifierTag) onItemClick(set.identifierTag)
}, },
onFollowSetRename = { onFollowSetRename = {
onItemRename(index, it) onItemRename(set, it)
}, },
onFollowSetDelete = { onFollowSetDelete = {
onItemDelete(index) onItemDelete(set.identifierTag)
}, },
) )
Spacer(modifier = StdVertSpacer) Spacer(modifier = StdVertSpacer)