Refactor FollowSetsManagementDialog UI to correctly adapt to follow set state. Small adjustment in the NostrUserListFeedViewModel.

This commit is contained in:
KotlinGeekDev
2025-09-22 15:41:17 +01:00
parent 9f5301d30a
commit 1e18edfc02
2 changed files with 131 additions and 72 deletions

View File

@@ -57,7 +57,7 @@ class NostrUserListFeedViewModel(
private val _feedContent = MutableStateFlow<FollowSetState>(FollowSetState.Loading)
val feedContent = _feedContent.asStateFlow()
private fun refresh() {
fun refresh() {
viewModelScope.launch(Dispatchers.IO) {
refreshSuspended()
}
@@ -105,7 +105,7 @@ class NostrUserListFeedViewModel(
}
} catch (e: Exception) {
Log.e(
"NostrUserListFeedViewModel",
this.javaClass.simpleName,
"refreshSuspended: Error loading or refreshing feed -> ${e.message}",
)
_feedContent.update { FollowSetState.FeedError(e.message.toString()) }

View File

@@ -29,6 +29,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
@@ -43,6 +44,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.HorizontalDivider
@@ -80,6 +82,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.FollowSetState
import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.ListVisibility
import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.NewListCreationDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.NostrUserListFeedViewModel
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.ButtonBorder
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
@@ -95,10 +98,6 @@ fun FollowSetsManagementDialog(
val followSetsState by followSetsViewModel.feedContent.collectAsState()
val userInfo by remember { derivedStateOf { LocalCache.getOrCreateUser(userHex) } }
when (followSetsState) {
is FollowSetState.Loaded -> {
val lists = (followSetsState as FollowSetState.Loaded).feed
Scaffold(
modifier =
Modifier
@@ -155,6 +154,10 @@ fun FollowSetsManagementDialog(
).consumeWindowInsets(contentPadding)
.imePadding(),
) {
when (followSetsState) {
is FollowSetState.Loaded -> {
val lists = (followSetsState as FollowSetState.Loaded).feed
lists.forEachIndexed { index, list ->
Spacer(StdVertSpacer)
FollowSetItem(
@@ -191,6 +194,23 @@ fun FollowSetsManagementDialog(
},
)
}
}
FollowSetState.Empty -> {
EmptyOrNoneFound { followSetsViewModel.refresh() }
}
is FollowSetState.FeedError -> {
val errorMsg = (followSetsState as FollowSetState.FeedError).errorMessage
ErrorMessage(errorMsg) { followSetsViewModel.refresh() }
}
FollowSetState.Loading -> {
Loading()
}
}
if (followSetsState != FollowSetState.Loading) {
FollowSetsCreationMenu(
userName = userInfo.toBestDisplayName(),
onSetCreate = { setName, setIsPrivate, description ->
@@ -206,7 +226,46 @@ fun FollowSetsManagementDialog(
}
}
}
else -> {}
}
@Composable
private fun Loading() {
Column(
Modifier.fillMaxWidth().fillMaxHeight(0.5f),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
CircularProgressIndicator()
Text(stringRes(R.string.loading_feed))
}
}
@Composable
private fun EmptyOrNoneFound(onRefresh: () -> Unit) {
Column(
Modifier.fillMaxWidth().fillMaxHeight(0.5f),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text("No follow sets were found, or you don't have any follow sets. Tap below to refresh, or use the menu to create one.")
Spacer(modifier = StdVertSpacer)
OutlinedButton(onClick = onRefresh) { Text(text = stringRes(R.string.refresh)) }
}
}
@Composable
private fun ErrorMessage(
errorMsg: String,
onRefresh: () -> Unit,
) {
Column(
Modifier.fillMaxWidth().fillMaxHeight(0.5f),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text("There was a problem while fetching: $errorMsg")
Spacer(modifier = StdVertSpacer)
OutlinedButton(onClick = onRefresh) { Text(text = stringRes(R.string.refresh)) }
}
}