Skip to content

Commit

Permalink
fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Apr 24, 2024
1 parent aedbbe6 commit 344b7ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Gauge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

private val artifact = "com.github.yamin8000.gauge"
private val version = "1.0.2"
private val version = "1.0.3"

plugins {
id("com.android.library")
Expand Down Expand Up @@ -61,7 +61,7 @@ android {
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
kotlinCompilerExtensionVersion = "1.5.12"
}

packaging {
Expand All @@ -72,15 +72,15 @@ android {
}

dependencies {
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation(platform("androidx.compose:compose-bom:2024.04.01"))
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-tooling-preview")
debugImplementation("androidx.compose.ui:ui-tooling")
}

mavenPublishing {
publishToMavenCentral(com.vanniktech.maven.publish.SonatypeHost.S01)
publishToMavenCentral(com.vanniktech.maven.publish.SonatypeHost.S01, true)
signAllPublications()

coordinates(artifact, "Gauge", version)
Expand Down
40 changes: 25 additions & 15 deletions Gauge/src/main/java/com/github/yamin8000/gauge/main/Gauge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package com.github.yamin8000.gauge.main

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -56,9 +55,11 @@ import kotlin.math.cos
import kotlin.math.sin

/**
* Gauge Composable is a fusion of classic and modern Gauges with some customization options.
* Gauge Composable is a fusion of classic and modern Gauges with some
* customization options.
*
* @param value current value of the [Gauge], this value directly affects Gauge's arc and Gauge's needle style
* @param value current value of the [Gauge], this value directly affects
* Gauge's arc and Gauge's needle style
* @param modifier refer to [Modifier]
* @param valueUnit unit of the Gauge's value like %, km/h or etc
* @param decimalFormat decimal formatter for value text
Expand All @@ -71,10 +72,11 @@ import kotlin.math.sin
* @param needleColors
* @param arcColors
* @param ticksColors
* @param ticksColorProvider a lambda for fine tune of individual tick's color
* @param ticksColorProvider a lambda for fine tune of individual tick's
* color
* @param arcColorsProvider a lambda for fine tune of arc colors
*
* @throws IllegalArgumentException when some parameters are inconsistent with the design
* @throws IllegalArgumentException when some parameters are inconsistent
* with the design
*/
@Composable
fun Gauge(
Expand Down Expand Up @@ -137,7 +139,7 @@ fun Gauge(
drawCompatibleText(
textMeasurer = textMeasurer,
text = decimalFormat.format(value).trim(),
topLeft = center.plus(Offset(0f, size.toPx() / 12)),
topLeft = center.plus(Offset(0f, size.toPx() / 8)),
color = valueColor,
totalSize = size
)
Expand Down Expand Up @@ -271,19 +273,27 @@ private fun DrawScope.drawTicks(
val isBigTick = isSmallTick && (value % numerics.bigTicksStep == 0)
val isStartOrEnd =
isBigTick && (degreeInt == numerics.startAngle || degreeInt == totalAngle)
val endRatio = if (isBigTick) size.toPx().div(5f) else size.toPx().div(6f)
val tickEndRatio = if (isBigTick) size.toPx().div(5f) else size.toPx().div(6f)
val width = if (isBigTick) size.div(500f).toPx() else size.div(700f).toPx()
val tickColor = if (isBigTick) colors.bigTicks else ticksColors[value].second
val tickColor = if (isBigTick) colors.bigTicks
else ticksColors[value - numerics.valueRange.start.toInt()].second

val radian = Math.toRadians(degree.toDouble())
val cos = cos(radian).toFloat()
val sin = sin(radian).toFloat()
val x = translate(cos, -1f..1f, 0f..size.toPx())
val y = translate(sin, -1f..1f, 0f..size.toPx())
val endOffset = Offset(
x.minus(cos.times(endRatio)),
y.minus(sin.times(endRatio))
val tickEndOffset = Offset(
x.minus(cos.times(tickEndRatio)),
y.minus(sin.times(tickEndRatio))
)

val textEndRatio = size.toPx().div(4.25f)
var textOffset = Offset(
x.minus(cos.times(textEndRatio)),
y.minus(sin.times(textEndRatio))
)

if (isSmallTick) {
drawLine(
color = tickColor,
Expand All @@ -293,17 +303,17 @@ private fun DrawScope.drawTicks(
x.minus(cos.times(startRatio)),
y.minus(sin.times(startRatio))
).plus(borderOffset),
end = endOffset.plus(borderOffset)
end = tickEndOffset.plus(borderOffset)
)
}
if (hasNumbers && isBigTick) {
val textSizeFactor = 20f
val textSizeFactor = 15f
val textStyle = TextStyle(
color = colors.bigTicksLabels,
fontSize = size.toSp() / textSizeFactor
)
val textLayout = textMeasurer.measure("$value", textStyle)
var textOffset = endOffset.minus(
textOffset = textOffset.minus(
Offset(
textLayout.size.width.toFloat() / 2,
textLayout.size.height.toFloat() / 2
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ android {
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
kotlinCompilerExtensionVersion = "1.5.12"
}

packaging {
Expand All @@ -74,10 +74,10 @@ android {
}

dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.core:core-ktx:1.13.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.activity:activity-compose:1.9.0")
implementation(platform("androidx.compose:compose-bom:2024.04.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/github/yamin8000/gauge/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ class MainActivity : ComponentActivity() {
) {
val configuration = LocalConfiguration.current
val screenWidth = configuration.screenWidthDp.dp
var value by remember { mutableFloatStateOf(0f) }
var value by remember { mutableFloatStateOf(15f) }
var totalSize by remember { mutableFloatStateOf(350f) }
val valueRange = 0f..220f
val valueRange = 10f..20f
Gauge(
value = value,
totalSize = totalSize.dp,
numerics = GaugeNumerics(
startAngle = 120,
sweepAngle = 300,
valueRange = valueRange,
bigTicksStep = 20,
smallTicksStep = 2
bigTicksStep = 2,
smallTicksStep = 1
),
style = GaugeStyle(
hasBorder = true,
Expand Down
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

plugins {
id("com.android.application") version "8.2.1" apply false
id("com.android.library") version "8.2.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.10" apply false
id("com.vanniktech.maven.publish") version "0.25.3"
id("com.android.application") version "8.3.2" apply false
id("com.android.library") version "8.3.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.23" apply false
id("com.vanniktech.maven.publish") version "0.28.0"
}

0 comments on commit 344b7ce

Please sign in to comment.