Skip to content

Commit

Permalink
NF: remove string resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur-Milchior authored and SanjaySargam committed Sep 21, 2024
1 parent 9051aa0 commit 6b75e7b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 149 deletions.
12 changes: 9 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ import androidx.browser.customtabs.CustomTabsIntent.Builder as CustomTabsIntentB

@UiThread
@KotlinCleanup("set activityName")
open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, ShortcutGroupProvider {
open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, ShortcutGroupProvider, AnkiActivityProvider {

/** The name of the parent class (example: 'Reviewer') */
private val activityName: String
val dialogHandler = DialogHandler(this)
override val ankiActivity = this

private val customTabActivityHelper: CustomTabActivityHelper = CustomTabActivityHelper()

Expand Down Expand Up @@ -596,7 +597,7 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, Shortc
val done = super.onKeyUp(keyCode, event)

// Show snackbar only if the current activity have shortcuts, a modifier key is pressed and the keyCode is an unmapped alphabet key
if (!done && shortcuts != null && (event.isCtrlPressed || event.isAltPressed || event.isShiftPressed) && (keyCode in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z) || (keyCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_9)) {
if (!done && shortcuts != null && (event.isCtrlPressed || event.isAltPressed) && (keyCode in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z) || (keyCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_9)) {
showSnackbar(R.string.show_shortcuts_message, Snackbar.LENGTH_SHORT)
return true
}
Expand All @@ -620,7 +621,8 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, Shortc
return false
}

override val shortcuts: CompatV24.ShortcutGroup? = null
override val shortcuts
get(): CompatV24.ShortcutGroup? = null

companion object {
const val DIALOG_FRAGMENT_TAG = "dialog"
Expand Down Expand Up @@ -655,3 +657,7 @@ fun Fragment.requireAnkiActivity(): AnkiActivity {
return requireActivity() as? AnkiActivity?
?: throw java.lang.IllegalStateException("Fragment $this not attached to an AnkiActivity.")
}

interface AnkiActivityProvider {
val ankiActivity: AnkiActivity
}
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ import timber.log.Timber
*/
// TODO: Consider refactoring to create AnkiInterface to consolidate common implementations between AnkiFragment and AnkiActivity.
// This could help reduce code repetition and improve maintainability.
open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout) {
open class AnkiFragment(@LayoutRes layout: Int) : Fragment(layout), AnkiActivityProvider {

val getColUnsafe: Collection
get() = CollectionManager.getColUnsafe()

val ankiActivity: AnkiActivity
override val ankiActivity: AnkiActivity
get() = requireAnkiActivity()

val mainToolbar: Toolbar
Expand Down
71 changes: 36 additions & 35 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import com.ichi2.async.renderBrowserQA
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.registerReceiverCompat
import com.ichi2.compat.CompatV24
import com.ichi2.compat.CompatV24.Shortcut
import com.ichi2.compat.shortcut
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.ChangeManager
Expand Down Expand Up @@ -2411,40 +2411,41 @@ open class CardBrowser :
}
}

override val shortcuts = CompatV24.ShortcutGroup(
listOf(
Shortcut("Ctrl+Shift+A", R.string.edit_tags_dialog),
Shortcut("Ctrl+A", R.string.card_browser_select_all),
Shortcut("Ctrl+Shift+E", Translations::exportingExport),
Shortcut("Ctrl+E", R.string.menu_add_note),
Shortcut("E", R.string.cardeditor_title_edit_card),
Shortcut("Ctrl+D", R.string.card_browser_change_deck),
Shortcut("Ctrl+K", Translations::browsingToggleMark),
Shortcut("Ctrl+Alt+R", Translations::browsingReschedule),
Shortcut("DEL", R.string.delete_card_title),
Shortcut("Ctrl+Alt+N", R.string.reset_card_dialog_title),
Shortcut("Ctrl+Alt+T", R.string.toggle_cards_notes),
Shortcut("Ctrl+T", R.string.card_browser_search_by_tag),
Shortcut("Ctrl+Shift+S", Translations::actionsReposition),
Shortcut("Ctrl+Alt+S", R.string.card_browser_list_my_searches),
Shortcut("Ctrl+S", R.string.card_browser_list_my_searches_save),
Shortcut("Alt+S", R.string.card_browser_show_suspended),
Shortcut("Ctrl+Shift+J", Translations::browsingToggleBury),
Shortcut("Ctrl+J", Translations::browsingToggleSuspend),
Shortcut("Ctrl+Shift+I", Translations::actionsCardInfo),
Shortcut("Ctrl+O", R.string.show_order_dialog),
Shortcut("Ctrl+M", R.string.card_browser_show_marked),
Shortcut("Esc", R.string.card_browser_select_none),
Shortcut("Ctrl+1", R.string.gesture_flag_red),
Shortcut("Ctrl+2", R.string.gesture_flag_orange),
Shortcut("Ctrl+3", R.string.gesture_flag_green),
Shortcut("Ctrl+4", R.string.gesture_flag_blue),
Shortcut("Ctrl+5", R.string.gesture_flag_pink),
Shortcut("Ctrl+6", R.string.gesture_flag_turquoise),
Shortcut("Ctrl+7", R.string.gesture_flag_purple)
),
R.string.card_browser_context_menu
)
override val shortcuts
get() = CompatV24.ShortcutGroup(
listOf(
shortcut("Ctrl+Shift+A", R.string.edit_tags_dialog),
shortcut("Ctrl+A", R.string.card_browser_select_all),
shortcut("Ctrl+Shift+E", Translations::exportingExport),
shortcut("Ctrl+E", R.string.menu_add_note),
shortcut("E", R.string.cardeditor_title_edit_card),
shortcut("Ctrl+D", R.string.card_browser_change_deck),
shortcut("Ctrl+K", Translations::browsingToggleMark),
shortcut("Ctrl+Alt+R", Translations::browsingReschedule),
shortcut("DEL", R.string.delete_card_title),
shortcut("Ctrl+Alt+N", R.string.reset_card_dialog_title),
shortcut("Ctrl+Alt+T", R.string.toggle_cards_notes),
shortcut("Ctrl+T", R.string.card_browser_search_by_tag),
shortcut("Ctrl+Shift+S", Translations::actionsReposition),
shortcut("Ctrl+Alt+S", R.string.card_browser_list_my_searches),
shortcut("Ctrl+S", R.string.card_browser_list_my_searches_save),
shortcut("Alt+S", R.string.card_browser_show_suspended),
shortcut("Ctrl+Shift+J", Translations::browsingToggleBury),
shortcut("Ctrl+J", Translations::browsingToggleSuspend),
shortcut("Ctrl+Shift+I", Translations::actionsCardInfo),
shortcut("Ctrl+O", R.string.show_order_dialog),
shortcut("Ctrl+M", R.string.card_browser_show_marked),
shortcut("Esc", R.string.card_browser_select_none),
shortcut("Ctrl+1", R.string.gesture_flag_red),
shortcut("Ctrl+2", R.string.gesture_flag_orange),
shortcut("Ctrl+3", R.string.gesture_flag_green),
shortcut("Ctrl+4", R.string.gesture_flag_blue),
shortcut("Ctrl+5", R.string.gesture_flag_pink),
shortcut("Ctrl+6", R.string.gesture_flag_turquoise),
shortcut("Ctrl+7", R.string.gesture_flag_purple)
),
R.string.card_browser_context_menu
)

companion object {
/**
Expand Down
37 changes: 19 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import com.ichi2.anki.utils.postDelayed
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.compat.CompatV24
import com.ichi2.compat.CompatV24.Shortcut
import com.ichi2.compat.shortcut
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Note
import com.ichi2.libanki.NoteId
Expand Down Expand Up @@ -426,23 +426,24 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
}
}

override val shortcuts = CompatV24.ShortcutGroup(
listOf(
Shortcut("Ctrl+P", R.string.card_editor_preview_card),
Shortcut("Ctrl+1", R.string.edit_question),
Shortcut("Ctrl+2", R.string.edit_answer),
Shortcut("Ctrl+3", R.string.edit_styling),
Shortcut("Ctrl+S", R.string.save),
Shortcut("Ctrl+I", R.string.card_template_editor_insert_field),
Shortcut("Ctrl+A", Translations::cardTemplatesAddCardType),
Shortcut("Ctrl+R", Translations::cardTemplatesRenameCardType),
Shortcut("Ctrl+B", R.string.edit_browser_appearance),
Shortcut("Ctrl+D", Translations::cardTemplatesRemoveCardType),
Shortcut("Ctrl+O", Translations::cardTemplatesDeckOverride),
Shortcut("Ctrl+M", R.string.copy_the_template)
),
R.string.card_template_editor_group
)
override val shortcuts
get() = CompatV24.ShortcutGroup(
listOf(
shortcut("Ctrl+P", R.string.card_editor_preview_card),
shortcut("Ctrl+1", R.string.edit_question),
shortcut("Ctrl+2", R.string.edit_answer),
shortcut("Ctrl+3", R.string.edit_styling),
shortcut("Ctrl+S", R.string.save),
shortcut("Ctrl+I", R.string.card_template_editor_insert_field),
shortcut("Ctrl+A", Translations::cardTemplatesAddCardType),
shortcut("Ctrl+R", Translations::cardTemplatesRenameCardType),
shortcut("Ctrl+B", R.string.edit_browser_appearance),
shortcut("Ctrl+D", Translations::cardTemplatesRemoveCardType),
shortcut("Ctrl+O", Translations::cardTemplatesDeckOverride),
shortcut("Ctrl+M", R.string.copy_the_template)
),
R.string.card_template_editor_group
)

class CardTemplateFragment : Fragment() {
private val refreshFragmentHandler = Handler(Looper.getMainLooper())
Expand Down
47 changes: 24 additions & 23 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.compat.CompatHelper.Companion.registerReceiverCompat
import com.ichi2.compat.CompatHelper.Companion.sdkVersion
import com.ichi2.compat.CompatV24
import com.ichi2.compat.CompatV24.Shortcut
import com.ichi2.compat.shortcut
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.Consts
import com.ichi2.libanki.DeckId
Expand Down Expand Up @@ -2420,28 +2420,29 @@ open class DeckPicker :
SKIP_STUDY_OPTIONS
}

override val shortcuts = CompatV24.ShortcutGroup(
listOf(
Shortcut("A", R.string.menu_add_note),
Shortcut("B", R.string.card_browser_context_menu),
Shortcut("Y", R.string.pref_cat_sync),
Shortcut("/", R.string.deck_conf_cram_search),
Shortcut("S", Translations::decksStudyDeck),
Shortcut("T", R.string.open_statistics),
Shortcut("C", R.string.check_db),
Shortcut("D", R.string.new_deck),
Shortcut("F", R.string.new_dynamic_deck),
Shortcut("DEL", R.string.delete_deck_title),
Shortcut("Shift+DEL", R.string.delete_deck_without_confirmation),
Shortcut("R", R.string.rename_deck),
Shortcut("P", R.string.open_settings),
Shortcut("M", R.string.check_media),
Shortcut("Ctrl+E", R.string.export_collection),
Shortcut("Ctrl+Shift+I", R.string.menu_import),
Shortcut("Ctrl+Shift+N", R.string.model_browser_label)
),
R.string.deck_picker_group
)
override val shortcuts
get() = CompatV24.ShortcutGroup(
listOf(
shortcut("A", R.string.menu_add_note),
shortcut("B", R.string.card_browser_context_menu),
shortcut("Y", R.string.pref_cat_sync),
shortcut("/", R.string.deck_conf_cram_search),
shortcut("S", Translations::decksStudyDeck),
shortcut("T", R.string.open_statistics),
shortcut("C", R.string.check_db),
shortcut("D", R.string.new_deck),
shortcut("F", R.string.new_dynamic_deck),
shortcut("DEL", R.string.delete_deck_title),
shortcut("Shift+DEL", R.string.delete_deck_without_confirmation),
shortcut("R", R.string.rename_deck),
shortcut("P", R.string.open_settings),
shortcut("M", R.string.check_media),
shortcut("Ctrl+E", R.string.export_collection),
shortcut("Ctrl+Shift+I", R.string.menu_import),
shortcut("Ctrl+Shift+N", R.string.model_browser_label)
),
R.string.deck_picker_group
)

companion object {
/**
Expand Down
27 changes: 14 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.compat.CompatHelper.Companion.registerReceiverCompat
import com.ichi2.compat.CompatV24
import com.ichi2.compat.CompatV24.Shortcut
import com.ichi2.compat.ShortcutGroupProvider
import com.ichi2.compat.setTooltipTextCompat
import com.ichi2.compat.shortcut
import com.ichi2.imagecropper.ImageCropper
import com.ichi2.imagecropper.ImageCropper.Companion.CROP_IMAGE_RESULT
import com.ichi2.imagecropper.ImageCropperLauncher
Expand Down Expand Up @@ -2384,18 +2384,19 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
noteTypeSpinner!!.setSelection(position, false)
}

override val shortcuts = CompatV24.ShortcutGroup(
listOf(
Shortcut("Ctrl+ENTER", R.string.save),
Shortcut("Ctrl+D", R.string.select_deck),
Shortcut("Ctrl+L", R.string.card_template_editor_group),
Shortcut("Ctrl+N", R.string.select_note_type),
Shortcut("Ctrl+Shift+T", R.string.tag_editor),
Shortcut("Ctrl+Shift+C", R.string.multimedia_editor_popup_cloze),
Shortcut("Ctrl+P", R.string.card_editor_preview_card)
),
R.string.note_editor_group
)
override val shortcuts
get() = CompatV24.ShortcutGroup(
listOf(
shortcut("Ctrl+ENTER", R.string.save),
shortcut("Ctrl+D", R.string.select_deck),
shortcut("Ctrl+L", R.string.card_template_editor_group),
shortcut("Ctrl+N", R.string.select_note_type),
shortcut("Ctrl+Shift+T", R.string.tag_editor),
shortcut("Ctrl+Shift+C", R.string.multimedia_editor_popup_cloze),
shortcut("Ctrl+P", R.string.card_editor_preview_card)
),
R.string.note_editor_group
)

private fun updateTags() {
if (selectedTags == null) {
Expand Down
28 changes: 19 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.compat

import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.icu.util.ULocale
import android.view.KeyEvent
import android.view.KeyboardShortcutGroup
Expand All @@ -29,6 +28,8 @@ import androidx.annotation.StringRes
import androidx.core.view.OnReceiveContentListener
import androidx.draganddrop.DropHelper
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.AnkiActivityProvider
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R
import com.ichi2.anki.common.utils.android.isRobolectric
import net.ankiweb.rsdroid.Translations
Expand Down Expand Up @@ -78,8 +79,8 @@ open class CompatV24 : CompatV23(), Compat {
override fun getShortcuts(activity: AnkiActivity): List<KeyboardShortcutGroup> {
val generalShortcutGroup = ShortcutGroup(
listOf(
Shortcut("Alt+K", R.string.show_keyboard_shortcuts_dialog),
Shortcut("Ctrl+Z", R.string.undo)
activity.shortcut("Alt+K", R.string.show_keyboard_shortcuts_dialog),
activity.shortcut("Ctrl+Z", R.string.undo)
),
R.string.pref_cat_general
).toShortcutGroup(activity)
Expand All @@ -93,18 +94,15 @@ open class CompatV24 : CompatV23(), Compat {
* @param shortcut The string representation of the keyboard shortcut (e.g., "Ctrl+Alt+S").
* @param label The string resource for the shortcut label.
*/
data class Shortcut(val shortcut: String, val label: StringResource) {
constructor(shortcut: String, @StringRes labelRes: Int) : this(shortcut, StringResource.AndroidTranslation(labelRes))
constructor(shortcut: String, getTranslation: Translations.() -> String) : this(shortcut, StringResource.AnkiBackendTranslation(getTranslation))
data class Shortcut(val shortcut: String, val label: String) {

/**
* Converts the shortcut string into a KeyboardShortcutInfo object.
*
* @param context The context used to retrieve the string label resource.
* @return A KeyboardShortcutInfo object representing the keyboard shortcut.
*/
fun toShortcutInfo(context: Context): KeyboardShortcutInfo {
val label: String = label.toDisplayString(context)
fun toShortcutInfo(): KeyboardShortcutInfo {
val parts = shortcut.split("+")
val key = parts.last()
val keycode: Int = getKey(key)
Expand Down Expand Up @@ -146,7 +144,7 @@ open class CompatV24 : CompatV23(), Compat {

data class ShortcutGroup(val shortcuts: List<Shortcut>, @StringRes val id: Int) {
fun toShortcutGroup(activity: AnkiActivity): KeyboardShortcutGroup {
val shortcuts = shortcuts.map { it.toShortcutInfo(activity) }
val shortcuts = shortcuts.map { it.toShortcutInfo() }
val groupLabel = activity.getString(id)
return KeyboardShortcutGroup(groupLabel, shortcuts)
}
Expand All @@ -162,3 +160,15 @@ interface ShortcutGroupProvider {
*/
val shortcuts: CompatV24.ShortcutGroup?
}

/**
* Provides a [CompatV24.Shortcut], from the shortcut keys and the resource id of its description.
*/
fun AnkiActivityProvider.shortcut(shortcut: String, @StringRes labelRes: Int) =
CompatV24.Shortcut(shortcut, ankiActivity.getString(labelRes))

/**
* Provides a [CompatV24.Shortcut], from the shortcut keys and the function from anki strings.
*/
fun shortcut(shortcut: String, getTranslation: Translations.() -> String) =
CompatV24.Shortcut(shortcut, getTranslation(TR))
Loading

0 comments on commit 6b75e7b

Please sign in to comment.