Skip to content

Commit

Permalink
Migrate on material3
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-ananiev committed Mar 24, 2023
1 parent 55c3737 commit ddad5b2
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 })
// }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BarChartActivity : ComponentActivity() {
AppBarWithBackButton(
stringResource(id = R.string.title_bar_chart),
onBackPressed = {
onBackPressed()
// onBackPressed()
})
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CombinedLineAndBarChartActivity : ComponentActivity() {
AppBarWithBackButton(
stringResource(id = R.string.title_bar_with_line_chart),
onBackPressed = {
onBackPressed()
//onBackPressed()
})
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DonutChartActivity : ComponentActivity() {
AppBarWithBackButton(
stringResource(id = R.string.title_donut_chart),
onBackPressed = {
onBackPressed()
//onBackPressed()
})
})
{
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LineChartActivity : ComponentActivity() {
AppBarWithBackButton(
stringResource(id = R.string.title_line_chart),
onBackPressed = {
onBackPressed()
// onBackPressed()
})
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PieChartActivity : ComponentActivity() {
AppBarWithBackButton(
stringResource(id = R.string.title_pie_chart),
onBackPressed = {
onBackPressed()
// onBackPressed()
})
})
{
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -36,7 +34,7 @@ fun YChartsTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composabl
}

MaterialTheme(
colors = colors,
colorScheme = colors,
typography = Typography,
shapes = Shapes,
content = content
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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
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
Expand Down
4 changes: 2 additions & 2 deletions experiments/piechartcontainer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalMaterialApi::class)

package com.example.piechartcontainer

import android.os.Bundle
Expand All @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -36,7 +34,7 @@ fun YChartsTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composabl
}

MaterialTheme(
colors = colors,
colorScheme = colors,
typography = Typography,
shapes = Shapes,
content = content
Expand Down
Loading

0 comments on commit ddad5b2

Please sign in to comment.