Skip to content

Commit

Permalink
feat: refactor iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
pklatka committed Apr 23, 2024
1 parent e269d86 commit 2f4da7f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 41 deletions.
7 changes: 3 additions & 4 deletions packages/turbo/ios/RNSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ class RNSession: NSObject {
private var visitableView: RNVisitableView?
private var sessionHandle: NSString
private var wkUiDelegate: WKUIDelegate?
private var sessionConfiguration: RNSessionConfiguration
private var webViewConfiguration: WKWebViewConfiguration

init(sessionHandle: NSString, sessionConfiguration: RNSessionConfiguration) {
init(sessionHandle: NSString, webViewConfiguration: WKWebViewConfiguration) {
self.sessionHandle = sessionHandle
self.sessionConfiguration = sessionConfiguration
self.webViewConfiguration = webViewConfiguration
}

public lazy var turboSession: Session = {
let webViewConfiguration = sessionConfiguration.webViewConfiguration
webViewConfiguration.userContentController.add(self, name: "nativeApp")

self.wkUiDelegate = TurboUIDelegate(uiHandler: self)
Expand Down
27 changes: 0 additions & 27 deletions packages/turbo/ios/RNSessionConfiguration.swift

This file was deleted.

6 changes: 3 additions & 3 deletions packages/turbo/ios/RNSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class RNSessionManager: NSObject {
private var processPool = WKProcessPool()
static var shared: RNSessionManager = RNSessionManager()

func findOrCreateSession(sessionHandle: NSString, sessionConfiguration: RNSessionConfiguration) -> RNSession {
func findOrCreateSession(sessionHandle: NSString, webViewConfiguration: WKWebViewConfiguration) -> RNSession {
if(sessions[sessionHandle] == nil) {
sessionConfiguration.webViewConfiguration.processPool = processPool
sessions[sessionHandle] = RNSession(sessionHandle: sessionHandle, sessionConfiguration: sessionConfiguration)
webViewConfiguration.processPool = processPool
sessions[sessionHandle] = RNSession(sessionHandle: sessionHandle, webViewConfiguration: webViewConfiguration)
}
return sessions[sessionHandle]!
}
Expand Down
19 changes: 12 additions & 7 deletions packages/turbo/ios/RNVisitableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class RNVisitableView: UIView, RNSessionSubscriber {
}
@objc var applicationNameForUserAgent: NSString? = nil {
didSet {
sessionConfiguration.webViewConfiguration.applicationNameForUserAgent = applicationNameForUserAgent as? String
configuration.applicationNameForUserAgent = applicationNameForUserAgent as? String
}
}
@objc var scrollEnabled: Bool = true {
didSet {
sessionConfiguration.isScrollEnabled = scrollEnabled
configuration.scrollEnabled = scrollEnabled
}
}
@objc var pullToRefreshEnabled: Bool = true {
didSet {
controller!.visitableView.allowsPullToRefresh = pullToRefreshEnabled
configuration.pullToRefreshEnabled = pullToRefreshEnabled
}
}
@objc var onMessage: RCTDirectEventBlock?
Expand All @@ -53,12 +53,17 @@ class RNVisitableView: UIView, RNSessionSubscriber {
private var onAlertHandler: (() -> Void)?

private lazy var session: RNSession = {
let session = RNSessionManager.shared.findOrCreateSession(sessionHandle: sessionHandle!, sessionConfiguration: sessionConfiguration)
sessionConfiguration.sessionDelegate = session
let session = RNSessionManager.shared.findOrCreateSession(sessionHandle: sessionHandle!, webViewConfiguration: configuration.webViewConfiguration)
configuration.webView = session.webView
return session
}()
private lazy var webView: WKWebView = session.webView
private var sessionConfiguration: RNSessionConfiguration = RNSessionConfiguration()

private lazy var configuration: RNVisitableViewConfiguration = {
let configuration = RNVisitableViewConfiguration()
configuration.visitableView = controller!.visitableView
return configuration
}()

lazy var controller: RNVisitableViewController? = RNVisitableViewController(reactViewController: reactViewController(), delegate: self)

Expand Down Expand Up @@ -97,7 +102,7 @@ class RNVisitableView: UIView, RNSessionSubscriber {
controller!.endAppearanceTransition()
}

sessionConfiguration.updateConfiguration()
configuration.updateWebViewConfiguration()
}

override func removeFromSuperview() {
Expand Down
46 changes: 46 additions & 0 deletions packages/turbo/ios/RNVisitableViewConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// RNSessionConfiguration.swift
// RNTurbo
//
// Created by Patryk Klatka on 22/04/2024.
//

import Foundation

class RNVisitableViewConfiguration {
var visitableView: VisitableView? = nil {
didSet {
updateVisitableViewConfiguration()
}
}
var webView: WKWebView? = nil {
didSet {
updateWebViewConfiguration()
}
}
var webViewConfiguration: WKWebViewConfiguration = WKWebViewConfiguration()

var pullToRefreshEnabled: Bool = true {
didSet {
updateVisitableViewConfiguration()
}
}
var applicationNameForUserAgent: String? = nil {
didSet {
webViewConfiguration.applicationNameForUserAgent = applicationNameForUserAgent
}
}
var scrollEnabled: Bool = true {
didSet {
updateWebViewConfiguration()
}
}

func updateVisitableViewConfiguration() {
visitableView?.allowsPullToRefresh = pullToRefreshEnabled
}

func updateWebViewConfiguration() {
webView?.scrollView.isScrollEnabled = scrollEnabled
}
}

0 comments on commit 2f4da7f

Please sign in to comment.