forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.swift
More file actions
45 lines (36 loc) · 1.37 KB
/
Keyboard.swift
File metadata and controls
45 lines (36 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Keyboard.swift
// LoopKitUI
//
// Created by Michael Pangburn on 7/18/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import Combine
import UIKit
public final class Keyboard: ObservableObject {
public struct State {
public var height: CGFloat = 0
public var animationDuration: TimeInterval = 0.25
}
@Published var state = State()
private var keyboardFrameChangeCancellable: AnyCancellable?
static let shared = Keyboard()
private init() {
keyboardFrameChangeCancellable = NotificationCenter.default
.publisher(for: UIResponder.keyboardWillChangeFrameNotification)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
guard let self = self, let userInfo = notification.userInfo else {
return
}
let height: CGFloat
if let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
height = UIScreen.main.bounds.intersection(keyboardFrame).height
} else {
height = 0
}
let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0.25
self.state = State(height: height, animationDuration: animationDuration)
}
}
}