|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Hari Srinivasan <harisrini21@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ichi2.anki |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.content.Intent |
| 21 | +import android.os.Bundle |
| 22 | +import androidx.fragment.app.Fragment |
| 23 | +import androidx.fragment.app.commit |
| 24 | +import com.ichi2.anki.android.input.ShortcutGroup |
| 25 | +import com.ichi2.anki.android.input.ShortcutGroupProvider |
| 26 | +import com.ichi2.anki.libanki.Collection |
| 27 | +import com.ichi2.anki.noteeditor.NoteEditorLauncher |
| 28 | +import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider |
| 29 | +import com.ichi2.anki.snackbar.SnackbarBuilder |
| 30 | +import timber.log.Timber |
| 31 | +import kotlin.reflect.KClass |
| 32 | +import kotlin.reflect.jvm.jvmName |
| 33 | + |
| 34 | +/** |
| 35 | + * To find the actual note Editor, @see [NoteEditorFragment] |
| 36 | + * This activity contains the NoteEditorFragment, and, on x-large screens, the previewer fragment. |
| 37 | + * It also ensures that changes in the note are transmitted to the previewer |
| 38 | + */ |
| 39 | + |
| 40 | +// TODO: Move intent handling to [NoteEditorActivity] from [NoteEditorFragment] |
| 41 | +class NoteEditorActivity : |
| 42 | + AnkiActivity(), |
| 43 | + BaseSnackbarBuilderProvider, |
| 44 | + DispatchKeyEventListener, |
| 45 | + ShortcutGroupProvider { |
| 46 | + override val baseSnackbarBuilder: SnackbarBuilder = { } |
| 47 | + |
| 48 | + lateinit var noteEditorFragment: NoteEditorFragment |
| 49 | + |
| 50 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 51 | + if (showedActivityFailedScreen(savedInstanceState)) { |
| 52 | + return |
| 53 | + } |
| 54 | + super.onCreate(savedInstanceState) |
| 55 | + if (!ensureStoragePermissions()) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + setContentView(R.layout.note_editor) |
| 60 | + |
| 61 | + /** |
| 62 | + * The [NoteEditorActivity] activity supports multiple note editing workflows using fragments. |
| 63 | + * It dynamically chooses the appropriate fragment to load and the arguments to pass to it, |
| 64 | + * based on intent extras provided at launch time. |
| 65 | + * |
| 66 | + * - [FRAGMENT_NAME_EXTRA]: Fully qualified name of the fragment class to instantiate. |
| 67 | + * If set to [NoteEditorFragment], the activity initializes it with the arguments in |
| 68 | + * [FRAGMENT_ARGS_EXTRA]. |
| 69 | + * |
| 70 | + * - [FRAGMENT_ARGS_EXTRA]: Bundle containing parameters for the fragment (e.g. note ID, |
| 71 | + * deck ID, etc.). Used to populate fields or determine editor behavior. |
| 72 | + * |
| 73 | + * This logic is encapsulated in the [launcher] assignment, which selects the correct |
| 74 | + * fragment mode (e.g. add note, edit note) based on intent contents. |
| 75 | + */ |
| 76 | + val launcher = |
| 77 | + if (intent.hasExtra(FRAGMENT_NAME_EXTRA)) { |
| 78 | + val fragmentClassName = intent.getStringExtra(FRAGMENT_NAME_EXTRA) |
| 79 | + if (fragmentClassName == NoteEditorFragment::class.java.name) { |
| 80 | + val fragmentArgs = intent.getBundleExtra(FRAGMENT_ARGS_EXTRA) |
| 81 | + if (fragmentArgs != null) { |
| 82 | + NoteEditorLauncher.PassArguments(fragmentArgs) |
| 83 | + } else { |
| 84 | + NoteEditorLauncher.AddNote() |
| 85 | + } |
| 86 | + } else { |
| 87 | + NoteEditorLauncher.AddNote() |
| 88 | + } |
| 89 | + } else { |
| 90 | + // Regular NoteEditor intent handling |
| 91 | + intent.getBundleExtra(FRAGMENT_ARGS_EXTRA)?.let { fragmentArgs -> |
| 92 | + // If FRAGMENT_ARGS_EXTRA is provided, use it directly |
| 93 | + NoteEditorLauncher.PassArguments(fragmentArgs) |
| 94 | + } ?: intent.extras?.let { bundle -> |
| 95 | + // Check if the bundle contains FRAGMENT_ARGS_EXTRA (for launchers that wrap their args) |
| 96 | + bundle.getBundle(FRAGMENT_ARGS_EXTRA)?.let { wrappedFragmentArgs -> |
| 97 | + NoteEditorLauncher.PassArguments(wrappedFragmentArgs) |
| 98 | + } ?: NoteEditorLauncher.PassArguments(bundle) |
| 99 | + } ?: NoteEditorLauncher.AddNote() |
| 100 | + } |
| 101 | + |
| 102 | + val existingFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) |
| 103 | + |
| 104 | + if (existingFragment == null) { |
| 105 | + supportFragmentManager.commit { |
| 106 | + replace(R.id.note_editor_fragment_frame, NoteEditorFragment.newInstance(launcher), FRAGMENT_TAG) |
| 107 | + setReorderingAllowed(true) |
| 108 | + /** |
| 109 | + * Initializes the noteEditorFragment reference only after the transaction is committed. |
| 110 | + * This ensures the fragment is fully created and available in the activity before |
| 111 | + * any code attempts to interact with it, preventing potential null reference issues. |
| 112 | + */ |
| 113 | + runOnCommit { |
| 114 | + noteEditorFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as NoteEditorFragment |
| 115 | + } |
| 116 | + } |
| 117 | + } else { |
| 118 | + noteEditorFragment = existingFragment as NoteEditorFragment |
| 119 | + } |
| 120 | + |
| 121 | + startLoadingCollection() |
| 122 | + } |
| 123 | + |
| 124 | + override fun onCollectionLoaded(col: Collection) { |
| 125 | + super.onCollectionLoaded(col) |
| 126 | + Timber.d("onCollectionLoaded()") |
| 127 | + registerReceiver() |
| 128 | + } |
| 129 | + |
| 130 | + override fun dispatchKeyEvent(event: android.view.KeyEvent): Boolean = |
| 131 | + noteEditorFragment.dispatchKeyEvent(event) || super.dispatchKeyEvent(event) |
| 132 | + |
| 133 | + override val shortcuts: ShortcutGroup |
| 134 | + get() = noteEditorFragment.shortcuts |
| 135 | + |
| 136 | + companion object { |
| 137 | + const val FRAGMENT_ARGS_EXTRA = "fragmentArgs" |
| 138 | + const val FRAGMENT_NAME_EXTRA = "fragmentName" |
| 139 | + const val FRAGMENT_TAG = "NoteEditorFragmentTag" |
| 140 | + |
| 141 | + /** |
| 142 | + * Creates an Intent to launch the NoteEditor activity with a specific fragment class and arguments. |
| 143 | + * |
| 144 | + * @param context The context from which the intent will be launched |
| 145 | + * @param fragmentClass The Kotlin class of the Fragment to instantiate |
| 146 | + * @param arguments Optional bundle of arguments to pass to the fragment |
| 147 | + * @param intentAction Optional action to set on the intent |
| 148 | + * @return An Intent configured to launch NoteEditor with the specified fragment |
| 149 | + */ |
| 150 | + fun getIntent( |
| 151 | + context: Context, |
| 152 | + fragmentClass: KClass<out Fragment>, |
| 153 | + arguments: Bundle? = null, |
| 154 | + intentAction: String? = null, |
| 155 | + ): Intent = |
| 156 | + Intent(context, NoteEditorActivity::class.java).apply { |
| 157 | + putExtra(FRAGMENT_NAME_EXTRA, fragmentClass.jvmName) |
| 158 | + putExtra(FRAGMENT_ARGS_EXTRA, arguments) |
| 159 | + action = intentAction |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments