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

enhance: improve workers #2

Merged
merged 6 commits into from
Oct 23, 2024
Merged
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
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name=".android.HijriWidgetApp"
android:allowBackup="true"
android:description="@string/description"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -398,10 +399,11 @@
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.amrbashir.hijriwidget.android

import android.app.Application
import me.amrbashir.hijriwidget.preferences.HijriWidgetLauncherIconWorker
import me.amrbashir.hijriwidget.widget.HijriWidgetWorker

class HijriWidgetApp : Application() {

override fun onCreate() {
super.onCreate()

HijriWidgetLauncherIconWorker.setup24Periodic(this)
HijriWidgetWorker.setup24Periodic(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ class HijriWidgetLauncherIconWorker(
params: WorkerParameters
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
changeLauncherIcon(this.applicationContext)
return Result.success()
try {
changeLauncherIcon(this.applicationContext)
return Result.success()
} catch (e: Exception) {
e.printStackTrace()
return Result.retry()
}
}

companion object {
Expand Down Expand Up @@ -73,7 +78,7 @@ class HijriWidgetLauncherIconWorker(

WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"hijriWidgetLauncherIconWorker",
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequest.Builder(
HijriWidgetLauncherIconWorker::class.java,
24,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ open class WidgetConfiguration(private val autoClose: Boolean = true) : Componen
}

override fun onDestroy() {
// TODO: find a better way to change icon when app is first installed
HijriWidgetLauncherIconWorker.changeLauncherIcon(this.baseContext)
super.onDestroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class HijriWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
Preferences.load(context)
HijriDate.load(Preferences.language.value)
HijriWidgetWorker.setup24Periodic(context)

provideContent {
GlanceTheme {
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/kotlin/me/amrbashir/hijriwidget/widget/Worker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ class HijriWidgetWorker(
params: WorkerParameters
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
HijriWidget.update(applicationContext)
return Result.success()
try {
HijriWidget.update(applicationContext)
return Result.success()
} catch (ex: Exception) {
ex.printStackTrace()
return Result.retry()
}
}


Expand All @@ -33,7 +38,7 @@ class HijriWidgetWorker(

WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"hijriWidgetWorker",
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequest.Builder(
HijriWidgetWorker::class.java,
24,
Expand Down