Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parameter objects from composable list #17

Merged
merged 3 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ fun LoginPage(loginPageNavigator: LoginPageNavigator) =
@Composable
@Destination
fun HomePage(homePageNavigator: HomePageNavigator) =
HomePageContent(homePageNavigator = homePageNavigator)
HomePageContent(navigator = homePageNavigator)

This file was deleted.

98 changes: 56 additions & 42 deletions core/ui/src/main/java/com/jacob/wakatimeapp/core/ui/StatsCards.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jacob.wakatimeapp.core.ui

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand All @@ -22,50 +23,59 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.ui.theme.Gradient
import com.jacob.wakatimeapp.core.ui.theme.Gradients
import com.jacob.wakatimeapp.core.ui.theme.WakaTimeAppTheme

@Composable
fun StatsCard(
parameters: StatsCardParameters,
private fun StatsCard(
gradient: Gradient,
roundedCornerPercent: Int = 25,
@DrawableRes iconId: Int,
mainText: String,
text: String,
weights: Pair<Float, Float> = Pair(1f, 1f),
iconOffset: Int = 50,
iconSize: Int = 80,
onClick: () -> Unit,
) {
val cardGradient =
Brush.horizontalGradient(listOf(parameters.gradient.startColor,
parameters.gradient.endColor))
val cardShape = RoundedCornerShape(parameters.roundedCornerPercent)
Brush.horizontalGradient(listOf(gradient.startColor,
gradient.endColor))
val cardShape = RoundedCornerShape(roundedCornerPercent)
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.shadow(elevation = 10.dp, shape = cardShape)
.clickable { parameters.onClick() }
.clickable { onClick() }
.fillMaxWidth()
.background(cardGradient, cardShape)
.padding(horizontal = 22.dp)
) {
Image(
painter = painterResource(id = parameters.iconId),
painter = painterResource(id = iconId),
contentDescription = "",
contentScale = ContentScale.FillBounds,
modifier = Modifier
.padding(start = parameters.iconOffset.dp)
.size(parameters.iconSize.dp)
.padding(start = iconOffset.dp)
.size(iconSize.dp)
)
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = parameters.mainText,
text = mainText,
maxLines = 2,
modifier = Modifier.weight(parameters.weights.first, true),
modifier = Modifier.weight(weights.first, true),
style = TextStyle(
fontSize = 18.sp,
color = Color.White,
fontWeight = FontWeight.SemiBold
),
)
Text(
text = parameters.text,
modifier = Modifier.weight(parameters.weights.second, true),
text = text,
modifier = Modifier.weight(weights.second, true),
textAlign = TextAlign.End,
style = TextStyle(
color = Color.White,
Expand All @@ -79,16 +89,19 @@ fun StatsCard(

@Composable
fun TimeSpentCard(
parameters: TimeSpentCardParameters,
gradient: Gradient,
roundedCornerPercent: Int,
@DrawableRes iconId: Int,
mainText: String,
time: Time,
onClick: () -> Unit,
) = StatsCard(
parameters = StatsCardParameters(
gradient = parameters.gradient,
roundedCornerPercent = parameters.roundedCornerPercent,
iconId = parameters.iconId,
mainText = parameters.mainText,
text = "${parameters.time.hours}H, ${parameters.time.minutes}M",
onClick = parameters.onClick
),
gradient = gradient,
roundedCornerPercent = roundedCornerPercent,
iconId = iconId,
mainText = mainText,
text = "${time.hours}H, ${time.minutes}M",
onClick = onClick,
)

@Composable
Expand All @@ -97,32 +110,33 @@ fun TimeSpentChip() {

@Composable
fun OtherStatsCard(
parameters: OtherStatsCardParameters,
gradient: Gradient,
roundedCornerPercent: Int,
@DrawableRes iconId: Int,
mainText: String,
language: String,
onClick: () -> Unit,
) = StatsCard(
parameters = StatsCardParameters(
gradient = parameters.gradient,
roundedCornerPercent = parameters.roundedCornerPercent,
iconId = parameters.iconId,
mainText = parameters.mainText,
text = parameters.language,
weights = Pair(1f, 0.5f),
iconOffset = 90,
iconSize = 70,
onClick = parameters.onClick
),
gradient = gradient,
roundedCornerPercent = roundedCornerPercent,
iconId = iconId,
mainText = mainText,
text = language,
weights = Pair(1f, 0.5f),
iconOffset = 90,
iconSize = 70,
onClick = onClick,
)

@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Composable
fun TimeSpentCardPreview() = WakaTimeAppTheme(darkTheme = true) {
TimeSpentCard(
parameters = TimeSpentCardParameters(
Gradients.primary,
25,
R.drawable.ic_time,
"Total Time Spent Today",
Time(42, 22, 0f),
onClick = {}
)
gradient = Gradients.primary,
roundedCornerPercent = 25,
iconId = R.drawable.ic_time,
mainText = "Total Time Spent Today",
time = Time(42, 22, 0f),
onClick = {}
)
}
78 changes: 35 additions & 43 deletions home/src/main/java/com/jacob/wakatimeapp/home/ui/HomePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,42 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Scaffold
import androidx.compose.material.SnackbarDuration.Long
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.jacob.wakatimeapp.core.common.observeInLifecycle
import com.jacob.wakatimeapp.core.ui.R.drawable
import com.jacob.wakatimeapp.core.ui.TimeSpentCard
import com.jacob.wakatimeapp.core.ui.TimeSpentCardParameters
import com.jacob.wakatimeapp.core.ui.theme.Gradients
import com.jacob.wakatimeapp.home.ui.components.*
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import timber.log.Timber

interface HomePageNavigator {
fun toDetailsPage()
}

@Composable
fun HomePageContent(
viewModel: HomePageViewModel = hiltViewModel(),
homePageNavigator: HomePageNavigator,
navigator: HomePageNavigator,
) {
Timber.e("Loaded")
val scaffoldState = rememberScaffoldState()
val snackBarCoroutineScope = rememberCoroutineScope()
val viewState by viewModel.homePageState.collectAsState()

viewModel.errors
.onEach {
snackBarCoroutineScope.launch {
scaffoldState.snackbarHostState.showSnackbar(
message = it.errorMessage,
actionLabel = "Action",
duration = Long
)
LaunchedEffect(Unit) {
viewModel.errors
.collectLatest {
snackBarCoroutineScope.launch {
scaffoldState.snackbarHostState.showSnackbar(
message = it.errorMessage,
actionLabel = "Action",
duration = Long
)
}
}
}
.observeInLifecycle(LocalLifecycleOwner.current)
}

Scaffold(
scaffoldState = scaffoldState,
Expand All @@ -53,22 +50,19 @@ fun HomePageContent(
when (viewState) {
is HomePageViewState.Loading -> HomePageLoading()
is HomePageViewState.Loaded -> HomePageLoaded(
HomePageLoadedParameters(
homePageViewState = viewState as HomePageViewState.Loaded,
navigator = homePageNavigator
),
homePageNavigator = homePageNavigator,
homePageViewState = viewState as HomePageViewState.Loaded,
navigator = navigator,
)

is HomePageViewState.Error -> HomePageError(HomePageErrorParameters(viewState as HomePageViewState.Error))
is HomePageViewState.Error -> HomePageError(viewState as HomePageViewState.Error)
}
}
}

@Composable
private fun HomePageLoaded(
parameters: HomePageLoadedParameters,
homePageNavigator: HomePageNavigator,
homePageViewState: HomePageViewState.Loaded,
navigator: HomePageNavigator,
) {
val scrollState = rememberScrollState()
Column(
Expand All @@ -77,31 +71,29 @@ private fun HomePageLoaded(
.padding(top = 30.dp)
.verticalScroll(scrollState)
) {
UserDetailsSection(UserDetailsSectionParameters(parameters.homePageViewState.userDetails))
UserDetailsSection(homePageViewState.userDetails)
Spacer(modifier = Modifier.height(25.dp))

TimeSpentCard(
parameters = TimeSpentCardParameters(
gradient = Gradients.primary,
roundedCornerPercent = 25,
iconId = drawable.ic_time,
mainText = "Total Time Spent Today",
time = parameters.homePageViewState.contentData.todaysStats.timeSpent,
onClick = homePageNavigator::toDetailsPage
)
gradient = Gradients.primary,
roundedCornerPercent = 25,
iconId = drawable.ic_time,
mainText = "Total Time Spent Today",
time = homePageViewState.contentData.todaysStats.timeSpent,
onClick = navigator::toDetailsPage,
)
Spacer(modifier = Modifier.height(25.dp))

RecentProjects(RecentProjectsParameters(parameters.homePageViewState.contentData.todaysStats))
RecentProjects(homePageViewState.contentData.todaysStats)
Spacer(modifier = Modifier.height(10.dp))

WeeklyReport(WeeklyReportParameters(parameters.homePageViewState.contentData.dailyStats))
WeeklyReport(homePageViewState.contentData.dailyStats)
Spacer(modifier = Modifier.height(25.dp))

OtherDailyStatsSection(OtherDailyStatsSectionParameters(
parameters.homePageViewState.contentData.todaysStats,
OtherDailyStatsSection(
homePageViewState.contentData.todaysStats,
onClick = {}
))
)
Spacer(modifier = Modifier.height(25.dp))
}
}

This file was deleted.

Loading