Skip to content

Commit ebeaac9

Browse files
authored
Fix GutenbergKit editor issues during dark mode toggle (#22135)
* Remove isEditorStarted flag to fix content loss during activity restart The isEditorStarted flag was preventing editor reinitialization during activity restarts, but this caused content loss because: 1. Activity restart triggers new fragment creation 2. isEditorStarted gets reset to false in onDestroy() 3. startWithEditorSettings() gets called but always rebuilds from original data 4. Current editor content is lost instead of being preserved Root cause: The flag was designed to prevent duplicate initialization within a single fragment instance, but during activity restarts, we WANT the editor to reinitialize with the current content, not block reinitialization entirely. Changes: - Remove isEditorStarted field and related state management - Remove isEditorStarted checks in startWithEditorSettings() - Allow editor to reinitialize after activity restart - Fixes content loss during dark mode toggle * Fix double ViewPager setup during auth state changes Prevent duplicate editor initialization by: 1. Only calling fetchWpComCookies() if not already in Success state 2. Adding ViewPager state check to prevent duplicate setup 3. Using distinctUntilChanged() to prevent duplicate LiveData emissions 4. Consolidating Success handling logic in handleSuccessfulAuth() Changes: - Check auth state before calling fetchWpComCookies() - Add ViewPager adapter check with error logging for duplicate setup attempts - Add distinctUntilChanged() to auth state observer - Extract handleSuccessfulAuth() for consistent Success state handling - Remove debug logging from production code
1 parent 205b7da commit ebeaac9

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import androidx.fragment.app.FragmentManager
4141
import androidx.fragment.app.FragmentPagerAdapter
4242
import androidx.lifecycle.LiveData
4343
import androidx.lifecycle.ViewModelProvider
44+
import androidx.lifecycle.distinctUntilChanged
4445
import com.automattic.android.tracks.crashlogging.CrashLogging
4546
import com.automattic.android.tracks.crashlogging.JsException
4647
import com.automattic.android.tracks.crashlogging.JsExceptionCallback
@@ -617,8 +618,13 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
617618
)
618619
}
619620
siteModel.isWPCom && !siteModel.isWPComAtomic && siteModel.isPrivate -> {
620-
showIfNecessary(fragmentManager)
621-
editPostAuthViewModel.fetchWpComCookies()
621+
val currentAuthState = editPostAuthViewModel.wpComCookieAuthState.value
622+
if (currentAuthState !is EditPostAuthViewModel.WpComCookieAuthState.Success) {
623+
showIfNecessary(fragmentManager)
624+
editPostAuthViewModel.fetchWpComCookies()
625+
} else {
626+
handleSuccessfulWpComCookieAuthState()
627+
}
622628
}
623629
else -> {
624630
setupViewPager()
@@ -991,18 +997,13 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
991997
updateUIForDestination(destination)
992998
}
993999

994-
editPostAuthViewModel.wpComCookieAuthState.observe(this) { authState ->
1000+
editPostAuthViewModel.wpComCookieAuthState.distinctUntilChanged().observe(this) { authState ->
9951001
when (authState) {
9961002
is EditPostAuthViewModel.WpComCookieAuthState.Loading -> {
9971003
showIfNecessary(supportFragmentManager)
9981004
}
9991005
is EditPostAuthViewModel.WpComCookieAuthState.Success -> {
1000-
if (isShowing(supportFragmentManager)) {
1001-
setupViewPager()
1002-
dismissIfNecessary(supportFragmentManager)
1003-
} else {
1004-
setupViewPager()
1005-
}
1006+
handleSuccessfulWpComCookieAuthState()
10061007
}
10071008
is EditPostAuthViewModel.WpComCookieAuthState.Error -> {
10081009
if (isShowing(supportFragmentManager)) {
@@ -1078,7 +1079,26 @@ class EditPostActivity : BaseAppCompatActivity(), EditorFragmentActivity, Editor
10781079
invalidateOptionsMenu()
10791080
}
10801081

1082+
private fun handleSuccessfulWpComCookieAuthState() {
1083+
if (isShowing(supportFragmentManager)) {
1084+
setupViewPager()
1085+
dismissIfNecessary(supportFragmentManager)
1086+
} else {
1087+
setupViewPager()
1088+
}
1089+
}
1090+
10811091
private fun setupViewPager() {
1092+
// Check if ViewPager is already configured
1093+
if (viewPager?.adapter != null) {
1094+
AppLog.e(
1095+
AppLog.T.EDITOR,
1096+
"EditPostActivity: setupViewPager() called but ViewPager already has adapter" +
1097+
" - possible duplicate setup"
1098+
)
1099+
return
1100+
}
1101+
10821102
// Set up the ViewPager with the sections adapter.
10831103
viewPager = findViewById(R.id.pager)
10841104
viewPager?.adapter = sectionsPagerAdapter

WordPress/src/main/java/org/wordpress/android/ui/posts/editor/GutenbergKitEditorFragment.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
6666
private var openMediaLibraryListener: OpenMediaLibraryListener? = null
6767
private var onLogJsExceptionListener: LogJsExceptionListener? = null
6868

69-
private var isEditorStarted = false
7069
private var isEditorDidMount = false
7170
private var rootView: View? = null
7271

@@ -78,7 +77,6 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
7877

7978
if (savedInstanceState != null) {
8079
isHtmlModeEnabled = savedInstanceState.getBoolean(KEY_HTML_MODE_ENABLED)
81-
isEditorStarted = savedInstanceState.getBoolean(KEY_EDITOR_STARTED)
8280
isEditorDidMount = savedInstanceState.getBoolean(KEY_EDITOR_DID_MOUNT)
8381
mFeaturedImageId = savedInstanceState.getLong(ARG_FEATURED_IMAGE_ID)
8482
}
@@ -235,7 +233,6 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
235233

236234
override fun onSaveInstanceState(outState: Bundle) {
237235
outState.putBoolean(KEY_HTML_MODE_ENABLED, isHtmlModeEnabled)
238-
outState.putBoolean(KEY_EDITOR_STARTED, isEditorStarted)
239236
outState.putBoolean(KEY_EDITOR_DID_MOUNT, isEditorDidMount)
240237
outState.putLong(ARG_FEATURED_IMAGE_ID, mFeaturedImageId)
241238
}
@@ -464,7 +461,6 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
464461
historyChangeListener = null
465462
featuredImageChangeListener = null
466463
}
467-
isEditorStarted = false
468464
super.onDestroy()
469465
}
470466

@@ -507,12 +503,11 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
507503
}
508504

509505
fun startWithEditorSettings(editorSettings: String) {
510-
if (gutenbergView == null || isEditorStarted) {
506+
if (gutenbergView == null) {
511507
return
512508
}
513509

514510
val config = buildEditorConfiguration(editorSettings)
515-
isEditorStarted = true
516511
gutenbergView?.start(config)
517512
}
518513

@@ -583,7 +578,6 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
583578
companion object {
584579
private const val GUTENBERG_EDITOR_NAME = "gutenberg"
585580
private const val KEY_HTML_MODE_ENABLED = "KEY_HTML_MODE_ENABLED"
586-
private const val KEY_EDITOR_STARTED = "KEY_EDITOR_STARTED"
587581
private const val KEY_EDITOR_DID_MOUNT = "KEY_EDITOR_DID_MOUNT"
588582
private const val ARG_IS_NEW_POST = "param_is_new_post"
589583
private const val ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = "param_gutenberg_web_view_auth_data"

0 commit comments

Comments
 (0)