Swifterswift: A convenient way to get keyboard size, animation duration, start frame and end frame from Notification

Created on 14 May 2018  路  4Comments  路  Source: SwifterSwift/SwifterSwift

You know, iOS sends notifications for keyboard events. I was wondering if we can use extensions to get information from notifications by extending the type Notification. all properties should be optional since the notification might be not for a keyboard event.

discussion help wanted

Most helpful comment

IMO we shouldn't add new types to SwifterSwift. We should only provide extensions on Apple libraries.
If you're creating new types to abstract logic, you're creating a new library at that point.

All 4 comments

Hey @omaralbeik :))
I'm not sure if extending notification for this kind of functionality is an aproach that makes sense in a type abstaction. Mostly because of doing that, I think we kind of assuming that a Notification, that is a more generic class that abstracts a system notification have some that have a relation to keyboard.
For this kind of functionality I'll probably go for something like:

class KeyboardEvent {
    private var note: Notification
    private(set) var keyBoardFrame: CGRect?

    init(notification: Notification) {
       /// populate all the properties 
    }
}
// So using will be readable and clear, saying that we might be able to construct a keyboard event with
// a note, and once we have a KeyboardEvent we may access the keyboard properties.
@objc keyboardWillHide(_ note: Notification) {
    let keyboardEvent = KeyboardEvent(notification: note )
    let keyboardFrame = keyboardEvent.keyBoardFrame
}

I don't know, maybe I'm being to verbose with this 馃槀 where just extending the notification will be way more simple 馃槀But I think with something like this we avoid coupling a generic Notification type to keyboard event and make usage more clear. But maybe I'm making something that should be simple too much complex 馃槄
Let me know your toughts about this? @omaralbeik
Also this solution do not applies to SwifterSwift I think, because it requires a new type :((
So maybe just extending notification is sufficient. And also I'm starting to get confused with my own toughts 馃ぃ 馃ぃ 馃ぃ

@LucianoPAlmeida I agree, the type Notification is too generic for this. I'd prefer to use a custom struct like the example below, however I'm not sure this is the best fit for this SwifterSwift @SD10 what do you think? how do you handle keyboard events in your apps

import UIKit

// swiftlint:disable identifier_name
public struct UIKeyboardNotification {

    private(set) var _startFrame: CGRect!
    public var startFrame: CGRect {
        return _startFrame

    }

    private(set) var _endFrame: CGRect!
    public var endFrame: CGRect {
        return _endFrame
    }

    private(set) var _animationCurve: UIViewAnimationCurve!
    public var animationCurve: UIViewAnimationCurve {
        return _animationCurve
    }

    private(set) var _animationDuration: TimeInterval!
    public var animationDuration: TimeInterval {
        return _animationDuration
    }

    private(set) var _isLocalUser: Bool!
    public var isLocalUser: Bool {
        return _isLocalUser
    }

    init?(notification: Notification) {
        guard let userInfo = notification.userInfo else { return nil }
        guard let startFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect else { return nil }
        guard let endFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return nil }
        guard let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval else { return nil }
        guard let isLocalUser = userInfo[UIKeyboardIsLocalUserInfoKey] as? Bool else { return nil }

        _startFrame = startFrame
        _endFrame = endFrame

        if let curveRawValue = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int {
            _animationCurve = UIViewAnimationCurve(rawValue: curveRawValue)
        }

        _animationDuration = animationDuration
        _isLocalUser = isLocalUser
    }

}
// swiftlint:enable identifier_name

@omaralbeik That's just a nice solution :)) But yeah not fits SwifterSwift

Sent with GitHawk

IMO we shouldn't add new types to SwifterSwift. We should only provide extensions on Apple libraries.
If you're creating new types to abstract logic, you're creating a new library at that point.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanglinwei picture fanglinwei  路  5Comments

omaralbeik picture omaralbeik  路  5Comments

SD10 picture SD10  路  3Comments

omaralbeik picture omaralbeik  路  5Comments

omaralbeik picture omaralbeik  路  3Comments