Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Aug 16, 2024
1 parent 3624ff2 commit 6ef8ab4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
56 changes: 30 additions & 26 deletions AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.ichi2.widget
import android.app.AlarmManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.SystemClock
Expand All @@ -34,7 +35,6 @@ import com.ichi2.libanki.ChangeManager
import kotlinx.coroutines.launch
import timber.log.Timber
import kotlin.time.Duration.Companion.seconds

typealias DeckId = Long
typealias AppWidgetId = Int

Expand Down Expand Up @@ -261,9 +261,6 @@ class DeckPickerWidget : AnalyticsWidgetProvider(), ChangeManager.Subscriber {
}
setRecurringAlarm(context, widgetId)
}

// Subscribe to changes
ChangeManager.subscribe(this)
}

override fun onReceive(context: Context?, intent: Intent?) {
Expand Down Expand Up @@ -315,39 +312,49 @@ class DeckPickerWidget : AnalyticsWidgetProvider(), ChangeManager.Subscriber {
}
}
}
AppWidgetManager.ACTION_APPWIDGET_DELETED -> {
val appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID
)
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
cancelRecurringAlarm(context, appWidgetId)
widgetPreferences.deleteDeckPickerWidgetData(appWidgetId)
}
}
else -> {
Timber.e("Unexpected action received: ${intent.action}")
CrashReportService.sendExceptionReport("Unexpected action received", "DeckPickerWidget - onReceive")
}
}
}

/**
* Updates the `DeckPickerWidget` when changes in the Anki collection affect deck data.
*
* This method is triggered by the `ChangeManager` when operations impact study queues, such as adding or
* removing cards from decks. It updates all relevant widgets with the latest deck information.
*
* @param changes The `OpChanges` object containing details of the operations performed.
* @param handler The object that executed the operation. Updates are ignored if the handler is the current instance.
*/
override fun opExecuted(changes: OpChanges, handler: Any?) {
if (changes.studyQueues && handler !== this) {
val context = AnkiDroidApp.instance.applicationContext
val appWidgetManager = AppWidgetManager.getInstance(context)
val widgetPreferences = WidgetPreferences(context)
// Handle changes and update the widget
updateWidgetFromChanges()
}

val widgetIds = widgetPreferences.getAllWidgetIds()
private fun updateWidgetFromChanges() {
val context = AnkiDroidApp.instance.applicationContext
val appWidgetManager = AppWidgetManager.getInstance(context)

for (widgetId in widgetIds) {
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(widgetId)
if (selectedDeckIds.isNotEmpty()) {
updateWidget(context, appWidgetManager, widgetId, selectedDeckIds)
}
// Get the list of widget IDs and update each widget
val widgetIds = appWidgetManager.getAppWidgetIds(
ComponentName(context, DeckPickerWidget::class.java)
)

for (widgetId in widgetIds) {
val selectedDeckIds = WidgetPreferences(context).getSelectedDeckIdsFromPreferencesDeckPickerWidget(widgetId)
if (selectedDeckIds.isNotEmpty()) {
updateWidget(context, appWidgetManager, widgetId, selectedDeckIds)
}
}
}

init {
ChangeManager.subscribe(this)
}

override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
if (context == null) {
Timber.e("Context is null in onDeleted")
Expand All @@ -360,9 +367,6 @@ class DeckPickerWidget : AnalyticsWidgetProvider(), ChangeManager.Subscriber {
cancelRecurringAlarm(context, widgetId)
widgetPreferences.deleteDeckPickerWidgetData(widgetId)
}

// Unsubscribe from changes
ChangeManager.clearSubscribers()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener {
}

// Method to initialize UI components
private fun initializeUIComponents() {
fun initializeUIComponents() {
deckAdapter = WidgetConfigScreenAdapter { deck, position ->
deckAdapter.removeDeck(deck.deckId)
showSnackbar(R.string.deck_removed_from_widget)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)

widgetPreferences = WidgetPreferences(ApplicationProvider.getApplicationContext())

// Ensure deckAdapter is initialized
activity.initializeUIComponents()
}

/**
Expand Down

0 comments on commit 6ef8ab4

Please sign in to comment.