|
| 1 | +## Kotlin |
| 2 | + |
| 3 | +React Native Brownfield provides first-class support for Kotlin. |
| 4 | + |
| 5 | +### Linking |
| 6 | + |
| 7 | +The library is meant to work with [auto linking](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md). In case you can't use this feature, please check out the following options: |
| 8 | + |
| 9 | +<details> |
| 10 | +<summary>react-native link</summary> |
| 11 | +Run the following command in your terminal: |
| 12 | + |
| 13 | +```bash |
| 14 | + react-native link @callstack/react-native-brownfield |
| 15 | +``` |
| 16 | +</details> |
| 17 | + |
| 18 | +<details> |
| 19 | +<summary>Manually link the library on Android</summary> |
| 20 | + |
| 21 | +#### `android/settings.gradle` |
| 22 | +```groovy |
| 23 | +include ':react-native-brownfield' |
| 24 | +project(':react-native-brownfield').projectDir = new File(rootProject.projectDir, '../node_modules/@callstack/react-native-brownfield/android') |
| 25 | +``` |
| 26 | + |
| 27 | +#### `android/app/build.gradle` |
| 28 | +```groovy |
| 29 | +dependencies { |
| 30 | + ... |
| 31 | + implementation project(':react-native-brownfield') |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +#### `android/app/src/main/.../MainApplication.kt` |
| 36 | +On top, where imports are: |
| 37 | + |
| 38 | +```kotlin |
| 39 | +import com.callstack.reactnativebrownfield.ReactNativeBrownfieldPackage |
| 40 | +``` |
| 41 | + |
| 42 | +Add the `ReactNativeBrownfieldPackage` class to your list of exported packages. |
| 43 | + |
| 44 | +```kotlin |
| 45 | +val packages = listOf<ReactPackage>(MainReactPackage(), ReactNativeBrownfieldPackage()) |
| 46 | +``` |
| 47 | +</details> |
| 48 | + |
| 49 | +### API Reference |
| 50 | + |
| 51 | +#### ReactNativeBrownfield |
| 52 | + |
| 53 | +You can import the object from: |
| 54 | + |
| 55 | +```java |
| 56 | + import com.callstack.reactnativebrownfield.ReactNativeBrownfield |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +**Statics:** |
| 62 | + |
| 63 | +`initialize` |
| 64 | + |
| 65 | +A function used to initialize a React Native Brownfield singleton. Keep in mind that it **doesn't** load React Native bundle. |
| 66 | + |
| 67 | +Params: |
| 68 | + |
| 69 | +| Param | Required | Type | Description | |
| 70 | +| ----------------------- | -------- | -------------------- | --------------------------------------------------------- | |
| 71 | +| application | Yes | Application | Main application. | |
| 72 | +| rnHost | No* | ReactNativeHost | An instance of [ReactNativeHost](https://bit.ly/2ZnwgnA). | |
| 73 | +| packages | No* | List<ReactPackage> | List of your React Native Native modules. | |
| 74 | +| options | No* | HashMap<String, Any> | Map of initial options. __Options listed below.__ | |
| 75 | + |
| 76 | +> * - Those fields aren't itself required, but at least one of them is. See examples below. |
| 77 | +
|
| 78 | +Available options: |
| 79 | +- useDeveloperSupport: Boolean - Flag to use dev support. |
| 80 | +- packages: List<ReactPackage> - List of your React Native Native modules. |
| 81 | +- mainModuleName: String - Path to react native entry file. |
| 82 | + |
| 83 | +Examples: |
| 84 | + |
| 85 | +```kotlin |
| 86 | + val mReactNativeHost = object : ReactNativeHost(application) { |
| 87 | + override fun getUseDeveloperSupport(): Boolean { |
| 88 | + return BuildConfig.DEBUG |
| 89 | + } |
| 90 | + |
| 91 | + override fun getPackages(): List<ReactPackage> { |
| 92 | + return listOf<ReactPackage>(PackageList(this).getPackages()) |
| 93 | + } |
| 94 | + |
| 95 | + override fun getJSMainModuleName(): String { |
| 96 | + return "index" |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + ReactNativeBrownfield.initialize(this, mReactNativeHost) |
| 101 | +``` |
| 102 | + |
| 103 | +```kotlin |
| 104 | + val packages = PackageList(this).getPackages() |
| 105 | + |
| 106 | + ReactNativeBrownfield.initialize(this, packages) |
| 107 | +``` |
| 108 | + |
| 109 | +```kotlin |
| 110 | + val packages = PackageList(this).getPackages() |
| 111 | + val options = hashMapOf<String, Any>( |
| 112 | + "packages" to packages, |
| 113 | + "mainModuleName" to "example/index" |
| 114 | + ) |
| 115 | + |
| 116 | + ReactNativeBrownfield.initialize(this, options) |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +`shared` |
| 122 | + |
| 123 | +A singleton that keeps an instance of ReactNativeBrownfield object. |
| 124 | + |
| 125 | +Examples: |
| 126 | + |
| 127 | +```kotlin |
| 128 | + ReactNativeBrownfield.shared |
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +**Properties:** |
| 134 | + |
| 135 | +| Property | Type | Default | Description | |
| 136 | +| --------------- | --------------- | -------------- | --------------------------------------------------------- | |
| 137 | +| reactNativeHost | ReactNativeHost | null | An instance of [ReactNativeHost](https://bit.ly/2ZnwgnA). | |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +**Methods:** |
| 142 | + |
| 143 | +`startReactNative` |
| 144 | + |
| 145 | +Starts React Native, produces an instance of a bridge. You can use it to initialize React Native in your app. |
| 146 | + |
| 147 | +Params: |
| 148 | + |
| 149 | +| Param | Required | Type | Description | |
| 150 | +| ----------------------- | -------- | ------------- | ----------------------------------------------------- | |
| 151 | +| startReactNative | No | (loaded: boolean) -> Unit | Callback invoked after JS bundle is fully loaded. | |
| 152 | + |
| 153 | +Examples: |
| 154 | + |
| 155 | +```kotlin |
| 156 | + ReactNativeBrownfield.shared.startReactNative() |
| 157 | +``` |
| 158 | + |
| 159 | +```kotlin |
| 160 | + ReactNativeBrownfield.shared.startReactNative { |
| 161 | + Log.d("loaded", "React Native loaded"); |
| 162 | + } |
| 163 | +``` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +#### ReactNativeActivity |
| 168 | + |
| 169 | +An activity rendering `ReactRootView` with a given module name. It automatically uses an instance of a bridge created in `startReactNative` method. It works well with exposed JavaScript module. All the lifecycles are proxied to `ReactInstanceManager`. |
| 170 | + |
| 171 | +```kotlin |
| 172 | + import com.callstack.reactnativebrownfield.ReactNativeActivity |
| 173 | +``` |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +**Statics:** |
| 178 | + |
| 179 | +`createReactActivityIntent` |
| 180 | + |
| 181 | +Creates an Intent with ReactNativeActivity, you can use it as a parameter in the `startActivity` method in order to push a new activity with embedded React Native. |
| 182 | + |
| 183 | +Params: |
| 184 | + |
| 185 | +| Param | Required | Type | Description | |
| 186 | +| ----------------------- | -------- | ------------------------------------------- | ----------------------------------------------------------- | |
| 187 | +| context | Yes | Context | Application context. | |
| 188 | +| moduleName | Yes | String | Name of React Native component registered to `AppRegistry`. | |
| 189 | +| initialProps | No | Bundle || HashMap<String, *> || ReadableMap | Initial properties to be passed to React Native component. | |
| 190 | + |
| 191 | +Examples: |
| 192 | + |
| 193 | +```kotlin |
| 194 | + ReactNativeActivity.createReactActivityIntent(context, "ReactNative") |
| 195 | +``` |
| 196 | + |
| 197 | +```kotlin |
| 198 | + val bundle = Bundle() |
| 199 | + bundle.putInt("score", 12) |
| 200 | + |
| 201 | + ReactNativeActivity.createReactActivityIntent(context, "ReactNative", bundle) |
| 202 | +``` |
| 203 | + |
| 204 | +```kotlin |
| 205 | + val map = hasnMapOf<String, *>("score" to 12) |
| 206 | + |
| 207 | + ReactNativeActivity.createReactActivityIntent(context, "ReactNative", map) |
| 208 | +``` |
| 209 | + |
| 210 | +```kotlin |
| 211 | + val map = WritableMap(); |
| 212 | + map.putInt("score", 12); |
| 213 | + |
| 214 | + ReactNativeActivity.createReactActivityIntent(context, "ReactNative", map) |
| 215 | +``` |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +#### ReactNativeFragment |
| 220 | + |
| 221 | +An fragment rendering `ReactRootView` with a given module name. It automatically uses an instance of a bridge created in `startReactNative` method. It works well with exposed JavaScript module. All the lifecycles are proxied to `ReactInstanceManager`. It's the simplest way to embed React Native into your navigation stack. |
| 222 | + |
| 223 | +```kotlin |
| 224 | + import com.callstack.reactnativebrownfield.ReactNativeActivity |
| 225 | +``` |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +**Statics:** |
| 230 | + |
| 231 | +`createReactNativeFragment` |
| 232 | + |
| 233 | +Creates a Fragment with ReactNativeActivity, you can use it as a parameter in the `startActivity` method in order to push a new activity with embedded React Native. |
| 234 | + |
| 235 | +Params: |
| 236 | + |
| 237 | +| Param | Required | Type | Description | |
| 238 | +| ----------------------- | -------- | ------------------------------------------- | ----------------------------------------------------------- | |
| 239 | +| moduleName | Yes | String | Name of React Native component registered to `AppRegistry`. | |
| 240 | +| initialProps | No | Bundle || HashMap<String, *> || ReadableMap | Initial properties to be passed to React Native component. | |
| 241 | + |
| 242 | +Examples: |
| 243 | + |
| 244 | +```kotlin |
| 245 | + ReactNativeActivity.createReactNativeFragment("ReactNative") |
| 246 | +``` |
| 247 | + |
| 248 | +```kotlin |
| 249 | + val bundle = new Bundle() |
| 250 | + bundle.putInt("score", 12) |
| 251 | + |
| 252 | + ReactNativeActivity.createReactNativeFragment("ReactNative", bundle) |
| 253 | +``` |
| 254 | + |
| 255 | +```kotlin |
| 256 | + val map = hashMapOf<String, *>("score" to 12) |
| 257 | + |
| 258 | + ReactNativeActivity.createReactNativeFragment("ReactNative", map) |
| 259 | +``` |
| 260 | + |
| 261 | +```kotlin |
| 262 | + val map = WritableMap() |
| 263 | + map.putInt("score", 12) |
| 264 | + |
| 265 | + ReactNativeActivity.createReactActivityIntent(context, "ReactNative", map) |
| 266 | +``` |
| 267 | + |
| 268 | +--- |
| 269 | + |
| 270 | +### Linking |
| 271 | + |
| 272 | +You can find an example app [here](../example/java). |
| 273 | + |
| 274 | + |
0 commit comments