-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
431 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
android/src/main/kotlin/es/antonborri/home_widget/HomeWidgetGlanceState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Environment | ||
import androidx.datastore.core.DataStore | ||
import androidx.glance.state.GlanceStateDefinition | ||
import es.antonborri.home_widget.HomeWidgetPlugin | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import java.io.File | ||
|
||
class HomeWidgetGlanceState(val preferences: SharedPreferences) | ||
|
||
class HomeWidgetGlanceStateDefinition : GlanceStateDefinition<HomeWidgetGlanceState> { | ||
override suspend fun getDataStore(context: Context, fileKey: String): DataStore<HomeWidgetGlanceState> { | ||
val preferences = context.getSharedPreferences(HomeWidgetPlugin.PREFERENCES, Context.MODE_PRIVATE) | ||
return HomeWidgetGlanceDataStore(preferences) | ||
} | ||
|
||
override fun getLocation(context: Context, fileKey: String): File { | ||
return Environment.getDataDirectory() | ||
} | ||
|
||
} | ||
|
||
private class HomeWidgetGlanceDataStore(private val preferences: SharedPreferences) : DataStore<HomeWidgetGlanceState> { | ||
override val data: Flow<HomeWidgetGlanceState> | ||
get() = flow { emit(HomeWidgetGlanceState(preferences)) } | ||
|
||
override suspend fun updateData(transform: suspend (t: HomeWidgetGlanceState) -> HomeWidgetGlanceState): HomeWidgetGlanceState { | ||
return transform(HomeWidgetGlanceState(preferences)) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
android/src/main/kotlin/es/antonborri/home_widget/HomeWidgetGlanceWidgetReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import android.appwidget.AppWidgetManager | ||
import android.content.Context | ||
import androidx.glance.appwidget.GlanceAppWidget | ||
import androidx.glance.appwidget.GlanceAppWidgetManager | ||
import androidx.glance.appwidget.GlanceAppWidgetReceiver | ||
import androidx.glance.appwidget.state.updateAppWidgetState | ||
import kotlinx.coroutines.runBlocking | ||
|
||
abstract class HomeWidgetGlanceWidgetReceiver<T : GlanceAppWidget> : GlanceAppWidgetReceiver() { | ||
|
||
abstract override val glanceAppWidget: T | ||
|
||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { | ||
super.onUpdate(context, appWidgetManager, appWidgetIds) | ||
runBlocking { | ||
appWidgetIds.forEach { | ||
val glanceId = GlanceAppWidgetManager(context).getGlanceIdBy(it) | ||
glanceAppWidget.apply { | ||
if (this.stateDefinition is HomeWidgetGlanceStateDefinition) { | ||
// Must Update State | ||
updateAppWidgetState<HomeWidgetGlanceState>(context = context, this.stateDefinition as HomeWidgetGlanceStateDefinition, glanceId) { currentState -> currentState } | ||
} | ||
// Update widget. | ||
update(context, glanceId) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.