Skip to content

Commit

Permalink
Fixing some error state things and layout issues (#497)
Browse files Browse the repository at this point in the history
- Fixing an issue where cached data flashes when clicking retry
- Fixing an issue where you can't get to settings without an internet
connection
- Fixing an issue where you can't logout without an internet connection
- Fixing some padding issues with settings columns

---------

Co-authored-by: John Oberhauser <j.git-global@obez.io>
  • Loading branch information
JohnOberhauser and John Oberhauser committed Apr 26, 2024
1 parent 524eca0 commit bb79e40
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 115 deletions.
24 changes: 5 additions & 19 deletions core/common/src/main/java/social/firefly/common/Resource.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package social.firefly.common

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import social.firefly.common.Resource.Error
import social.firefly.common.Resource.Loaded
Expand Down Expand Up @@ -36,9 +34,9 @@ sealed class Resource<T> {
}

fun <T> MutableStateFlow<Resource<T>>.updateData(block: T.() -> T) {
with(value as? Resource.Loaded ?: return) {
with(value as? Loaded ?: return) {
update {
Resource.Loaded(
Loaded(
data = data.block(),
)
}
Expand All @@ -47,23 +45,11 @@ fun <T> MutableStateFlow<Resource<T>>.updateData(block: T.() -> T) {

fun <T> loadResource(block: suspend () -> T) =
flow {
this.emit(Resource.Loading())
emit(Loading())
try {
this.emit(Resource.Loaded(block()))
emit(Loaded(block()))
} catch (e: Exception) {
Timber.e(e)
this.emit(Resource.Error(e))
emit(Error(e))
}
}

fun <I, O> Resource<I>.mapData(transform: (I) -> O): Resource<O> {
return when (this) {
is Resource.Error -> Resource.Error(exception)
is Resource.Loaded -> Resource.Loaded(transform(data))
is Resource.Loading -> Resource.Loading()
}
}

fun <I, O> Flow<Resource<I>>.mapData(transform: (I) -> O): Flow<Resource<O>> {
return this.map { it.mapData(transform = transform) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import social.firefly.core.designsystem.theme.FfTheme
@Composable
fun FfCircularProgressIndicator(
modifier: Modifier = Modifier,
color: Color = FfTheme.colors.actionPrimary,
color: Color = FfTheme.colors.borderAccent,
strokeWidth: Dp = ProgressIndicatorDefaults.CircularStrokeWidth,
trackColor: Color = ProgressIndicatorDefaults.circularTrackColor,
strokeCap: StrokeCap = ProgressIndicatorDefaults.CircularIndeterminateStrokeCap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -29,11 +33,14 @@ fun <A : Any> PagingLazyColumn(
lazyPagingItems: LazyPagingItems<A>,
modifier: Modifier = Modifier,
noResultText: String = stringResource(id = R.string.theres_nothing_here),
showLoadingSpinnerOnRefresh: Boolean = true,
listState: LazyListState = rememberLazyListState(),
emptyListState: LazyListState = rememberLazyListState(),
headerContent: LazyListScope.() -> Unit = {},
content: LazyListScope.() -> Unit,
) {
var isRetryingAfterError by remember { mutableStateOf(false) }

Box(
modifier = modifier
.fillMaxSize(),
Expand All @@ -50,16 +57,19 @@ fun <A : Any> PagingLazyColumn(
when (lazyPagingItems.loadState.refresh) {
is LoadState.Error -> {} // handle the error outside the lazy column
is LoadState.Loading -> {
content()
item {
DelayedVisibility {
FfCircularProgressIndicator(
modifier =
Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
.padding(16.dp),
)
if (!isRetryingAfterError) {
content()
}
if (showLoadingSpinnerOnRefresh) {
item {
DelayedVisibility {
FfCircularProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
.padding(16.dp),
)
}
}
}
}
Expand Down Expand Up @@ -87,8 +97,7 @@ fun <A : Any> PagingLazyColumn(
is LoadState.Loading -> {
item {
FfCircularProgressIndicator(
modifier =
Modifier
modifier = Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
.padding(16.dp),
Expand All @@ -113,8 +122,13 @@ fun <A : Any> PagingLazyColumn(
modifier = Modifier
.fillMaxSize()
.background(FfTheme.colors.layer1),
onRetryClicked = { lazyPagingItems.refresh() },
onRetryClicked = {
isRetryingAfterError = true
lazyPagingItems.refresh()
},
)
} else if (lazyPagingItems.loadState.refresh is LoadState.NotLoading) {
isRetryingAfterError = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fun <A : Any> PullRefreshLazyColumn(
refreshing = refreshing,
state = state
)

},
listState: LazyListState = rememberLazyListState(),
emptyListState: LazyListState = rememberLazyListState(),
Expand All @@ -43,6 +42,7 @@ fun <A : Any> PullRefreshLazyColumn(
PagingLazyColumn(
lazyPagingItems = lazyPagingItems,
listState = listState,
showLoadingSpinnerOnRefresh = false,
emptyListState = emptyListState,
headerContent = headerContent,
content = content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ import social.firefly.core.model.AccountTimelineType

interface AccountInteractions : OverflowInteractions {
fun onFollowersClicked() = Unit

fun onFollowingClicked() = Unit

fun onFollowClicked() = Unit

fun onUnfollowClicked() = Unit

fun onRetryClicked() = Unit

fun onTabClicked(timelineType: AccountTimelineType) = Unit

fun onSettingsClicked() = Unit

fun onScreenViewed() = Unit

fun onEditAccountClicked() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ internal fun AccountScreen(
postCardInteractions = viewModel.postCardDelegate,
accountInteractions = viewModel,
windowInsets = windowInsets,
navigateToSettings = viewModel::onSettingsClicked,
)

LaunchedEffect(Unit) {
Expand All @@ -117,7 +116,6 @@ internal fun AccountScreen(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun AccountScreen(
navigateToSettings: () -> Unit,
uiState: Resource<AccountUiState>,
closeButtonVisible: Boolean,
isUsersProfile: Boolean,
Expand All @@ -129,16 +127,22 @@ private fun AccountScreen(
) {
FfSurface {
Column(
modifier =
Modifier
modifier = Modifier
.windowInsetsPadding(windowInsets),
) {
when (uiState) {
is Resource.Loading -> {
FfCloseableTopAppBar(showCloseButton = closeButtonVisible)
FfCloseableTopAppBar(
showCloseButton = closeButtonVisible,
actions = {
SettingsTopBarActions(
accountInteractions = accountInteractions,
isUsersProfile = isUsersProfile
)
},
)
Box(
modifier =
Modifier
modifier = Modifier
.fillMaxSize()
.wrapContentSize(
align = Alignment.Center,
Expand All @@ -153,17 +157,10 @@ private fun AccountScreen(
title = uiState.data.displayName,
showCloseButton = closeButtonVisible,
actions = {
if (isUsersProfile) {
IconButton(
modifier = Modifier.width(IntrinsicSize.Max),
onClick = navigateToSettings,
) {
Icon(
painter = FfIcons.gear(),
contentDescription = "Settings"
)
}
}
SettingsTopBarActions(
accountInteractions = accountInteractions,
isUsersProfile = isUsersProfile
)
},
showDivider = false,
)
Expand All @@ -179,10 +176,17 @@ private fun AccountScreen(
}

is Resource.Error -> {
FfCloseableTopAppBar(showCloseButton = closeButtonVisible)
FfCloseableTopAppBar(
showCloseButton = closeButtonVisible,
actions = {
SettingsTopBarActions(
accountInteractions = accountInteractions,
isUsersProfile = isUsersProfile
)
},
)
Box(
modifier =
Modifier
modifier = Modifier
.fillMaxSize()
.wrapContentSize(
align = Alignment.Center,
Expand All @@ -200,6 +204,24 @@ private fun AccountScreen(
}
}

@Composable
private fun SettingsTopBarActions(
accountInteractions: AccountInteractions,
isUsersProfile: Boolean,
) {
if (isUsersProfile) {
IconButton(
modifier = Modifier.width(IntrinsicSize.Max),
onClick = accountInteractions::onSettingsClicked,
) {
Icon(
painter = FfIcons.gear(),
contentDescription = "Settings"
)
}
}
}

@Composable
private fun MainContent(
account: AccountUiState,
Expand All @@ -210,8 +232,7 @@ private fun MainContent(
accountInteractions: AccountInteractions,
) {
Column(
modifier =
Modifier
modifier = Modifier
.fillMaxSize(),
) {
val postsFeed = timeline.postsFeed.collectAsLazyPagingItems()
Expand Down Expand Up @@ -706,7 +727,32 @@ fun AccountScreenPreview() {
postCardInteractions = PostCardInteractionsNoOp,
accountInteractions = object : AccountInteractions {},
windowInsets = WindowInsets.systemBars,
navigateToSettings = {},
)
}
}

@Suppress("MagicNumber")
@Preview
@Composable
fun AccountScreenErrorPreview() {
PreviewTheme(modules = listOf(navigationModule)) {
AccountScreen(
uiState =
Resource.Error(
exception = Exception("")
),
closeButtonVisible = true,
isUsersProfile = false,
timeline = Timeline(
type = AccountTimelineType.POSTS,
postsFeed = flowOf(),
postsAndRepliesFeed = flowOf(),
mediaFeed = flowOf(),
),
htmlContentInteractions = object : HtmlContentInteractions {},
postCardInteractions = PostCardInteractionsNoOp,
accountInteractions = object : AccountInteractions {},
windowInsets = WindowInsets.systemBars,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -18,6 +19,7 @@ import androidx.compose.ui.tooling.preview.Preview
import org.koin.androidx.compose.koinViewModel
import social.firefly.common.Version
import social.firefly.core.designsystem.icon.FfIcons
import social.firefly.core.designsystem.theme.FfSpacing
import social.firefly.core.navigation.navigationModule
import social.firefly.core.ui.common.FfSurface
import social.firefly.core.ui.common.utils.PreviewTheme
Expand All @@ -44,6 +46,8 @@ fun SettingsScreen(
.systemBarsPadding()
) {
SettingsColumn(
modifier = Modifier
.padding(horizontal = FfSpacing.md),
title = stringResource(id = R.string.settings_title)
) {
SettingsSection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private fun LoadedScreen(
Column(
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(FfSpacing.lg)
.padding(FfSpacing.md)
) {
AsyncImage(
modifier =
Expand Down

0 comments on commit bb79e40

Please sign in to comment.