Skip to content

Commit 120ae9e

Browse files
ShaanNarendrandavid-allison
authored andcommitted
Refactor: Added utility class NoteIntentParser.kt
1 parent f04cb45 commit 120ae9e

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorActivity.kt

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,46 +63,7 @@ class NoteEditorActivity :
6363

6464
setContentView(R.layout.note_editor)
6565

66-
/**
67-
* The [NoteEditorActivity] activity supports multiple note editing workflows using fragments.
68-
* It dynamically chooses the appropriate fragment to load and the arguments to pass to it,
69-
* based on intent extras provided at launch time.
70-
*
71-
* - [FRAGMENT_NAME_EXTRA]: Fully qualified name of the fragment class to instantiate.
72-
* If set to [NoteEditorFragment], the activity initializes it with the arguments in
73-
* [FRAGMENT_ARGS_EXTRA].
74-
*
75-
* - [FRAGMENT_ARGS_EXTRA]: Bundle containing parameters for the fragment (e.g. note ID,
76-
* deck ID, etc.). Used to populate fields or determine editor behavior.
77-
*
78-
* This logic is encapsulated in the [launcher] assignment, which selects the correct
79-
* fragment mode (e.g. add note, edit note) based on intent contents.
80-
*/
81-
val launcher =
82-
if (intent.hasExtra(FRAGMENT_NAME_EXTRA)) {
83-
val fragmentClassName = intent.getStringExtra(FRAGMENT_NAME_EXTRA)
84-
if (fragmentClassName == NoteEditorFragment::class.java.name) {
85-
val fragmentArgs = intent.getBundleExtra(FRAGMENT_ARGS_EXTRA)
86-
if (fragmentArgs != null) {
87-
NoteEditorLauncher.PassArguments(fragmentArgs)
88-
} else {
89-
NoteEditorLauncher.AddNote()
90-
}
91-
} else {
92-
NoteEditorLauncher.AddNote()
93-
}
94-
} else {
95-
// Regular NoteEditor intent handling
96-
intent.getBundleExtra(FRAGMENT_ARGS_EXTRA)?.let { fragmentArgs ->
97-
// If FRAGMENT_ARGS_EXTRA is provided, use it directly
98-
NoteEditorLauncher.PassArguments(fragmentArgs)
99-
} ?: intent.extras?.let { bundle ->
100-
// Check if the bundle contains FRAGMENT_ARGS_EXTRA (for launchers that wrap their args)
101-
bundle.getBundle(FRAGMENT_ARGS_EXTRA)?.let { wrappedFragmentArgs ->
102-
NoteEditorLauncher.PassArguments(wrappedFragmentArgs)
103-
} ?: NoteEditorLauncher.PassArguments(bundle)
104-
} ?: NoteEditorLauncher.AddNote()
105-
}
66+
val launcher = NoteIntentParser.parse(intent)
10667

10768
val existingFragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG)
10869

@@ -174,3 +135,48 @@ class NoteEditorActivity :
174135
}
175136
}
176137
}
138+
139+
/**
140+
* Helper to parse Intents for [NoteEditorActivity].
141+
*
142+
* It supports multiple note editing workflows using fragments by choosing the
143+
* appropriate [noteeditor.NoteEditorLauncher] based on intent extras:
144+
*
145+
* - [NoteEditorActivity.Companion.FRAGMENT_NAME_EXTRA]: Fully qualified name of the fragment class.
146+
* If set to [NoteEditorFragment], it initializes with arguments in [NoteEditorActivity.Companion.FRAGMENT_ARGS_EXTRA].
147+
*
148+
* - [NoteEditorActivity.Companion.FRAGMENT_ARGS_EXTRA]: Bundle containing parameters (note ID, deck ID, etc.).
149+
*/
150+
private object NoteIntentParser {
151+
fun parse(intent: Intent): NoteEditorLauncher =
152+
if (intent.hasExtra(FRAGMENT_NAME_EXTRA)) {
153+
handleFragmentIntent(intent)
154+
} else {
155+
handleLegacyIntent(intent)
156+
}
157+
158+
private fun handleFragmentIntent(intent: Intent): NoteEditorLauncher =
159+
intent.getStringExtra(FRAGMENT_NAME_EXTRA)?.let { fragmentName ->
160+
when (fragmentName) {
161+
NoteEditorFragment::class.java.name ->
162+
intent
163+
.getBundleExtra(FRAGMENT_ARGS_EXTRA)
164+
?.let { NoteEditorLauncher.PassArguments(it) }
165+
?: NoteEditorLauncher.AddNote()
166+
else -> NoteEditorLauncher.AddNote()
167+
}
168+
} ?: NoteEditorLauncher.AddNote()
169+
170+
private fun handleLegacyIntent(intent: Intent): NoteEditorLauncher {
171+
intent.getBundleExtra(FRAGMENT_ARGS_EXTRA)?.let { args ->
172+
return NoteEditorLauncher.PassArguments(args)
173+
}
174+
intent.extras?.let { bundle ->
175+
bundle.getBundle(FRAGMENT_ARGS_EXTRA)?.let { wrappedArgs ->
176+
return NoteEditorLauncher.PassArguments(wrappedArgs)
177+
}
178+
return NoteEditorLauncher.PassArguments(bundle)
179+
}
180+
return NoteEditorLauncher.AddNote()
181+
}
182+
}

0 commit comments

Comments
 (0)