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

Feature/255 선물 등록하기 #268

Merged
merged 17 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.nexters.boolti.data.network.request

import kotlinx.serialization.Serializable

@Serializable
data class GiftReceiveRequest(
val giftUuid: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class GiftResponse(
val id: String,
val userId: String,
HamBP marked this conversation as resolved.
Show resolved Hide resolved
val giftUuid: String,
val orderId: String,
val reservationId: String,
Expand All @@ -23,6 +24,7 @@ data class GiftResponse(
fun toDomain(): Gift {
return Gift(
id = id,
userId = userId,
uuid = giftUuid,
orderId = orderId,
reservationId = reservationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.nexters.boolti.domain.request.LoginRequest
import com.nexters.boolti.domain.request.SignUpRequest
import com.nexters.boolti.domain.request.SignoutRequest
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
Expand All @@ -34,6 +35,7 @@ internal class AuthRepositoryImpl @Inject constructor(
return authDataSource.login(request).onSuccess { response ->
tokenDataSource.saveTokens(response.accessToken ?: "", response.refreshToken ?: "")
deviceTokenDataSource.sendFcmToken()
getUserAndCache().first()
}.mapCatching(LoginResponse::toDomain)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.time.LocalDate

data class Gift(
val id: String,
val userId: String,
val uuid: String,
val orderId: String,
val reservationId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.nexters.boolti.presentation.component

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -27,14 +29,19 @@ import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.nexters.boolti.presentation.R
import com.nexters.boolti.presentation.theme.BooltiTheme
import com.nexters.boolti.presentation.theme.Grey05
import com.nexters.boolti.presentation.theme.Grey50
import com.nexters.boolti.presentation.theme.Grey80

@Composable
fun BTDialog(
modifier: Modifier = Modifier,
enableDismiss: Boolean = true,
showCloseButton: Boolean = true,
onDismiss: () -> Unit = {},
hasNegativeButton: Boolean = false,
HamBP marked this conversation as resolved.
Show resolved Hide resolved
negativeButtonLabel: String = stringResource(R.string.cancel),
onClickNegativeButton: () -> Unit = {},
positiveButtonLabel: String = stringResource(R.string.btn_ok),
positiveButtonEnabled: Boolean = true,
horizontalAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
Expand Down Expand Up @@ -79,12 +86,27 @@ fun BTDialog(
}
content()
Spacer(modifier = Modifier.size(28.dp))
MainButton(
modifier = Modifier.fillMaxWidth(),
label = positiveButtonLabel,
enabled = positiveButtonEnabled,
onClick = onClickPositiveButton,
)
Row(
horizontalArrangement = Arrangement.spacedBy(9.dp)
HamBP marked this conversation as resolved.
Show resolved Hide resolved
) {
if (hasNegativeButton) {
MainButton(
modifier = Modifier.weight(1f),
label = negativeButtonLabel,
onClick = onClickNegativeButton,
colors = MainButtonDefaults.buttonColors(
containerColor = Grey80,
contentColor = Grey05,
)
)
}
MainButton(
modifier = Modifier.weight(1f),
label = positiveButtonLabel,
enabled = positiveButtonEnabled,
onClick = onClickPositiveButton,
)
}
}
}
}
Expand All @@ -101,3 +123,17 @@ fun BTDialogPreview() {
}
}
}

@Preview
@Composable
fun BTDialogHavingNegativeButtonPreview() {
BooltiTheme {
Surface {
BTDialog(
hasNegativeButton = true
) {
Text(text = "관리자 코드로 입장 확인")
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.nexters.boolti.presentation.screen.home

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.nexters.boolti.presentation.R
import com.nexters.boolti.presentation.component.BTDialog
import com.nexters.boolti.presentation.theme.Grey15
import com.nexters.boolti.presentation.theme.Grey50

@Composable
fun GiftDialog(
status: GiftStatus,
onDismiss: () -> Unit,
receiveGift: () -> Unit,
requireLogin: () -> Unit,
onFailed: () -> Unit,
) {
val buttonTextRes = when (status) {
GiftStatus.SELF, GiftStatus.CAN_REGISTER -> R.string.gift_register
GiftStatus.NEED_LOGIN -> R.string.gift_login
GiftStatus.FAILED -> R.string.description_close_button
}

val textRes = when (status) {
GiftStatus.SELF -> R.string.gift_self_dialog
GiftStatus.NEED_LOGIN -> R.string.gift_need_login
GiftStatus.CAN_REGISTER -> R.string.gift_register_dialog
GiftStatus.FAILED -> R.string.gift_registration_failed
}

val action: () -> Unit = when (status) {
GiftStatus.SELF -> {
{
receiveGift()
onDismiss()
}
}

GiftStatus.NEED_LOGIN -> requireLogin
HamBP marked this conversation as resolved.
Show resolved Hide resolved
GiftStatus.CAN_REGISTER -> {
{
receiveGift()
onDismiss()
}
}

GiftStatus.FAILED -> onDismiss
}

val hasNegativeButton = status in listOf(GiftStatus.SELF, GiftStatus.CAN_REGISTER)

BTDialog(
onDismiss = onDismiss,
onClickPositiveButton = action,
positiveButtonLabel = stringResource(id = buttonTextRes),
hasNegativeButton = hasNegativeButton,
onClickNegativeButton = onFailed
) {
Text(
text = stringResource(id = textRes),
style = MaterialTheme.typography.titleLarge.copy(
color = Grey15,
textAlign = TextAlign.Center
),
)
if (status == GiftStatus.FAILED) {
Text(
modifier = Modifier.padding(top = 4.dp),
text = stringResource(id = R.string.gift_registration_failed_dialog),
style = MaterialTheme.typography.bodySmall.copy(
color = Grey50,
textAlign = TextAlign.Center
),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nexters.boolti.presentation.screen.home

enum class GiftStatus {
NEED_LOGIN,
FAILED,
SELF,
CAN_REGISTER,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nexters.boolti.presentation.screen.home

sealed interface HomeEvent {
data class DeepLinkEvent(
val deepLink: String,
) : HomeEvent

data class ShowMessage(
val status: GiftStatus
) : HomeEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -32,7 +36,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navDeepLink
import com.nexters.boolti.presentation.R
import com.nexters.boolti.presentation.screen.HomeViewModel
import com.nexters.boolti.presentation.extension.requireActivity
import com.nexters.boolti.presentation.screen.my.MyScreen
import com.nexters.boolti.presentation.screen.show.ShowScreen
import com.nexters.boolti.presentation.screen.ticket.TicketLoginScreen
Expand All @@ -58,13 +62,38 @@ fun HomeScreen(
val currentDestination = navBackStackEntry?.destination?.route ?: Destination.Show.route

val loggedIn by viewModel.loggedIn.collectAsStateWithLifecycle()
val context = LocalContext.current

var dialog: GiftStatus? by rememberSaveable { mutableStateOf(null) }
HamBP marked this conversation as resolved.
Show resolved Hide resolved

LaunchedEffect(Unit) {
viewModel.events.collect { event ->
when (event) {
is HomeEvent.DeepLinkEvent -> navController.navigate(Uri.parse(event.deepLink))
is HomeEvent.ShowMessage -> {
dialog = event.status
}
}
}
}

LaunchedEffect(Unit) {
viewModel.event.collect { deepLink ->
navController.navigate(Uri.parse(deepLink))
val intent = context.requireActivity().intent
intent.action?.let { _ ->
val deepLink = intent.data.toString()
intent.data = null
val regex = "^https://app.boolti.in/gift/([\\w-])+$".toRegex()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후 스키마 형태가 바뀌거나 쿼리 파람이 추가되면 처리를 못하거나 giftUuid 를 잘못 꺼내올 수 있어서 방어로직을 추가하거나 확실히 인지하고 있는 게 좋을 듯

안 그러면 문제가 발생했을 때 원인을 찾기 힘들 수도 있을 거 같다는 생각이 드네

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흐음... 방어로직 추천 받습니다
(어떻게 짜야할 지 모르겠...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun main() {
    val regex = Regex("^https://app\\.boolti\\.in/gift/([^?]+)")
    val url1 = "https://app.boolti.in/gift/abc123"
    val url2 = "https://app.boolti.in/gift/abc123?name=\"booti\"&age=18"

    val match1 = regex.find(url1)
    val match2 = regex.find(url2)

    match1?.let {
        println(it.groupValues[1]) // Output: abc123
    }

    match2?.let {
        println(it.groupValues[1]) // Output: abc123
    }
}

지피티한테 위임했어

if (regex.matches(deepLink)) {
val giftUuid = deepLink.split("/").last()
viewModel.processGift(giftUuid)
}
}
}

LaunchedEffect(loggedIn) {
if (loggedIn == true) viewModel.processGift()
}

Scaffold(
bottomBar = {
HomeNavigationBar(
Expand All @@ -86,6 +115,12 @@ fun HomeScreen(
) {
composable(
route = Destination.Show.route,
deepLinks = listOf(
navDeepLink {
uriPattern = "https://app.boolti.in/home/shows"
action = Intent.ACTION_VIEW
}
)
) {
ShowScreen(
modifier = modifier.padding(innerPadding),
Expand Down Expand Up @@ -130,6 +165,25 @@ fun HomeScreen(
}
}
}

if (dialog != null) {
GiftDialog(
status = dialog!!,
onDismiss = {
dialog = null
viewModel.cancelGift()
},
receiveGift = viewModel::receiveGift,
requireLogin = {
dialog = null
requireLogin()
},
onFailed = {
dialog = GiftStatus.FAILED
viewModel.cancelGift()
}
)
}
}

@Stable
Expand Down
Loading
Loading