Skip to content

Commit

Permalink
hanlde update app widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ppupha committed Oct 30, 2024
1 parent 712df58 commit 9f91677
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 25 deletions.
9 changes: 8 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ plugins {
id "kotlinx-serialization"
id "com.google.gms.google-services"
id "dev.flutter.flutter-gradle-plugin"
id 'kotlin-kapt'

}

def localProperties = new Properties()
Expand Down Expand Up @@ -91,7 +93,8 @@ android {
keyAlias releaseProperties['key.alias']
keyPassword releaseProperties['key.alias.password']
storePassword releaseProperties['key.store.password']
storeFile rootProject.file('release.keystore') // Update this with the path to your keystore
storeFile rootProject.file('release.keystore')
// Update this with the path to your keystore
}
}

Expand Down Expand Up @@ -175,6 +178,7 @@ flutter {
}

dependencies {
implementation 'com.google.firebase:protolite-well-known-types:18.0.0'
def lifecycle_version = "2.6.0-alpha03"
def beacon_version = "c2434dcc73"

Expand Down Expand Up @@ -210,4 +214,7 @@ dependencies {
exclude group: 'com.google.protobuf'
exclude module: 'jetified-protobuf-java'
}

implementation 'com.github.bumptech.glide:glide:4.12.0'
// kapt 'com.github.bumptech.glide:compiler:4.12.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ package com.bitmark.autonomy_flutter
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.RemoteViews
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.AppWidgetTarget
import com.bumptech.glide.request.transition.Transition
import es.antonborri.home_widget.HomeWidgetPlugin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* Implementation of App Widget functionality.
Expand All @@ -13,9 +24,10 @@ class FeralfileDaily : AppWidgetProvider() {
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
getDailyInfo(context = context, date = "2023-10-31") { dailyInfo ->
updateAppWidget(context, appWidgetManager, appWidgetId, dailyInfo)
}
}
}

Expand All @@ -35,13 +47,102 @@ class FeralfileDaily : AppWidgetProvider() {
internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
appWidgetId: Int,
dailyInfo: DailyInfo // dailyInfo is guaranteed to be non-null
) {
val widgetText = context.getString(R.string.appwidget_text)
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.feralfile_daily)
views.setTextViewText(R.id.appwidget_text, widgetText)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
// Set text fields with DailyInfo
views.setTextViewText(R.id.appwidget_title, dailyInfo.title)
views.setTextViewText(R.id.appwidget_artist, dailyInfo.artistName)
views.setTextViewText(R.id.appwidget_medium, dailyInfo.medium)

// Load the thumbnail image
val thumbnailUrl = dailyInfo.thumbnailUrl
if (thumbnailUrl.isNotEmpty()) {
// Use Glide to load the image into the widget
Glide.with(context)
.asBitmap()
.load(thumbnailUrl)
.into(object : AppWidgetTarget(context, R.id.appwidget_image, views, appWidgetId) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
views.setImageViewBitmap(R.id.appwidget_image, resource)
// Update the widget with the new image
appWidgetManager.updateAppWidget(appWidgetId, views)
}

override fun onLoadFailed(errorDrawable: Drawable?) {
// Handle error, if needed, set a placeholder image
views.setImageViewResource(R.id.appwidget_image, R.drawable.failed_daily_image)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
})
} else {
// If no thumbnail URL, set a placeholder image
views.setImageViewResource(R.id.appwidget_image, R.drawable.no_thumbnail)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

data class DailyInfo(
val thumbnailUrl: String,
val title: String,
val artistName: String,
val medium: String
)


fun getDailyInfo(context: Context, date: String, callback: (DailyInfo) -> Unit) {
CoroutineScope(Dispatchers.IO).launch {
try {

// Create a DailyInfo from the API response, if available
val dailyInfo = DailyInfo(
thumbnailUrl = "https://via.placeholder.com/150",
title = "Title",
artistName = "Artist",
medium = "Medium"
)

// If dailyInfo is null, retrieve already set data from local storage
if (dailyInfo == null) {
val localDailyInfo =
getStoredDailyInfo(context = context) // Method to get previously stored data
withContext(Dispatchers.Main) {
callback(localDailyInfo)
}
} else {
// If dailyInfo is valid, return it
withContext(Dispatchers.Main) {
callback(dailyInfo)
}
}
} catch (e: Exception) {
// In case of an exception, fallback to retrieving stored data
val localDailyInfo = getStoredDailyInfo(context = context)
withContext(Dispatchers.Main) {
callback(localDailyInfo)
}
}
}
}

private fun getStoredDailyInfo(context: Context): DailyInfo {
val widgetData = HomeWidgetPlugin.getData(context)
val thumbnailUrl =
widgetData.getString("thumbnailUrl", "default_thumbnail_url") ?: "default_thumbnail_url"
val title = widgetData.getString("title", "default_title") ?: "default_title"
val artistName =
widgetData.getString("artistName", "default_artist_name") ?: "default_artist_name"
val medium = widgetData.getString("medium", "default_medium") ?: "default_medium"
// return
return DailyInfo(
thumbnailUrl = thumbnailUrl,
title = title,
artistName = artistName,
medium = medium
)
}


64 changes: 50 additions & 14 deletions android/app/src/main/res/layout/feralfile_daily.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.Android.AppWidget.Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Android.AppWidgetContainer">

<TextView
android:id="@+id/appwidget_text"
style="@style/Widget.Android.AppWidget.InnerView"
android:layout_width="wrap_content"
<ImageView
android:id="@+id/appwidget_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="12dp"
android:scaleType="centerCrop"
tools:srcCompat="@tools:sample/backgrounds/scenic[0]" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>
android:layout_below="@id/appwidget_image"
android:layout_marginTop="120dp"
android:orientation="vertical">

<TextView
android:id="@+id/appwidget_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
tools:text="@tools:sample/last_names" />

<TextView
android:id="@+id/appwidget_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/darker_gray"
android:textSize="16sp"
tools:text="@tools:sample/cities" />

<TextView
android:id="@+id/appwidget_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textColor="@android:color/darker_gray"
android:textSize="14sp"
tools:text="@tools:sample/lorem" />

</LinearLayout>

</RelativeLayout>
4 changes: 2 additions & 2 deletions android/app/src/main/res/xml/feralfile_daily_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:previewImage="@drawable/example_appwidget_preview"
android:previewLayout="@layout/feralfile_daily"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:targetCellWidth="2"
android:targetCellHeight="2"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />

0 comments on commit 9f91677

Please sign in to comment.