Skip to content

Commit

Permalink
Backport turbo-ios PR #181
Browse files Browse the repository at this point in the history
  • Loading branch information
joemasilotti committed Mar 23, 2024
1 parent 0e1f220 commit 487f950
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
15 changes: 14 additions & 1 deletion Source/Turbo/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Session: NSObject {

private var currentVisit: Visit?
private var topmostVisit: Visit?
private var disappearingVisitForSnapshotting: Visit?

/// The topmost visitable is the visitable that has most recently completed a visit
public var topmostVisitable: Visitable? {
Expand Down Expand Up @@ -227,6 +228,9 @@ extension Session: VisitDelegate {

extension Session: VisitableDelegate {
public func visitableViewWillAppear(_ visitable: Visitable) {
let lastDisappearingVisit = disappearingVisitForSnapshotting
disappearingVisitForSnapshotting = nil

guard let topmostVisit = topmostVisit, let currentVisit = currentVisit else { return }

if isSnapshotCacheStale {
Expand All @@ -247,7 +251,7 @@ extension Session: VisitableDelegate {
} else if visitable === currentVisit.visitable && currentVisit.state == .started {
// Navigating forward - complete navigation early
completeNavigationForCurrentVisit()
} else if visitable !== topmostVisit.visitable {
} else if visitable !== topmostVisit.visitable || visitable === lastDisappearingVisit?.visitable {
// Navigating backward
visit(visitable, action: .restore)
}
Expand All @@ -266,6 +270,15 @@ extension Session: VisitableDelegate {
}
}

public func visitableViewWillDisappear(_ visitable: Visitable) {
disappearingVisitForSnapshotting = topmostVisit
}

public func visitableViewDidDisappear(_ visitable: Visitable) {
disappearingVisitForSnapshotting?.cacheSnapshot()
deactivateVisitable(visitable)
}

public func visitableDidRequestReload(_ visitable: Visitable) {
guard visitable === topmostVisitable else { return }
reload()
Expand Down
4 changes: 4 additions & 0 deletions Source/Turbo/Visit/Visit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Visit: NSObject {
delegate?.visitDidFinish(self)
}

func cacheSnapshot() {
bridge.cacheSnapshot()
}

func startVisit() {}
func cancelVisit() {}
func completeVisit() {}
Expand Down
2 changes: 2 additions & 0 deletions Source/Turbo/Visitable/Visitable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import WebKit
public protocol VisitableDelegate: AnyObject {
func visitableViewWillAppear(_ visitable: Visitable)
func visitableViewDidAppear(_ visitable: Visitable)
func visitableViewWillDisappear(_ visitable: Visitable)
func visitableViewDidDisappear(_ visitable: Visitable)
func visitableDidRequestReload(_ visitable: Visitable)
func visitableDidRequestRefresh(_ visitable: Visitable)
}
Expand Down
10 changes: 10 additions & 0 deletions Source/Turbo/Visitable/VisitableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ open class VisitableViewController: UIViewController, Visitable {
visitableDelegate?.visitableViewDidAppear(self)
}

override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
visitableDelegate?.visitableViewWillDisappear(self)
}

override open func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
visitableDelegate?.visitableViewDidDisappear(self)
}

// MARK: Visitable

open func visitableDidRender() {
Expand Down
4 changes: 4 additions & 0 deletions Source/Turbo/WebView/WebViewBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ final class WebViewBridge {
callJavaScript(function: "window.turboNative.clearSnapshotCache")
}

func cacheSnapshot() {
callJavaScript(function: "window.turboNative.cacheSnapshot")
}

func cancelVisit(withIdentifier identifier: String) {
callJavaScript(function: "window.turboNative.cancelVisitWithIdentifier", arguments: [identifier])
}
Expand Down
10 changes: 8 additions & 2 deletions Source/Turbo/WebView/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@
}
}
}

clearSnapshotCache() {
if (window.Turbo) {
Turbo.session.clearCache()
}
}

cacheSnapshot() {
if (window.Turbo) {
Turbo.session.view.cacheSnapshot()
}
}

// Current visit

issueRequestForVisitWithIdentifier(identifier) {
Expand Down Expand Up @@ -150,7 +156,7 @@
visitCompleted(visit) {
this.postMessage("visitCompleted", { identifier: visit.identifier, restorationIdentifier: visit.restorationIdentifier })
}

formSubmissionStarted(formSubmission) {
this.postMessage("formSubmissionStarted", { location: formSubmission.location.toString() })
}
Expand Down

0 comments on commit 487f950

Please sign in to comment.