Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add better support for keyboard info
- Loading branch information
Showing
4 changed files
with
71 additions
and
23 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,36 @@ | ||
import Foundation | ||
import UIKit | ||
|
||
extension Notification { | ||
|
||
/// Struct modeling keyboard updates | ||
public struct KeyboardInfo { | ||
/// Keyboard height | ||
public var height: CGFloat = 0 | ||
/// Keyboard animation curve | ||
public var animationCurve: UIView.AnimationCurve = UIView.AnimationCurve.easeInOut | ||
/// Keyboard animation duration | ||
public var animationDuration: TimeInterval = 0.0 | ||
/// Is the keyboard visible | ||
public var isVisible: Bool { | ||
height != 0 | ||
} | ||
} | ||
|
||
/// Get keyboard info updates | ||
var keyboardInfo: KeyboardInfo { | ||
var info = KeyboardInfo() | ||
if let value = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect { | ||
info.height = value.height | ||
} | ||
if let value = userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber { | ||
if let curve = UIView.AnimationCurve(rawValue: value.intValue) { | ||
info.animationCurve = curve | ||
} | ||
} | ||
if let value = userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber { | ||
info.animationDuration = value.doubleValue | ||
} | ||
return info | ||
} | ||
} |
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 |
---|---|---|
@@ -1,26 +1,20 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
/// Get keyboard status updates | ||
struct KeyboardStateModifier: ViewModifier { | ||
|
||
@Binding var isVisible: Bool | ||
|
||
init(_ isVisible: Binding<Bool>) { | ||
_isVisible = isVisible | ||
} | ||
|
||
private let keyboardWillAppear = NotificationCenter.default | ||
.publisher(for: UIResponder.keyboardWillShowNotification) | ||
.map { _ -> Bool in true } | ||
|
||
private let keyboardWillHide = NotificationCenter.default | ||
.publisher(for: UIResponder.keyboardWillHideNotification) | ||
.map { _ -> Bool in false } | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.onReceive(keyboardWillAppear.merge(with: keyboardWillHide)) { | ||
self.isVisible = $0 | ||
} | ||
} | ||
|
||
@Binding var keyboardInfo: Notification.KeyboardInfo | ||
|
||
init(_ keyboardInfo: Binding<Notification.KeyboardInfo>) { | ||
_keyboardInfo = keyboardInfo | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.onReceive( | ||
AnyPublisher<Notification.KeyboardInfo, Never>.keyboardInfo, | ||
assignTo: $keyboardInfo | ||
) | ||
} | ||
} |
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