mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-18 16:49:10 +02:00
Merge pull request #1217 from mfcar/mf/networkStatusSwift
Implement network monitoring on iOS
This commit is contained in:
commit
dda95bd69c
1 changed files with 61 additions and 34 deletions
|
@ -8,12 +8,15 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
import Capacitor
|
import Capacitor
|
||||||
import RealmSwift
|
import RealmSwift
|
||||||
|
import Network
|
||||||
|
|
||||||
@objc(AbsAudioPlayer)
|
@objc(AbsAudioPlayer)
|
||||||
public class AbsAudioPlayer: CAPPlugin {
|
public class AbsAudioPlayer: CAPPlugin {
|
||||||
private let logger = AppLogger(category: "AbsAudioPlayer")
|
private let logger = AppLogger(category: "AbsAudioPlayer")
|
||||||
|
|
||||||
private var initialPlayWhenReady = false
|
private var initialPlayWhenReady = false
|
||||||
|
private var monitor: NWPathMonitor?
|
||||||
|
private let queue = DispatchQueue.global(qos: .background)
|
||||||
|
|
||||||
override public func load() {
|
override public func load() {
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(sendMetadata), name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(sendMetadata), name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
|
||||||
|
@ -28,6 +31,11 @@ public class AbsAudioPlayer: CAPPlugin {
|
||||||
self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
|
self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
|
||||||
self.bridge?.webView?.scrollView.alwaysBounceVertical = false;
|
self.bridge?.webView?.scrollView.alwaysBounceVertical = false;
|
||||||
|
|
||||||
|
setupNetworkMonitor()
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
monitor?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func onReady(_ call: CAPPluginCall) {
|
@objc func onReady(_ call: CAPPluginCall) {
|
||||||
|
@ -275,6 +283,25 @@ public class AbsAudioPlayer: CAPPlugin {
|
||||||
@objc func sendPlaybackSession(session: [String: Any]) {
|
@objc func sendPlaybackSession(session: [String: Any]) {
|
||||||
self.notifyListeners("onPlaybackSession", data: session)
|
self.notifyListeners("onPlaybackSession", data: session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setupNetworkMonitor() {
|
||||||
|
monitor = NWPathMonitor()
|
||||||
|
monitor?.pathUpdateHandler = { [weak self] path in
|
||||||
|
guard let self = self else { return }
|
||||||
|
|
||||||
|
let isUnmetered = !path.isExpensive && !path.isConstrained
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.notifyNetworkMeteredChanged(isUnmetered: isUnmetered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
monitor?.start(queue: queue)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func notifyNetworkMeteredChanged(isUnmetered: Bool) {
|
||||||
|
let data: [String: Any] = ["value": isUnmetered]
|
||||||
|
self.notifyListeners("onNetworkMeteredChanged", data: data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PlayerError: String, Error {
|
enum PlayerError: String, Error {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue