From 93ba477c2a92bcf6d0986e9f5d2766ece3ea924c Mon Sep 17 00:00:00 2001 From: akanshamaheshwari Date: Sat, 1 Jun 2024 19:45:18 +0530 Subject: [PATCH 1/4] feat: Added compose --- app/build.gradle | 43 ++++++ app/src/main/AndroidManifest.xml | 11 +- .../com/example/ToDo/compose/HomeActivity.kt | 129 ++++++++++++++++++ .../example/ToDo/compose/ui/theme/Color.kt | 11 ++ .../example/ToDo/compose/ui/theme/Theme.kt | 57 ++++++++ .../com/example/ToDo/compose/ui/theme/Type.kt | 34 +++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/themes.xml | 5 + 8 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/example/ToDo/compose/HomeActivity.kt create mode 100644 app/src/main/java/com/example/ToDo/compose/ui/theme/Color.kt create mode 100644 app/src/main/java/com/example/ToDo/compose/ui/theme/Theme.kt create mode 100644 app/src/main/java/com/example/ToDo/compose/ui/theme/Type.kt create mode 100644 app/src/main/res/values/themes.xml diff --git a/app/build.gradle b/app/build.gradle index e1a3b54..5493b7d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,9 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" namespace 'com.example.ToDo' + vectorDrawables { + useSupportLibrary true + } } buildTypes { @@ -44,12 +47,36 @@ android { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } + + buildFeatures { + compose true + } + + composeOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + kotlinCompilerExtensionVersion = "1.5.14" + } + + kotlinOptions { + jvmTarget = '17' + } + + packaging { + resources { + excludes += '/META-INF/{AL2.0,LGPL2.1}' + } + } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.core:core-ktx:1.13.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.1' + implementation 'androidx.activity:activity-compose:1.9.0' + implementation 'androidx.compose.ui:ui-graphics' + implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' @@ -69,4 +96,20 @@ dependencies { // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.1" + + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.24" + androidTestImplementation platform('androidx.compose:compose-bom:2024.05.00') + androidTestImplementation 'androidx.compose.ui:ui-test-junit4' + + + def composeBom = platform('androidx.compose:compose-bom:2024.05.00') + implementation composeBom + + implementation 'androidx.compose.ui:ui' + + // Android Studio Preview support + implementation 'androidx.compose.ui:ui-tooling-preview' + debugImplementation 'androidx.compose.ui:ui-tooling' + debugImplementation 'androidx.compose.ui:ui-test-manifest' + } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e6f1ef0..8429719 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,14 +9,21 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + + - + \ No newline at end of file diff --git a/app/src/main/java/com/example/ToDo/compose/HomeActivity.kt b/app/src/main/java/com/example/ToDo/compose/HomeActivity.kt new file mode 100644 index 0000000..a8907e1 --- /dev/null +++ b/app/src/main/java/com/example/ToDo/compose/HomeActivity.kt @@ -0,0 +1,129 @@ +package com.example.ToDo.compose + +import android.os.Bundle +import android.widget.Toast +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add +import androidx.compose.material3.Checkbox +import androidx.compose.material3.CheckboxColors +import androidx.compose.material3.Icon +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +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.graphics.Color +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.example.ToDo.compose.ui.theme.Purple80 +import com.example.ToDo.compose.ui.theme.ToDoTheme + +class HomeActivity : ComponentActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + ToDoTheme { + LoadUI() + } + } + } + +} + +@Composable +fun LoadUI() { + Column(modifier = Modifier.padding(5.dp)) { + Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { + Text( + text = "Hi, Akansha", + textAlign = TextAlign.Left, + modifier = Modifier + .background(Color.Transparent), + fontSize = 30.sp, + fontFamily = FontFamily.SansSerif, + fontWeight = FontWeight.SemiBold + ) + //val context = LocalContext.current + Icon(imageVector = Icons.Filled.Add, + contentDescription = "create a new task", + modifier = Modifier + .background(color = Purple80, shape = CircleShape) + .size(30.dp) + .clickable { + // todo: to open a container for adding the tasks + } + ) + } + + LazyColumn { + items(items = listOf("Item 1", "Item 2", "Item 3")) { item -> + ToDoListItem(item) + } + } + } +} + + +@Composable +fun ToDoListItem(item : String) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.Start + ) { + var checked by remember { mutableStateOf(false) } + Checkbox( + checked = checked, + onCheckedChange = { + checked = it + }, + colors = CheckboxColors( + checkedBoxColor = Purple80, + checkedCheckmarkColor = Color.Black, + checkedBorderColor = Color.White, + disabledBorderColor = Color.Gray, + disabledCheckedBoxColor = Color.Gray, + disabledIndeterminateBorderColor = Color.Gray, + uncheckedBorderColor = Purple80, + uncheckedBoxColor = Color.White, + uncheckedCheckmarkColor = Color.White, + disabledUncheckedBoxColor = Color.Gray, + disabledUncheckedBorderColor = Color.Gray, + disabledIndeterminateBoxColor = Color.Gray + ) + ) + Text(text = item, fontFamily = FontFamily.SansSerif) + } + +} + +@Preview(showBackground = true) +@Composable +fun LoadUIPreview() { + ToDoTheme { + LoadUI() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/ToDo/compose/ui/theme/Color.kt b/app/src/main/java/com/example/ToDo/compose/ui/theme/Color.kt new file mode 100644 index 0000000..537094e --- /dev/null +++ b/app/src/main/java/com/example/ToDo/compose/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.example.ToDo.compose.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/app/src/main/java/com/example/ToDo/compose/ui/theme/Theme.kt b/app/src/main/java/com/example/ToDo/compose/ui/theme/Theme.kt new file mode 100644 index 0000000..78441d3 --- /dev/null +++ b/app/src/main/java/com/example/ToDo/compose/ui/theme/Theme.kt @@ -0,0 +1,57 @@ +package com.example.ToDo.compose.ui.theme + +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun ToDoTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/ToDo/compose/ui/theme/Type.kt b/app/src/main/java/com/example/ToDo/compose/ui/theme/Type.kt new file mode 100644 index 0000000..3648739 --- /dev/null +++ b/app/src/main/java/com/example/ToDo/compose/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.example.ToDo.compose.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 42730b3..6813b00 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,4 +3,5 @@ Save New Task Tasks + HomeActivity \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..4ac2d6f --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +