Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Jul 31, 2024
1 parent e0b15f8 commit f3ad682
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
67 changes: 32 additions & 35 deletions AnkiDroid/src/main/java/com/ichi2/widget/DeckPickerWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ data class DeckPickerWidgetData(

/**
* AnalyticsWidgetProvider class for Deck Picker Widget that integrates
* * with UsageAnalytics to send analytics events when the widget is enabled, disabled,
* * or updated..
* with UsageAnalytics to send analytics events when the widget is enabled, disabled,
* or updated..
* This widget displays a list of decks with their respective new, learning, and review card counts.
* It updates every minute .
* It can be resized vertically & horizontally.
Expand Down Expand Up @@ -98,23 +98,6 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
}
}

/**
* Retrieves the selected deck IDs from shared preferences for the widget.
*
* @param context the context of the application
* @param appWidgetId the ID of the widget
* @return the array of selected deck IDs
*/
private fun getSelectedDeckIdsFromPreferences(context: Context, appWidgetId: Int): LongArray {
val sharedPreferences = context.getSharedPreferences("DeckPickerWidgetPrefs", Context.MODE_PRIVATE)
val selectedDecksString = sharedPreferences.getString("deck_picker_widget_selected_decks_$appWidgetId", "")
return if (!selectedDecksString.isNullOrEmpty()) {
selectedDecksString.split(",").map { it.toLong() }.toLongArray()
} else {
longArrayOf()
}
}

/**
* Sets a recurring alarm to update the widget every minute.
*
Expand All @@ -129,21 +112,29 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
}
val pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_NO_CREATE or PendingIntent.FLAG_IMMUTABLE)

if (pendingIntent == null) {
Timber.d("PendingIntent is null, creating a new one for widget ID: $appWidgetId")
val newPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
/**
* When onUpdate is called, the code checks if an existing alarm PendingIntent
* is already set for the widget .If an Alarm Already Exists: PendingIntent.getBroadcast
* returns the existing PendingIntent, and pendingIntent is not null.
* The if block is skipped, and no new alarm is set.
*/

// Set alarm to trigger every minute
val ONE_MINUTE_MILLIS = 60.seconds.inWholeMilliseconds
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + ONE_MINUTE_MILLIS,
ONE_MINUTE_MILLIS,
newPendingIntent
)
} else {
Timber.d("PendingIntent already exists for widget ID: $appWidgetId")
if (pendingIntent != null) {
Timber.v("Recurring alarm PendingIntent already exists for widget ID: $appWidgetId")
return
}

Timber.v("Creating a new recurring alarm PendingIntent for widget ID: $appWidgetId")
val newPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

// Set alarm to trigger every minute
val ONE_MINUTE_MILLIS = 60.seconds.inWholeMilliseconds
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + ONE_MINUTE_MILLIS,
ONE_MINUTE_MILLIS,
newPendingIntent
)
}

/**
Expand Down Expand Up @@ -172,8 +163,10 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Timber.d("onUpdate")

val widgetPreferences = WidgetPreferences(context)

for (widgetId in appWidgetIds) {
val selectedDeckIds = getSelectedDeckIdsFromPreferences(context, widgetId)
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(widgetId)
if (selectedDeckIds.isNotEmpty()) {
updateWidget(context, appWidgetManager, intArrayOf(widgetId), selectedDeckIds)
}
Expand All @@ -188,6 +181,8 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
}
super.onReceive(context, intent)

val widgetPreferences = WidgetPreferences(context)

when (intent.action) {
ACTION_APPWIDGET_UPDATE -> {
val appWidgetManager = AppWidgetManager.getInstance(context)
Expand All @@ -202,7 +197,7 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
val selectedDeckIds = getSelectedDeckIdsFromPreferences(context, appWidgetId)
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(appWidgetId)
if (selectedDeckIds.isNotEmpty()) {
updateWidget(context, appWidgetManager, intArrayOf(appWidgetId), selectedDeckIds)
}
Expand All @@ -225,8 +220,10 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
}

override fun performUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, usageAnalytics: UsageAnalytics) {
val widgetPreferences = WidgetPreferences(context)

for (widgetId in appWidgetIds) {
val selectedDeckIds = getSelectedDeckIdsFromPreferences(context, widgetId)
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(widgetId)
if (selectedDeckIds.isNotEmpty()) {
updateWidget(context, appWidgetManager, intArrayOf(widgetId), selectedDeckIds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class DeckPickerWidgetConfig : FragmentActivity(), DeckSelectionListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setResult(RESULT_CANCELED)
setContentView(R.layout.widget_deck_picker_config)

DeckPickerWidgetPreferences = WidgetPreferences(this)
Expand All @@ -65,7 +64,7 @@ class DeckPickerWidgetConfig : FragmentActivity(), DeckSelectionListener {
) ?: AppWidgetManager.INVALID_APPWIDGET_ID

if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
Timber.tag("DeckPickerWidgetConfig").w("Invalid App Widget ID")
Timber.v("Invalid App Widget ID")
finish()
return
}
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/res/layout/widget_item_deck_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:padding="10dp"
android:theme="@style/Theme.Material3.DynamicColors.DayNight">
Expand All @@ -12,7 +13,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="10dp"
android:text="@string/widget_config_screen_deck_name"
tools:text="Deck Name"
android:textColor="?android:attr/textColorPrimary"
android:textSize="18sp" />

Expand Down
17 changes: 8 additions & 9 deletions AnkiDroid/src/main/res/values/08-widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@
<string name="deck_picker_widget_description">Deck Picker</string>

<!-- Strings to explain out usage in Deck Picker and Card Analysis Extra Widget Configuration screen -->
<string name="select_deck_title">Mark as Favorite</string>
<string name="no_favourite_deck_placeholder_title">No decks are marked as favorites</string>
<string name="no_favourite_deck_placeholder_description">Start marking decks as favorites using the + icon.</string>
<string name="deck_limit_reached">Only 5 decks can be marked favourite.</string>
<string name="widget_config_screen_deck_name">Deck Name</string>
<string name="select_deck_title" comment="Title for Deck Selection Dialog">Mark as Favorite</string>
<string name="no_favourite_deck_placeholder_title" comment="Placeholder title when no decks are marked as favorites">No decks are marked as favorites</string>
<string name="no_favourite_deck_placeholder_description" comment="Description for starting to mark decks as favorites">Start marking decks as favorites using the + icon.</string>
<string name="deck_limit_reached" comment="Message when the deck limit is reached">Only 5 decks can be marked favorite.</string>

<!-- sample deck names and properties for Deck Picker Widget layout in widget picker screen -->
<string name="deck1Name_deck_picker_widget" >Chemistry</string>
<string name="deck2Name_deck_picker_widget">Physics</string>
<string name="deck3Name_deck_picker_widget">Math</string>
<!-- Sample deck names and properties for Deck Picker Widget layout in widget picker screen -->
<string name="deck1Name_deck_picker_widget" comment="Sample deck name for Deck 1 for Deck Picker Widget layout in widget picker">Chemistry</string>
<string name="deck2Name_deck_picker_widget" comment="Sample deck name for Deck 2 for Deck Picker Widget layout in widget picker">Physics</string>
<string name="deck3Name_deck_picker_widget" comment="Sample deck name for Deck 3 for Deck Picker Widget layout in widget picker">Math</string>

</resources>

0 comments on commit f3ad682

Please sign in to comment.