Skip to content

Commit 4061080

Browse files
Main Groovy to Kotlin DSL (#43)
1 parent 054a669 commit 4061080

File tree

16 files changed

+209
-171
lines changed

16 files changed

+209
-171
lines changed

app/build.gradle

Lines changed: 0 additions & 89 deletions
This file was deleted.

app/build.gradle.kts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("UnstableApiUsage")
18+
19+
plugins {
20+
id("com.android.application")
21+
id("org.jetbrains.kotlin.android")
22+
id("com.google.devtools.ksp") version "1.8.21-1.0.11"
23+
}
24+
25+
android {
26+
compileSdk = 33
27+
28+
defaultConfig {
29+
applicationId = "com.example.inventory"
30+
minSdk = 24
31+
targetSdk = 33
32+
versionCode = 1
33+
versionName = "1.0"
34+
35+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
36+
vectorDrawables {
37+
useSupportLibrary = true
38+
}
39+
}
40+
41+
buildTypes {
42+
release {
43+
isMinifyEnabled = false
44+
proguardFiles(
45+
getDefaultProguardFile("proguard-android-optimize.txt"),
46+
"proguard-rules.pro"
47+
)
48+
}
49+
}
50+
compileOptions {
51+
sourceCompatibility = JavaVersion.VERSION_17
52+
targetCompatibility = JavaVersion.VERSION_17
53+
}
54+
kotlinOptions {
55+
jvmTarget = "17"
56+
}
57+
buildFeatures {
58+
compose = true
59+
}
60+
composeOptions {
61+
kotlinCompilerExtensionVersion = "1.4.7"
62+
}
63+
packaging {
64+
resources {
65+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
66+
}
67+
}
68+
namespace = "com.example.inventory"
69+
}
70+
71+
dependencies {
72+
// Import the Compose BOM
73+
implementation(platform("androidx.compose:compose-bom:2023.06.01"))
74+
implementation("androidx.activity:activity-compose:1.7.2")
75+
implementation("androidx.compose.material3:material3")
76+
implementation("androidx.compose.ui:ui")
77+
implementation("androidx.compose.ui:ui-tooling")
78+
implementation("androidx.compose.ui:ui-tooling-preview")
79+
implementation("androidx.core:core-ktx:1.10.1")
80+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
81+
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
82+
implementation("androidx.navigation:navigation-compose:2.6.0")
83+
84+
//Room
85+
implementation("androidx.room:room-runtime:${rootProject.extra["room_version"]}")
86+
implementation("androidx.core:core-ktx:1.10.1")
87+
ksp("androidx.room:room-compiler:${rootProject.extra["room_version"]}")
88+
implementation("androidx.room:room-ktx:${rootProject.extra["room_version"]}")
89+
90+
// Testing
91+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
92+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
93+
}

app/src/main/java/com/example/inventory/InventoryApp.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.annotation.SuppressLint
2020
import androidx.compose.material.icons.Icons.Filled
2121
import androidx.compose.material.icons.filled.ArrowBack
2222
import androidx.compose.material3.CenterAlignedTopAppBar
23+
import androidx.compose.material3.ExperimentalMaterial3Api
2324
import androidx.compose.material3.Icon
2425
import androidx.compose.material3.IconButton
2526
import androidx.compose.material3.Text
@@ -44,6 +45,7 @@ fun InventoryApp(navController: NavHostController = rememberNavController()) {
4445
/**
4546
* App bar to display title and conditionally display the back navigation.
4647
*/
48+
@OptIn(ExperimentalMaterial3Api::class)
4749
@Composable
4850
fun InventoryTopAppBar(
4951
title: String,

app/src/main/java/com/example/inventory/InventoryApplication.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class InventoryApplication : Application() {
2626
* AppContainer instance used by the rest of classes to obtain dependencies
2727
*/
2828
lateinit var container: AppContainer
29+
2930
override fun onCreate() {
3031
super.onCreate()
3132
container = AppDataContainer(this)

app/src/main/java/com/example/inventory/ui/home/HomeScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.compose.material.icons.Icons
3131
import androidx.compose.material.icons.filled.Add
3232
import androidx.compose.material3.Card
3333
import androidx.compose.material3.CardDefaults
34+
import androidx.compose.material3.ExperimentalMaterial3Api
3435
import androidx.compose.material3.FloatingActionButton
3536
import androidx.compose.material3.Icon
3637
import androidx.compose.material3.MaterialTheme
@@ -65,6 +66,7 @@ object HomeDestination : NavigationDestination {
6566
/**
6667
* Entry route for Home screen
6768
*/
69+
@OptIn(ExperimentalMaterial3Api::class)
6870
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
6971
@Composable
7072
fun HomeScreen(

app/src/main/java/com/example/inventory/ui/home/HomeViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.map
2626
import kotlinx.coroutines.flow.stateIn
2727

2828
/**
29-
* View Model to retrieve all items in the Room database.
29+
* ViewModel to retrieve all items in the Room database.
3030
*/
3131
class HomeViewModel(itemsRepository: ItemsRepository) : ViewModel() {
3232

app/src/main/java/com/example/inventory/ui/item/ItemDetailsScreen.kt

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.compose.material3.AlertDialog
3131
import androidx.compose.material3.Button
3232
import androidx.compose.material3.Card
3333
import androidx.compose.material3.CardDefaults
34+
import androidx.compose.material3.ExperimentalMaterial3Api
3435
import androidx.compose.material3.FloatingActionButton
3536
import androidx.compose.material3.Icon
3637
import androidx.compose.material3.MaterialTheme
@@ -66,6 +67,7 @@ object ItemDetailsDestination : NavigationDestination {
6667
val routeWithArgs = "$route/{$itemIdArg}"
6768
}
6869

70+
@OptIn(ExperimentalMaterial3Api::class)
6971
@Composable
7072
fun ItemDetailsScreen(
7173
navigateToEditItem: (Int) -> Unit,
@@ -75,25 +77,26 @@ fun ItemDetailsScreen(
7577
) {
7678
val uiState = viewModel.uiState.collectAsState()
7779
val coroutineScope = rememberCoroutineScope()
78-
Scaffold(topBar = {
79-
InventoryTopAppBar(
80-
title = stringResource(ItemDetailsDestination.titleRes),
81-
canNavigateBack = true,
82-
navigateUp = navigateBack
83-
)
84-
}, floatingActionButton = {
85-
FloatingActionButton(
86-
onClick = { navigateToEditItem(uiState.value.itemDetails.id) },
87-
shape = MaterialTheme.shapes.medium,
88-
modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_large))
89-
90-
) {
91-
Icon(
92-
imageVector = Icons.Default.Edit,
93-
contentDescription = stringResource(R.string.edit_item_title),
80+
Scaffold(
81+
topBar = {
82+
InventoryTopAppBar(
83+
title = stringResource(ItemDetailsDestination.titleRes),
84+
canNavigateBack = true,
85+
navigateUp = navigateBack
9486
)
95-
}
96-
}, modifier = modifier
87+
}, floatingActionButton = {
88+
FloatingActionButton(
89+
onClick = { navigateToEditItem(uiState.value.itemDetails.id) },
90+
shape = MaterialTheme.shapes.medium,
91+
modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_large))
92+
93+
) {
94+
Icon(
95+
imageVector = Icons.Default.Edit,
96+
contentDescription = stringResource(R.string.edit_item_title),
97+
)
98+
}
99+
}, modifier = modifier
97100
) { innerPadding ->
98101
ItemDetailsBody(
99102
itemDetailsUiState = uiState.value,
@@ -111,7 +114,7 @@ fun ItemDetailsScreen(
111114
modifier = Modifier
112115
.padding(innerPadding)
113116
.verticalScroll(rememberScrollState())
114-
)
117+
)
115118
}
116119
}
117120

@@ -146,10 +149,11 @@ private fun ItemDetailsBody(
146149
Text(stringResource(R.string.delete))
147150
}
148151
if (deleteConfirmationRequired) {
149-
DeleteConfirmationDialog(onDeleteConfirm = {
150-
deleteConfirmationRequired = false
151-
onDelete()
152-
},
152+
DeleteConfirmationDialog(
153+
onDeleteConfirm = {
154+
deleteConfirmationRequired = false
155+
onDelete()
156+
},
153157
onDeleteCancel = { deleteConfirmationRequired = false },
154158
modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_medium))
155159
)
@@ -177,20 +181,32 @@ fun ItemDetails(
177181
ItemDetailsRow(
178182
labelResID = R.string.item,
179183
itemDetail = item.name,
180-
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen
181-
.padding_medium))
184+
modifier = Modifier.padding(
185+
horizontal = dimensionResource(
186+
id = R.dimen
187+
.padding_medium
188+
)
189+
)
182190
)
183191
ItemDetailsRow(
184192
labelResID = R.string.quantity_in_stock,
185193
itemDetail = item.quantity.toString(),
186-
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen
187-
.padding_medium))
194+
modifier = Modifier.padding(
195+
horizontal = dimensionResource(
196+
id = R.dimen
197+
.padding_medium
198+
)
199+
)
188200
)
189201
ItemDetailsRow(
190202
labelResID = R.string.price,
191203
itemDetail = item.formatedPrice(),
192-
modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen
193-
.padding_medium))
204+
modifier = Modifier.padding(
205+
horizontal = dimensionResource(
206+
id = R.dimen
207+
.padding_medium
208+
)
209+
)
194210
)
195211
}
196212

app/src/main/java/com/example/inventory/ui/item/ItemEditScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.inventory.ui.item
1818

1919
import androidx.compose.foundation.layout.padding
20+
import androidx.compose.material3.ExperimentalMaterial3Api
2021
import androidx.compose.material3.Scaffold
2122
import androidx.compose.runtime.Composable
2223
import androidx.compose.runtime.rememberCoroutineScope
@@ -38,6 +39,7 @@ object ItemEditDestination : NavigationDestination {
3839
val routeWithArgs = "$route/{$itemIdArg}"
3940
}
4041

42+
@OptIn(ExperimentalMaterial3Api::class)
4143
@Composable
4244
fun ItemEditScreen(
4345
navigateBack: () -> Unit,

0 commit comments

Comments
 (0)