Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update custom webview documentation with kotlin code examples #3060

Merged
merged 7 commits into from
May 3, 2022

Conversation

jazibjafri
Copy link
Contributor

This PR adds kotlin code examples in custom webview documentation.

Related to #3018

@jazibjafri
Copy link
Contributor Author

@cortinico @ShikaSD @mdvacca

@netlify
Copy link

netlify bot commented Apr 7, 2022

Deploy Preview for react-native ready!

Name Link
🔨 Latest commit 0f3365d
🔍 Latest deploy log https://app.netlify.com/sites/react-native/deploys/626777a420d644000898655f
😎 Deploy Preview https://deploy-preview-3060--react-native.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Contributor

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! 👍
Have you actually tried the code? As there are some odd fragments which I'd like to make sure they're tested before they end on the docs.

Comment on lines 73 to 74
val name = "RCTCustomWebView"
get() = Companion.field
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a const val

protected class CustomWebViewClient : ReactWebViewClient()
protected class CustomWebView(reactContext: ThemedReactContext?) : ReactWebView(reactContext)

protected fun createReactWebViewInstance(reactContext: ThemedReactContext?): ReactWebView {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that here the is no override looks odd to me. Have you tested this code?

```kotlin
class CustomWebViewManager : ReactWebViewManager() {
protected class CustomWebView(reactContext: ThemedReactContext?) : ReactWebView(reactContext) {
@Nullable var finalUrl: String? = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nullable should not be used in Kotlin (that's part of the type system).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this can maybe be an inner class

```kotlin
// NavigationCompletedEvent.kt
class NavigationCompletedEvent(viewTag: Int, params: WritableMap) :
Event<NavigationCompletedEvent?>(viewTag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Event<NavigationCompletedEvent?>(viewTag) {
Event<NavigationCompletedEvent>(viewTag) {

// NavigationCompletedEvent.kt
class NavigationCompletedEvent(viewTag: Int, params: WritableMap) :
Event<NavigationCompletedEvent?>(viewTag) {
private val mParams: WritableMap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can go

fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
val shouldOverride: Boolean = super.shouldOverrideUrlLoading(view, url)
val finalUrl: String = (view as CustomWebView).getFinalUrl()
if (!shouldOverride && url != null && finalUrl != null && String(url) == finalUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!shouldOverride && url != null && finalUrl != null && String(url) == finalUrl) {
if (!shouldOverride && url != null && finalUrl != null && url == finalUrl) {


```kotlin
class CustomWebViewManager : ReactWebViewManager() {
@get:Nullable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nullable should go

```kotlin
class CustomWebViewManager : ReactWebViewManager() {
@get:Nullable
val exportedCustomDirectEventTypeConstants: Map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val exportedCustomDirectEventTypeConstants: Map
val exportedCustomDirectEventTypeConstants: Map<String, Any?>

Comment on lines 309 to 310
var export: Map<String?, Any?> = super.getExportedCustomDirectEventTypeConstants()
if (export == null) {
export = MapBuilder.newHashMap()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var export: Map<String?, Any?> = super.getExportedCustomDirectEventTypeConstants()
if (export == null) {
export = MapBuilder.newHashMap()
}
val export = if (super.getExportedCustomDirectEventTypeConstants() != null) {
super.getExportedCustomDirectEventTypeConstants()
} else {
export = MapBuilder.newHashMap()
}

Comment on lines 313 to 316
export.put(
"navigationCompleted",
MapBuilder.of("registrationName", "onNavigationCompleted")
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export.put(
"navigationCompleted",
MapBuilder.of("registrationName", "onNavigationCompleted")
)
export["navigationCompleted"] = MapBuilder.of("registrationName", "onNavigationCompleted")

Not sure if you have set access here, but is worth to try.

@jazibjafri
Copy link
Contributor Author

Great stuff! 👍 Have you actually tried the code? As there are some odd fragments which I'd like to make sure they're tested before they end on the docs.

I have tested some of them, but you're right. I really should've tested everything. Don't worry I will test properly and update the requested changes, as soon as I can. You can take a look again then.

@cortinico
Copy link
Contributor

Great stuff! 👍 Have you actually tried the code? As there are some odd fragments which I'd like to make sure they're tested before they end on the docs.

I have tested some of them, but you're right. I really should've tested everything. Don't worry I will test properly and update the requested changes, as soon as I can. You can take a look again then.

Great thanks for doing that. Ping one of us again once you're done with it 👍

@jazibjafri
Copy link
Contributor Author

jazibjafri commented Apr 10, 2022

Hey @cortinico I was thinking maybe I can use 'RNCWebview' by react native community in examples instead of ReactWebView. As the docs already recommend that, which is why I had problems testing the code in the first place because references were not resolving in the latest react native version.
So can I use react-native-community/react-native-webview?. I would need to replace some code like

ReactWebViewManager -> RNCWebViewManager
ReactWebViewClient -> RNCWebViewClient

If yes, I can maybe update the java code in a separate PR?

@cortinico
Copy link
Contributor

If yes, I can maybe update the java code in a separate PR?

Totally. I'd say let's hold this PR for now, update the Java code to use react-native-webview and get back to this once the first one is merged.

@jazibjafri
Copy link
Contributor Author

Sure, awesome. I will make a separate pr for java.

@jazibjafri
Copy link
Contributor Author

@cortinico I have updated the code. Have a look when you can.


companion object {
/* This name must match what we're referring to in JS */
const val name = "RCTCustomWebView"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const val name = "RCTCustomWebView"
const val REACT_CLASS = "RCTCustomWebView"


```kotlin
class CustomWebViewManager : RNCWebViewManager() {
protected inner class CustomWebView(reactContext: ThemedReactContext?) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected inner class CustomWebView(reactContext: ThemedReactContext?) :
protected inner class CustomWebView(reactContext: ThemedReactContext?, var finalUrl: String? = null) : RNCWebView(reactContext)

class NavigationCompletedEvent(viewTag: Int, val params: WritableMap) :
Event<NavigationCompletedEvent>(viewTag) {
private val eventName: String = "navigationCompleted"
override fun getEventName() = this.eventName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override fun getEventName() = this.eventName
override fun getEventName() : String = "navigationCompleted"

// NavigationCompletedEvent.kt
class NavigationCompletedEvent(viewTag: Int, val params: WritableMap) :
Event<NavigationCompletedEvent>(viewTag) {
private val eventName: String = "navigationCompleted"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this

class NavigationCompletedEvent(viewTag: Int, val params: WritableMap) :
Event<NavigationCompletedEvent>(viewTag) {

private val eventName = "navigationCompleted"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Comment on lines 305 to 310
val export =
if (super.getExportedCustomDirectEventTypeConstants() != null) {
super.getExportedCustomDirectEventTypeConstants()
} else {
MapBuilder.newHashMap<Any, Any?>()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val export =
if (super.getExportedCustomDirectEventTypeConstants() != null) {
super.getExportedCustomDirectEventTypeConstants()
} else {
MapBuilder.newHashMap<Any, Any?>()
}
val super = super.getExportedCustomDirectEventTypeConstants()
val export = if (super != null) {
super
} else {
MapBuilder.newHashMap<Any, Any?>()
}

Copy link
Contributor Author

@jazibjafri jazibjafri Apr 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super is a keyword, it cannot be used as a variable name, should I change to something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my bad 🤦‍♂️ superTypeConstants also work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 311 to 314
export?.set(
"navigationCompleted",
MapBuilder.of("registrationName", "onNavigationCompleted")
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export?.set(
"navigationCompleted",
MapBuilder.of("registrationName", "onNavigationCompleted")
)
export["navigationCompleted"] =
MapBuilder.of("registrationName", "onNavigationCompleted")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shows error "only safe or non-null asserted calls are allowed on receiver"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work once you refactor the rest of the code as export will be non nullable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, done

@jazibjafri
Copy link
Contributor Author

@cortinico, I have completed the changes. Check out when you can.

@cortinico
Copy link
Contributor

@cortinico, I have completed the changes. Check out when you can.

Yup. Looks good but there are a couple of minor things to address still 👍

Comment on lines +303 to +305
val superTypeConstants = super.getExportedCustomDirectEventTypeConstants()
val export = superTypeConstants ?: MapBuilder.newHashMap<Any, Any?>()
export["navigationCompleted"] = MapBuilder.of("registrationName", "onNavigationCompleted")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@cortinico cortinico merged commit 7d976f3 into facebook:main May 3, 2022
@jazibjafri jazibjafri deleted the kotlin-webview-example branch May 3, 2022 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants