Skip to content

Commit

Permalink
Changed assert to assertThat in the test file and other refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
xenonnn4w authored and Arthur-Milchior committed Aug 27, 2024
1 parent d5e1f21 commit a03d3c1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
21 changes: 9 additions & 12 deletions AnkiDroid/src/main/java/com/ichi2/widget/WidgetAlarm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private fun getPendingIntent(
}

/**
* Ensure a recurring alarm is set to update the widget every 10 minutes.
* Ensure a recurring alarm is set to update the widget every 1 minute.
*
* If the alarm is already set for the widget, this method does nothing.
* This ensures that multiple alarms are not created for the same widget,
Expand All @@ -109,18 +109,15 @@ fun setRecurringAlarm(
Timber.v("Creating a new recurring alarm PendingIntent for widget ID: $appWidgetId")

val alarmManager = alarmManager(context)
val newPendingIntent = getPendingIntent(context, appWidgetId, widgetClass, create = true)
val newPendingIntent = getPendingIntent(context, appWidgetId, widgetClass, create = true) ?: return

// Set alarm to trigger every 10 minutes
val TEN_MINUTES_MILLIS = 10.minutes.inWholeMilliseconds
if (newPendingIntent != null) {
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + TEN_MINUTES_MILLIS,
TEN_MINUTES_MILLIS,
newPendingIntent
)
}
val ONE_MINUTE_MILLIS = 1.minutes.inWholeMilliseconds
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + ONE_MINUTE_MILLIS,
ONE_MINUTE_MILLIS,
newPendingIntent
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {
* Each ID corresponds to a specific deck, and the view will
* contain exactly the decks whose IDs are in this list.
*
* TODO: If the deck is completely empty (no cards at all), display a Snackbar or Toast message
* saying "The deck is empty" instead of opening any activity.
*
*/
fun updateWidget(
context: Context,
Expand Down Expand Up @@ -264,7 +261,7 @@ class DeckPickerWidget : AnalyticsWidgetProvider() {

override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
if (context == null) {
Timber.e("Context is null in onDeleted")
Timber.w("Context is null in onDeleted")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnackb
setupDoneButton()

// TODO: Implement multi-select functionality so that user can select desired decks in once.
// TODO: Implement a functionality to hide already selected deck.
findViewById<FloatingActionButton>(R.id.fabWidgetDeckPicker).setOnClickListener {
showDeckSelectionDialog()
}
Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/res/layout/widget_item_deck_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
android:id="@+id/action_button_remove_deck"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="?attr/selectableItemBackgroundBorderless"
android:paddingEnd="10dp"
android:src="@drawable/ic_delete_white" />

</LinearLayout>
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/res/values/08-widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
</plurals>

<string name="widget_add_note_button">Add new AnkiDroid note</string>
<string name="deck_picker_widget_description">Deck Picker Widget</string>
<string name="deck_picker_widget_description">Deck Picker</string>

<!-- Strings to explain usage in Deck Picker Widget Configuration screen -->
<string name="select_deck_title" comment="Title for Deck Selection Dialog">Select decks</string>
<string name="no_selected_deck_placeholder_title" comment="Placeholder title when no decks are selected">Select decks to display in the widget. Select decks with the + icon.</string>
<string name="deck_removed_from_widget" comment="Snackbar when deck is removed from widget">Deck Removed</string>
<string name="deck_removed_from_widget" comment="Snackbar when deck is removed from widget">Deck removed</string>
<string name="deck_already_selected_message" comment="Snackbar when user try to select the same deck again">This deck is already selected</string>
<plurals name="deck_limit_reached">
<item quantity="one">You can select up to %d deck.</item>
Expand Down
19 changes: 10 additions & 9 deletions AnkiDroid/src/main/res/xml/widget_provider_deck_picker.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<!--JPG type of file used in previewImage property because the SVG format is not supported on all devices.
<!-- JPG type of file used in previewImage property because the SVG format is not supported on all devices.
The widths and heights parameters are determined as follows:
The default is 3 cells in width and 2 in height.
The height is between 1 and 5 cells.
The width is between 3 and 4 cells.
Following https://developer.android.com/develop/ui/views/appwidgets/layouts#anatomy_determining_size
we used the portrait mode cell size for the width and the landscape mode cell size for the height. Leading to:
* height between 50 and 315
* width 203 and 276 -->

The widths and heights parameters are determined as follow.
The default is 3 cells in width and 2 in height
The height is between 1 and 5 cells.
The width is between 3 and 4 cells.
Following https://developer.android.com/develop/ui/views/appwidgets/layouts#anatomy_determining_size
we used the portrait mode cell size for the width and the landscape mode cell size for the height. Leading to:
* height between 50 and 315
* width 203 and 276-->
<!-- TODO: Use updatePeriodMillis instead of the 10-minute alarm for simpler widget updates.-->

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget_deck_picker_large"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import com.ichi2.anki.RobolectricTest
import com.ichi2.anki.dialogs.DeckSelectionDialog
import com.ichi2.widget.WidgetPreferences
import com.ichi2.widget.deckpicker.DeckPickerWidgetConfig
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -85,8 +87,8 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {
activity.saveSelectedDecksToPreferencesDeckPickerWidget()

// Verify saved decks
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(1)
assert(selectedDeckIds.contains(deck1.deckId))
val selectedDeckIds = widgetPreferences.getSelectedDeckIdsFromPreferencesDeckPickerWidget(1).toList()
assertThat(selectedDeckIds.contains(deck1.deckId), equalTo(true))
}

/**
Expand All @@ -112,7 +114,7 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {
val adapter = recyclerView.adapter

// Verify the adapter has the correct item count
assert(adapter != null && adapter.itemCount == deckIds.size)
assertThat(adapter?.itemCount, equalTo(deckIds.size))
}

/**
Expand All @@ -128,16 +130,16 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {

// Initially, no decks should be selected
activity.updateViewVisibility()
assert(noDecksPlaceholder.visibility == View.VISIBLE)
assert(widgetConfigContainer.visibility == View.GONE)
assertThat(noDecksPlaceholder.visibility, equalTo(View.VISIBLE))
assertThat(widgetConfigContainer.visibility, equalTo(View.GONE))

// Add a deck and update view visibility
val deck = DeckSelectionDialog.SelectableDeck(1, "Deck 1")
activity.deckAdapter.addDeck(deck)
activity.updateViewVisibility()

assert(noDecksPlaceholder.visibility == View.GONE)
assert(widgetConfigContainer.visibility == View.VISIBLE)
assertThat(noDecksPlaceholder.visibility, equalTo(View.GONE))
assertThat(widgetConfigContainer.visibility, equalTo(View.VISIBLE))
}

/**
Expand All @@ -153,6 +155,6 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {

// Verify deck is added to adapter
val recyclerView = activity.findViewById<RecyclerView>(R.id.recyclerViewSelectedDecks)
assert(recyclerView.adapter?.itemCount == 1)
assertThat(recyclerView.adapter?.itemCount, equalTo(1))
}
}

0 comments on commit a03d3c1

Please sign in to comment.