From ddad5b2e8cf200d8da7a9d05e738e54042d0fa9a Mon Sep 17 00:00:00 2001 From: Andrey Ananiev Date: Fri, 24 Mar 2023 11:47:12 +0300 Subject: [PATCH] Migrate on material3 --- .../gestures/GestureExtensions.kt | 71 +++++++++---------- .../co/yml/charts/common/utils/DataUtils.kt | 4 +- .../app/presentation/BarChartActivity.kt | 2 +- .../CombinedLineAndBarChartActivity.kt | 2 +- .../app/presentation/DonutChartActivity.kt | 6 +- .../app/presentation/LineChartActivity.kt | 2 +- .../app/presentation/PieChartActivity.kt | 2 +- build.gradle.kts | 2 +- .../app/chartcontainer/LineChartActivity.kt | 6 +- .../com/app/chartcontainer/MainActivity.kt | 4 +- .../com/app/chartcontainer/ui/theme/Shape.kt | 2 +- .../com/app/chartcontainer/ui/theme/Theme.kt | 14 ++-- .../com/app/chartcontainer/ui/theme/Type.kt | 4 +- .../piechartcontainer/build.gradle.kts | 4 +- .../piechartcontainer/BarChartActivity.kt | 9 +-- .../example/piechartcontainer/MainActivity.kt | 10 +-- .../piechartcontainer/PieChartActivity.kt | 6 +- .../piechartcontainer/ui/theme/Shape.kt | 2 +- .../piechartcontainer/ui/theme/Theme.kt | 14 ++-- .../piechartcontainer/ui/theme/Type.kt | 4 +- 20 files changed, 80 insertions(+), 90 deletions(-) diff --git a/YChartsLib/src/main/java/co/yml/charts/chartcontainer/gestures/GestureExtensions.kt b/YChartsLib/src/main/java/co/yml/charts/chartcontainer/gestures/GestureExtensions.kt index beb48a0b..90f76664 100644 --- a/YChartsLib/src/main/java/co/yml/charts/chartcontainer/gestures/GestureExtensions.kt +++ b/YChartsLib/src/main/java/co/yml/charts/chartcontainer/gestures/GestureExtensions.kt @@ -1,9 +1,6 @@ package co.yml.charts.chartcontainer.gestures -import androidx.compose.foundation.gestures.awaitFirstDown -import androidx.compose.foundation.gestures.calculateCentroidSize -import androidx.compose.foundation.gestures.calculateZoom -import androidx.compose.foundation.gestures.forEachGesture +import androidx.compose.foundation.gestures.* import androidx.compose.ui.input.pointer.PointerInputScope import androidx.compose.ui.input.pointer.positionChanged import kotlin.math.abs @@ -18,39 +15,39 @@ internal suspend fun PointerInputScope.detectZoomGesture( onZoom: (zoom: Float) -> Unit ) { if (isZoomAllowed) { - forEachGesture { - awaitPointerEventScope { - awaitFirstDown(requireUnconsumed = false) - } - awaitPointerEventScope { - var zoom = 1f - var pastTouchSlop = false - val touchSlop = viewConfiguration.touchSlop - - do { - val event = awaitPointerEvent() - val canceled = event.changes.any { it.isConsumed } - if (event.changes.size == 1) break - else if (event.changes.size == 2) { - if (isZoomAllowed) { - if (!canceled) { - val zoomChange = event.calculateZoom() - if (!pastTouchSlop) { - zoom *= zoomChange - val centroidSize = - event.calculateCentroidSize(useCurrent = false) - val zoomMotion = abs(1 - zoom) * centroidSize - if (zoomMotion > touchSlop) pastTouchSlop = true - } - if (pastTouchSlop) { - if (zoomChange != 1f) onZoom(zoomChange) - event.changes.forEach { if (it.positionChanged()) it.consume() } - } - } - } - } else break - } while (!canceled && event.changes.any { it.pressed }) - } + awaitEachGesture { +// awaitPointerEventScope { +// awaitFirstDown(requireUnconsumed = false) +// } +// awaitPointerEventScope { +// var zoom = 1f +// var pastTouchSlop = false +// val touchSlop = viewConfiguration.touchSlop +// +// do { +// val event = awaitPointerEvent() +// val canceled = event.changes.any { it.isConsumed } +// if (event.changes.size == 1) break +// else if (event.changes.size == 2) { +// if (isZoomAllowed) { +// if (!canceled) { +// val zoomChange = event.calculateZoom() +// if (!pastTouchSlop) { +// zoom *= zoomChange +// val centroidSize = +// event.calculateCentroidSize(useCurrent = false) +// val zoomMotion = abs(1 - zoom) * centroidSize +// if (zoomMotion > touchSlop) pastTouchSlop = true +// } +// if (pastTouchSlop) { +// if (zoomChange != 1f) onZoom(zoomChange) +// event.changes.forEach { if (it.positionChanged()) it.consume() } +// } +// } +// } +// } else break +// } while (!canceled && event.changes.any { it.pressed }) +// } } } } diff --git a/YChartsLib/src/main/java/co/yml/charts/common/utils/DataUtils.kt b/YChartsLib/src/main/java/co/yml/charts/common/utils/DataUtils.kt index ad8f2040..ae383441 100644 --- a/YChartsLib/src/main/java/co/yml/charts/common/utils/DataUtils.kt +++ b/YChartsLib/src/main/java/co/yml/charts/common/utils/DataUtils.kt @@ -44,7 +44,7 @@ object DataUtils { BarData( Point( index.toFloat(), - "%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).toFloat() + Random.nextDouble(1.0, maxRange.toDouble()).toFloat() ), Color( Random.nextInt(256), Random.nextInt(256), Random.nextInt(256) @@ -68,7 +68,7 @@ object DataUtils { BarData( point = Point( index.toFloat(), - "%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).toFloat() + Random.nextDouble(1.0, maxRange.toDouble()).toFloat() ), gradientColorList = listOf( Color( diff --git a/app/src/main/java/co/yml/ycharts/app/presentation/BarChartActivity.kt b/app/src/main/java/co/yml/ycharts/app/presentation/BarChartActivity.kt index 185d4e33..6f29835e 100644 --- a/app/src/main/java/co/yml/ycharts/app/presentation/BarChartActivity.kt +++ b/app/src/main/java/co/yml/ycharts/app/presentation/BarChartActivity.kt @@ -37,7 +37,7 @@ class BarChartActivity : ComponentActivity() { AppBarWithBackButton( stringResource(id = R.string.title_bar_chart), onBackPressed = { - onBackPressed() + // onBackPressed() }) }) { diff --git a/app/src/main/java/co/yml/ycharts/app/presentation/CombinedLineAndBarChartActivity.kt b/app/src/main/java/co/yml/ycharts/app/presentation/CombinedLineAndBarChartActivity.kt index 039227f4..0e95326e 100644 --- a/app/src/main/java/co/yml/ycharts/app/presentation/CombinedLineAndBarChartActivity.kt +++ b/app/src/main/java/co/yml/ycharts/app/presentation/CombinedLineAndBarChartActivity.kt @@ -44,7 +44,7 @@ class CombinedLineAndBarChartActivity : ComponentActivity() { AppBarWithBackButton( stringResource(id = R.string.title_bar_with_line_chart), onBackPressed = { - onBackPressed() + //onBackPressed() }) }) { diff --git a/app/src/main/java/co/yml/ycharts/app/presentation/DonutChartActivity.kt b/app/src/main/java/co/yml/ycharts/app/presentation/DonutChartActivity.kt index 7aa7c03f..ba51a4b5 100644 --- a/app/src/main/java/co/yml/ycharts/app/presentation/DonutChartActivity.kt +++ b/app/src/main/java/co/yml/ycharts/app/presentation/DonutChartActivity.kt @@ -37,7 +37,7 @@ class DonutChartActivity : ComponentActivity() { AppBarWithBackButton( stringResource(id = R.string.title_donut_chart), onBackPressed = { - onBackPressed() + //onBackPressed() }) }) { @@ -60,13 +60,13 @@ class DonutChartActivity : ComponentActivity() { private fun DonutChart1(context: Context) { // val accessibilitySheetState = // rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden) - val scope = rememberCoroutineScope() + // val scope = rememberCoroutineScope() val data = DataUtils.getDonutChartData() // Sum of all the values val sumOfValues = data.totalLength // Calculate each proportion value - val proportions = data.slices.proportion(sumOfValues) + // val proportions = data.slices.proportion(sumOfValues) val pieChartConfig = PieChartConfig( percentVisible = true, diff --git a/app/src/main/java/co/yml/ycharts/app/presentation/LineChartActivity.kt b/app/src/main/java/co/yml/ycharts/app/presentation/LineChartActivity.kt index 97b4e90b..c88aec3f 100644 --- a/app/src/main/java/co/yml/ycharts/app/presentation/LineChartActivity.kt +++ b/app/src/main/java/co/yml/ycharts/app/presentation/LineChartActivity.kt @@ -49,7 +49,7 @@ class LineChartActivity : ComponentActivity() { AppBarWithBackButton( stringResource(id = R.string.title_line_chart), onBackPressed = { - onBackPressed() + // onBackPressed() }) }) { diff --git a/app/src/main/java/co/yml/ycharts/app/presentation/PieChartActivity.kt b/app/src/main/java/co/yml/ycharts/app/presentation/PieChartActivity.kt index 020807eb..b4a77484 100644 --- a/app/src/main/java/co/yml/ycharts/app/presentation/PieChartActivity.kt +++ b/app/src/main/java/co/yml/ycharts/app/presentation/PieChartActivity.kt @@ -35,7 +35,7 @@ class PieChartActivity : ComponentActivity() { AppBarWithBackButton( stringResource(id = R.string.title_pie_chart), onBackPressed = { - onBackPressed() + // onBackPressed() }) }) { diff --git a/build.gradle.kts b/build.gradle.kts index f7eba855..8a1af3e0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ buildscript { // classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10") // classpath("org.jetbrains.kotlin:kotlin-android-extensions:1.8.10") // classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.7.20") - classpath("com.android.tools.build:gradle:7.2.0") + classpath("com.android.tools.build:gradle:7.2.2") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10") classpath("org.jetbrains.kotlin:kotlin-android-extensions:1.6.10") classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.6.10") diff --git a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/LineChartActivity.kt b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/LineChartActivity.kt index 737efafc..e1acb2e9 100644 --- a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/LineChartActivity.kt +++ b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/LineChartActivity.kt @@ -5,8 +5,8 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp @@ -32,7 +32,7 @@ class LineChartActivity : ComponentActivity() { // A surface container using the 'background' color from the theme Surface( modifier = Modifier, - color = MaterialTheme.colors.background + color = MaterialTheme.colorScheme.surface ) { Column( modifier = Modifier diff --git a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/MainActivity.kt b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/MainActivity.kt index aa80011b..2138fb3f 100644 --- a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/MainActivity.kt +++ b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/MainActivity.kt @@ -5,8 +5,8 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* -import androidx.compose.material.Button -import androidx.compose.material.Text +import androidx.compose.material3.Button +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember diff --git a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Shape.kt b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Shape.kt index 4f8ffed5..bf0468f1 100644 --- a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Shape.kt +++ b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Shape.kt @@ -1,7 +1,7 @@ package com.app.chartcontainer.ui.theme import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.Shapes +import androidx.compose.material3.Shapes import androidx.compose.ui.unit.dp val Shapes = Shapes( diff --git a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Theme.kt b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Theme.kt index 8fe3870b..a12ec443 100644 --- a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Theme.kt +++ b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Theme.kt @@ -1,20 +1,18 @@ package com.app.chartcontainer.ui.theme import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.material.MaterialTheme -import androidx.compose.material.darkColors -import androidx.compose.material.lightColors +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable -private val DarkColorPalette = darkColors( +private val DarkColorPalette = darkColorScheme( primary = Purple200, - primaryVariant = Purple700, secondary = Teal200 ) -private val LightColorPalette = lightColors( +private val LightColorPalette = lightColorScheme( primary = Purple500, - primaryVariant = Purple700, secondary = Teal200 /* Other default colors to override @@ -36,7 +34,7 @@ fun YChartsTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composabl } MaterialTheme( - colors = colors, + colorScheme = colors, typography = Typography, shapes = Shapes, content = content diff --git a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Type.kt b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Type.kt index 4c428b00..9d2db645 100644 --- a/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Type.kt +++ b/experiments/chartcontainer/src/main/java/com/app/chartcontainer/ui/theme/Type.kt @@ -1,6 +1,6 @@ package com.app.chartcontainer.ui.theme -import androidx.compose.material.Typography +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 @@ -8,7 +8,7 @@ import androidx.compose.ui.unit.sp // Set of Material typography styles to start with val Typography = Typography( - body1 = TextStyle( + bodyMedium = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.Normal, fontSize = 16.sp diff --git a/experiments/piechartcontainer/build.gradle.kts b/experiments/piechartcontainer/build.gradle.kts index 2703816b..6eb6f19d 100644 --- a/experiments/piechartcontainer/build.gradle.kts +++ b/experiments/piechartcontainer/build.gradle.kts @@ -4,12 +4,12 @@ plugins { } android { - compileSdk = 32 + compileSdk = 33 defaultConfig { applicationId = "com.example.piechartcontainer" minSdk = 26 - targetSdk = 32 + targetSdk = 33 versionCode = 1 versionName = "1.0" diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/BarChartActivity.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/BarChartActivity.kt index 9e86165d..cc3dcbec 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/BarChartActivity.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/BarChartActivity.kt @@ -1,5 +1,3 @@ -@file:OptIn(ExperimentalMaterialApi::class) - package com.example.piechartcontainer import android.os.Bundle @@ -8,9 +6,8 @@ import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height -import androidx.compose.material.ExperimentalMaterialApi -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.example.piechartcontainer.ui.theme.YChartsTheme @@ -30,7 +27,7 @@ class BarChartActivity : ComponentActivity() { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), - color = MaterialTheme.colors.background + color = MaterialTheme.colorScheme.surface ) { val barSize = 3 val groupBarData = getGroupBarChartData(50, 50, barSize) diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/MainActivity.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/MainActivity.kt index 8b86fa28..354144c9 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/MainActivity.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/MainActivity.kt @@ -5,10 +5,10 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* -import androidx.compose.material.Button -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface -import androidx.compose.material.Text +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview @@ -23,7 +23,7 @@ class MainActivity : ComponentActivity() { // A surface container using the 'background' color from the theme Surface( modifier = Modifier, - color = MaterialTheme.colors.background + color = MaterialTheme.colorScheme.surface ) { Column { Spacer(modifier = Modifier.height(10.dp)) diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/PieChartActivity.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/PieChartActivity.kt index 8c0044de..0fd9264c 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/PieChartActivity.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/PieChartActivity.kt @@ -10,8 +10,8 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext @@ -32,7 +32,7 @@ class PieChartActivity : ComponentActivity() { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), - color = MaterialTheme.colors.background + color = MaterialTheme.colorScheme.surface ) { val context = LocalContext.current diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Shape.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Shape.kt index e3e179c9..ec6542a8 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Shape.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Shape.kt @@ -1,7 +1,7 @@ package com.example.piechartcontainer.ui.theme import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.Shapes +import androidx.compose.material3.Shapes import androidx.compose.ui.unit.dp val Shapes = Shapes( diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Theme.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Theme.kt index 285a6b3a..37d79460 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Theme.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Theme.kt @@ -1,20 +1,18 @@ package com.example.piechartcontainer.ui.theme import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.material.MaterialTheme -import androidx.compose.material.darkColors -import androidx.compose.material.lightColors +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable -private val DarkColorPalette = darkColors( +private val DarkColorPalette = darkColorScheme( primary = Purple200, - primaryVariant = Purple700, secondary = Teal200 ) -private val LightColorPalette = lightColors( +private val LightColorPalette = lightColorScheme( primary = Purple500, - primaryVariant = Purple700, secondary = Teal200 /* Other default colors to override @@ -36,7 +34,7 @@ fun YChartsTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composabl } MaterialTheme( - colors = colors, + colorScheme = colors, typography = Typography, shapes = Shapes, content = content diff --git a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Type.kt b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Type.kt index 43a91cee..dbd38348 100644 --- a/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Type.kt +++ b/experiments/piechartcontainer/src/main/java/com/example/piechartcontainer/ui/theme/Type.kt @@ -1,6 +1,6 @@ package com.example.piechartcontainer.ui.theme -import androidx.compose.material.Typography +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 @@ -8,7 +8,7 @@ import androidx.compose.ui.unit.sp // Set of Material typography styles to start with val Typography = Typography( - body1 = TextStyle( + bodyMedium = TextStyle( fontFamily = FontFamily.Default, fontWeight = FontWeight.Normal, fontSize = 16.sp