@@ -50,19 +50,13 @@ import org.wordpress.gutenberg.GutenbergView.TitleAndContentCallback
5050import org.wordpress.gutenberg.GutenbergWebViewPool.getPreloadedWebView
5151import org.wordpress.gutenberg.GutenbergWebViewPool.recycleWebView
5252import org.wordpress.gutenberg.Media
53+ import org.wordpress.gutenberg.WebViewGlobal
54+ import java.io.Serializable
5355import java.util.concurrent.CountDownLatch
54- import androidx.lifecycle.ViewModelProvider
55- import org.wordpress.android.ui.posts.GutenbergKitSettings
56- import org.wordpress.android.ui.posts.GutenbergKitViewModel
57- import org.wordpress.android.WordPress
58- import javax.inject.Inject
5956
6057class GutenbergKitEditorFragment : EditorFragmentAbstract (), EditorMediaUploadListener, IHistoryListener,
6158 EditorThemeUpdateListener , GutenbergDialogPositiveClickInterface , GutenbergDialogNegativeClickInterface ,
6259 GutenbergNetworkConnectionListener {
63- @Inject lateinit var viewModelFactory: ViewModelProvider .Factory
64- private lateinit var gutenbergKitViewModel: GutenbergKitViewModel
65-
6660 private var gutenbergView: GutenbergView ? = null
6761 private var isHtmlModeEnabled = false
6862
@@ -76,29 +70,12 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
7670 private var isEditorDidMount = false
7771 private var rootView: View ? = null
7872
79- // Access settings through ViewModel
80- private val settings: GutenbergKitSettings ?
81- get() = if (::gutenbergKitViewModel.isInitialized) {
82- gutenbergKitViewModel.editorSettings.value
83- } else {
84- null
85- }
86-
8773 override fun onCreate (savedInstanceState : Bundle ? ) {
8874 super .onCreate(savedInstanceState)
8975
9076 ProfilingUtils .start(" Visual Editor Startup" )
9177 ProfilingUtils .split(" EditorFragment.onCreate" )
9278
93- // Trigger dependency injection
94- (requireActivity().applicationContext as WordPress ).component().inject(this )
95-
96- // Initialize shared ViewModel (same scope as Activity) - after DI is complete
97- gutenbergKitViewModel = ViewModelProvider (
98- requireActivity(),
99- viewModelFactory
100- )[GutenbergKitViewModel ::class .java]
101-
10279 if (savedInstanceState != null ) {
10380 isHtmlModeEnabled = savedInstanceState.getBoolean(KEY_HTML_MODE_ENABLED )
10481 isEditorStarted = savedInstanceState.getBoolean(KEY_EDITOR_STARTED )
@@ -110,6 +87,11 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
11087 override fun onCreateView (
11188 inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ?
11289 ): View ? {
90+ if (arguments != null ) {
91+ @Suppress(" UNCHECKED_CAST" , " DEPRECATION" )
92+ settings = requireArguments().getSerializable(ARG_GUTENBERG_KIT_SETTINGS ) as Map <String , Any ?>?
93+ }
94+
11395 // request dependency injection. Do this after setting min/max dimensions
11496 if (activity is EditorFragmentActivity ) {
11597 (activity as EditorFragmentActivity ).initializeEditorFragment()
@@ -235,6 +217,17 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
235217 }
236218 }
237219
220+ // Type-safe settings accessors
221+ private inline fun <reified T > Map <String , Any ?>.getSetting (key : String ): T ? = this [key] as ? T
222+ private inline fun <reified T > Map <String , Any ?>.getSettingOrDefault (key : String , default : T ): T =
223+ getSetting(key) ? : default
224+
225+ private fun Map <String , Any ?>.getStringArray (key : String ): Array <String > =
226+ getSetting<Array <String ?>>(key)?.asSequence()?.filterNotNull()?.toList()?.toTypedArray() ? : emptyArray()
227+
228+ private fun Map <String , Any ?>.getWebViewGlobals (key : String ): List <WebViewGlobal > =
229+ getSetting<List <WebViewGlobal >>(key) ? : emptyList()
230+
238231 // View extension functions
239232 private fun View?.setVisibleOrGone (visible : Boolean ) {
240233 this ?.visibility = if (visible) View .VISIBLE else View .GONE
@@ -524,31 +517,39 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
524517 }
525518
526519 private fun buildEditorConfiguration (editorSettings : String ): EditorConfiguration {
527- val kitSettings = settings!!
528-
529- val postId = kitSettings.postId?.let { if (it == 0 ) - 1 else it }
530- val firstNamespace = kitSettings.siteApiNamespace.firstOrNull() ? : " "
531- val editorAssetsEndpoint = " ${kitSettings.siteApiRoot} wpcom/v2/${firstNamespace} editor-assets"
532-
533- return EditorConfiguration .Builder ()
534- .setTitle(kitSettings.postTitle ? : " " )
535- .setContent(kitSettings.postContent ? : " " )
536- .setPostId(postId)
537- .setPostType(kitSettings.postType)
538- .setThemeStyles(kitSettings.themeStyles)
539- .setPlugins(kitSettings.plugins)
540- .setSiteApiRoot(kitSettings.siteApiRoot)
541- .setSiteApiNamespace(kitSettings.siteApiNamespace.toTypedArray())
542- .setNamespaceExcludedPaths(kitSettings.namespaceExcludedPaths.toTypedArray())
543- .setAuthHeader(kitSettings.authHeader)
544- .setWebViewGlobals(kitSettings.webViewGlobals)
545- .setEditorSettings(editorSettings)
546- .setLocale(kitSettings.locale)
547- .setEditorAssetsEndpoint(editorAssetsEndpoint)
548- .setCachedAssetHosts(setOf (" s0.wp.com" , UrlUtils .getHost(kitSettings.siteURL)))
549- .setEnableAssetCaching(true )
550- .setCookies(kitSettings.cookies)
551- .build()
520+ val settingsMap = settings!!
521+
522+ return settingsMap.run {
523+ val postId = getSetting<Int >(" postId" ).let { if (it == 0 ) - 1 else it }
524+ val siteURL = getSetting<String >(" siteURL" )
525+ val siteApiRoot = getSetting<String >(" siteApiRoot" )
526+ val siteApiNamespace = getStringArray(" siteApiNamespace" )
527+ val firstNamespace = siteApiNamespace.firstOrNull() ? : " "
528+ val editorAssetsEndpoint = " ${siteApiRoot} wpcom/v2/${firstNamespace} editor-assets"
529+ val cookies = getSetting<Map <String , String >>(" cookies" ) ? : emptyMap()
530+ val namespaceExcludedPaths = getStringArray(" namespaceExcludedPaths" )
531+ val webViewGlobals = getWebViewGlobals(" webViewGlobals" )
532+
533+ EditorConfiguration .Builder ()
534+ .setTitle(getSetting<String >(" postTitle" ) ? : " " )
535+ .setContent(getSetting<String >(" postContent" ) ? : " " )
536+ .setPostId(postId)
537+ .setPostType(getSetting<String >(" postType" ))
538+ .setThemeStyles(getSettingOrDefault(" themeStyles" , false ))
539+ .setPlugins(getSettingOrDefault(" plugins" , false ))
540+ .setSiteApiRoot(getSetting<String >(" siteApiRoot" ) ? : " " )
541+ .setSiteApiNamespace(siteApiNamespace)
542+ .setNamespaceExcludedPaths(namespaceExcludedPaths)
543+ .setAuthHeader(getSetting<String >(" authHeader" ) ? : " " )
544+ .setWebViewGlobals(webViewGlobals)
545+ .setEditorSettings(editorSettings)
546+ .setLocale(getSetting<String >(" locale" ))
547+ .setEditorAssetsEndpoint(editorAssetsEndpoint)
548+ .setCachedAssetHosts(setOf (" s0.wp.com" , UrlUtils .getHost(siteURL)))
549+ .setEnableAssetCaching(true )
550+ .setCookies(cookies)
551+ .build()
552+ }
552553 }
553554
554555 override fun showNotice (message : String? ) {
@@ -588,22 +589,28 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
588589 private const val ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = " param_gutenberg_web_view_auth_data"
589590 const val ARG_FEATURED_IMAGE_ID : String = " featured_image_id"
590591 const val ARG_JETPACK_FEATURES_ENABLED : String = " jetpack_features_enabled"
592+ const val ARG_GUTENBERG_KIT_SETTINGS : String = " gutenberg_kit_settings"
591593
592594 private const val CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101
593595 private const val CAPTURE_VIDEO_PERMISSION_REQUEST_CODE = 102
594596
597+ private var settings: Map <String , Any ?>? = null
598+
595599 fun newInstance (
596600 context : Context ,
597601 isNewPost : Boolean ,
598602 webViewAuthorizationData : GutenbergWebViewAuthorizationData ? ,
599603 jetpackFeaturesEnabled : Boolean ,
604+ settings : Map <String , Any ?>?
600605 ): GutenbergKitEditorFragment {
601606 val fragment = GutenbergKitEditorFragment ()
602607 val args = Bundle ()
603608 args.putBoolean(ARG_IS_NEW_POST , isNewPost)
604609 args.putBoolean(ARG_JETPACK_FEATURES_ENABLED , jetpackFeaturesEnabled)
610+ args.putSerializable(ARG_GUTENBERG_KIT_SETTINGS , settings as Serializable ? )
605611 fragment.setArguments(args)
606612 val db = getDatabase(context)
613+ GutenbergKitEditorFragment .settings = settings
607614 db?.addParcel(ARG_GUTENBERG_WEB_VIEW_AUTH_DATA , webViewAuthorizationData)
608615 return fragment
609616 }
0 commit comments