From 451935b5204d5d9f38b8db5b8153f85d9b4d2631 Mon Sep 17 00:00:00 2001 From: Anoop Date: Thu, 3 Oct 2024 01:24:12 +0530 Subject: [PATCH] Refactored; Changed the empty collection string. --- .../widget/deckpicker/DeckPickerWidget.kt | 77 +++++++++++-------- .../res/layout/widget_deck_picker_large.xml | 2 +- AnkiDroid/src/main/res/values/08-widget.xml | 3 +- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidget.kt b/AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidget.kt index 95ab3de3ec21..43c739eedff2 100644 --- a/AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidget.kt +++ b/AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidget.kt @@ -124,44 +124,41 @@ class DeckPickerWidget : AnalyticsWidgetProvider() { appWidgetManager: AppWidgetManager, appWidgetId: AppWidgetId, remoteViews: RemoteViews, - deckIds: LongArray? + deckIds: LongArray ) { remoteViews.removeAllViews(R.id.deckCollection) - - val deckData = deckIds?.let { getDeckNamesAndStats(it.toList()) } - if (deckData != null) { - for (deck in deckData) { - val deckView = RemoteViews(context.packageName, R.layout.widget_item_deck_main) - - remoteViews.setViewVisibility(R.id.empty_widget, View.GONE) - remoteViews.setViewVisibility(R.id.deckCollection, View.VISIBLE) - deckView.setTextViewText(R.id.deckName, deck.name) - deckView.setTextViewText(R.id.deckNew, deck.newCount.toString()) - deckView.setTextViewText(R.id.deckDue, deck.reviewCount.toString()) - deckView.setTextViewText(R.id.deckLearn, deck.learnCount.toString()) - - val isEmptyDeck = deck.newCount == 0 && deck.reviewCount == 0 && deck.learnCount == 0 - - val intent = if (!isEmptyDeck) { - Intent(context, Reviewer::class.java).apply { - action = Intent.ACTION_VIEW - putExtra("deckId", deck.deckId) - addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - } - } else { - DeckOptions.getIntent(context, deck.deckId) + val deckData = getDeckNamesAndStats(deckIds.toList()) + for (deck in deckData) { + val deckView = RemoteViews(context.packageName, R.layout.widget_item_deck_main) + + remoteViews.setViewVisibility(R.id.empty_widget, View.GONE) + remoteViews.setViewVisibility(R.id.deckCollection, View.VISIBLE) + deckView.setTextViewText(R.id.deckName, deck.name) + deckView.setTextViewText(R.id.deckNew, deck.newCount.toString()) + deckView.setTextViewText(R.id.deckDue, deck.reviewCount.toString()) + deckView.setTextViewText(R.id.deckLearn, deck.learnCount.toString()) + + val isEmptyDeck = deck.newCount == 0 && deck.reviewCount == 0 && deck.learnCount == 0 + + val intent = if (!isEmptyDeck) { + Intent(context, Reviewer::class.java).apply { + action = Intent.ACTION_VIEW + putExtra("deckId", deck.deckId) + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) } + } else { + DeckOptions.getIntent(context, deck.deckId) + } - val pendingIntent = PendingIntent.getActivity( - context, - deck.deckId.toInt(), - intent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - ) + val pendingIntent = PendingIntent.getActivity( + context, + deck.deckId.toInt(), + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + ) - deckView.setOnClickPendingIntent(R.id.deckName, pendingIntent) - remoteViews.addView(R.id.deckCollection, deckView) - } + deckView.setOnClickPendingIntent(R.id.deckName, pendingIntent) + remoteViews.addView(R.id.deckCollection, deckView) } appWidgetManager.updateAppWidget(appWidgetId, remoteViews) @@ -173,10 +170,22 @@ class DeckPickerWidget : AnalyticsWidgetProvider() { appWidgetId: AppWidgetId, remoteViews: RemoteViews ) { - remoteViews.setTextViewText(R.id.empty_widget, context.getString(R.string.app_not_initialized_new)) + remoteViews.setTextViewText(R.id.empty_widget, context.getString(R.string.empty_collection)) remoteViews.setViewVisibility(R.id.empty_widget, View.VISIBLE) remoteViews.setViewVisibility(R.id.deckCollection, View.GONE) + val configIntent = Intent(context, DeckPickerWidgetConfig::class.java).apply { + putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + val configPendingIntent = PendingIntent.getActivity( + context, + appWidgetId, + configIntent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + ) + remoteViews.setOnClickPendingIntent(R.id.empty_widget, configPendingIntent) + appWidgetManager.updateAppWidget(appWidgetId, remoteViews) } diff --git a/AnkiDroid/src/main/res/layout/widget_deck_picker_large.xml b/AnkiDroid/src/main/res/layout/widget_deck_picker_large.xml index ab8c98c5b3ed..0b62ae77c85c 100644 --- a/AnkiDroid/src/main/res/layout/widget_deck_picker_large.xml +++ b/AnkiDroid/src/main/res/layout/widget_deck_picker_large.xml @@ -13,7 +13,7 @@ style="@style/TextAppearance.AppCompat.Medium" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginStart="7dp" + android:layout_marginStart="8dp" android:gravity="center" android:padding="20dp" android:text="@string/empty_widget" diff --git a/AnkiDroid/src/main/res/values/08-widget.xml b/AnkiDroid/src/main/res/values/08-widget.xml index 873cdb9c9838..2f521d9c5745 100644 --- a/AnkiDroid/src/main/res/values/08-widget.xml +++ b/AnkiDroid/src/main/res/values/08-widget.xml @@ -51,6 +51,7 @@ You can select up to %d deck. You can select up to %d decks. - Missing deck. Please reconfigure + Missing deck. Click to reconfigure + Collection is empty, use app to add decks then reconfigure"