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
@Composable | |
private fun CategoryDetailsCollapsingToolbar(category: FoodItem?) { | |
Row { | |
Image(modifier = Modifier.size(128.dp), ...) | |
Column { | |
Text(text = category?.name) | |
Text( | |
text = category.description, | |
maxLines = 6 | |
) |
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
@Composable | |
fun FoodCategoryDetailsScreen(state: FoodCategoryDetailsContract.State) { | |
Column { | |
CategoryDetailsCollapsingToolbar(state.category) | |
Spacer(modifier = Modifier.height(2.dp)) | |
LazyColumn { | |
items(state.categoryFoodItems) { item -> | |
FoodItemRow(item = item) | |
} | |
} |
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
@Composable | |
private fun FoodApp() { | |
val viewModel: FoodCategoriesViewModel = viewModel() | |
val state = viewModel.viewState.value | |
FoodCategoriesScreen( | |
state = state, | |
effectFlow = viewModel.effect, | |
onEventSent = { event -> viewModel.setEvent(event) }, | |
onNavigationRequested = { navigationEffect -> | |
if (navigationEffect is FoodCategoriesContract.Effect.Navigation.ToCategoryDetails) { |
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
@Composable | |
fun FoodCategoriesScreen( | |
state: FoodCategoriesContract.State, | |
effectFlow: Flow<FoodCategoriesContract.Effect>?, | |
onEventSent: (event: FoodCategoriesContract.Event) -> Unit, | |
onNavigationRequested: (navigationEffect: FoodCategoriesContract.Effect.Navigation) -> Unit | |
) { | |
// Listen for side effects from the VM | |
LaunchedEffect(LAUNCH_LISTEN_FOR_EFFECTS) { | |
effectFlow?.onEach { effect -> |
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
class FoodCategoriesViewModel constructor(private val repository: FoodMenuRepository) : | |
BaseViewModel< | |
FoodCategoriesContract.Event, | |
FoodCategoriesContract.State, | |
FoodCategoriesContract.Effect>() { | |
init { | |
viewModelScope.launch { getFoodCategories() } | |
} |
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
class FoodCategoriesContract { | |
sealed class Event : ViewEvent { | |
data class CategorySelection(val categoryName: String) : Event() | |
} | |
data class State(val categories: List<FoodItem> = listOf(), val isLoading: Boolean = false) : ViewState | |
sealed class Effect : ViewSideEffect { | |
object ToastDataWasLoaded : Effect() |
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
abstract class BaseViewModel | |
<Event : ViewEvent, | |
UiState : ViewState, | |
Effect : ViewSideEffect> : ViewModel() { | |
private val initialState: UiState by lazy { setInitialState() } | |
abstract fun setInitialState(): UiState | |
private val _viewState: MutableState<UiState> = mutableStateOf(initialState) | |
val viewState: State<UiState> = _viewState |
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
interface ViewState | |
interface ViewEvent | |
interface ViewSideEffect |
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
import android.os.Bundle | |
import androidx.activity.compose.setContent | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.shape.CircleShape | |
import androidx.compose.foundation.shape.RoundedCornerShape |
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
import android.os.Bundle | |
import androidx.activity.compose.setContent | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.shape.CircleShape | |
import androidx.compose.foundation.shape.RoundedCornerShape |