Skip to content

Specific id that were int #18393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ open class CardBrowser :
savedFilters,
mySearchesDialogListener,
"",
CardBrowserMySearchesDialog.CARD_BROWSER_MY_SEARCHES_TYPE_LIST,
CardBrowserMySearchesDialog.SavedSearchesType.List,
),
)
}
Expand All @@ -1343,7 +1343,7 @@ open class CardBrowser :
null,
mySearchesDialogListener,
searchTerms,
CardBrowserMySearchesDialog.CARD_BROWSER_MY_SEARCHES_TYPE_SAVE,
CardBrowserMySearchesDialog.SavedSearchesType.Save,
),
)
}
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import com.ichi2.anki.noteeditor.Toolbar
import com.ichi2.anki.noteeditor.Toolbar.TextFormatListener
import com.ichi2.anki.noteeditor.Toolbar.TextWrapper
import com.ichi2.anki.pages.ImageOcclusion
import com.ichi2.anki.pages.NoteOrNoteTypeId
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.previewer.TemplatePreviewerArguments
import com.ichi2.anki.previewer.TemplatePreviewerPage
Expand Down Expand Up @@ -2657,7 +2658,7 @@ class NoteEditor :

private fun setupImageOcclusionEditor(imagePath: String = "") {
val kind: String
val id: Long
val id: NoteOrNoteTypeId
if (addNote) {
kind = "add"
// if opened from an intent, the selected note type may not be suitable for IO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import com.ichi2.libanki.NoteId
* It is not included in the class as this class is primarily provided in a [BrowserRowCollection]
* and storing the same value for every item in the list is a waste
*/
typealias CardOrNoteIdUnderlyingType = Long

@JvmInline
value class CardOrNoteId(
val cardOrNoteId: Long,
val cardOrNoteId: CardOrNoteIdUnderlyingType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert - no purpose

) {
override fun toString(): String = cardOrNoteId.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class CardBrowserMySearchesDialog : AnalyticsDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreate(savedInstanceState)
val dialog = AlertDialog.Builder(requireActivity())
val type = requireArguments().getInt("type")
if (type == CARD_BROWSER_MY_SEARCHES_TYPE_LIST) {
savedFilters = requireArguments().getSerializableCompat("savedFilters")
val type = SavedSearchesType.fromCode(requireArguments().getInt(SAVED_TYPE_KEY))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not serialise/deserialise the enum directly?

if (type == SavedSearchesType.List) {
savedFilters = requireArguments().getSerializableCompat(SAVED_FILTER_KEY)

savedFilters?.let {
savedFilterKeys = ArrayList(it.keys)
Expand All @@ -66,8 +66,8 @@ class CardBrowserMySearchesDialog : AnalyticsDialogFragment() {
.title(text = resources.getString(R.string.card_browser_list_my_searches_title))
.customListAdapterWithDecoration(this, requireActivity())
}
} else if (type == CARD_BROWSER_MY_SEARCHES_TYPE_SAVE) {
val currentSearchTerms = requireArguments().getString("currentSearchTerms")
} else if (type == SavedSearchesType.Save) {
val currentSearchTerms = requireArguments().getString(SAVED_CURRENT_SEARCH_TERMS_KEY)
return dialog
.show {
title(text = getString(R.string.card_browser_list_my_searches_save))
Expand Down Expand Up @@ -107,23 +107,36 @@ class CardBrowserMySearchesDialog : AnalyticsDialogFragment() {
}
}

enum class SavedSearchesType(
val code: Int,
) {
List(0),
Save(1),
;

companion object {
fun fromCode(c: Int) = SavedSearchesType.entries.first { it.code == c }
}
}

companion object {
const val CARD_BROWSER_MY_SEARCHES_TYPE_LIST = 0 // list searches dialog
const val CARD_BROWSER_MY_SEARCHES_TYPE_SAVE = 1 // save searches dialog
const val SAVED_FILTER_KEY = "savedFilters"
const val SAVED_TYPE_KEY = "type"
const val SAVED_CURRENT_SEARCH_TERMS_KEY = "currentSearchTerms"
private var mySearchesDialogListener: MySearchesDialogListener? = null

fun newInstance(
savedFilters: Map<String, String>?,
mySearchesDialogListener: MySearchesDialogListener?,
currentSearchTerms: String?,
type: Int,
type: SavedSearchesType,
): CardBrowserMySearchesDialog {
this.mySearchesDialogListener = mySearchesDialogListener
val cardBrowserMySearchesDialog = CardBrowserMySearchesDialog()
val args = Bundle()
args.putSerializable("savedFilters", savedFilters?.let(::HashMap))
args.putInt("type", type)
args.putString("currentSearchTerms", currentSearchTerms)
args.putSerializable(SAVED_FILTER_KEY, savedFilters?.let(::HashMap))
args.putInt(SAVED_TYPE_KEY, type.code)
args.putString(SAVED_CURRENT_SEARCH_TERMS_KEY, currentSearchTerms)
cardBrowserMySearchesDialog.arguments = args
return cardBrowserMySearchesDialog
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.ichi2.anki.showThemedToast
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.DeckNameId
import com.ichi2.libanki.Decks.Companion.NOT_FOUND_DECK_ID
import com.ichi2.libanki.sched.DeckNode
import com.ichi2.ui.AccessibleSearchView
import com.ichi2.utils.TypedFilter
Expand Down Expand Up @@ -289,7 +290,7 @@ open class DeckSelectionDialog : AnalyticsDialogFragment() {
deckHolder: View,
) : RecyclerView.ViewHolder(deckHolder) {
var deckName: String = ""
private var deckID: Long = -1L
private var deckID: DeckId = NOT_FOUND_DECK_ID

private val deckTextView: TextView = deckHolder.findViewById(R.id.deckpicker_name)
val expander: ImageButton = deckHolder.findViewById(R.id.deckpicker_expander)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class EmptyCardsDialogFragment : DialogFragment() {
*/
private class BrowserSearchByNidSpan(
val context: Context,
val nid: Long,
val nid: NoteId,
) : ClickableSpan() {
override fun onClick(widget: View) {
val browserSearchIntent = Intent(context, CardBrowser::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import kotlinx.parcelize.Parcelize

typealias HelpItemId = Long
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it private, I don't think it serves much purpose


/**
* A class containing all the information required to show an entry in the application help/support
* menus.
Expand All @@ -36,12 +38,12 @@ data class HelpItem(
/**
* Unique(per menu) identifier for this menu item.
*/
val id: Long,
val id: HelpItemId,
/**
* If not null this property indicates that this menu item belongs to the submenu of the
* referenced menu item.
*/
val parentId: Long? = null,
val parentId: HelpItemId? = null,
/**
* Reference to the action that needs to be done when the user selects this menu item. Can be
* null, in which case this is a top level item(with or without children).
Expand Down
15 changes: 2 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ class TagsDialog : AnalyticsDialogFragment {
}
check(0)
}
selectedOption = radioButtonIdToCardState(optionsGroup.checkedRadioButtonId)
selectedOption = CardStateFilter.fromCode(optionsGroup.checkedRadioButtonId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert - we should remove this functionality

optionsGroup.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
selectedOption = radioButtonIdToCardState(checkedId)
selectedOption = CardStateFilter.fromCode(checkedId)
}

adjustToolbar(view)
Expand Down Expand Up @@ -268,17 +268,6 @@ class TagsDialog : AnalyticsDialogFragment {
dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}

private fun radioButtonIdToCardState(id: Int) =
when (id) {
0 -> CardStateFilter.ALL_CARDS
1 -> CardStateFilter.NEW
2 -> CardStateFilter.DUE
else -> {
Timber.w("unexpected value: %d", id)
CardStateFilter.ALL_CARDS
}
}

private fun adjustToolbar(tagsDialogView: View) {
val toolbar: Toolbar = tagsDialogView.findViewById(R.id.tags_dialog_toolbar)
val titleRes = if (type == DialogType.EDIT_TAGS) R.string.card_details_tags else R.string.studyoptions_limit_select_tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ExportDialogFragment : DialogFragment() {
).apply { setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) }
adapter = exportTypesAdapter
onItemSelectedListener =
BasicItemSelectedListener { position, _ ->
BasicItemSelectedListener { position ->
showExtrasOptionsFor(this@initializeCommonUi, ExportConfiguration.from(position))
}
}
Expand Down
28 changes: 17 additions & 11 deletions AnkiDroid/src/main/java/com/ichi2/anki/model/CardStateFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,28 @@

package com.ichi2.anki.model

import timber.log.Timber

/**
* Allows filtering a search by the state of cards
*
* @see [anki.search.SearchNode.CardState]
*/
enum class CardStateFilter {
ALL_CARDS,
NEW,
DUE,
enum class CardStateFilter(
val code: Int,
val toSearch: String,
) {
ALL_CARDS(0, ""),
NEW(1, "is:new "),
DUE(2, "is:due "),
;

val toSearch: String
get() =
when (this) {
ALL_CARDS -> ""
NEW -> "is:new "
DUE -> "is:due "
}
companion object {
fun fromCode(id: Int): CardStateFilter {
val filter = CardStateFilter.entries.firstOrNull { it.code == id }
if (filter != null) return filter
Timber.w("unexpected value: %d", id)
return ALL_CARDS
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.os.Bundle
import android.view.Menu
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.StringRes
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SearchView
Expand Down Expand Up @@ -193,7 +194,7 @@ class ManageNotetypes : AnkiActivity() {

private fun deleteNotetype(manageNoteTypeUiModel: ManageNoteTypeUiModel) {
launchCatchingTask {
val messageResourceId: Int? =
@StringRes val messageResourceId: Int? =
if (userAcceptsSchemaChange()) {
withProgress {
withCol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import kotlinx.coroutines.launch
import org.json.JSONObject
import timber.log.Timber

internal typealias NoteOrNoteTypeId = Long

class ImageOcclusion : PageFragment(R.layout.image_occlusion) {
override fun onViewCreated(
view: View,
Expand Down Expand Up @@ -122,7 +124,7 @@ class ImageOcclusion : PageFragment(R.layout.image_occlusion) {
fun getIntent(
context: Context,
kind: String,
noteOrNotetypeId: Long,
noteOrNotetypeId: NoteOrNoteTypeId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert? Or Document, or strongly type as a union

imagePath: String?,
editorWorkingDeckId: DeckId,
): Intent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ class ReviewerMenuSettingsAdapter(
viewType: Int,
): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
ReviewerMenuSettingsRecyclerItem.ACTION_VIEW_TYPE -> {
return when (ReviewerMenuSettingsType.fromCode(viewType)) {
ReviewerMenuSettingsType.Action -> {
val itemView = inflater.inflate(R.layout.reviewer_menu_item, parent, false)
ActionViewHolder(itemView)
}
ReviewerMenuSettingsRecyclerItem.DISPLAY_TYPE_VIEW_TYPE -> {
ReviewerMenuSettingsType.DisplayType -> {
val itemView = inflater.inflate(R.layout.reviewer_menu_display_type, parent, false)
DisplayTypeViewHolder(itemView)
}
else -> throw IllegalArgumentException("Unexpected viewType")
}
}

Expand All @@ -60,7 +59,7 @@ class ReviewerMenuSettingsAdapter(

override fun getItemCount(): Int = items.size

override fun getItemViewType(position: Int): Int = items[position].viewType
override fun getItemViewType(position: Int) = items[position].viewType.code

private var onDragHandleTouchedListener: ((RecyclerView.ViewHolder) -> Unit)? = null

Expand Down Expand Up @@ -95,22 +94,29 @@ class ReviewerMenuSettingsAdapter(
}
}

enum class ReviewerMenuSettingsType(
val code: Int,
) {
Action(0),
DisplayType(1),
;

companion object {
fun fromCode(c: Int) = ReviewerMenuSettingsType.entries.first { it.code == c }
}
}

/**
* @param viewType type to be returned at [RecyclerView.Adapter.getItemViewType]
*/
sealed class ReviewerMenuSettingsRecyclerItem(
val viewType: Int,
val viewType: ReviewerMenuSettingsType,
) {
data class Action(
val viewerAction: ViewerAction,
) : ReviewerMenuSettingsRecyclerItem(ACTION_VIEW_TYPE)
) : ReviewerMenuSettingsRecyclerItem(ReviewerMenuSettingsType.Action)

data class DisplayType(
val menuDisplayType: MenuDisplayType,
) : ReviewerMenuSettingsRecyclerItem(DISPLAY_TYPE_VIEW_TYPE)

companion object {
const val ACTION_VIEW_TYPE = 0
const val DISPLAY_TYPE_VIEW_TYPE = 1
}
) : ReviewerMenuSettingsRecyclerItem(ReviewerMenuSettingsType.DisplayType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ReviewerMenuSettingsTouchHelperCallback(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
): Int =
if (viewHolder.itemViewType == ReviewerMenuSettingsRecyclerItem.DISPLAY_TYPE_VIEW_TYPE) {
if (viewHolder.itemViewType == ReviewerMenuSettingsType.DisplayType.code) {
0
} else {
movementFlags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ class CardContentProvider : ContentProvider() {
val columns = projection ?: FlashCardsContract.CardTemplate.DEFAULT_PROJECTION
val rv = MatrixCursor(columns, 1)
try {
for ((idx, template) in currentNoteType!!.templates.withIndex()) {
addTemplateToCursor(template, currentNoteType, idx + 1, col.notetypes, rv, columns)
for ((ord, template) in currentNoteType!!.templates.withIndex()) {
addTemplateToCursor(template, currentNoteType, ord + 1, col.notetypes, rv, columns)
}
} catch (e: JSONException) {
throw IllegalArgumentException("Note type is malformed", e)
Expand Down Expand Up @@ -1184,10 +1184,13 @@ class CardContentProvider : ContentProvider() {
}
}

/**
* @param [ord] The index of the template in the note type. First template is number 1.
*/
private fun addTemplateToCursor(
tmpl: CardTemplate,
notetype: NotetypeJson?,
id: Int,
ord: Int,
notetypes: Notetypes,
rv: MatrixCursor,
columns: Array<String>,
Expand All @@ -1196,7 +1199,7 @@ class CardContentProvider : ContentProvider() {
val rb = rv.newRow()
for (column in columns) {
when (column) {
FlashCardsContract.CardTemplate._ID -> rb.add(id)
FlashCardsContract.CardTemplate._ID -> rb.add(ord)
FlashCardsContract.CardTemplate.MODEL_ID -> rb.add(notetype!!.id)
FlashCardsContract.CardTemplate.ORD -> rb.add(tmpl.ord)
FlashCardsContract.CardTemplate.NAME -> rb.add(tmpl.name)
Expand Down
Loading
Loading