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 #181

Merged
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
15 changes: 14 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 disappearingVisitForSnapshotting: Visit?
Copy link
Contributor

Choose a reason for hiding this comment

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

I've renamed disappearingVisitForSnapshotting to previousVisit in the feedback PR #189.
cc @jayohms @joemasilotti What do you think about the name?


/// The topmost visitable is the visitable that has most recently completed a visit
public var topmostVisitable: Visitable? {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This covers the case of restoring from native screen back to web, which previously wasn't considered a restore. The web view would both topmost and current visit, even when navigating back.

Copy link
Contributor

Choose a reason for hiding this comment

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

This works as expected.
Since this is an or operation I'd propose to evaluate the latter in a separate else if statement.
This will make it clear that we're dealing with two types of backward navigation.
I've addressed it and added comments in my feedback PR #189

// Navigating backward
visit(visitable, action: .restore)
}
Expand All @@ -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()
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