generated from ajou4095/template-android
-
Notifications
You must be signed in to change notification settings - Fork 2
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
3 changed files
with
130 additions
and
114 deletions.
There are no files selected for viewing
222 changes: 112 additions & 110 deletions
222
...ation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/common/view/DialogScreen.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,156 +1,158 @@ | ||
package ac.dnd.bookkeeping.android.presentation.common.view | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import ac.dnd.bookkeeping.android.presentation.R | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.layout.wrapContentSize | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.ButtonDefaults | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.draw.shadow | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.compose.ui.window.DialogProperties | ||
|
||
@Composable | ||
fun DialogScreen( | ||
dialogIsShowingState: MutableState<Boolean>, | ||
width: Dp = 300.dp, | ||
content: @Composable () -> Unit = { SampleDialogComponent(dialogIsShowingState) } | ||
isShowing: Boolean, | ||
isCancelable: Boolean = true, | ||
title: String = "", | ||
message: String = "", | ||
confirmMessage: String = stringResource(id = R.string.dialog_confirm), | ||
cancelMessage: String = stringResource(id = R.string.dialog_cancel), | ||
onConfirm: () -> Unit = {}, | ||
onCancel: (() -> Unit)? = null, | ||
onDismissRequest: (() -> Unit), | ||
) { | ||
val animateIn = remember { mutableStateOf(false) } | ||
|
||
if (dialogIsShowingState.value) { | ||
if (isShowing) { | ||
Dialog( | ||
properties = DialogProperties( | ||
dismissOnBackPress = isCancelable, | ||
dismissOnClickOutside = isCancelable | ||
), | ||
onDismissRequest = { | ||
dialogIsShowingState.value = false | ||
onDismissRequest() | ||
} | ||
) { | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
Card( | ||
modifier = Modifier.background( | ||
color = Color.White, | ||
shape = RoundedCornerShape(10.dp) | ||
), | ||
) { | ||
LaunchedEffect(Unit) { animateIn.value = true } | ||
AnimatedVisibility( | ||
visible = animateIn.value && dialogIsShowingState.value, | ||
enter = fadeIn(), | ||
exit = fadeOut() | ||
Column( | ||
modifier = Modifier | ||
.wrapContentSize() | ||
.padding(top = 25.dp, start = 20.dp, end = 20.dp, bottom = 20.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Box( | ||
modifier = Modifier | ||
.shadow( | ||
elevation = 8.dp, | ||
shape = RoundedCornerShape(10.dp) | ||
) | ||
.width(width) | ||
.clip(RoundedCornerShape(16.dp)) | ||
.background( | ||
color = Color.LightGray | ||
Text( | ||
text = title, | ||
fontSize = 16.sp, | ||
color = Color.Black | ||
) | ||
|
||
if (message.isNotEmpty()) { | ||
Spacer(modifier = Modifier.height(15.dp)) | ||
|
||
Text( | ||
text = message, | ||
fontSize = 12.sp, | ||
color = Color.Black | ||
) | ||
} | ||
|
||
Spacer(modifier = Modifier.height(20.dp)) | ||
|
||
Row(modifier = Modifier.wrapContentSize()) { | ||
if (onCancel != null) { | ||
TextButton( | ||
modifier = Modifier | ||
.weight(1f), | ||
shape = RoundedCornerShape(4.dp), | ||
colors = ButtonDefaults.textButtonColors( | ||
backgroundColor = Color.Gray | ||
), | ||
onClick = { | ||
onCancel() | ||
onDismissRequest() | ||
}, | ||
) { | ||
Text(text = cancelMessage) | ||
} | ||
|
||
Spacer(modifier = Modifier.width(10.dp)) | ||
} | ||
|
||
TextButton( | ||
modifier = Modifier | ||
.weight(1f), | ||
shape = RoundedCornerShape(4.dp), | ||
colors = ButtonDefaults.textButtonColors( | ||
backgroundColor = Color.Gray | ||
), | ||
contentAlignment = Alignment.Center | ||
) { | ||
content() | ||
onClick = { | ||
onConfirm() | ||
onDismissRequest() | ||
} | ||
) { | ||
Text( | ||
text = confirmMessage, | ||
textAlign = TextAlign.Center | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun SampleDialogComponent( | ||
dialogState: MutableState<Boolean> | ||
) { | ||
Surface( | ||
modifier = Modifier.background( | ||
color = Color.White, | ||
shape = RoundedCornerShape(8.dp) | ||
), | ||
) { | ||
Column( | ||
modifier = Modifier | ||
.wrapContentSize() | ||
.padding(24.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text( | ||
"Sample Dialog Component", | ||
fontSize = 16.sp, | ||
color = Color.Black | ||
) | ||
Spacer(modifier = Modifier.height(50.dp)) | ||
Row(modifier = Modifier.wrapContentSize()) { | ||
Box( | ||
modifier = Modifier | ||
.background( | ||
color = Color.Gray, | ||
shape = RoundedCornerShape(4.dp) | ||
) | ||
.weight(1f) | ||
.clickable { | ||
dialogState.value = false | ||
}, | ||
contentAlignment = Alignment.Center | ||
) { | ||
Text( | ||
"취소", | ||
color = Color.White, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.padding( | ||
vertical = 10.5.dp, | ||
horizontal = 12.dp | ||
), | ||
) | ||
} | ||
fun DialogScreenPreview1() { | ||
var isShowing by remember { mutableStateOf(true) } | ||
|
||
Spacer(modifier = Modifier.width(8.dp)) | ||
DialogScreen( | ||
isShowing = isShowing, | ||
title = "제목", | ||
message = "내용", | ||
onCancel = {}, | ||
onDismissRequest = { | ||
isShowing = false | ||
} | ||
) | ||
} | ||
|
||
Box( | ||
modifier = Modifier | ||
.background( | ||
color = Color.Gray, | ||
shape = RoundedCornerShape(4.dp) | ||
) | ||
.weight(1f) | ||
.clickable { | ||
dialogState.value = false | ||
}, | ||
contentAlignment = Alignment.Center | ||
) { | ||
Text( | ||
"확인", | ||
color = Color.White, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.padding( | ||
vertical = 10.5.dp, | ||
horizontal = 12.dp | ||
) | ||
) | ||
} | ||
} | ||
@Preview | ||
@Composable | ||
fun DialogScreenPreview2() { | ||
var isShowing by remember { mutableStateOf(true) } | ||
|
||
DialogScreen( | ||
isShowing = isShowing, | ||
title = "제목", | ||
onDismissRequest = { | ||
isShowing = false | ||
} | ||
} | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="bottom_sample">홈</string> | ||
<!-- <screen_name>_<anything> 의 양식으로 작성 부탁드립니다. --> | ||
|
||
<string name="dialog_confirm">확인</string> | ||
<string name="dialog_cancel">취소</string> | ||
|
||
<string name="setting_dialog_title">테스트</string> | ||
|
||
</resources> |