Skip to content

Commit f641e02

Browse files
committed
code: home widget colors api
1 parent 2c9fc7d commit f641e02

File tree

4 files changed

+79
-35
lines changed

4 files changed

+79
-35
lines changed

android/app/src/main/kotlin/com/msob7y/namida/glance/ImageWrapper.kt

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ import android.graphics.Path
77
import android.graphics.PorterDuff
88
import android.graphics.PorterDuffXfermode
99
import android.graphics.RectF
10-
import androidx.compose.ui.graphics.Color
11-
import androidx.compose.ui.graphics.toArgb
12-
import androidx.glance.appwidget.cornerRadius
13-
import androidx.glance.layout.height
14-
import androidx.glance.layout.width
1510

1611
class ImageWrapper {
1712
companion object {
18-
fun createRoundedBitmap(width: Int, height: Int, radius: Float, color: Color): Bitmap {
13+
fun createRoundedBitmap(width: Int, height: Int, radius: Float, color: Int): Bitmap {
1914
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
2015
val canvas = Canvas(bitmap)
2116
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
22-
paint.color = color.toArgb()
17+
paint.color = color
2318

2419
val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
2520
val path = Path().apply { addRoundRect(rect, radius, radius, Path.Direction.CW) }

android/app/src/main/kotlin/com/msob7y/namida/glance/NamidaWidgetColors.kt

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.msob7y.namida.glance
22

3+
import android.graphics.Bitmap
34
import androidx.compose.ui.graphics.Color
5+
import androidx.compose.ui.graphics.toArgb
6+
import androidx.core.graphics.ColorUtils
47

58
sealed class NamidaWidgetColors(
69
val boxColor: Color,
@@ -11,11 +14,29 @@ sealed class NamidaWidgetColors(
1114
) {
1215

1316
companion object {
14-
val light = NamidaWidgetColors.buildLight()
15-
val dark = NamidaWidgetColors.buildDark()
17+
private val light = BuildLight()
18+
private val dark = BuildDark()
19+
20+
fun getDefault(isDark: Boolean): NamidaWidgetColors {
21+
return if (isDark) dark else light
22+
}
23+
24+
fun buildColors(mainColor: Int?, isDark: Boolean): NamidaWidgetColors {
25+
return if (isDark) {
26+
if (mainColor == null) dark else BuildDarkColors(mainColor)
27+
} else {
28+
if (mainColor == null) light else BuildLightColors(mainColor)
29+
}
30+
}
31+
32+
33+
fun mixColors(color1: Int?, color2: Int, ratio: Float = 0.2f): Int {
34+
if (color1 == null) return color2
35+
return ColorUtils.blendARGB(color1, color2, ratio)
36+
}
1637
}
1738

18-
class buildLight() :
39+
class BuildLight() :
1940
NamidaWidgetColors(
2041
Color.getPlainColor(240, 255),
2142
Color.getPlainColor(190),
@@ -24,7 +45,7 @@ sealed class NamidaWidgetColors(
2445
Color.getPlainColor(0, 160),
2546
)
2647

27-
class buildDark() :
48+
class BuildDark() :
2849
NamidaWidgetColors(
2950
Color.getPlainColor(16, 255),
3051
Color.getPlainColor(120),
@@ -33,6 +54,23 @@ sealed class NamidaWidgetColors(
3354
Color.getPlainColor(200),
3455
)
3556

57+
class BuildLightColors(mainColor: Int) :
58+
NamidaWidgetColors(
59+
Color.getPlainColor(mixColors(mainColor, light.boxColor.toArgb(), 0.8f), 255),
60+
Color.getPlainColor(mixColors(mainColor, light.imageColor.toArgb(), 0.8f)),
61+
Color.getPlainColor(mixColors(mainColor, light.titleColor.toArgb()), 180),
62+
Color.getPlainColor(mixColors(mainColor, light.subtitleColor.toArgb()), 160),
63+
Color.getPlainColor(mixColors(mainColor, light.iconsColor.toArgb()), 160),
64+
)
65+
66+
class BuildDarkColors(mainColor: Int) :
67+
NamidaWidgetColors(
68+
Color.getPlainColor(mixColors(mainColor, dark.boxColor.toArgb(), 0.8f), 255),
69+
Color.getPlainColor(mixColors(mainColor, dark.imageColor.toArgb(), 0.8f)),
70+
Color.getPlainColor(mixColors(mainColor, dark.titleColor.toArgb())),
71+
Color.getPlainColor(mixColors(mainColor, dark.subtitleColor.toArgb())),
72+
Color.getPlainColor(mixColors(mainColor, dark.iconsColor.toArgb())),
73+
)
3674

3775
}
3876

android/app/src/main/kotlin/com/msob7y/namida/glance/SchwarzSechsPrototypeMkII.kt

+34-23
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import android.graphics.BitmapFactory
1111
import android.net.Uri
1212
import android.view.KeyEvent
1313
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.remember
1415
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.graphics.toArgb
1517
import androidx.compose.ui.unit.Dp
1618
import androidx.compose.ui.unit.dp
1719
import androidx.compose.ui.unit.sp
1820
import androidx.glance.GlanceId
1921
import androidx.glance.GlanceModifier
2022
import androidx.glance.Image
21-
import androidx.glance.ImageProvider
2223
import androidx.glance.action.ActionParameters
2324
import androidx.glance.action.actionParametersOf
2425
import androidx.glance.action.clickable
@@ -58,7 +59,7 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
5859
}
5960

6061
private var _latestImagePath: String? = null
61-
private var _currentImageProvider: Bitmap? = null
62+
private var _currentImageProviderWrapper: ImageProviderWrapper? = null
6263
private var _fallbackImageProvider: Bitmap? = null
6364

6465
@Composable
@@ -72,48 +73,53 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
7273
val isFav = data.getBoolean("favourite", false)
7374
val imagePath = data.getString("image", null)
7475

75-
val colors =
76-
if (isDarkModeEnabled(context)) NamidaWidgetColors.dark else NamidaWidgetColors.light
77-
78-
val boxColor = colors.boxColor
79-
val imageColor = colors.imageColor
80-
val titleColor = colors.titleColor
81-
val subtitleColor = colors.subtitleColor
82-
val iconsColor = colors.iconsColor
83-
8476
val imageSize = 84.dp
8577
val imageCornerRadiusFloat = 64f
8678

8779
if (imagePath == null) {
88-
_currentImageProvider = null
80+
_currentImageProviderWrapper = null
8981
} else {
90-
if (_currentImageProvider == null ||
82+
if (_currentImageProviderWrapper == null ||
9183
_latestImagePath != imagePath ||
9284
data.getBoolean("evict", false)
9385
) {
86+
_currentImageProviderWrapper?.bitmap?.recycle()
9487
try {
9588
val bitmap = BitmapFactory.decodeFile(imagePath)
9689
val roundedBitmap = bitmap.toRoundedBitmap(imageCornerRadiusFloat)
97-
_currentImageProvider = roundedBitmap
90+
// TODO: provide good color
91+
_currentImageProviderWrapper = ImageProviderWrapper(roundedBitmap, null)
9892
} catch (_: Exception) {
99-
_currentImageProvider = null
93+
_currentImageProviderWrapper = null
10094
}
10195
}
10296
}
103-
if (_currentImageProvider == null) {
97+
98+
val isDark = isDarkModeEnabled(context)
99+
val mainColor = _currentImageProviderWrapper?.mainColor;
100+
val colors: NamidaWidgetColors = remember(mainColor, isDark) { NamidaWidgetColors.buildColors(mainColor, isDark) }
101+
102+
val boxColor = colors.boxColor
103+
val imageColor = colors.imageColor
104+
val titleColor = colors.titleColor
105+
val subtitleColor = colors.subtitleColor
106+
val iconsColor = colors.iconsColor
107+
108+
if (_currentImageProviderWrapper == null) {
104109
if (_fallbackImageProvider == null) {
105110
val imageSizeInt = imageSize.toPxInt(context)
106111
val bitmap =
107112
ImageWrapper.createRoundedBitmap(
108113
imageSizeInt * 2,
109114
imageSizeInt * 2,
110115
imageCornerRadiusFloat,
111-
imageColor
116+
imageColor.toArgb()
112117
)
113118
_fallbackImageProvider = bitmap
114119
}
115120
}
116121

122+
val finalBitmap = _currentImageProviderWrapper?.bitmap ?: _fallbackImageProvider!!
117123
_latestImagePath = imagePath
118124

119125

@@ -131,12 +137,14 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
131137
) {
132138

133139
Image(
134-
androidx.glance.ImageProvider(_currentImageProvider ?: _fallbackImageProvider!!),
140+
androidx.glance.ImageProvider(
141+
finalBitmap
142+
),
135143
null,
136144
modifier = GlanceModifier.fillMaxHeight().size(imageSize)
137145
)
138146

139-
horizontalSpace(12)
147+
HorizontalSpace(12)
140148

141149
Column(
142150
verticalAlignment = Alignment.Vertical.CenterVertically,
@@ -195,15 +203,15 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
195203
additionalModifier = additionalModifier
196204
)
197205

198-
horizontalSpace(4)
206+
HorizontalSpace(4)
199207
MediaControlButton(
200208
color = iconsColor,
201209
drawableRes = com.ryanheise.audioservice.R.drawable.audio_service_skip_previous,
202210
contentDescription = "Previous",
203211
keyEvent = KeyEvent.KEYCODE_MEDIA_PREVIOUS,
204212
additionalModifier = additionalModifier
205213
)
206-
horizontalSpace(4)
214+
HorizontalSpace(4)
207215
if (isPlaying)
208216
MediaControlButton(
209217
color = iconsColor,
@@ -221,7 +229,7 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
221229
keyEvent = KeyEvent.KEYCODE_MEDIA_PLAY,
222230
additionalModifier = additionalModifier
223231
)
224-
horizontalSpace(4)
232+
HorizontalSpace(4)
225233
MediaControlButton(
226234
color = iconsColor,
227235
drawableRes = com.ryanheise.audioservice.R.drawable.audio_service_skip_next,
@@ -235,6 +243,7 @@ class SchwarzSechsPrototypeMkII : GlanceAppWidget() {
235243
}
236244
}
237245
}
246+
238247
}
239248
}
240249

@@ -305,6 +314,8 @@ class MediaButtonAction : ActionCallback {
305314
}
306315
}
307316

317+
private class ImageProviderWrapper(val bitmap: Bitmap, val mainColor: Int?) { }
318+
308319
inline fun <reified T : Activity> _startCustomActivity(
309320
context: Context,
310321
uri: Uri? = null,
@@ -322,7 +333,7 @@ inline fun <reified T : Activity> _startCustomActivity(
322333
}
323334

324335
@Composable
325-
private fun horizontalSpace(width: Int): Unit {
336+
private fun HorizontalSpace(width: Int): Unit {
326337
return Spacer(GlanceModifier.width(width.dp))
327338
}
328339

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: namida
22
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
33
publish_to: "none"
4-
version: 5.0.31-beta+250302211
4+
version: 5.0.32-beta+250302411
55

66
environment:
77
sdk: ">=3.6.0 <4.0.0"

0 commit comments

Comments
 (0)