Skip to content

Commit

Permalink
WTA #71: Added setup for Project details page.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed May 13, 2023
1 parent 4e8540b commit 92813f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ package com.jacob.wakatimeapp.details.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.SnackbarDuration.Long
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.jacob.wakatimeapp.core.ui.components.WtaAnimation
import com.jacob.wakatimeapp.core.ui.theme.assets
import com.jacob.wakatimeapp.core.ui.theme.spacing
import com.jacob.wakatimeapp.details.ui.components.DetailsPageHeader
import com.ramcosta.composedestinations.annotation.Destination
import kotlinx.coroutines.launch

@Composable
@Destination(navArgsDelegate = DetailsPageNavArgs::class)
fun DetailsPage(
navigator: DetailsPageNavigator,
snackbarHostState: SnackbarHostState,
modifier: Modifier = Modifier,
) = DetailsPageScreen(
navigator = navigator,
modifier = modifier,
snackbarHostState = snackbarHostState,
viewModel = hiltViewModel(),
)

Expand All @@ -26,16 +42,60 @@ fun DetailsPage(
private fun DetailsPageScreen(
navigator: DetailsPageNavigator,
viewModel: DetailsPageViewModel,
snackbarHostState: SnackbarHostState,
modifier: Modifier = Modifier,
) {
val snackBarCoroutineScope = rememberCoroutineScope()
val viewState by viewModel.viewState.collectAsState()

LaunchedEffect(viewState) {
if (viewState !is DetailsPageViewState.Error) return@LaunchedEffect
val viewStateError = viewState as DetailsPageViewState.Error

snackBarCoroutineScope.launch {
snackbarHostState.showSnackbar(
message = viewStateError.error.message,
duration = Long,
)
}
}

DetailsPageContent(viewState = viewState, modifier = modifier)
}

@Composable
private fun DetailsPageContent(viewState: DetailsPageViewState, modifier: Modifier = Modifier) =
Column(
modifier = modifier,
modifier = modifier.statusBarsPadding(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
text = "Details Page",
style = MaterialTheme.typography.headlineLarge,
when (viewState) {
is DetailsPageViewState.Loading -> DetailsPageLoading()
is DetailsPageViewState.Loaded -> DetailsPageLoaded(viewState)
is DetailsPageViewState.Error -> DetailsPageError(viewState)
}
}

@Composable
private fun DetailsPageLoaded(viewState: DetailsPageViewState.Loaded) {
Column {
Spacer(modifier = Modifier.height(MaterialTheme.spacing.small))
DetailsPageHeader(
viewState.projectName,
modifier = Modifier.padding(horizontal = MaterialTheme.spacing.extraSmall),
)
}
}

@Composable
private fun DetailsPageError(errorMessage: DetailsPageViewState.Error) = WtaAnimation(
animation = MaterialTheme.assets.animations.randomErrorAnimation,
text = errorMessage.error.message,
)

@Composable
private fun DetailsPageLoading() = WtaAnimation(
animation = MaterialTheme.assets.animations.randomLoadingAnimation,
text = "Loading..",
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class DetailsPageViewModel @Inject constructor(
viewModelScope.launch {
val data = getProjectStatsUC(args.projectName)
Timber.w(data.toString())
_viewState.value = DetailsPageViewState.Loaded(args.projectName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.jacob.wakatimeapp.details.ui
sealed class DetailsPageViewState {
object Loading : DetailsPageViewState()

data class Loaded(val detUiDetailsPageViewState: DetailsPageViewState) : DetailsPageViewState()
data class Loaded(val projectName: String) : DetailsPageViewState()

data class Error(val error: com.jacob.wakatimeapp.core.models.Error) : DetailsPageViewState()
}

0 comments on commit 92813f5

Please sign in to comment.