Skip to content

Commit

Permalink
Library migration to compose multiplatform
Browse files Browse the repository at this point in the history
- Add support for iOS platform
- Implement expect / actual ScreenWidth.kt for getting screen width.
- Replace all occurrences of TextPaint to draw text with drawText accepting TextMeasurer
- Utils text measurer measurement to calculate text bounds
- Move all classes commonMain modules
- Update build files to add support for multiplatform
  • Loading branch information
sagar-viradiya committed Jul 27, 2024
1 parent bb8faa7 commit 7277296
Show file tree
Hide file tree
Showing 63 changed files with 599 additions and 425 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions build.gradle

This file was deleted.

16 changes: 16 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.0")
classpath("org.jlleitschuh.gradle:ktlint-gradle:11.3.2")
}
}

plugins {
id("com.android.application").version("8.2.0") apply false
id("com.android.library").version("8.2.0") apply false
id("org.jetbrains.kotlin.android").version("1.9.21") apply false
kotlin("multiplatform").version("1.9.21") apply false
id("org.jetbrains.compose").version("1.6.11") apply false
id("org.jetbrains.dokka").version("1.9.0")
}
68 changes: 0 additions & 68 deletions charty/build.gradle

This file was deleted.

87 changes: 87 additions & 0 deletions charty/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import com.vanniktech.maven.publish.SonatypeHost

plugins {
kotlin("multiplatform")
id("com.android.library")
id("org.jetbrains.compose")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish") version "0.25.3"
}

apply("../quality/static-check.gradle")

kotlin {
androidTarget() {
publishLibraryVariants("release")
}

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "charty"
isStatic = true
}
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
implementation(compose.materialIconsExtended)
implementation(compose.ui)
implementation("org.jetbrains.compose.ui:ui-util:1.6.11")
}
}
}
}

android {
namespace = "com.himanshoe.charty"
compileSdk = 34

defaultConfig {
minSdk = 24
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlin {
jvmToolchain(17)
}
}

mavenPublishing {
publishToMavenCentral(SonatypeHost.S01)
signAllPublications()
}

dependencies {
debugImplementation("androidx.compose.ui:ui-tooling")
dokkaPlugin("org.jetbrains.dokka:android-documentation-plugin:1.8.20")
lintChecks("com.slack.lint.compose:compose-lint-checks:1.2.0")

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

}
2 changes: 1 addition & 1 deletion charty/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
# proguardFiles setting in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
Expand Down
135 changes: 135 additions & 0 deletions charty/src/androidMain/kotlin/com/himanshoe/charty/util/Previews.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.himanshoe.charty.util

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.himanshoe.charty.circle.CircleChart
import com.himanshoe.charty.circle.model.CircleData
import com.himanshoe.charty.common.toChartDataCollection
import com.himanshoe.charty.gauge.GaugeChart
import com.himanshoe.charty.line.CurveLineChart
import com.himanshoe.charty.line.model.LineData
import com.himanshoe.charty.pie.PieChart
import com.himanshoe.charty.pie.model.PieData
import com.himanshoe.charty.point.PointChart
import com.himanshoe.charty.point.model.PointData

/*
Collection of all chart previews. This previews are here because KMM does not have support
for previews in the common module.
*/
@Preview
@Composable
internal fun CircleChartScreen() {
val circleData = listOf(
CircleData(10F, 235F, color = Color(0xFFfafa6e)),
CircleData(10F, 135F, color = Color(0xFFc4ec74)),
CircleData(10F, 315F, color = Color(0xFF92dc7e)),
CircleData(20F, 50F, color = Color(0xFF64c987)),
CircleData(30F, 315F, color = Color(0xFF39b48e))
)
CircleChart(
modifier = Modifier
.scale(1f)
.size(400.dp)
.padding(20.dp),
dataCollection = circleData.toChartDataCollection(),
)
}

@Preview
@Composable
internal fun GaugeChartPreview() {
val percentValue = 100
GaugeChart(percentValue = percentValue)
}

@Preview
@Composable
internal fun LineChartPreview(modifier: Modifier = Modifier) {
Column(modifier) {
CurveLineChart(
dataCollection = generateMockLineData().toChartDataCollection(),
modifier = Modifier
.size(450.dp),
)
}
}

internal fun generateMockLineData(): List<LineData> {
return listOf(
LineData(0F, "Jan"),
LineData(10F, "Feb"),
LineData(05F, "Mar"),
LineData(50F, "Apr"),
LineData(03F, "June"),
LineData(9F, "July"),
LineData(40F, "Aug"),
LineData(60F, "Sept"),
LineData(33F, "Oct"),
LineData(11F, "Nov"),
LineData(27F, "Dec"),
LineData(10F, "Jan"),
LineData(13F, "Oct"),
LineData(-10F, "Nov"),
LineData(0F, "Dec"),
LineData(10F, "Jan"),
)
}

@Preview
@Composable
internal fun PieChartPreview() {
val data = listOf(
PieData(30f, "Category A", Color.Blue),
PieData(20f, "Category B", Color.Red),
PieData(10f, "Category C", Color.Green),
PieData(10f, "Category C", Color.Black),
)
PieChart(
dataCollection = data.toChartDataCollection(),
modifier = Modifier.wrapContentSize()
)
}

@Preview
@Composable
internal fun PointChartPreview(modifier: Modifier = Modifier) {
Column(modifier) {
PointChart(
dataCollection = generateMockPointData().toChartDataCollection(),
modifier = Modifier
.size(450.dp),
contentColor = Color.Red,
)
}
}

internal fun generateMockPointData(): List<PointData> {
return listOf(
PointData(-10F, "Jan"),
PointData(10F, "Feb"),
PointData(05F, "Mar"),
PointData(50F, "Apr"),
PointData(03F, "June"),
PointData(9F, "July"),
PointData(40F, "Aug"),
PointData(60F, "Sept"),
PointData(33F, "Oct"),
PointData(11F, "Nov"),
PointData(27F, "Dec"),
PointData(10F, "Jan"),
PointData(73F, "Oct"),
PointData(-20F, "Nov"),
PointData(0F, "Dec"),
PointData(10F, "Jan"),
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.himanshoe.charty.util

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp

@Composable
actual fun getScreenWidth(): Float {
val wDp = LocalConfiguration.current.screenWidthDp.dp
return with(LocalDensity.current) { wDp.roundToPx().toFloat() }
}
Loading

0 comments on commit 7277296

Please sign in to comment.