Skip to content

Commit

Permalink
Implemention of Deck Picker Widget !Add Deck Picker Widget to display…
Browse files Browse the repository at this point in the history
… deck statistics

This commit introduces the Deck Picker Widget, which displays a list of decks along with the number of cards that are new, in learning, and due for review. It is a display-only widget.

Features:
- Displays deck names and statistics (new, learning, and review counts).
- Retrieves selected decks from shared preferences.
- Can be reconfigured by holding the widget.

This widget provides users with a quick overview of their decks without needing to open the app.
  • Loading branch information
xenonnn4w committed Aug 22, 2024
1 parent 2aa2040 commit 958ed5c
Show file tree
Hide file tree
Showing 18 changed files with 1,672 additions and 14 deletions.
27 changes: 27 additions & 0 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,33 @@
/>
</receiver>

<!-- A widget that displays a few decks's name and number of cards to review on the Android home screen.
The way to add it depends on the phone. It usually consists in a long press on the screen, followed by finding a "widget" button"-->
<receiver
android:name="com.ichi2.widget.DeckPickerWidget"
android:label="@string/deck_picker_widget_description"
android:exported="false"
>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider_deck_picker" />
</receiver>

<!-- Configuration view for the DeckPickerWidget above.
It is opened when adding a new deck picker widget and
by configuration button which appears when the widget is hold or resized.-->
<activity
android:name="com.ichi2.widget.DeckPickerWidgetConfig"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>

<receiver android:name="com.ichi2.widget.WidgetPermissionReceiver"
android:exported="true">
<intent-filter>
Expand Down
36 changes: 23 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/snackbar/Snackbars.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ interface BaseSnackbarBuilderProvider {
fun Activity.showSnackbar(
@StringRes textResource: Int,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val text = getText(textResource)
showSnackbar(text, duration, snackbarBuilder)
showSnackbar(text, duration, anchorView, snackbarBuilder)
}

/**
Expand Down Expand Up @@ -104,13 +105,14 @@ fun Activity.showSnackbar(
fun Activity.showSnackbar(
text: CharSequence,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val view: View? = findViewById(R.id.root_layout) as? CoordinatorLayout

if (view != null) {
val baseSnackbarBuilder = (this as? BaseSnackbarBuilderProvider)?.baseSnackbarBuilder
view.showSnackbar(text, duration) {
view.showSnackbar(text, duration, anchorView = anchorView) {
baseSnackbarBuilder?.invoke(this)
snackbarBuilder?.invoke(this)
Timber.d("displayed snackbar: '%s'", text)
Expand Down Expand Up @@ -157,10 +159,11 @@ fun Activity.showSnackbar(
fun View.showSnackbar(
@StringRes textResource: Int,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val text = resources.getText(textResource)
showSnackbar(text, duration, snackbarBuilder)
showSnackbar(text, duration, anchorView, snackbarBuilder)
}

/**
Expand Down Expand Up @@ -192,19 +195,24 @@ fun View.showSnackbar(
fun View.showSnackbar(
text: CharSequence,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val snackbar = Snackbar.make(this, text, duration)
snackbar.setMaxLines(4)
snackbar.behavior = SwipeDismissBehaviorFix()
Snackbar.make(this, text, duration).apply {
this.anchorView = anchorView
setMaxLines(4)
behavior = SwipeDismissBehaviorFix()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
snackbar.fixMarginsWhenInsetsChange()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
fixMarginsWhenInsetsChange()
}

if (snackbarBuilder != null) { snackbar.snackbarBuilder() }
if (snackbarBuilder != null) {
snackbarBuilder()
}

snackbar.show()
show()
}
}

/**
Expand Down Expand Up @@ -236,10 +244,11 @@ fun View.showSnackbar(
fun Fragment.showSnackbar(
text: CharSequence,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val baseSnackbarBuilder = (this as? BaseSnackbarBuilderProvider)?.baseSnackbarBuilder
requireActivity().showSnackbar(text, duration) {
requireActivity().showSnackbar(text, duration, anchorView = anchorView) {
baseSnackbarBuilder?.invoke(this)
snackbarBuilder?.invoke(this)
Timber.d("displayed snackbar: '%s'", text)
Expand Down Expand Up @@ -275,10 +284,11 @@ fun Fragment.showSnackbar(
fun Fragment.showSnackbar(
@StringRes textResource: Int,
duration: Int = Snackbar.LENGTH_LONG,
anchorView: View? = null,
snackbarBuilder: SnackbarBuilder? = null
) {
val text = resources.getText(textResource)
showSnackbar(text, duration, snackbarBuilder)
showSnackbar(text, duration, anchorView, snackbarBuilder)
}

/* ********************************************************************************************** */
Expand Down
Loading

0 comments on commit 958ed5c

Please sign in to comment.