diff --git a/app/build.gradle b/app/build.gradle index 75677b5..a571c7a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,12 +5,12 @@ plugins { android { namespace 'com.lahsuak.apps.jetpackcomposebasic' - compileSdk 33 + compileSdk 34 defaultConfig { applicationId "com.lahsuak.apps.jetpackcomposebasic" minSdk 23 - targetSdk 33 + targetSdk 34 versionCode 1 versionName "1.0" @@ -27,17 +27,17 @@ android { } } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { - jvmTarget = '1.8' + jvmTarget = '17' } buildFeatures { compose true } composeOptions { - kotlinCompilerExtensionVersion '1.1.1' + kotlinCompilerExtensionVersion '1.4.3' } packagingOptions { resources { @@ -48,15 +48,16 @@ android { dependencies { - implementation 'androidx.core:core-ktx:1.9.0' - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' - implementation 'androidx.activity:activity-compose:1.6.1' + implementation 'androidx.core:core-ktx:1.10.1' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' + implementation 'androidx.activity:activity-compose:1.7.2' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" - implementation 'androidx.compose.material3:material3:1.1.0-alpha03' + implementation 'androidx.compose.material3:material3:1.2.0-alpha03' + implementation 'androidx.compose.material:material:1.4.3' testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.4' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" diff --git a/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt index 187ee32..a7ee581 100644 --- a/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt +++ b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt @@ -3,14 +3,21 @@ package com.lahsuak.apps.jetpackcomposebasic import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.size import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import com.lahsuak.apps.jetpackcomposebasic.ui.screen.Timer import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme +import com.lahsuak.apps.jetpackcomposebasic.ui.theme.LightGreen2 class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -22,29 +29,34 @@ class MainActivity : ComponentActivity() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - Greeting("Android") + Box(contentAlignment = Alignment.Center) { + Timer( + 100 * 1000L, + LightGreen2, + Color.Green, + Color.Gray, + modifier = Modifier.size(200.dp) + ) + } } } } } } -/*** -Composable functions : -A composable function is a regular function annotated with @Composable. -This enables your function to call other @Composable functions within it. -You can see how the Greeting function is marked as @Composable. -This function will produce a piece of UI hierarchy displaying the given input, -String. Text is a composable function provided by the library. -***/ -@Composable -fun Greeting(name: String) { - Text(text = "Hello $name!") -} + @Preview(showBackground = true) @Composable fun DefaultPreview() { JetPackComposeBasicTheme { - Greeting("Android") + Box(contentAlignment = Alignment.Center) { + Timer( + 100 * 1000L, + LightGreen2, + Color.Green, + Color.Gray, + modifier = Modifier.size(200.dp) + ) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/screen/Timer.kt b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/screen/Timer.kt new file mode 100644 index 0000000..dfa32fc --- /dev/null +++ b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/screen/Timer.kt @@ -0,0 +1,143 @@ +package com.lahsuak.apps.jetpackcomposebasic.ui.screen + +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.layout.Box +import androidx.compose.material.Button +import androidx.compose.material.ButtonDefaults +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.PointMode +import androidx.compose.ui.graphics.StrokeCap +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.IntSize +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import kotlinx.coroutines.delay +import kotlin.math.PI +import kotlin.math.cos +import kotlin.math.sin + +@Composable +fun Timer( + totalTime: Long, + handleColor: Color, + activeBarColor: Color, + inactiveBarColor: Color, + modifier: Modifier, + initialValue: Float = 1f, + strokeWidth: Dp = 6.dp, +) { + var size by remember { + mutableStateOf(IntSize.Zero) + } + + var value by remember { + mutableFloatStateOf(initialValue) + } + + var currentTime by rememberSaveable { + mutableLongStateOf(totalTime) + } + + var isTimerRunning by rememberSaveable { + mutableStateOf(false) + } + + LaunchedEffect(key1 = currentTime, key2 = isTimerRunning) { + if (currentTime > 0 && isTimerRunning) { + delay(100L) + currentTime -= 100L + value = currentTime / totalTime.toFloat() + } + } + + Box(contentAlignment = Alignment.Center, + modifier = modifier.onSizeChanged { + size = it + } + ) { + Canvas(modifier = modifier) { + drawArc( + color = inactiveBarColor, + startAngle = -215f, + sweepAngle = 250f, + useCenter = false, + size = Size(size.width.toFloat(), size.height.toFloat()), + style = Stroke(strokeWidth.toPx(), cap = StrokeCap.Round) + ) + drawArc( + color = activeBarColor, + startAngle = -215f, + sweepAngle = 250f * value, + useCenter = false, + size = Size(size.width.toFloat(), size.height.toFloat()), + style = Stroke(strokeWidth.toPx(), cap = StrokeCap.Round) + ) + + val center = Offset(size.width.toFloat() / 2f, size.height.toFloat() / 2f) + val angle = (250f * value + 145f) * (PI / 180f).toFloat() + val r = size.width / 2f + val a = cos(angle) * r + val b = sin(angle) * r + + drawPoints( + listOf(Offset(center.x + a, center.y + b)), + pointMode = PointMode.Points, + color = handleColor, + strokeWidth = (strokeWidth * 3f).toPx(), + cap = StrokeCap.Round + ) + } + Text( + text = (currentTime / 1000L).toString(), + fontSize = 44.sp, + fontWeight = FontWeight.Bold + ) + + Button( + onClick = { + if (currentTime <= 0L) { + currentTime = totalTime + isTimerRunning = true + } else { + isTimerRunning = !isTimerRunning + } + }, + modifier = Modifier.align(Alignment.BottomCenter), + colors = ButtonDefaults.buttonColors( + backgroundColor = if (!isTimerRunning || currentTime <= 0L) { + Color.Green + } else { + Color.Red + } + ) + ) { + Text( + text = if (isTimerRunning && currentTime >= 0L) { + "Stop" + } else if (!isTimerRunning && currentTime >= 0L) + "Start" + else { + "Restart" + }, + color = Color.Black + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/theme/Color.kt b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/theme/Color.kt index e0a4930..6a917c7 100644 --- a/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/theme/Color.kt +++ b/app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/theme/Color.kt @@ -8,4 +8,8 @@ val Pink80 = Color(0xFFEFB8C8) val Purple40 = Color(0xFF6650a4) val PurpleGrey40 = Color(0xFF625b71) -val Pink40 = Color(0xFF7D5260) \ No newline at end of file +val Pink40 = Color(0xFF7D5260) + + +val LightGreen1 = Color(0xff54e1b6) +val LightGreen2 = Color(0xff36ddab) \ No newline at end of file diff --git a/build.gradle b/build.gradle index e868fc8..b0f2f02 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ buildscript { ext { - compose_version = '1.3.2' + compose_version = '1.4.3' } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '7.3.0' apply false - id 'com.android.library' version '7.3.0' apply false - id 'org.jetbrains.kotlin.android' version '1.6.10' apply false + id 'com.android.application' version '8.0.2' apply false + id 'com.android.library' version '8.0.2' apply false + id 'org.jetbrains.kotlin.android' version '1.8.10' apply false } \ No newline at end of file