-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMendixReactFragment.kt
More file actions
126 lines (105 loc) · 3.96 KB
/
Copy pathMendixReactFragment.kt
File metadata and controls
126 lines (105 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.mendix.mendixnative.fragment
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.mendix.mendixnative.DevAppMenuHandler
import com.mendix.mendixnative.MendixInitializer
import com.mendix.mendixnative.activity.LaunchScreenHandler
import com.mendix.mendixnative.react.MendixApp
import com.mendix.mendixnative.util.MendixDoubleTapRecognizer
/**
* Class used for Sample apps
*/
open class MendixReactFragment : ReactFragment(), MendixReactFragmentView {
protected var mendixApp: MendixApp? = null
private lateinit var mendixInitializer: MendixInitializer
private var doubleTapReloadRecognizer = MendixDoubleTapRecognizer()
val currentDevSupportManager: DevSupportManager?
get() = reactHost?.devSupportManager
companion object {
const val ARG_MENDIX_APP = "arg_mendix_app"
const val ARG_CLEAR_DATA = "arg_clear_data"
const val ARG_USE_DEVELOPER_SUPPORT = "arg_use_developer_support"
const val ARG_COMPONENT_NAME = "arg_component_name"
const val ARG_LAUNCH_OPTIONS = "arg_launch_options"
fun newInstance(
componentName: String,
launchOptions: Bundle?,
mendixApp: MendixApp,
clearData: Boolean,
useDeveloperSupport: Boolean
): MendixReactFragment {
val mendixReactFragment = MendixReactFragment()
val args = Bundle()
args.putString(ARG_COMPONENT_NAME, componentName)
args.putBundle(ARG_LAUNCH_OPTIONS, launchOptions)
args.putBoolean(ARG_CLEAR_DATA, clearData)
args.putBoolean(ARG_USE_DEVELOPER_SUPPORT, useDeveloperSupport)
args.putSerializable(ARG_MENDIX_APP, mendixApp)
mendixReactFragment.arguments = args
return mendixReactFragment
}
}
override fun onCreate(savedInstanceState: Bundle?) {
(activity !is LaunchScreenHandler).let {
if (it) throw java.lang.IllegalArgumentException("The Activity needs to implement LaunchScreenHandler")
}
if (mendixApp == null) {
mendixApp = requireArguments().getSerializable(ARG_MENDIX_APP) as MendixApp?
?: throw IllegalArgumentException("Mendix app is required")
}
if (reactHost == null) {
throw IllegalStateException("ReactHost is not set. Make sure that the activity extends MendixReactActivity or that you set the reactHost manually")
}
val clearData = requireArguments().getBoolean(ARG_CLEAR_DATA, false)
val hasRNDeveloperSupport = requireArguments().getBoolean(ARG_USE_DEVELOPER_SUPPORT, false)
mendixInitializer =
MendixInitializer(requireActivity(), reactHost!!, hasRNDeveloperSupport).also {
it.onCreate(mendixApp!!, clearData)
}
super.onCreate(savedInstanceState)
}
fun onNewIntent(intent: Intent) {
if (reactNativeHost.hasInstance()) {
reactNativeHost.reactInstanceManager.onNewIntent(intent);
}
}
override fun onDestroy() {
mendixInitializer.onDestroy()
super.onDestroy()
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_MENU || doubleTapReloadRecognizer.didDoubleTapBacktick(
keyCode,
view
)
) {
showDevAppMenu()
return true
}
return super.onKeyUp(keyCode, event)
}
override fun showDevAppMenu() {
activity?.let {
currentDevSupportManager?.showDevOptionsDialog();
}
}
open fun onCloseProjectSelected() {
// Closing shake detection to avoid dialog from triggering while closing
mendixInitializer.stopShakeDetector();
}
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
return mendixInitializer.dispatchTouchEvent(ev)
}
}
interface MendixReactFragmentView : DevAppMenuHandler, TouchEventDispatcher, BackButtonHandler {
fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean
}
interface TouchEventDispatcher {
fun dispatchTouchEvent(ev: MotionEvent?): Boolean
}
interface BackButtonHandler {
fun onBackPressed(): Boolean
}