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

Refactor image loading from ContentProvider to IO thread #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@

package com.google.android.samples.socialite.ui

import android.graphics.drawable.Drawable
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.core.graphics.drawable.IconCompat
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* Creates and remembers a [Painter] from a bitmap icon specified by the [contentUri].
*/
@Composable
fun rememberIconPainter(contentUri: Uri): Painter {
val icon = IconCompat.createWithAdaptiveBitmapContentUri(contentUri)
val context = LocalContext.current
return rememberDrawablePainter(drawable = icon.loadDrawable(context))

val loadDrawable by produceState<Drawable?>(
initialValue = null,
key1 = contentUri,
) {
value = withContext(Dispatchers.IO) {
val icon = IconCompat.createWithAdaptiveBitmapContentUri(contentUri)
icon.loadDrawable(context)
}
}

return rememberDrawablePainter(drawable = loadDrawable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.samples.socialite.ui.home

import android.annotation.SuppressLint
import androidx.activity.compose.ReportDrawnWhen
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down Expand Up @@ -47,6 +48,8 @@ internal fun ChatList(
onChatClicked: (chatId: Long) -> Unit,
modifier: Modifier = Modifier,
) {
ReportDrawnWhen { chats.isNotEmpty() }

@SuppressLint("InlinedApi") // Granted at install time on API <33.
val notificationPermissionState = rememberPermissionState(
android.Manifest.permission.POST_NOTIFICATIONS,
Expand Down