@@ -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
@@ -77,29 +71,12 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
7771 private var rootView: View ? = null
7872 private var isXPostsEnabled: Boolean = false
7973
80- // Access settings through ViewModel
81- private val settings: GutenbergKitSettings ?
82- get() = if (::gutenbergKitViewModel.isInitialized) {
83- gutenbergKitViewModel.editorSettings.value
84- } else {
85- null
86- }
87-
8874 override fun onCreate (savedInstanceState : Bundle ? ) {
8975 super .onCreate(savedInstanceState)
9076
9177 ProfilingUtils .start(" Visual Editor Startup" )
9278 ProfilingUtils .split(" EditorFragment.onCreate" )
9379
94- // Trigger dependency injection
95- (requireActivity().applicationContext as WordPress ).component().inject(this )
96-
97- // Initialize shared ViewModel (same scope as Activity) - after DI is complete
98- gutenbergKitViewModel = ViewModelProvider (
99- requireActivity(),
100- viewModelFactory
101- )[GutenbergKitViewModel ::class .java]
102-
10380 if (savedInstanceState != null ) {
10481 isHtmlModeEnabled = savedInstanceState.getBoolean(KEY_HTML_MODE_ENABLED )
10582 isEditorStarted = savedInstanceState.getBoolean(KEY_EDITOR_STARTED )
@@ -111,6 +88,11 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
11188 override fun onCreateView (
11289 inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ?
11390 ): View ? {
91+ if (arguments != null ) {
92+ @Suppress(" UNCHECKED_CAST" , " DEPRECATION" )
93+ settings = requireArguments().getSerializable(ARG_GUTENBERG_KIT_SETTINGS ) as Map <String , Any ?>?
94+ }
95+
11496 // request dependency injection. Do this after setting min/max dimensions
11597 if (activity is EditorFragmentActivity ) {
11698 (activity as EditorFragmentActivity ).initializeEditorFragment()
@@ -261,6 +243,17 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
261243 }
262244 }
263245
246+ // Type-safe settings accessors
247+ private inline fun <reified T > Map <String , Any ?>.getSetting (key : String ): T ? = this [key] as ? T
248+ private inline fun <reified T > Map <String , Any ?>.getSettingOrDefault (key : String , default : T ): T =
249+ getSetting(key) ? : default
250+
251+ private fun Map <String , Any ?>.getStringArray (key : String ): Array <String > =
252+ getSetting<Array <String ?>>(key)?.asSequence()?.filterNotNull()?.toList()?.toTypedArray() ? : emptyArray()
253+
254+ private fun Map <String , Any ?>.getWebViewGlobals (key : String ): List <WebViewGlobal > =
255+ getSetting<List <WebViewGlobal >>(key) ? : emptyList()
256+
264257 // View extension functions
265258 private fun View?.setVisibleOrGone (visible : Boolean ) {
266259 this ?.visibility = if (visible) View .VISIBLE else View .GONE
@@ -554,31 +547,39 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
554547 }
555548
556549 private fun buildEditorConfiguration (editorSettings : String ): EditorConfiguration {
557- val kitSettings = settings!!
558-
559- val postId = kitSettings.postId?.let { if (it == 0 ) - 1 else it }
560- val firstNamespace = kitSettings.siteApiNamespace.firstOrNull() ? : " "
561- val editorAssetsEndpoint = " ${kitSettings.siteApiRoot} wpcom/v2/${firstNamespace} editor-assets"
562-
563- return EditorConfiguration .Builder ()
564- .setTitle(kitSettings.postTitle ? : " " )
565- .setContent(kitSettings.postContent ? : " " )
566- .setPostId(postId)
567- .setPostType(kitSettings.postType)
568- .setThemeStyles(kitSettings.themeStyles)
569- .setPlugins(kitSettings.plugins)
570- .setSiteApiRoot(kitSettings.siteApiRoot)
571- .setSiteApiNamespace(kitSettings.siteApiNamespace.toTypedArray())
572- .setNamespaceExcludedPaths(kitSettings.namespaceExcludedPaths.toTypedArray())
573- .setAuthHeader(kitSettings.authHeader)
574- .setWebViewGlobals(kitSettings.webViewGlobals)
575- .setEditorSettings(editorSettings)
576- .setLocale(kitSettings.locale)
577- .setEditorAssetsEndpoint(editorAssetsEndpoint)
578- .setCachedAssetHosts(setOf (" s0.wp.com" , UrlUtils .getHost(kitSettings.siteURL)))
579- .setEnableAssetCaching(true )
580- .setCookies(kitSettings.cookies)
581- .build()
550+ val settingsMap = settings!!
551+
552+ return settingsMap.run {
553+ val postId = getSetting<Int >(" postId" ).let { if (it == 0 ) - 1 else it }
554+ val siteURL = getSetting<String >(" siteURL" )
555+ val siteApiRoot = getSetting<String >(" siteApiRoot" )
556+ val siteApiNamespace = getStringArray(" siteApiNamespace" )
557+ val firstNamespace = siteApiNamespace.firstOrNull() ? : " "
558+ val editorAssetsEndpoint = " ${siteApiRoot} wpcom/v2/${firstNamespace} editor-assets"
559+ val cookies = getSetting<Map <String , String >>(" cookies" ) ? : emptyMap()
560+ val namespaceExcludedPaths = getStringArray(" namespaceExcludedPaths" )
561+ val webViewGlobals = getWebViewGlobals(" webViewGlobals" )
562+
563+ EditorConfiguration .Builder ()
564+ .setTitle(getSetting<String >(" postTitle" ) ? : " " )
565+ .setContent(getSetting<String >(" postContent" ) ? : " " )
566+ .setPostId(postId)
567+ .setPostType(getSetting<String >(" postType" ))
568+ .setThemeStyles(getSettingOrDefault(" themeStyles" , false ))
569+ .setPlugins(getSettingOrDefault(" plugins" , false ))
570+ .setSiteApiRoot(getSetting<String >(" siteApiRoot" ) ? : " " )
571+ .setSiteApiNamespace(siteApiNamespace)
572+ .setNamespaceExcludedPaths(namespaceExcludedPaths)
573+ .setAuthHeader(getSetting<String >(" authHeader" ) ? : " " )
574+ .setWebViewGlobals(webViewGlobals)
575+ .setEditorSettings(editorSettings)
576+ .setLocale(getSetting<String >(" locale" ))
577+ .setEditorAssetsEndpoint(editorAssetsEndpoint)
578+ .setCachedAssetHosts(setOf (" s0.wp.com" , UrlUtils .getHost(siteURL)))
579+ .setEnableAssetCaching(true )
580+ .setCookies(cookies)
581+ .build()
582+ }
582583 }
583584
584585 override fun showNotice (message : String? ) {
@@ -618,22 +619,28 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
618619 private const val ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = " param_gutenberg_web_view_auth_data"
619620 const val ARG_FEATURED_IMAGE_ID : String = " featured_image_id"
620621 const val ARG_JETPACK_FEATURES_ENABLED : String = " jetpack_features_enabled"
622+ const val ARG_GUTENBERG_KIT_SETTINGS : String = " gutenberg_kit_settings"
621623
622624 private const val CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101
623625 private const val CAPTURE_VIDEO_PERMISSION_REQUEST_CODE = 102
624626
627+ private var settings: Map <String , Any ?>? = null
628+
625629 fun newInstance (
626630 context : Context ,
627631 isNewPost : Boolean ,
628632 webViewAuthorizationData : GutenbergWebViewAuthorizationData ? ,
629633 jetpackFeaturesEnabled : Boolean ,
634+ settings : Map <String , Any ?>?
630635 ): GutenbergKitEditorFragment {
631636 val fragment = GutenbergKitEditorFragment ()
632637 val args = Bundle ()
633638 args.putBoolean(ARG_IS_NEW_POST , isNewPost)
634639 args.putBoolean(ARG_JETPACK_FEATURES_ENABLED , jetpackFeaturesEnabled)
640+ args.putSerializable(ARG_GUTENBERG_KIT_SETTINGS , settings as Serializable ? )
635641 fragment.setArguments(args)
636642 val db = getDatabase(context)
643+ GutenbergKitEditorFragment .settings = settings
637644 db?.addParcel(ARG_GUTENBERG_WEB_VIEW_AUTH_DATA , webViewAuthorizationData)
638645 return fragment
639646 }
0 commit comments