Skip to content

Commit

Permalink
icons + hiding label when not enough space
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastGimbus committed Sep 20, 2023
1 parent 55e5bcf commit 06175a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lastgimbus.the.freebuddy

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand Down Expand Up @@ -38,7 +39,9 @@ class BatteryWidget : GlanceAppWidget() {
)

override suspend fun provideGlance(context: Context, id: GlanceId) {
// TODO: Icons/charging
// TODO in future: Charging, but only when updates will be more "live"
// currently there is so much lag between updates that it doesn't make sense :/
// but it will if we'll have foreground service or something
provideContent {
GlanceTheme {
val sp = HomeWidgetPlugin.getData(LocalContext.current)
Expand All @@ -55,16 +58,19 @@ class BatteryWidget : GlanceAppWidget() {
)

@Composable
fun BatteryBox(passGlanceModifier: GlanceModifier, level: Int, label: String) {
fun BatteryBox(
passGlanceModifier: GlanceModifier,
icon: ImageProvider,
level: Int,
label: String,
availableWidth: Dp = size.width
) {
Box(
// this must be passed here because for some reason the .defaultWeight() is context aware??
modifier = passGlanceModifier,
contentAlignment = Alignment.Center
modifier = passGlanceModifier, contentAlignment = Alignment.Center
) {
LinearProgressIndicator(
modifier = GlanceModifier
.cornerRadius(R.dimen.batteryWidgetInnerRadius)
.fillMaxSize(),
modifier = GlanceModifier.cornerRadius(R.dimen.batteryWidgetInnerRadius).fillMaxSize(),
progress = max(0f, level / 100f),
color = barColor,
backgroundColor = barBackground
Expand All @@ -76,60 +82,58 @@ class BatteryWidget : GlanceAppWidget() {
) {
Box(modifier = GlanceModifier.padding(R.dimen.batteryWidgetPadding)) {
Image(
ImageProvider(R.drawable.left_earbud),
"Left earbud",
icon,
"$label icon",
modifier = GlanceModifier.defaultWeight().size(30.dp),
colorFilter = ColorFilter.tint(GlanceTheme.colors.primary),
)
}
Text(label, style = textStyle)
Spacer(modifier = GlanceModifier.defaultWeight())
if (availableWidth > 128.dp) Text(label, style = textStyle)
Spacer(modifier = GlanceModifier.size(0.dp).defaultWeight())
Box(
modifier = GlanceModifier.padding(R.dimen.batteryWidgetPadding),
contentAlignment = Alignment.Center
) {
Text(
if (level >= 0) "$level%" else "-",
style = textStyle,
modifier = GlanceModifier.defaultWeight().width(64.dp)
modifier = GlanceModifier.defaultWeight().width(40.dp)
)
}
}
}
}

Box(
modifier = GlanceModifier
.fillMaxSize()
.appWidgetBackground()
modifier = GlanceModifier.fillMaxSize().appWidgetBackground()
.clickable(actionStartActivity(activity = MainActivity::class.java))
.background(GlanceTheme.colors.background)
.cornerRadius(R.dimen.batteryWidgetBackgroundRadius)
.background(GlanceTheme.colors.background).cornerRadius(R.dimen.batteryWidgetBackgroundRadius)
.padding(R.dimen.batteryWidgetPadding)
) {
if (size.height < VERTICAL_RECTANGLE.height) {
Row(modifier = GlanceModifier.fillMaxSize()) {
// this must be passed here because for some reason the .defaultWeight() is context aware??
val mod = GlanceModifier.defaultWeight().fillMaxHeight()
if (size.width <= SMALL_SQUARE.width) {
BatteryBox(mod, max(left, right), "Buds")
BatteryBox(mod, ImageProvider(R.drawable.earbuds), max(left, right), "Buds")
} else {
BatteryBox(mod, left, "Left")
val avail = size.width / 3
BatteryBox(mod, ImageProvider(R.drawable.left_earbud), left, "Left", avail)
Spacer(modifier = GlanceModifier.size(R.dimen.batteryWidgetSquaresSpacerSize))
BatteryBox(mod, right, "Right")
BatteryBox(mod, ImageProvider(R.drawable.right_earbud), right, "Right", avail)
Spacer(modifier = GlanceModifier.size(R.dimen.batteryWidgetSquaresSpacerSize))
BatteryBox(mod, case, "Case")
BatteryBox(mod, ImageProvider(R.drawable.earbuds_case), case, "Case", avail)
}
}
} else {
Column(modifier = GlanceModifier.fillMaxSize()) {
// this must be passed here because for some reason the .defaultWeight() is context aware??
val mod = GlanceModifier.defaultWeight() // no .fillMaxHeight()!
BatteryBox(mod, left, "Left")
BatteryBox(mod, ImageProvider(R.drawable.left_earbud), left, "Left")
Spacer(modifier = GlanceModifier.size(R.dimen.batteryWidgetLinesSpacerSize))
BatteryBox(mod, right, "Right")
BatteryBox(mod, ImageProvider(R.drawable.right_earbud), right, "Right")
Spacer(modifier = GlanceModifier.size(R.dimen.batteryWidgetLinesSpacerSize))
BatteryBox(mod, case, "Case")
BatteryBox(mod, ImageProvider(R.drawable.earbuds_case), case, "Case")
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions android/app/src/main/res/drawable/earbuds.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- https://pictogrammers.com/library/mdi/icon/earbuds-outline/ <3 -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M5,3C3,3 2,5 2,6V11C2,12 3,14 5,14C5.61,14 6.32,13.72 7,13.27V20C7,20.55 7.45,21 8,21H9C9.55,21 10,20.55 10,20V8C10,6 7,3 5,3M8,10.23L5.91,11.6C5.4,11.93 5.08,12 5,12C4.3,12 4,11.08 4,11L4,6.03C4,5.92 4.3,5 5,5C5.9,5 8,7.1 8,8V10.23M16.5,15C17.2,15 17.87,14.89 18.5,14.68V20C18.5,20.55 18.05,21 17.5,21H15.5C14.95,21 14.5,20.55 14.5,20V14.68C15.13,14.89 15.8,15 16.5,15M16.5,3C13.46,3 11,5.46 11,8.5S13.46,14 16.5,14 22,11.54 22,8.5 19.54,3 16.5,3M16.5,12C14.57,12 13,10.43 13,8.5S14.57,5 16.5,5 20,6.57 20,8.5 18.43,12 16.5,12Z" />
</vector>

0 comments on commit 06175a4

Please sign in to comment.