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

Fix Homepage navigation #322

Merged
merged 1 commit into from
Nov 15, 2021
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
21 changes: 14 additions & 7 deletions DuckDuckGo/BrowserTab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ final class Tab: NSObject {
}

func update(url: URL?, userEntered: Bool = true) {
self.content = .url(url ?? .emptyPage)
self.content = url == .homePage ? .homepage : .url(url ?? .blankPage)

// This function is called when the user has manually typed in a new address, which should reset the login detection flow.
userEnteredUrl = userEntered
Expand Down Expand Up @@ -287,8 +287,19 @@ final class Tab: NSObject {
}

private func reloadIfNeeded(shouldLoadInBackground: Bool = false) {
let url: URL
switch self.content {
case .url(let value):
url = value
case .homepage:
url = .homePage
default:
url = .blankPage
}
guard webView.superview != nil || shouldLoadInBackground,
webView.url != self.content.url
webView.url != url
// Initial Home Page shouldn't show Back Button
&& webView.url != self.content.url
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

else { return }

if let sessionStateData = self.sessionStateData {
Expand All @@ -299,11 +310,7 @@ final class Tab: NSObject {
os_log("Tab:setupWebView could not restore session state %s", "\(error)")
}
}
if let url = self.content.url {
webView.load(url)
} else {
webView.load(URL.emptyPage)
}
webView.load(url)
}

func stopLoading() {
Expand Down
6 changes: 3 additions & 3 deletions DuckDuckGo/BrowserTab/ViewModel/TabViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ final class TabViewModel {
}

private func updateCanReload() {
canReload = tab.content.url ?? .emptyPage != .emptyPage
canReload = tab.content.url ?? .blankPage != .blankPage
}

func updateCanGoBack() {
canGoBack = tab.canGoBack || tab.canBeClosedWithBack
}

private func updateCanBeBookmarked() {
canBeBookmarked = tab.content.url ?? .emptyPage != .emptyPage
canBeBookmarked = tab.content.url ?? .blankPage != .blankPage
}

private func updateAddressBarStrings() {
Expand Down Expand Up @@ -170,7 +170,7 @@ final class TabViewModel {
if let searchQuery = url.searchQuery {
addressBarString = searchQuery
passiveAddressBarString = searchQuery
} else if url == URL.emptyPage {
} else if [.blankPage, .homePage].contains(url) {
addressBarString = ""
passiveAddressBarString = ""
} else {
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/BrowserTab/ViewModel/WebViewStateObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class WebViewStateObserver: NSObject {
switch keyPath {
case #keyPath(WKWebView.url):
if let url = webView.url {
tabViewModel.tab.setContent(.url(url))
tabViewModel.tab.setContent(url == .homePage ? .homepage : .url(url))
tabViewModel.tab.addVisit(of: url)
}
updateTitle() // The title might not change if webView doesn't think anything is different so update title here as well
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGo/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ extension URL {
return url
}

static var emptyPage: URL {
static var blankPage: URL {
return URL(string: "about:blank")!
}

static var homePage: URL {
return URL(string: "about:home")!
}

// MARK: Pixel

static let pixelBase = ProcessInfo.processInfo.environment["PIXEL_BASE_URL", default: "https://improving.duckduckgo.com"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private extension DownloadListItem {
self.init(identifier: UUID(),
added: now,
modified: now,
url: task.originalRequest?.url ?? .emptyPage,
url: task.originalRequest?.url ?? .blankPage,
websiteURL: task.originalRequest?.mainDocumentURL,
progress: task.progress,
destinationURL: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class WKBackForwardListItemViewModel {
var title: String {
switch backForwardListItem {
case .backForwardListItem(let item):
if item.url == URL.emptyPage {
if item.url == .homePage {
return UserText.tabHomeTitle
}

Expand All @@ -61,7 +61,7 @@ final class WKBackForwardListItemViewModel {
}

var image: NSImage? {
if backForwardListItem.url == URL.emptyPage {
if backForwardListItem.url == .homePage {
return NSImage(named: "HomeFavicon")
}

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/TabBar/View/TabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ extension TabBarViewController: NSCollectionViewDelegate {
if let url = tabCollectionViewModel.tabCollection.tabs[indexPath.item].content.url {
return url as NSURL
} else {
return URL.emptyPage as NSURL
return URL.blankPage as NSURL
}
}

Expand Down
8 changes: 4 additions & 4 deletions Unit Tests/Configuration/ConfigurationDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,22 +328,22 @@ fileprivate extension HTTPURLResponse {
static let ifNoneMatchHeader = "If-None-Match"
static let etagValue = "test-etag"

static let success = HTTPURLResponse(url: URL.emptyPage,
static let success = HTTPURLResponse(url: URL.blankPage,
statusCode: 200,
httpVersion: nil,
headerFields: [HTTPURLResponse.etagHeader: HTTPURLResponse.etagValue])!

static let notModified = HTTPURLResponse(url: URL.emptyPage,
static let notModified = HTTPURLResponse(url: URL.blankPage,
statusCode: 304,
httpVersion: nil,
headerFields: [HTTPURLResponse.etagHeader: HTTPURLResponse.etagValue])!

static let internalServerError = HTTPURLResponse(url: URL.emptyPage,
static let internalServerError = HTTPURLResponse(url: URL.blankPage,
statusCode: 500,
httpVersion: nil,
headerFields: [:])!

static let successNoEtag = HTTPURLResponse(url: URL.emptyPage,
static let successNoEtag = HTTPURLResponse(url: URL.blankPage,
statusCode: 200,
httpVersion: nil,
headerFields: [:])!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,6 @@ fileprivate extension TabCollectionViewModel {

extension Tab {
convenience init(parentTab: Tab) {
self.init(content: .url(.emptyPage), parentTab: parentTab)
self.init(content: .url(.blankPage), parentTab: parentTab)
}
}