Skip to content

Commit

Permalink
Migrate the showcase app to Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
mahozad committed Aug 22, 2021
1 parent 55aded2 commit 66abcd0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
26 changes: 25 additions & 1 deletion test/build.gradle
Expand Up @@ -19,6 +19,16 @@ android {

compileSdkVersion 30

buildFeatures {
// Enables Jetpack Compose for this module
compose true
}

composeOptions {
kotlinCompilerVersion "1.5.21"
kotlinCompilerExtensionVersion '1.0.1'
}

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
Expand Down Expand Up @@ -74,8 +84,22 @@ tasks.withType(Test) {
}

dependencies {
// Integration with activities
implementation 'androidx.activity:activity-compose:1.3.1'
// Compose Material Design
implementation 'androidx.compose.material:material:1.0.1'
// Animations
implementation 'androidx.compose.animation:animation:1.0.1'
// Tooling support (Previews, etc.)
implementation 'androidx.compose.ui:ui-tooling:1.0.1'
// Integration with ViewModels
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07'
// UI Tests (or use androidx.compose.ui:ui-test)
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.1'


implementation project(':piechart')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
Expand Down
36 changes: 36 additions & 0 deletions test/src/main/kotlin/chart/test/ShowCaseLegacyActivity.kt
@@ -0,0 +1,36 @@
package chart.test

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import ir.mahozad.android.PieChart

/**
* To use this activity, change the launcher activity to this in the AndroidManifest.
*/
class ShowCaseLegacyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val chart = findViewById<PieChart>(R.id.pieChart)
chart.setOnClickListener {
chart.slices = listOf(
PieChart.Slice(0.5f, Color.BLACK),
PieChart.Slice(0.5f, Color.RED)
)
}
}

private fun animateStartAngle(chart: PieChart) {
val handler = Handler(Looper.getMainLooper())
val runnable = object : Runnable {
override fun run() {
chart.startAngle = chart.startAngle + 1
handler.postDelayed(this, 15)
}
}
handler.postDelayed(runnable, 15)
}
}
57 changes: 54 additions & 3 deletions test/src/main/kotlin/chart/test/ShowcaseActivity.kt
@@ -1,17 +1,44 @@
package chart.test

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import ir.mahozad.android.PieChart

class ShowcaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val chart = findViewById<PieChart>(R.id.pieChart)
chart.setOnClickListener { animateStartAngle(chart) }

val sliceItems = listOf(
PieChart.Slice(0.4f, Color.MAGENTA, legend = "Legend A"),
PieChart.Slice(0.2f, Color.YELLOW, legend = "Legend B"),
PieChart.Slice(0.1f, Color.GREEN, legend = "Legend C"),
PieChart.Slice(0.3f, Color.BLUE, legend = "Legend D")
)

setContent {
val slices = rememberSaveable { mutableStateOf(sliceItems) }
// MaterialTheme {
PieChartView(slices) {
slices.value = listOf(
PieChart.Slice(0.63f, Color.BLACK),
PieChart.Slice(0.47f, Color.RED)
)
}
// }
}
}

private fun animateStartAngle(chart: PieChart) {
Expand All @@ -24,4 +51,28 @@ class ShowcaseActivity : AppCompatActivity() {
}
handler.postDelayed(runnable, 15)
}

@Composable
fun PieChartView(slicesState: MutableState<List<PieChart.Slice>>, onClick: () -> Unit) {
AndroidView(
modifier = Modifier
.fillMaxSize()
.clickable { onClick() },
factory = { context ->
PieChart(context).apply {
slices = slicesState.value
labelType = PieChart.LabelType.INSIDE
legendsTitle = "Legends"
isLegendEnabled = true
isLegendsPercentageEnabled = true
legendPosition = PieChart.LegendPosition.START
}
},
update = { chart ->
// View's been inflated or state read in this block has been updated
// Add logic here if necessary
chart.slices = slicesState.value
}
)
}
}

0 comments on commit 66abcd0

Please sign in to comment.