|
@@ -6,6 +6,10 @@ final class RedditWebViewStore {
|
|
|
static let shared = RedditWebViewStore()
|
|
static let shared = RedditWebViewStore()
|
|
|
|
|
|
|
|
private(set) var container: RedditWebViewContainer?
|
|
private(set) var container: RedditWebViewContainer?
|
|
|
|
|
+ private var isWindowMinimized = false
|
|
|
|
|
+ private var isRedditPageActive = false
|
|
|
|
|
+ private var isPaywallPresented = false
|
|
|
|
|
+ private var isAwaitingReturnFromSubscriptionManagement = false
|
|
|
private var isMediaPlaybackSuspended = false
|
|
private var isMediaPlaybackSuspended = false
|
|
|
|
|
|
|
|
private init() {}
|
|
private init() {}
|
|
@@ -18,18 +22,54 @@ final class RedditWebViewStore {
|
|
|
let webView = RedditWebViewManager.makeWebView()
|
|
let webView = RedditWebViewManager.makeWebView()
|
|
|
let newContainer = RedditWebViewContainer(webView: webView)
|
|
let newContainer = RedditWebViewContainer(webView: webView)
|
|
|
container = newContainer
|
|
container = newContainer
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
return newContainer
|
|
return newContainer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ func setRedditPageActive(_ active: Bool) {
|
|
|
|
|
+ isRedditPageActive = active
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ func setPaywallPresented(_ presented: Bool) {
|
|
|
|
|
+ isPaywallPresented = presented
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ func suspendForSubscriptionManagement() {
|
|
|
|
|
+ isAwaitingReturnFromSubscriptionManagement = true
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ func handleApplicationDidBecomeActive() {
|
|
|
|
|
+ guard isAwaitingReturnFromSubscriptionManagement else { return }
|
|
|
|
|
+ isAwaitingReturnFromSubscriptionManagement = false
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
func suspendMediaPlaybackForWindowMinimize() {
|
|
func suspendMediaPlaybackForWindowMinimize() {
|
|
|
- guard let webView = container?.webView, !isMediaPlaybackSuspended else { return }
|
|
|
|
|
- isMediaPlaybackSuspended = true
|
|
|
|
|
- webView.setAllMediaPlaybackSuspended(true, completionHandler: nil)
|
|
|
|
|
|
|
+ isWindowMinimized = true
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func resumeMediaPlaybackAfterWindowDeminiaturize() {
|
|
func resumeMediaPlaybackAfterWindowDeminiaturize() {
|
|
|
- guard let webView = container?.webView, isMediaPlaybackSuspended else { return }
|
|
|
|
|
- isMediaPlaybackSuspended = false
|
|
|
|
|
- webView.setAllMediaPlaybackSuspended(false, completionHandler: nil)
|
|
|
|
|
|
|
+ isWindowMinimized = false
|
|
|
|
|
+ updateMediaPlaybackSuspension()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func updateMediaPlaybackSuspension() {
|
|
|
|
|
+ let shouldSuspend = isWindowMinimized
|
|
|
|
|
+ || !isRedditPageActive
|
|
|
|
|
+ || isPaywallPresented
|
|
|
|
|
+ || isAwaitingReturnFromSubscriptionManagement
|
|
|
|
|
+ guard let webView = container?.webView else { return }
|
|
|
|
|
+
|
|
|
|
|
+ if shouldSuspend, !isMediaPlaybackSuspended {
|
|
|
|
|
+ isMediaPlaybackSuspended = true
|
|
|
|
|
+ webView.setAllMediaPlaybackSuspended(true, completionHandler: nil)
|
|
|
|
|
+ } else if !shouldSuspend, isMediaPlaybackSuspended {
|
|
|
|
|
+ isMediaPlaybackSuspended = false
|
|
|
|
|
+ webView.setAllMediaPlaybackSuspended(false, completionHandler: nil)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|