-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FeedScreenState to separate UI related logic, state and UI
- Loading branch information
Showing
5 changed files
with
137 additions
and
62 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
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
85 changes: 85 additions & 0 deletions
85
uicomponent-compose/feed/src/main/java/io/github/droidkaigi/feeder/feed/FeedScreenState.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,85 @@ | ||
package io.github.droidkaigi.feeder.feed | ||
|
||
import android.content.Context | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.material.BackdropScaffoldState | ||
import androidx.compose.material.BackdropValue | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.SnackbarResult | ||
import androidx.compose.material.rememberBackdropScaffoldState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
import io.github.droidkaigi.feeder.FeedItem | ||
import io.github.droidkaigi.feeder.Filters | ||
import io.github.droidkaigi.feeder.core.dispatcher | ||
import io.github.droidkaigi.feeder.core.getReadableMessage | ||
import io.github.droidkaigi.feeder.core.state | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
class FeedScreenState( | ||
private val feedViewModel: FeedViewModel, | ||
private val fmPlayerViewModel: FmPlayerViewModel, | ||
val scaffoldState: BackdropScaffoldState, | ||
val context: Context, | ||
) { | ||
val uiState: FeedViewModel.State @Composable get() = feedViewModel.state() | ||
private val dispatcher: (FeedViewModel.Event) -> Unit get() = feedViewModel.dispatcher() | ||
val effect: Flow<FeedViewModel.Effect> get() = feedViewModel.effect | ||
|
||
val fmPlayerUiState: FmPlayerViewModel.State @Composable get() = fmPlayerViewModel.state() | ||
private val fmPlayerDispatcher: (FmPlayerViewModel.Event) -> Unit | ||
get() = fmPlayerViewModel.dispatcher() | ||
|
||
val tabLazyListStates | ||
@Composable get() = FeedTab.values() | ||
.map { it to rememberLazyListState() } | ||
.toMap() | ||
|
||
|
||
suspend fun onErrorMessage(effect: FeedViewModel.Effect.ErrorMessage) { | ||
when ( | ||
scaffoldState.snackbarHostState.showSnackbar( | ||
message = effect.appError.getReadableMessage(context), | ||
actionLabel = "Reload", | ||
) | ||
) { | ||
SnackbarResult.ActionPerformed -> { | ||
dispatcher(FeedViewModel.Event.ReloadContent) | ||
} | ||
SnackbarResult.Dismissed -> { | ||
} | ||
} | ||
} | ||
|
||
fun onFavoriteChange(feedItem: FeedItem) { | ||
dispatcher(FeedViewModel.Event.ToggleFavorite(feedItem = feedItem)) | ||
} | ||
|
||
fun onFavoriteFilterChange(currentFilters: Filters, isFavoriteFiltered: Boolean) { | ||
dispatcher( | ||
FeedViewModel.Event.ChangeFavoriteFilter( | ||
filters = currentFilters.copy(filterFavorite = isFavoriteFiltered) | ||
|
||
) | ||
) | ||
} | ||
|
||
fun onPotcastPlayButtonClick(podcast: FeedItem.Podcast) { | ||
fmPlayerDispatcher(FmPlayerViewModel.Event.ChangePlayerState(podcast.podcastLink)) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun rememberFeedScreenState( | ||
feedViewModel: FeedViewModel = feedViewModel(), | ||
fmPlayerViewModel: FmPlayerViewModel = fmPlayerViewModel(), | ||
scaffoldState: BackdropScaffoldState = rememberBackdropScaffoldState(BackdropValue.Concealed), | ||
context: Context = LocalContext.current, | ||
): FeedScreenState { | ||
return remember { | ||
FeedScreenState(feedViewModel, fmPlayerViewModel, scaffoldState, context) | ||
} | ||
} |
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
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