-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget): impl widget update procedure.
- Loading branch information
Showing
13 changed files
with
185 additions
and
53 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
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
61 changes: 61 additions & 0 deletions
61
app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdateWorker.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,61 @@ | ||
package com.zjutjh.ijh.work | ||
|
||
import android.content.Context | ||
import androidx.glance.appwidget.updateAll | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.WorkerParameters | ||
import com.zjutjh.ijh.exception.UnauthorizedException | ||
import com.zjutjh.ijh.widget.ScheduleWidget | ||
import com.zjutjh.ijh.widget.ScheduleWidgetReceiver | ||
import dagger.hilt.android.EntryPointAccessors | ||
|
||
class ScheduleWidgetUpdateWorker( | ||
private val context: Context, | ||
workerParameters: WorkerParameters | ||
) : | ||
CoroutineWorker(context, workerParameters) { | ||
companion object { | ||
// Periodic work and one time work should have different unique names. | ||
const val UNIQUE_NAME = "ScheduleWidgetUpdater" | ||
const val PERIODIC_UNIQUE_NAME = "ScheduleWidgetPeriodicUpdater" | ||
|
||
fun enqueue(context: Context, force: Boolean = false) { | ||
val manager = androidx.work.WorkManager.getInstance(context) | ||
val request = androidx.work.OneTimeWorkRequestBuilder<ScheduleWidgetUpdateWorker>() | ||
.build() | ||
|
||
var policy = ExistingWorkPolicy.KEEP | ||
|
||
if (force) { | ||
policy = ExistingWorkPolicy.REPLACE | ||
} | ||
|
||
manager.enqueueUniqueWork( | ||
UNIQUE_NAME, | ||
policy, | ||
request | ||
) | ||
} | ||
} | ||
|
||
private val entryPoint = | ||
EntryPointAccessors.fromApplication<ScheduleWidgetReceiver.Repositories>(context) | ||
|
||
override suspend fun doWork(): Result { | ||
return try { | ||
val info = entryPoint.weJhInfoRepository.sync() | ||
entryPoint.courseRepository.sync(info.first, info.second) | ||
|
||
// Notify all existing widgets to update | ||
ScheduleWidget().updateAll(context) | ||
|
||
Result.success() | ||
} catch (e: UnauthorizedException) { | ||
// Not logged in, stop the worker | ||
Result.failure() | ||
} catch (_: Exception) { | ||
Result.retry() | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.