Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jetcaster] Add round widget #1406

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Jetcaster/glancewidget/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
android:name="android.appwidget.provider"
android:resource="@xml/jetcaster_info" />
</receiver>
<receiver
android:name="com.example.jetcaster.glancewidget.RoundAppWidgetReceiver"
android:enabled="true"
android:label="@string/app_widget_description"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/round_jetcaster_info" />
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.example.jetcaster.glancewidget

import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.glance.GlanceModifier
import androidx.glance.Image
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.appwidget.components.SquareIconButton
import androidx.glance.layout.ContentScale
import coil.ImageLoader
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Uses Coil to load images.
*/
@Composable
internal fun WidgetAsyncImage(
uri: Uri,
circleCrop: Boolean = false,
contentDescription: String?,
modifier: GlanceModifier = GlanceModifier,
) {
val placeholderProvider = remember { ImageProvider(R.drawable.empty_round) }
var imageProvider: ImageProvider by remember { mutableStateOf(placeholderProvider) }
val context = LocalContext.current
val scope = rememberCoroutineScope()
val sizePx: Int = if (circleCrop) 400 else 200

LaunchedEffect(key1 = uri) {

val transformations = if (circleCrop) {
listOf(CircleCropTransformation())
} else {
listOf()
}

val backup = com.example.jetcaster.core.designsystem.R.drawable.img_empty
val request = ImageRequest.Builder(context)
.data(uri)
.placeholder(backup)
.fallback(backup)
.size(sizePx, sizePx)
.target(
onStart = { _ ->
imageProvider = placeholderProvider
},
onError = { data: Drawable? ->
imageProvider = placeholderProvider
},
onSuccess = { data: Drawable ->
val bitmap = (data as BitmapDrawable).bitmap
imageProvider = ImageProvider(bitmap)
}
)
.transformations(transformations)
.build()

scope.launch(Dispatchers.IO) {
val result = ImageLoader(context).execute(request)
if (result is ErrorResult) {
val t = result.throwable
Log.e(TAG, "Image request error:", t)
}
}
}

Image(
provider = imageProvider,
contentDescription = contentDescription,
contentScale = if (circleCrop) ContentScale.Fit else ContentScale.FillBounds,
modifier = modifier
)
}

internal enum class PlayPauseIcon {
Play,
Pause;

companion object {
fun from(isPlaying: Boolean) = if (isPlaying) Pause else Play
}
}

@Composable
internal fun PlayPauseButton(
state: PlayPauseIcon,
onClick: () -> Unit,
modifier: GlanceModifier = GlanceModifier
) {
val (iconRes: Int, description: Int) = when (state) {
PlayPauseIcon.Play -> R.drawable.outline_play_arrow_24 to R.string.content_description_play
PlayPauseIcon.Pause -> R.drawable.outline_pause_24 to R.string.content_description_pause
}

val provider = ImageProvider(iconRes)
val contentDescription = LocalContext.current.getString(description)

SquareIconButton(
provider,
contentDescription = contentDescription,
onClick = onClick,
modifier = modifier
)
}

data class JetcasterAppWidgetViewState(
val episodeTitle: String,
val podcastTitle: String,
val isPlaying: Boolean,
val albumArtUri: String,
val useDynamicColor: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,28 @@
package com.example.jetcaster.glancewidget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Log
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.Image
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.components.Scaffold
import androidx.glance.appwidget.components.SquareIconButton
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.layout.Alignment
import androidx.glance.layout.Box
import androidx.glance.layout.Column
import androidx.glance.layout.ContentScale
import androidx.glance.layout.Row
import androidx.glance.layout.Spacer
import androidx.glance.layout.fillMaxSize
Expand All @@ -64,11 +49,6 @@ import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import coil.ImageLoader
import coil.request.ErrorResult
import coil.request.ImageRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

internal val TAG = "JetcasterAppWidegt"

Expand All @@ -80,14 +60,6 @@ class JetcasterAppWidgetReceiver : GlanceAppWidgetReceiver() {
get() = JetcasterAppWidget()
}

data class JetcasterAppWidgetViewState(
val episodeTitle: String,
val podcastTitle: String,
val isPlaying: Boolean,
val albumArtUri: String,
val useDynamicColor: Boolean
)

private object Sizes {
val minWidth = 140.dp
val smallBucketCutoffWidth = 250.dp // anything from minWidth to this will have no title
Expand Down Expand Up @@ -116,20 +88,12 @@ class JetcasterAppWidget : GlanceAppWidget() {

override suspend fun provideGlance(context: Context, id: GlanceId) {

val testState = JetcasterAppWidgetViewState(
episodeTitle =
"100 - Android 15 DP 1, Stable Studio Iguana, Cloud Photo Picker, and more!",
podcastTitle = "Now in Android",
isPlaying = false,
albumArtUri = "https://static.libsyn.com/p/assets/9/f/f/3/" +
"9ff3cb5dc6cfb3e2e5bbc093207a2619/NIA000_PodcastThumbnail.png",
useDynamicColor = false
)
val state = TEST_DATA

provideContent {
val sizeBucket = calculateSizeBucket()
val playPauseIcon = if (testState.isPlaying) PlayPauseIcon.Pause else PlayPauseIcon.Play
val artUri = Uri.parse(testState.albumArtUri)
val playPauseIcon = if (state.isPlaying) PlayPauseIcon.Pause else PlayPauseIcon.Play
val artUri = Uri.parse(state.albumArtUri)

GlanceTheme(
colors = ColorProviders(
Expand All @@ -145,8 +109,8 @@ class JetcasterAppWidget : GlanceAppWidget() {
)

SizeBucket.Normal -> WidgetUiNormal(
title = testState.episodeTitle,
subtitle = testState.podcastTitle,
title = state.episodeTitle,
subtitle = state.podcastTitle,
imageUri = artUri,
playPauseIcon = playPauseIcon
)
Expand Down Expand Up @@ -203,7 +167,11 @@ private fun AlbumArt(
imageUri: Uri,
modifier: GlanceModifier = GlanceModifier
) {
WidgetAsyncImage(uri = imageUri, contentDescription = null, modifier = modifier)
WidgetAsyncImage(
uri = imageUri,
contentDescription = null,
modifier = modifier.cornerRadius(12.dp),
)
}

@Composable
Expand All @@ -222,63 +190,3 @@ fun PodcastText(title: String, subtitle: String, modifier: GlanceModifier = Glan
)
}
}

@Composable
private fun PlayPauseButton(state: PlayPauseIcon, onClick: () -> Unit) {
val (iconRes: Int, description: Int) = when (state) {
PlayPauseIcon.Play -> R.drawable.outline_play_arrow_24 to R.string.content_description_play
PlayPauseIcon.Pause -> R.drawable.outline_pause_24 to R.string.content_description_pause
}

val provider = ImageProvider(iconRes)
val contentDescription = LocalContext.current.getString(description)

SquareIconButton(
provider,
contentDescription = contentDescription,
onClick = onClick
)
}

enum class PlayPauseIcon { Play, Pause }

/**
* Uses Coil to load images.
*/
@Composable
private fun WidgetAsyncImage(
uri: Uri,
contentDescription: String?,
modifier: GlanceModifier = GlanceModifier
) {
var bitmap by remember { mutableStateOf<Bitmap?>(null) }
val context = LocalContext.current
val scope = rememberCoroutineScope()

LaunchedEffect(key1 = uri) {
val request = ImageRequest.Builder(context)
.data(uri)
.size(200, 200)
.target { data: Drawable ->
bitmap = (data as BitmapDrawable).bitmap
}
.build()

scope.launch(Dispatchers.IO) {
val result = ImageLoader(context).execute(request)
if (result is ErrorResult) {
val t = result.throwable
Log.e(TAG, "Image request error:", t)
}
}
}

bitmap?.let { bitmap ->
Image(
provider = ImageProvider(bitmap),
contentDescription = contentDescription,
contentScale = ContentScale.FillBounds,
modifier = modifier.cornerRadius(12.dp) // TODO: confirm radius with design
)
}
}
Loading
Loading