Skip to content

Commit

Permalink
Refactored; Changed the empty collection string.
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w committed Oct 2, 2024
1 parent d562a59 commit 451935b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/res/layout/widget_deck_picker_large.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/res/values/08-widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<item quantity="one">You can select up to %d deck.</item>
<item quantity="other">You can select up to %d decks.</item>
</plurals>
<string name="empty_widget" comment="To denote the widget is empty">Missing deck. Please reconfigure</string>
<string name="empty_widget" comment="To denote the widget is empty">Missing deck. Click to reconfigure</string>
<string name="empty_collection" comment="To denote the collection is empty">Collection is empty, use app to add decks then reconfigure</string>"

</resources>

0 comments on commit 451935b

Please sign in to comment.