Skip to content

Commit

Permalink
Don't reload if there's no internet connection
Browse files Browse the repository at this point in the history
Workaround for #41
  • Loading branch information
sindresorhus committed Feb 6, 2020
1 parent de84110 commit 42ae54e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Plash/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Cocoa
import Combine
import AppCenter
import AppCenterCrashes
import Defaults

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
var cancellables = Set<AnyCancellable>()

let menu = SSMenu()
let powerSourceWatcher = PowerSourceWatcher()

Expand Down Expand Up @@ -128,6 +131,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
self.setEnabledStatus()
}

NSWorkspace.shared.notificationCenter
.publisher(for: NSWorkspace.didWakeNotification)
.sink { _ in
self.loadUserURL()
}
.store(in: &cancellables)

Defaults.observe(.url, options: [.new]) { change in
self.resetTimer()
self.loadURL(change.newValue)
Expand Down Expand Up @@ -236,6 +246,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
return
}

// TODO: This is just a quick fix. The proper fix is to create a new web view below the existing one (with no opacity), load the URL, if it succeeds, we fade out the old one while fading in the new one. If it fails, we discard the new web view.
guard Reachability.isOnlineExtensive() else {
webViewError = NSError.appError(message: "No internet connection.")
return
}

// TODO: Report the bug to Apple.
// WKWebView has a bug where it can only load a local file once. So if you load file A, load file B, and load file A again, it errors. And if you load the same file as the existing one, nothing happens. Quality engineering.
if url.isFileURL {
Expand Down
49 changes: 49 additions & 0 deletions Plash/util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import WebKit
import SwiftUI
import Combine
import Network
import SystemConfiguration
import LaunchAtLogin
import Defaults

Expand Down Expand Up @@ -2587,3 +2588,51 @@ extension URL {
return components.url ?? self
}
}


struct Reachability {
/// Checks whether we're currently online.
static func isOnline(host: String = "apple.com") -> Bool {
guard let ref = SCNetworkReachabilityCreateWithName(nil, host) else {
return false
}

var flags = SCNetworkReachabilityFlags.connectionAutomatic
if !SCNetworkReachabilityGetFlags(ref, &flags) {
return false
}

return flags.contains(.reachable) && !flags.contains(.connectionRequired)
}

/// Checks multiple sources of whether we're currently online.
static func isOnlineExtensive() -> Bool {
let hosts = [
"apple.com",
"google.com",
"cloudflare.com",
"baidu.com",
"yandex.ru"
]

return hosts.contains { isOnline(host: $0) }
}
}


extension NSError {
/**
- Parameter domainPostfix: String to append to the `domain`.
*/
static func appError(
message: String,
userInfo: [String: Any] = [:],
domainPostfix: String? = nil
) -> Self {
.init(
domain: domainPostfix != nil ? "\(App.id) - \(domainPostfix!)" : App.id,
code: 0,
userInfo: [NSLocalizedDescriptionKey: message]
)
}
}

0 comments on commit 42ae54e

Please sign in to comment.