Skip to content

Commit da02fab

Browse files
NF: Replace "id: Int" by more specific type
1 parent 3f3980d commit da02fab

File tree

9 files changed

+53
-52
lines changed

9 files changed

+53
-52
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ class TagsDialog : AnalyticsDialogFragment {
183183
}
184184
check(0)
185185
}
186-
selectedOption = radioButtonIdToCardState(optionsGroup.checkedRadioButtonId)
186+
selectedOption = CardStateFilter.fromCode(optionsGroup.checkedRadioButtonId)
187187
optionsGroup.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
188-
selectedOption = radioButtonIdToCardState(checkedId)
188+
selectedOption = CardStateFilter.fromCode(checkedId)
189189
}
190190

191191
adjustToolbar(view)
@@ -268,17 +268,6 @@ class TagsDialog : AnalyticsDialogFragment {
268268
dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
269269
}
270270

271-
private fun radioButtonIdToCardState(id: Int) =
272-
when (id) {
273-
0 -> CardStateFilter.ALL_CARDS
274-
1 -> CardStateFilter.NEW
275-
2 -> CardStateFilter.DUE
276-
else -> {
277-
Timber.w("unexpected value: %d", id)
278-
CardStateFilter.ALL_CARDS
279-
}
280-
}
281-
282271
private fun adjustToolbar(tagsDialogView: View) {
283272
val toolbar: Toolbar = tagsDialogView.findViewById(R.id.tags_dialog_toolbar)
284273
val titleRes = if (type == DialogType.EDIT_TAGS) R.string.card_details_tags else R.string.studyoptions_limit_select_tags

AnkiDroid/src/main/java/com/ichi2/anki/model/CardStateFilter.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616

1717
package com.ichi2.anki.model
1818

19+
import timber.log.Timber
20+
1921
/**
2022
* Allows filtering a search by the state of cards
2123
*
2224
* @see [anki.search.SearchNode.CardState]
2325
*/
24-
enum class CardStateFilter {
25-
ALL_CARDS,
26-
NEW,
27-
DUE,
26+
enum class CardStateFilter(
27+
val code: Int,
28+
val toSearch: String,
29+
) {
30+
ALL_CARDS(0, ""),
31+
NEW(1, "is:new "),
32+
DUE(2, "is:due "),
2833
;
2934

30-
val toSearch: String
31-
get() =
32-
when (this) {
33-
ALL_CARDS -> ""
34-
NEW -> "is:new "
35-
DUE -> "is:due "
36-
}
35+
companion object {
36+
fun fromCode(id: Int): CardStateFilter {
37+
val filter = CardStateFilter.entries.firstOrNull { it.code == id }
38+
if (filter != null) return filter
39+
Timber.w("unexpected value: %d", id)
40+
return ALL_CARDS
41+
}
42+
}
3743
}

AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import android.os.Bundle
2323
import android.view.Menu
2424
import androidx.activity.result.ActivityResult
2525
import androidx.activity.result.contract.ActivityResultContracts
26+
import androidx.annotation.StringRes
2627
import androidx.appcompat.app.ActionBar
2728
import androidx.appcompat.app.AlertDialog
2829
import androidx.appcompat.widget.SearchView
@@ -193,7 +194,7 @@ class ManageNotetypes : AnkiActivity() {
193194

194195
private fun deleteNotetype(manageNoteTypeUiModel: ManageNoteTypeUiModel) {
195196
launchCatchingTask {
196-
val messageResourceId: Int? =
197+
@StringRes val messageResourceId: Int? =
197198
if (userAcceptsSchemaChange()) {
198199
withProgress {
199200
withCol {

AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ class CardContentProvider : ContentProvider() {
283283
val columns = projection ?: FlashCardsContract.CardTemplate.DEFAULT_PROJECTION
284284
val rv = MatrixCursor(columns, 1)
285285
try {
286-
for ((idx, template) in currentNoteType!!.templates.withIndex()) {
287-
addTemplateToCursor(template, currentNoteType, idx + 1, col.notetypes, rv, columns)
286+
for ((ord, template) in currentNoteType!!.templates.withIndex()) {
287+
addTemplateToCursor(template, currentNoteType, ord + 1, col.notetypes, rv, columns)
288288
}
289289
} catch (e: JSONException) {
290290
throw IllegalArgumentException("Note type is malformed", e)
@@ -1184,10 +1184,13 @@ class CardContentProvider : ContentProvider() {
11841184
}
11851185
}
11861186

1187+
/**
1188+
* @param [ord] The index of the template in the note type. First template is number 1.
1189+
*/
11871190
private fun addTemplateToCursor(
11881191
tmpl: CardTemplate,
11891192
notetype: NotetypeJson?,
1190-
id: Int,
1193+
ord: Int,
11911194
notetypes: Notetypes,
11921195
rv: MatrixCursor,
11931196
columns: Array<String>,
@@ -1196,7 +1199,7 @@ class CardContentProvider : ContentProvider() {
11961199
val rb = rv.newRow()
11971200
for (column in columns) {
11981201
when (column) {
1199-
FlashCardsContract.CardTemplate._ID -> rb.add(id)
1202+
FlashCardsContract.CardTemplate._ID -> rb.add(ord)
12001203
FlashCardsContract.CardTemplate.MODEL_ID -> rb.add(notetype!!.id)
12011204
FlashCardsContract.CardTemplate.ORD -> rb.add(tmpl.ord)
12021205
FlashCardsContract.CardTemplate.NAME -> rb.add(tmpl.name)

AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ class Notetypes(
7373
#############################################################
7474
*/
7575

76-
/**
77-
* Associating a note type id to its note type.
78-
*/
79-
@LibAnkiAlias("_cache")
80-
private val cache = HashMap<NoteTypeId, NotetypeJson>()
76+
@Suppress("ktlint:standard:backing-property-naming")
77+
private val _cache = HashMap<NoteTypeId, NotetypeJson>()
8178

8279
/** Save changes made to provided note type. */
8380
fun save(notetype: NotetypeJson) {
@@ -100,20 +97,20 @@ class Notetypes(
10097

10198
@LibAnkiAlias("_update_cache")
10299
private fun updateCache(nt: NotetypeJson) {
103-
cache[nt.id] = nt
100+
_cache[nt.id] = nt
104101
}
105102

106103
@LibAnkiAlias("_remove_from_cache")
107104
internal fun removeFromCache(ntid: NoteTypeId) {
108-
cache.remove(ntid)
105+
_cache.remove(ntid)
109106
}
110107

111108
@LibAnkiAlias("_get_cached")
112-
private fun getCached(ntid: NoteTypeId): NotetypeJson? = cache[ntid]
109+
private fun getCached(ntid: NoteTypeId): NotetypeJson? = _cache[ntid]
113110

114111
@NeedsTest("14827: styles are updated after syncing style changes")
115112
@LibAnkiAlias("_clear_cache")
116-
fun clearCache() = cache.clear()
113+
fun clearCache() = _cache.clear()
117114

118115
/*
119116
# Listing note types

AnkiDroid/src/main/java/com/ichi2/widget/cardanalysis/CardAnalysisWidget.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import com.ichi2.widget.setRecurringAlarm
4242
import kotlinx.coroutines.launch
4343
import timber.log.Timber
4444

45+
typealias AppWidgetId = Int
46+
4547
/**
4648
* This widget displays a deck with the respective new, learning, and review card counts.
4749
* It updates every minute and if there is any changes in study queues.
@@ -68,7 +70,7 @@ class CardAnalysisWidget : AnalyticsWidgetProvider() {
6870
fun updateWidget(
6971
context: Context,
7072
appWidgetManager: AppWidgetManager,
71-
appWidgetId: Int,
73+
appWidgetId: AppWidgetId,
7274
) {
7375
val deckId = getDeckIdForWidget(context, appWidgetId)
7476
val remoteViews = RemoteViews(context.packageName, R.layout.widget_card_analysis)
@@ -103,7 +105,7 @@ class CardAnalysisWidget : AnalyticsWidgetProvider() {
103105

104106
private fun getDeckIdForWidget(
105107
context: Context,
106-
appWidgetId: Int,
108+
appWidgetId: AppWidgetId,
107109
): DeckId {
108110
val widgetPreferences = CardAnalysisWidgetPreferences(context)
109111
return widgetPreferences.getSelectedDeckIdFromPreferences(appWidgetId) ?: NOT_FOUND_DECK_ID
@@ -112,7 +114,7 @@ class CardAnalysisWidget : AnalyticsWidgetProvider() {
112114
private fun showCollectionDeck(
113115
context: Context,
114116
appWidgetManager: AppWidgetManager,
115-
appWidgetId: Int,
117+
appWidgetId: AppWidgetId,
116118
remoteViews: RemoteViews,
117119
) {
118120
remoteViews.setTextViewText(R.id.empty_widget, context.getString(R.string.empty_collection_state_in_widget))
@@ -140,7 +142,7 @@ class CardAnalysisWidget : AnalyticsWidgetProvider() {
140142
private fun showMissingDeck(
141143
context: Context,
142144
appWidgetManager: AppWidgetManager,
143-
appWidgetId: Int,
145+
appWidgetId: AppWidgetId,
144146
remoteViews: RemoteViews,
145147
) {
146148
// Show empty_widget and set click listener to open configuration
@@ -169,7 +171,7 @@ class CardAnalysisWidget : AnalyticsWidgetProvider() {
169171
private fun showDeck(
170172
context: Context,
171173
appWidgetManager: AppWidgetManager,
172-
appWidgetId: Int,
174+
appWidgetId: AppWidgetId,
173175
remoteViews: RemoteViews,
174176
deckData: DeckWidgetData,
175177
) {

AnkiDroid/src/main/java/com/ichi2/widget/cardanalysis/CardAnalysisWidgetPreferences.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class CardAnalysisWidgetPreferences(
3636
/**
3737
* Deletes the selected deck ID from the shared preferences for the given widget ID.
3838
*/
39-
fun deleteDeckData(appWidgetId: Int) {
39+
fun deleteDeckData(appWidgetId: AppWidgetId) {
4040
cardAnalysisWidgetSharedPreferences.edit {
4141
remove(getCardAnalysisExtraWidgetKey(appWidgetId))
4242
}
4343
}
4444

45-
fun getSelectedDeckIdFromPreferences(appWidgetId: Int): DeckId? {
45+
fun getSelectedDeckIdFromPreferences(appWidgetId: AppWidgetId): DeckId? {
4646
val selectedDeckString =
4747
cardAnalysisWidgetSharedPreferences.getLong(
4848
getCardAnalysisExtraWidgetKey(appWidgetId),
@@ -52,7 +52,7 @@ class CardAnalysisWidgetPreferences(
5252
}
5353

5454
fun saveSelectedDeck(
55-
appWidgetId: Int,
55+
appWidgetId: AppWidgetId,
5656
selectedDeck: DeckId?,
5757
) {
5858
cardAnalysisWidgetSharedPreferences.edit {
@@ -64,4 +64,4 @@ class CardAnalysisWidgetPreferences(
6464
/**
6565
* Generates the key for the shared preferences for the given widget ID.
6666
*/
67-
private fun getCardAnalysisExtraWidgetKey(appWidgetId: Int): String = "card_analysis_extra_widget_selected_deck_$appWidgetId"
67+
private fun getCardAnalysisExtraWidgetKey(appWidgetId: AppWidgetId): String = "card_analysis_extra_widget_selected_deck_$appWidgetId"

AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidgetConfig.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import android.os.Bundle
2626
import android.view.View
2727
import android.widget.Button
2828
import androidx.activity.OnBackPressedCallback
29+
import androidx.annotation.StringRes
2930
import androidx.annotation.VisibleForTesting
3031
import androidx.core.view.isVisible
3132
import androidx.lifecycle.lifecycleScope
@@ -126,7 +127,9 @@ class DeckPickerWidgetConfig :
126127
)
127128
}
128129

129-
fun showSnackbar(messageResId: Int) {
130+
fun showSnackbar(
131+
@StringRes messageResId: Int,
132+
) {
130133
showSnackbar(getString(messageResId))
131134
}
132135

AnkiDroid/src/main/java/com/ichi2/widget/deckpicker/DeckPickerWidgetPreferences.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DeckPickerWidgetPreferences(
3434
/**
3535
* Deletes the selected deck IDs from the shared preferences for the given widget ID.
3636
*/
37-
fun deleteDeckData(appWidgetId: Int) {
37+
fun deleteDeckData(appWidgetId: AppWidgetId) {
3838
deckPickerSharedPreferences.edit {
3939
remove(getDeckPickerWidgetKey(appWidgetId))
4040
}
@@ -44,7 +44,7 @@ class DeckPickerWidgetPreferences(
4444
* Retrieves the selected deck IDs from the shared preferences for the given widget ID.
4545
* Note: There's no guarantee that these IDs still represent decks that exist at the time of execution.
4646
*/
47-
fun getSelectedDeckIdsFromPreferences(appWidgetId: Int): LongArray {
47+
fun getSelectedDeckIdsFromPreferences(appWidgetId: AppWidgetId): LongArray {
4848
val selectedDecksString = deckPickerSharedPreferences.getString(getDeckPickerWidgetKey(appWidgetId), "")
4949
return if (!selectedDecksString.isNullOrEmpty()) {
5050
selectedDecksString.split(",").map { it.toLong() }.toLongArray()
@@ -57,7 +57,7 @@ class DeckPickerWidgetPreferences(
5757
* Saves the selected deck IDs to the shared preferences for the given widget ID.
5858
*/
5959
fun saveSelectedDecks(
60-
appWidgetId: Int,
60+
appWidgetId: AppWidgetId,
6161
selectedDecks: List<String>,
6262
) {
6363
deckPickerSharedPreferences.edit {
@@ -69,4 +69,4 @@ class DeckPickerWidgetPreferences(
6969
/**
7070
* Generates the key for the shared preferences for the given widget ID.
7171
*/
72-
private fun getDeckPickerWidgetKey(appWidgetId: Int): String = "deck_picker_widget_selected_decks_$appWidgetId"
72+
private fun getDeckPickerWidgetKey(appWidgetId: AppWidgetId): String = "deck_picker_widget_selected_decks_$appWidgetId"

0 commit comments

Comments
 (0)