videos: improve precision & sensitivity of auto-pause mechanism

Closes: https://github.com/damus-io/damus/pull/1308
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
gladiusKatana
2023-06-21 03:11:50 -04:00
committed by William Casarin
parent c5d8e4a4a1
commit 5caa4a6e97
2 changed files with 37 additions and 8 deletions

View File

@@ -20,11 +20,13 @@ struct damusApp: App {
struct MainView: View {
@State var needs_setup = false;
@State var keypair: Keypair? = nil;
@StateObject private var orientationTracker = OrientationTracker()
var body: some View {
Group {
if let kp = keypair, !needs_setup {
ContentView(keypair: kp)
.environmentObject(orientationTracker)
} else {
SetupView()
.onReceive(handle_notify(.login)) { notif in
@@ -38,7 +40,11 @@ struct MainView: View {
try? clear_keypair()
keypair = nil
}
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
orientationTracker.setDeviceMajorAxis()
}
.onAppear {
orientationTracker.setDeviceMajorAxis()
keypair = get_saved_keypair()
}
}
@@ -65,3 +71,14 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
completionHandler()
}
}
class OrientationTracker: ObservableObject {
var deviceMajorAxis: CGFloat = 0
func setDeviceMajorAxis() {
let bounds = UIScreen.main.bounds
let height = max(bounds.height, bounds.width) /// device's longest dimension
let width = min(bounds.height, bounds.width) /// device's shortest dimension
let orientation = UIDevice.current.orientation
deviceMajorAxis = (orientation == .portrait || orientation == .unknown) ? height : width
}
}