-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect and monitor network type during LO and apply right FPS settings
This PR creates to monitoring systems: 1. Network connection monitor, that keeps an eye on the type of connection the phone has. 2. Fps monitor keeps an eye on the connection type, and updates needed fps accordingly MOB-2706
- Loading branch information
1 parent
9102f5a
commit 9889bc0
Showing
7 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
import Combine | ||
|
||
class FpsMonitor { | ||
@Published private(set) var currentFPS: Int = 1 | ||
|
||
private let wifiFps: Int | ||
private let cellularFps: Int | ||
private var cancellables: Set<AnyCancellable> = [] | ||
|
||
init( | ||
environment: Environment, | ||
wifiFps: Int, | ||
cellularFps: Int | ||
) { | ||
self.wifiFps = wifiFps | ||
self.cellularFps = cellularFps | ||
|
||
environment.networkMonitor.$connectionType | ||
.map { connectionType -> Int in | ||
switch connectionType { | ||
case .unlimited: | ||
return self.wifiFps | ||
case .metered, .unknown: | ||
return self.cellularFps | ||
} | ||
} | ||
.assign(to: \.currentFPS, on: self) | ||
.store(in: &cancellables) | ||
} | ||
} | ||
|
||
extension FpsMonitor { | ||
struct Environment { | ||
let networkMonitor: NetworkConnectionMonitor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
GliaWidgets/Sources/NetworkConnectionMonitor/NetworkConnectionMonitor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Network | ||
import Combine | ||
|
||
class NetworkConnectionMonitor: ObservableObject { | ||
@Published private(set) var connectionType: ConnectionType = .unknown | ||
|
||
private var monitor: NWPathMonitor? | ||
private var queue = DispatchQueue(label: "NetworkMonitor") | ||
|
||
init() { | ||
monitor = NWPathMonitor() | ||
monitor?.pathUpdateHandler = { [weak self] path in | ||
guard let self = self else { return } | ||
guard path.status == .satisfied else { | ||
// No conection to the internet | ||
self.connectionType = .unknown | ||
return | ||
} | ||
|
||
if path.usesInterfaceType(.wifi) { | ||
self.connectionType = .unlimited | ||
} else if path.usesInterfaceType(.cellular) { | ||
self.connectionType = .metered | ||
} else { | ||
self.connectionType = .unknown | ||
} | ||
} | ||
|
||
monitor?.start(queue: queue) | ||
} | ||
} | ||
|
||
extension NetworkConnectionMonitor { | ||
enum ConnectionType { | ||
case unlimited | ||
case metered | ||
case unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters