-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype evolved. Use new version of Donut Fork with StrokeCap support
- Loading branch information
1 parent
15845a7
commit 556eae9
Showing
7 changed files
with
103 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 79 additions & 12 deletions
91
myExpenses/src/main/java/org/totschnig/myexpenses/dialog/CriterionReachedDialogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,107 @@ | ||
package org.totschnig.myexpenses.dialog | ||
|
||
import androidx.compose.animation.core.CubicBezierEasing | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.ColumnScope | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.mutableFloatStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.StrokeCap | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import app.futured.donut.compose.DonutProgress | ||
import app.futured.donut.compose.data.DonutConfig | ||
import app.futured.donut.compose.data.DonutModel | ||
import kotlinx.coroutines.delay | ||
import org.totschnig.myexpenses.R | ||
import org.totschnig.myexpenses.dialog.CriterionReachedDialogFragment.Companion.ANIMATION_DURATION | ||
import org.totschnig.myexpenses.dialog.CriterionReachedDialogFragment.Companion.ANIMATION_DURATION_L | ||
import org.totschnig.myexpenses.util.ui.calcProgressVisualRepresentation | ||
import org.totschnig.myexpenses.util.ui.forCompose | ||
import kotlin.math.absoluteValue | ||
|
||
data class CriterionReachedEventInfo( | ||
val startBalance: Long, | ||
val criterion: Long, | ||
val transactionAmount: Long | ||
) { | ||
init { | ||
require((startBalance + transactionAmount).absoluteValue >= criterion.absoluteValue) | ||
} | ||
|
||
val newBalance = startBalance + transactionAmount | ||
|
||
val startProgress = startBalance * 100F / criterion | ||
val endProgress = newBalance * 100f / criterion | ||
} | ||
|
||
class CriterionReachedDialogFragment: ComposeBaseDialogFragment3() { | ||
val info = CriterionReachedEventInfo( | ||
startBalance = 65, | ||
criterion = 100, | ||
transactionAmount = 75 | ||
) | ||
|
||
@Composable | ||
override fun ColumnScope.MainContent() { | ||
|
||
Column { | ||
Text("Warning") | ||
DonutProgress( | ||
modifier = Modifier.fillMaxWidth().height(240.dp).width(240.dp), | ||
model = DonutModel( | ||
cap = 100f, | ||
sections = calcProgressVisualRepresentation(120).forCompose( | ||
valueColor = Color.Green, excessColor = Color.Red | ||
) | ||
) | ||
) | ||
Text("You have reached your credit limit") | ||
CriterionReachedGraph(info) | ||
Text("You have reached your credit limit.") | ||
} | ||
} | ||
|
||
override val title: CharSequence | ||
get() = getString(R.string.credit_limit) | ||
|
||
companion object { | ||
const val ANIMATION_DURATION = 1000 | ||
const val ANIMATION_DURATION_L = ANIMATION_DURATION.toLong() | ||
} | ||
} | ||
|
||
@Composable | ||
fun CriterionReachedGraph(info: CriterionReachedEventInfo) { | ||
val progress = remember { mutableFloatStateOf(info.startProgress) } | ||
DonutProgress( | ||
modifier = Modifier.height(240.dp).width(240.dp), | ||
model = DonutModel( | ||
cap = 100f, | ||
sections = calcProgressVisualRepresentation(progress.floatValue).forCompose(Color.Green, Color.Red), | ||
gapWidthDegrees = 0f, | ||
gapAngleDegrees = 0f, | ||
strokeCap = StrokeCap.Butt, | ||
backgroundLineColor = Color(0xFFE7E8E9) | ||
), | ||
config = DonutConfig.create( | ||
layoutAnimationSpec = tween( | ||
durationMillis = 1000, | ||
easing = CubicBezierEasing(0.18f, 0.7f, 0.16f, 1f) | ||
), | ||
colorAnimationSpec = tween(durationMillis = ANIMATION_DURATION) | ||
) | ||
) | ||
LaunchedEffect(Unit) { | ||
delay(ANIMATION_DURATION_L) | ||
progress.floatValue = 100F | ||
delay(ANIMATION_DURATION_L) | ||
progress.floatValue = info.endProgress | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun Demo() { | ||
CriterionReachedGraph(CriterionReachedEventInfo( | ||
startBalance = 95, | ||
criterion = 100, | ||
transactionAmount = 10 | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters