-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
215 additions
and
39 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
4 changes: 4 additions & 0 deletions
4
app/src/main/java/com/maruchin/domaindrivenandroid/data/ID.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,4 @@ | ||
package com.maruchin.domaindrivenandroid.data | ||
|
||
@JvmInline | ||
value class ID(val value: String) |
13 changes: 7 additions & 6 deletions
13
app/src/main/java/com/maruchin/domaindrivenandroid/data/coupon/Coupon.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
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
29 changes: 29 additions & 0 deletions
29
...c/main/java/com/maruchin/domaindrivenandroid/ui/couponPreview/CouponPreviewDestination.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,29 @@ | ||
package com.maruchin.domaindrivenandroid.ui.couponPreview | ||
|
||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.compose.composable | ||
import com.maruchin.domaindrivenandroid.data.ID | ||
|
||
const val COUPON_PREVIEW_ROUTE = "coupon-preview" | ||
const val COUPON_IN = "couponId" | ||
|
||
fun NavGraphBuilder.couponPreviewScreen(onBack: () -> Unit) { | ||
composable("$COUPON_PREVIEW_ROUTE/{$COUPON_IN}") { | ||
val couponId = ID(it.arguments?.getString(COUPON_IN) ?: "") | ||
val viewModel = hiltViewModel<CouponPreviewViewModel>() | ||
val state by viewModel.uiState.collectAsState() | ||
LaunchedEffect(Unit) { | ||
viewModel.selectCoupon(couponId) | ||
} | ||
CouponPreviewScreen(state = state, onBack = onBack, onCollect = {}) | ||
} | ||
} | ||
|
||
fun NavController.navigateToCouponPreview(couponId: ID) { | ||
navigate("$COUPON_PREVIEW_ROUTE/${couponId.value}") | ||
} |
80 changes: 80 additions & 0 deletions
80
app/src/main/java/com/maruchin/domaindrivenandroid/ui/couponPreview/CouponPreviewScreen.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,80 @@ | ||
package com.maruchin.domaindrivenandroid.ui.couponPreview | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.ArrowBack | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.OutlinedCard | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import coil.compose.AsyncImage | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun CouponPreviewScreen(state: CouponPreviewUiState, onBack: () -> Unit, onCollect: () -> Unit) { | ||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { Text(text = "My Coupons") }, | ||
navigationIcon = { | ||
IconButton(onClick = onBack) { | ||
Icon( | ||
imageVector = Icons.Outlined.ArrowBack, | ||
contentDescription = "Navigate up", | ||
) | ||
} | ||
}, | ||
) | ||
} | ||
) { paddingValues -> | ||
when (state) { | ||
CouponPreviewUiState.Loading -> {} | ||
is CouponPreviewUiState.Ready -> { | ||
Column(modifier = Modifier.padding(paddingValues)) { | ||
OutlinedCard(modifier = Modifier.padding(12.dp)) { | ||
AsyncImage( | ||
model = state.imageUrl, | ||
contentDescription = null, | ||
modifier = Modifier.fillMaxWidth(), | ||
contentScale = ContentScale.FillWidth, | ||
) | ||
} | ||
Text( | ||
text = state.couponName, | ||
style = MaterialTheme.typography.displaySmall, | ||
modifier = Modifier.padding(vertical = 12.dp, horizontal = 20.dp) | ||
) | ||
Text( | ||
text = state.price, | ||
style = MaterialTheme.typography.headlineMedium, | ||
fontWeight = FontWeight.SemiBold, | ||
color = MaterialTheme.colorScheme.primary, | ||
modifier = Modifier.padding(vertical = 12.dp, horizontal = 20.dp), | ||
) | ||
Spacer(modifier = Modifier.weight(1f)) | ||
Button( | ||
onClick = onCollect, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 16.dp, horizontal = 12.dp), | ||
) { | ||
Text(text = "Collect".uppercase()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/maruchin/domaindrivenandroid/ui/couponPreview/CouponPreviewUiState.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,23 @@ | ||
package com.maruchin.domaindrivenandroid.ui.couponPreview | ||
|
||
import androidx.compose.runtime.Immutable | ||
import com.maruchin.domaindrivenandroid.data.coupon.Coupon | ||
|
||
@Immutable | ||
sealed class CouponPreviewUiState { | ||
|
||
object Loading : CouponPreviewUiState() | ||
|
||
class Ready( | ||
val imageUrl: String, | ||
val couponName: String, | ||
val price: String, | ||
) : CouponPreviewUiState() { | ||
|
||
constructor(coupon: Coupon) : this( | ||
imageUrl = coupon.image.toString(), | ||
couponName = coupon.name, | ||
price = coupon.price.toString(), | ||
) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...src/main/java/com/maruchin/domaindrivenandroid/ui/couponPreview/CouponPreviewViewModel.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,29 @@ | ||
package com.maruchin.domaindrivenandroid.ui.couponPreview | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.maruchin.domaindrivenandroid.data.ID | ||
import com.maruchin.domaindrivenandroid.data.coupon.CouponsRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class CouponPreviewViewModel @Inject constructor( | ||
private val couponsRepository: CouponsRepository, | ||
) : ViewModel() { | ||
|
||
private val _uiState = MutableStateFlow<CouponPreviewUiState>(CouponPreviewUiState.Loading) | ||
val uiState = _uiState.asStateFlow() | ||
|
||
fun selectCoupon(couponId: ID) = viewModelScope.launch { | ||
couponsRepository.getCoupon(couponId)?.let { coupon -> | ||
_uiState.update { | ||
CouponPreviewUiState.Ready(coupon) | ||
} | ||
} | ||
} | ||
} |
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
19 changes: 18 additions & 1 deletion
19
app/src/main/java/com/maruchin/domaindrivenandroid/ui/home/HomeUiState.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 |
---|---|---|
@@ -1,10 +1,27 @@ | ||
package com.maruchin.domaindrivenandroid.ui.home | ||
|
||
import androidx.compose.runtime.Immutable | ||
import com.maruchin.domaindrivenandroid.data.ID | ||
import com.maruchin.domaindrivenandroid.data.coupon.Coupon | ||
|
||
@Immutable | ||
data class HomeUiState( | ||
val coupons: List<Coupon> = emptyList(), | ||
val coupons: List<CouponUiState> = emptyList(), | ||
val loading: Boolean = true, | ||
) | ||
|
||
@Immutable | ||
data class CouponUiState( | ||
val id: ID, | ||
val imageUrl: String, | ||
val couponName: String, | ||
val price: String, | ||
) { | ||
|
||
constructor(coupon: Coupon) : this( | ||
id = coupon.id, | ||
imageUrl = coupon.image.toString(), | ||
couponName = coupon.name, | ||
price = coupon.price.toString(), | ||
) | ||
} |
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