mirror of
https://github.com/immich-app/immich.git
synced 2025-07-09 03:04:16 -04:00
working random widget
This commit is contained in:
parent
d8f3627beb
commit
7f7a3bc6ef
@ -16,32 +16,25 @@ package app.alextran.immich.widget
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import HomeWidgetGlanceState
|
|
||||||
import android.appwidget.AppWidgetManager
|
|
||||||
import android.content.ComponentName
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.os.Handler
|
|
||||||
import android.os.Looper
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.glance.*
|
import androidx.glance.*
|
||||||
import androidx.glance.appwidget.GlanceAppWidgetManager
|
import androidx.glance.appwidget.GlanceAppWidgetManager
|
||||||
import androidx.glance.appwidget.state.updateAppWidgetState
|
import androidx.glance.appwidget.state.updateAppWidgetState
|
||||||
import androidx.glance.appwidget.updateAll
|
|
||||||
import androidx.glance.state.GlanceStateDefinition
|
|
||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import es.antonborri.home_widget.HomeWidgetPlugin
|
import es.antonborri.home_widget.HomeWidgetPlugin
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.random.Random
|
import androidx.datastore.preferences.core.longPreferencesKey
|
||||||
|
import androidx.glance.appwidget.state.getAppWidgetState
|
||||||
|
import androidx.glance.state.PreferencesGlanceStateDefinition
|
||||||
|
|
||||||
class ImageDownloadWorker(
|
class ImageDownloadWorker(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@ -106,15 +99,24 @@ class ImageDownloadWorker(
|
|||||||
val configString = inputData.getString("config")
|
val configString = inputData.getString("config")
|
||||||
val config = Gson().fromJson(configString, WidgetConfig::class.java)
|
val config = Gson().fromJson(configString, WidgetConfig::class.java)
|
||||||
val widgetId = inputData.getInt("widgetId", -1)
|
val widgetId = inputData.getInt("widgetId", -1)
|
||||||
|
val glanceId = GlanceAppWidgetManager(context).getGlanceIdBy(widgetId)
|
||||||
val serverConfig = getServerConfig() ?: return Result.success()
|
val serverConfig = getServerConfig() ?: return Result.success()
|
||||||
|
|
||||||
|
// clear current image if it exists
|
||||||
|
val state = getAppWidgetState(context, PreferencesGlanceStateDefinition, glanceId)
|
||||||
|
val currentImgUUID = state[stringPreferencesKey("uuid")]
|
||||||
|
if (currentImgUUID != null) {
|
||||||
|
deleteImage(currentImgUUID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch new image
|
||||||
val newBitmap = when (config.widgetType) {
|
val newBitmap = when (config.widgetType) {
|
||||||
WidgetType.RANDOM -> fetchRandom(serverConfig)
|
WidgetType.RANDOM -> fetchRandom(serverConfig)
|
||||||
}
|
}
|
||||||
|
val imgUUID = saveImage(newBitmap)
|
||||||
|
|
||||||
saveImage(newBitmap, widgetId)
|
// trigger the update routine with new image uuid
|
||||||
updateWidget(config, widgetId)
|
updateWidget(config, glanceId, imgUUID)
|
||||||
|
|
||||||
Result.success()
|
Result.success()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -127,13 +129,13 @@ class ImageDownloadWorker(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateWidget(config: WidgetConfig, widgetID: Int) {
|
private suspend fun updateWidget(config: WidgetConfig, glanceId: GlanceId, imageUUID: String) {
|
||||||
val manager = GlanceAppWidgetManager(context)
|
updateAppWidgetState(context, glanceId) { prefs ->
|
||||||
val glanceId = manager.getGlanceIdBy(widgetID)
|
prefs[longPreferencesKey("now")] = System.currentTimeMillis()
|
||||||
|
prefs[stringPreferencesKey("uuid")] = imageUUID
|
||||||
|
}
|
||||||
|
|
||||||
RandomWidget().update(context, glanceId)
|
RandomWidget().update(context,glanceId)
|
||||||
|
|
||||||
Log.w("WIDGET_BG", "SENT THE UPDATE COMMAND: $widgetID")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun fetchRandom(serverConfig: ServerConfig): Bitmap {
|
private suspend fun fetchRandom(serverConfig: ServerConfig): Bitmap {
|
||||||
@ -145,10 +147,18 @@ class ImageDownloadWorker(
|
|||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun saveImage(bitmap: Bitmap, widgetId: Int) = withContext(Dispatchers.IO) {
|
private suspend fun deleteImage(uuid: String) = withContext(Dispatchers.IO) {
|
||||||
val file = File(context.cacheDir, "widget_image_$widgetId.jpg")
|
val file = File(context.cacheDir, "widget_image_$uuid.jpg")
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun saveImage(bitmap: Bitmap): String = withContext(Dispatchers.IO) {
|
||||||
|
val uuid = UUID.randomUUID().toString()
|
||||||
|
val file = File(context.cacheDir, "widget_image_$uuid.jpg")
|
||||||
FileOutputStream(file).use { out ->
|
FileOutputStream(file).use { out ->
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uuid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,6 @@ fun PhotoWidget(image: Bitmap?, error: String?, subtitle: String?) {
|
|||||||
provider = ImageProvider(R.drawable.splash),
|
provider = ImageProvider(R.drawable.splash),
|
||||||
contentDescription = null,
|
contentDescription = null,
|
||||||
)
|
)
|
||||||
Text(subtitle ?: "NOPERS")
|
|
||||||
Text(error ?: "NOPERS")
|
Text(error ?: "NOPERS")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,30 +5,36 @@ import HomeWidgetGlanceStateDefinition
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.datastore.preferences.core.MutablePreferences
|
||||||
|
import androidx.datastore.preferences.core.longPreferencesKey
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.glance.appwidget.*
|
import androidx.glance.appwidget.*
|
||||||
import androidx.glance.*
|
import androidx.glance.*
|
||||||
import androidx.glance.state.GlanceStateDefinition
|
import androidx.glance.state.GlanceStateDefinition
|
||||||
|
import androidx.glance.state.PreferencesGlanceStateDefinition
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.prefs.Preferences
|
||||||
|
|
||||||
class RandomWidget : GlanceAppWidget() {
|
class RandomWidget : GlanceAppWidget() {
|
||||||
override val stateDefinition: GlanceStateDefinition<HomeWidgetGlanceState>
|
override var stateDefinition: GlanceStateDefinition<*> = PreferencesGlanceStateDefinition
|
||||||
get() = HomeWidgetGlanceStateDefinition()
|
|
||||||
|
|
||||||
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
||||||
Log.w("WIDGET_UPDATE", "PROVIDED GLANCE")
|
|
||||||
// fetch a random photo from server
|
|
||||||
val appWidgetId = GlanceAppWidgetManager(context).getAppWidgetId(id)
|
|
||||||
val file = File(context.cacheDir, "widget_image_$appWidgetId.jpg")
|
|
||||||
|
|
||||||
var bitmap: Bitmap? = null
|
|
||||||
|
|
||||||
if (file.exists()) {
|
|
||||||
bitmap = loadScaledBitmap(file, 500, 500)
|
|
||||||
}
|
|
||||||
|
|
||||||
provideContent {
|
provideContent {
|
||||||
PhotoWidget(image = bitmap, error = null, subtitle = "hello")
|
val prefs = currentState<MutablePreferences>()
|
||||||
|
val imageUUID = prefs[stringPreferencesKey("uuid")]
|
||||||
|
var bitmap: Bitmap? = null
|
||||||
|
|
||||||
|
if (imageUUID != null) {
|
||||||
|
// fetch a random photo from server
|
||||||
|
val file = File(context.cacheDir, "widget_image_$imageUUID.jpg")
|
||||||
|
|
||||||
|
if (file.exists()) {
|
||||||
|
bitmap = loadScaledBitmap(file, 500, 500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PhotoWidget(image = bitmap, error = "NOPE", subtitle = "hello")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user