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

Snapshot and deactivate Visitable during it's lifecycle feedback #189

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Session: NSObject {

private var currentVisit: Visit?
private var topmostVisit: Visit?
private var previousVisit: Visit?

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

extension Session: VisitableDelegate {
public func visitableViewWillAppear(_ visitable: Visitable) {
defer {
/// Nilling out the previous visit here prevents `double-snapshotting` for web -> web visits.
previousVisit = nil
}

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

if visitable === topmostVisit.visitable && visitable.visitableViewController.isMovingToParent {
Expand All @@ -226,7 +232,10 @@ extension Session: VisitableDelegate {
// Navigating forward - complete navigation early
completeNavigationForCurrentVisit()
} else if visitable !== topmostVisit.visitable {
// Navigating backward
// Navigating backward from a web view screen to a web view screen.
visit(visitable, action: .restore)
} else if visitable === previousVisit?.visitable {
// Navigating backward from a native to a web view screen.
visit(visitable, action: .restore)
}
}
Expand All @@ -244,6 +253,15 @@ extension Session: VisitableDelegate {
}
}

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

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

public func visitableDidRequestReload(_ visitable: Visitable) {
guard visitable === topmostVisitable else { return }
reload()
Expand Down
4 changes: 4 additions & 0 deletions Source/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/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/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)
}

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() {
Expand Down
4 changes: 4 additions & 0 deletions Source/WebView/WebViewBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
6 changes: 6 additions & 0 deletions Source/WebView/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
}
}

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

// Current visit

issueRequestForVisitWithIdentifier(identifier) {
Expand Down