728x90
타이머 앱을 만들어 보았다.
생각보다 타이머에는 많은 조건이 포함되어 있음을 알게 되었다.
간단할줄 알았는데...
□ 작성한 코드
package com.jeong.mytimer
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.*
import androidx.compose.material.SnackbarDuration
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.SnackbarResult
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jeong.mytimer.ui.theme.MyTimerTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyTimerTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
TimerScreen()
}
}
}
}
}
@Composable
fun TimerScreen(){
//데이터
//타이머 카운트
var timerCount by remember { mutableStateOf(0) }
// 활성화 여부
var isActive by remember { mutableStateOf(false)}
val snackbarHostState = remember { SnackbarHostState()}
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(Unit){
while (true){
delay(1000L) //1초
if (isActive && timerCount > 0) timerCount --
if (isActive && timerCount == 0) isActive = false
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally
){
Text(timerCount.toString(),
fontSize = 120.sp,
modifier = Modifier.padding(vertical = 100.dp)
)
// 타이머 토글 버튼
Button(onClick = {
//TODO :: 타이머 토글
if (snackbarHostState.currentSnackbarData != null) return@Button
if (timerCount == 0){
coroutineScope.launch{
snackbarHostState
.showSnackbar(
"시간을 먼저 설정해주세요!",
actionLabel = "닫기", SnackbarDuration.Short
).let {
when(it){
SnackbarResult.Dismissed -> Log.d("Tag","스낵바 닫힘")
SnackbarResult.ActionPerformed -> Log.d("Tag","스낵바 닫힘 버튼 클릭")
}
}
return@launch
}
return@Button
}
isActive = !isActive
}) {
Text(if (isActive) "종료" else "시작",
fontSize = 30.sp,
modifier = Modifier.padding(10.dp))
}
AnimatedVisibility(visible = !isActive) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
){
Text("타이머 설정",
fontSize = 30.sp,
modifier = Modifier.padding(10.dp))
Row(){
Button(
modifier = Modifier.size(100.dp),
onClick = {
//TODO :: 시간 증가
timerCount +=1
}) {
Text("+",
fontSize = 30.sp,
modifier = Modifier.padding(10.dp))
}
Spacer(modifier = Modifier.width(30.dp))
Button(
modifier = Modifier.size(100.dp),
onClick = {
//TODO :: 시간 감소
if (timerCount > 0) timerCount -=1
}) {
Text("-",
fontSize = 30.sp,
modifier = Modifier.padding(10.dp))
}
}
}
}
Spacer(modifier = Modifier.weight(1f))
SnackbarHost(hostState = snackbarHostState)
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MyTimerTheme {
Greeting("Android")
}
}
728x90
'[여러가지 시도] > 코틀린 결과물' 카테고리의 다른 글
[결과물_앱] 챗봇앱 (0) | 2022.11.12 |
---|---|
[결과물_앱] 계산기 (0) | 2022.10.31 |
[결과물_앱] 로또번호 생성기 (0) | 2022.10.28 |