@@ -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
@@ -76,29 +70,12 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
76
70
private var isEditorDidMount = false
77
71
private var rootView: View ? = null
78
72
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
-
87
73
override fun onCreate (savedInstanceState : Bundle ? ) {
88
74
super .onCreate(savedInstanceState)
89
75
90
76
ProfilingUtils .start(" Visual Editor Startup" )
91
77
ProfilingUtils .split(" EditorFragment.onCreate" )
92
78
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
-
102
79
if (savedInstanceState != null ) {
103
80
isHtmlModeEnabled = savedInstanceState.getBoolean(KEY_HTML_MODE_ENABLED )
104
81
isEditorStarted = savedInstanceState.getBoolean(KEY_EDITOR_STARTED )
@@ -110,6 +87,11 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
110
87
override fun onCreateView (
111
88
inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ?
112
89
): View ? {
90
+ if (arguments != null ) {
91
+ @Suppress(" UNCHECKED_CAST" , " DEPRECATION" )
92
+ settings = requireArguments().getSerializable(ARG_GUTENBERG_KIT_SETTINGS ) as Map <String , Any ?>?
93
+ }
94
+
113
95
// request dependency injection. Do this after setting min/max dimensions
114
96
if (activity is EditorFragmentActivity ) {
115
97
(activity as EditorFragmentActivity ).initializeEditorFragment()
@@ -235,6 +217,17 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
235
217
}
236
218
}
237
219
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
+
238
231
// View extension functions
239
232
private fun View?.setVisibleOrGone (visible : Boolean ) {
240
233
this ?.visibility = if (visible) View .VISIBLE else View .GONE
@@ -524,31 +517,39 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
524
517
}
525
518
526
519
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
+ }
552
553
}
553
554
554
555
override fun showNotice (message : String? ) {
@@ -588,22 +589,28 @@ class GutenbergKitEditorFragment : EditorFragmentAbstract(), EditorMediaUploadLi
588
589
private const val ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = " param_gutenberg_web_view_auth_data"
589
590
const val ARG_FEATURED_IMAGE_ID : String = " featured_image_id"
590
591
const val ARG_JETPACK_FEATURES_ENABLED : String = " jetpack_features_enabled"
592
+ const val ARG_GUTENBERG_KIT_SETTINGS : String = " gutenberg_kit_settings"
591
593
592
594
private const val CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101
593
595
private const val CAPTURE_VIDEO_PERMISSION_REQUEST_CODE = 102
594
596
597
+ private var settings: Map <String , Any ?>? = null
598
+
595
599
fun newInstance (
596
600
context : Context ,
597
601
isNewPost : Boolean ,
598
602
webViewAuthorizationData : GutenbergWebViewAuthorizationData ? ,
599
603
jetpackFeaturesEnabled : Boolean ,
604
+ settings : Map <String , Any ?>?
600
605
): GutenbergKitEditorFragment {
601
606
val fragment = GutenbergKitEditorFragment ()
602
607
val args = Bundle ()
603
608
args.putBoolean(ARG_IS_NEW_POST , isNewPost)
604
609
args.putBoolean(ARG_JETPACK_FEATURES_ENABLED , jetpackFeaturesEnabled)
610
+ args.putSerializable(ARG_GUTENBERG_KIT_SETTINGS , settings as Serializable ? )
605
611
fragment.setArguments(args)
606
612
val db = getDatabase(context)
613
+ GutenbergKitEditorFragment .settings = settings
607
614
db?.addParcel(ARG_GUTENBERG_WEB_VIEW_AUTH_DATA , webViewAuthorizationData)
608
615
return fragment
609
616
}
0 commit comments