diff --git a/Source/Session/Session.swift b/Source/Session/Session.swift index bcfdab3..a1a0991 100644 --- a/Source/Session/Session.swift +++ b/Source/Session/Session.swift @@ -35,6 +35,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? { @@ -213,6 +214,9 @@ extension Session: VisitDelegate { extension Session: VisitableDelegate { public func visitableViewWillAppear(_ visitable: Visitable) { + let lastDisappearingVisit = self.disappearingVisitForSnapshotting + self.disappearingVisitForSnapshotting = nil + guard let topmostVisit = self.topmostVisit, let currentVisit = self.currentVisit else { return } if visitable === topmostVisit.visitable && visitable.visitableViewController.isMovingToParent { @@ -225,7 +229,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) } @@ -244,6 +248,15 @@ extension Session: VisitableDelegate { } } + public func visitableViewWillDisappear(_ visitable: Visitable) { + self.disappearingVisitForSnapshotting = topmostVisit + } + + public func visitableViewDidDisappear(_ visitable: Visitable) { + disappearingVisitForSnapshotting?.cacheSnapshot() + deactivateVisitable(visitable) + } + public func visitableDidRequestReload(_ visitable: Visitable) { guard visitable === topmostVisitable else { return } reload() diff --git a/Source/Visit/Visit.swift b/Source/Visit/Visit.swift index 15ed413..7385bc0 100644 --- a/Source/Visit/Visit.swift +++ b/Source/Visit/Visit.swift @@ -68,6 +68,10 @@ class Visit: NSObject { delegate?.visitDidFinish(self) } + func cacheSnapshot() { + bridge.cacheSnapshot() + } + func startVisit() {} func cancelVisit() {} func completeVisit() {} diff --git a/Source/Visitable/Visitable.swift b/Source/Visitable/Visitable.swift index 6f51247..079c48b 100644 --- a/Source/Visitable/Visitable.swift +++ b/Source/Visitable/Visitable.swift @@ -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) } diff --git a/Source/Visitable/VisitableViewController.swift b/Source/Visitable/VisitableViewController.swift index b245952..47433ab 100644 --- a/Source/Visitable/VisitableViewController.swift +++ b/Source/Visitable/VisitableViewController.swift @@ -28,6 +28,16 @@ open class VisitableViewController: UIViewController, Visitable { visitableDelegate?.visitableViewDidAppear(self) } + open override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + visitableDelegate?.visitableViewWillDisappear(self) + } + + open override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + visitableDelegate?.visitableViewDidDisappear(self) + } + // MARK: Visitable open func visitableDidRender() { diff --git a/Source/WebView/WebViewBridge.swift b/Source/WebView/WebViewBridge.swift index a7bdc73..74c23fb 100644 --- a/Source/WebView/WebViewBridge.swift +++ b/Source/WebView/WebViewBridge.swift @@ -76,6 +76,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]) } diff --git a/Source/WebView/turbo.js b/Source/WebView/turbo.js index 329c90c..b9299bd 100644 --- a/Source/WebView/turbo.js +++ b/Source/WebView/turbo.js @@ -64,6 +64,12 @@ } } + cacheSnapshot() { + if (window.Turbo) { + Turbo.session.view.cacheSnapshot() + } + } + // Current visit issueRequestForVisitWithIdentifier(identifier) {