@@ -50,19 +50,13 @@ import org.wordpress.gutenberg.GutenbergView.TitleAndContentCallback
50
50
import org.wordpress.gutenberg.GutenbergWebViewPool.getPreloadedWebView
51
51
import org.wordpress.gutenberg.GutenbergWebViewPool.recycleWebView
52
52
import org.wordpress.gutenberg.Media
53
+ import org.wordpress.gutenberg.WebViewGlobal
54
+ import java.io.Serializable
53
55
import 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
59
56
60
57
class GutenbergKitEditorFragment : EditorFragmentAbstract (), EditorMediaUploadListener, IHistoryListener,
61
58
EditorThemeUpdateListener , GutenbergDialogPositiveClickInterface , GutenbergDialogNegativeClickInterface ,
62
59
GutenbergNetworkConnectionListener {
63
- @Inject lateinit var viewModelFactory: ViewModelProvider .Factory
64
- private lateinit var gutenbergKitViewModel: GutenbergKitViewModel
65
-
66
60
private var gutenbergView: GutenbergView ? = null
67
61
private var isHtmlModeEnabled = false
68
62
@@ -77,29 +71,12 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
77
71
private var rootView: View ? = null
78
72
private var isXPostsEnabled: Boolean = false
79
73
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
-
88
74
override fun onCreate (savedInstanceState : Bundle ? ) {
89
75
super .onCreate(savedInstanceState)
90
76
91
77
ProfilingUtils .start(" Visual Editor Startup" )
92
78
ProfilingUtils .split(" EditorFragment.onCreate" )
93
79
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
-
103
80
if (savedInstanceState != null ) {
104
81
isHtmlModeEnabled = savedInstanceState.getBoolean(KEY_HTML_MODE_ENABLED )
105
82
isEditorStarted = savedInstanceState.getBoolean(KEY_EDITOR_STARTED )
@@ -111,6 +88,11 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
111
88
override fun onCreateView (
112
89
inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ?
113
90
): View ? {
91
+ if (arguments != null ) {
92
+ @Suppress(" UNCHECKED_CAST" , " DEPRECATION" )
93
+ settings = requireArguments().getSerializable(ARG_GUTENBERG_KIT_SETTINGS ) as Map <String , Any ?>?
94
+ }
95
+
114
96
// request dependency injection. Do this after setting min/max dimensions
115
97
if (activity is EditorFragmentActivity ) {
116
98
(activity as EditorFragmentActivity ).initializeEditorFragment()
@@ -261,6 +243,17 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
261
243
}
262
244
}
263
245
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
+
264
257
// View extension functions
265
258
private fun View?.setVisibleOrGone (visible : Boolean ) {
266
259
this ?.visibility = if (visible) View .VISIBLE else View .GONE
@@ -554,31 +547,39 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
554
547
}
555
548
556
549
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
+ }
582
583
}
583
584
584
585
override fun showNotice (message : String? ) {
@@ -618,22 +619,28 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
618
619
private const val ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = " param_gutenberg_web_view_auth_data"
619
620
const val ARG_FEATURED_IMAGE_ID : String = " featured_image_id"
620
621
const val ARG_JETPACK_FEATURES_ENABLED : String = " jetpack_features_enabled"
622
+ const val ARG_GUTENBERG_KIT_SETTINGS : String = " gutenberg_kit_settings"
621
623
622
624
private const val CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101
623
625
private const val CAPTURE_VIDEO_PERMISSION_REQUEST_CODE = 102
624
626
627
+ private var settings: Map <String , Any ?>? = null
628
+
625
629
fun newInstance (
626
630
context : Context ,
627
631
isNewPost : Boolean ,
628
632
webViewAuthorizationData : GutenbergWebViewAuthorizationData ? ,
629
633
jetpackFeaturesEnabled : Boolean ,
634
+ settings : Map <String , Any ?>?
630
635
): GutenbergKitEditorFragment {
631
636
val fragment = GutenbergKitEditorFragment ()
632
637
val args = Bundle ()
633
638
args.putBoolean(ARG_IS_NEW_POST , isNewPost)
634
639
args.putBoolean(ARG_JETPACK_FEATURES_ENABLED , jetpackFeaturesEnabled)
640
+ args.putSerializable(ARG_GUTENBERG_KIT_SETTINGS , settings as Serializable ? )
635
641
fragment.setArguments(args)
636
642
val db = getDatabase(context)
643
+ GutenbergKitEditorFragment .settings = settings
637
644
db?.addParcel(ARG_GUTENBERG_WEB_VIEW_AUTH_DATA , webViewAuthorizationData)
638
645
return fragment
639
646
}
0 commit comments