Skip to content

Commit

Permalink
fix: Address editor crashes from null listeners (#21658)
Browse files Browse the repository at this point in the history
* fix: Address editor crashes from null listeners

Setting the listeners within `instantiateItem` appears problematic as
this only runs for the initial launch, but not for other events that
destroy and recreate fragment--e.g., screen rotation events.

To address this, GutenbergKit now follow the pattern of the Aztec and
GutenbergMobile editors, invoking the `initializeEditorFragment` method.
This method is described as "requesting dependency injection," which
feels like an odd statement. There is likely further opportunity to
refactor this architecture for simplicity.

* docs: Add release note

* docs: Remove `TODO` comment

Apparently the linter prohibits this statement...
  • Loading branch information
dcalhoun authored Feb 4, 2025
1 parent c2baaaf commit ead73cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
5 changes: 4 additions & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
-----


25.6
25.6.1
-----
* [*] Fix experimental editor crash when changing the device orienation. [#21658]

25.6
-----

25.5
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,25 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
}

override fun initializeEditorFragment() {
if (editorFragment is AztecEditorFragment) {
if (editorFragment is GutenbergKitEditorFragment) {
editorFragment?.onEditorContentChanged(object : GutenbergView.ContentChangeListener {
override fun onContentChanged(title: String, content: String) {
storePostViewModel.savePostWithDelay()
}
})
editorFragment?.onOpenMediaLibrary(object: GutenbergView.OpenMediaLibraryListener {
override fun onOpenMediaLibrary(config: GutenbergView.OpenMediaLibraryConfig) {
editorPhotoPicker?.allowMultipleSelection = config.multiple
val mediaType = mapAllowedTypesToMediaBrowserType(config.allowedTypes, config.multiple)
val initialSelection = when (val value = config.value) {
is GutenbergView.Value.Single -> listOf(value.value)
is GutenbergView.Value.Multiple -> value.toList()
else -> emptyList()
}
openMediaLibrary(mediaType, initialSelection)
}
})
} else if (editorFragment is AztecEditorFragment) {
val aztecEditorFragment = editorFragment as AztecEditorFragment
aztecEditorFragment.setEditorImageSettingsListener(this@EditPostActivity)
aztecEditorFragment.setMediaToolbarButtonClickListener(editorPhotoPicker)
Expand Down Expand Up @@ -2508,25 +2526,8 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
PAGE_CONTENT -> {
editorFragment = fragment as EditorFragmentAbstract
editorFragment?.setImageLoader(imageLoader)
if (isGutenbergKitEditor) {
editorFragment?.onEditorContentChanged(object : GutenbergView.ContentChangeListener {
override fun onContentChanged(title: String, content: String) {
storePostViewModel.savePostWithDelay()
}
})
editorFragment?.onOpenMediaLibrary(object: GutenbergView.OpenMediaLibraryListener {
override fun onOpenMediaLibrary(config: GutenbergView.OpenMediaLibraryConfig) {
editorPhotoPicker?.allowMultipleSelection = config.multiple
val mediaType = mapAllowedTypesToMediaBrowserType(config.allowedTypes, config.multiple)
val initialSelection = when (val value = config.value) {
is GutenbergView.Value.Single -> listOf(value.value)
is GutenbergView.Value.Multiple -> value.toList()
else -> emptyList()
}
openMediaLibrary(mediaType, initialSelection)
}
})
} else {
// Refactor GutenbergKit to rely upon this observer rather than its custom implementation
if (editorFragment !is GutenbergKitEditorFragment) {
editorFragment?.titleOrContentChanged?.observe(this@EditPostActivity) { _: Editable? ->
storePostViewModel.savePostWithDelay()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.wordpress.android.editor.BuildConfig;
import org.wordpress.android.editor.EditorEditMediaListener;
import org.wordpress.android.editor.EditorFragmentAbstract;
import org.wordpress.android.editor.EditorFragmentActivity;
import org.wordpress.android.editor.EditorImagePreviewListener;
import org.wordpress.android.editor.EditorMediaUploadListener;
import org.wordpress.android.editor.EditorThemeUpdateListener;
Expand Down Expand Up @@ -125,6 +126,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mSettings = (Map<String, Object>) getArguments().getSerializable(ARG_GUTENBERG_KIT_SETTINGS);
}

// request dependency injection. Do this after setting min/max dimensions
if (getActivity() instanceof EditorFragmentActivity) {
((EditorFragmentActivity) getActivity()).initializeEditorFragment();
}

mGutenbergView = GutenbergWebViewPool.getPreloadedWebView(requireContext());
mGutenbergView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
Expand Down

0 comments on commit ead73cb

Please sign in to comment.