Skip to content

Commit

Permalink
Merge pull request #175 from cozzin/keyboardObserver
Browse files Browse the repository at this point in the history
Add keyboard observer
  • Loading branch information
Coeur authored Sep 7, 2019
2 parents 48e7cc9 + 063b380 commit 59a189c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
48 changes: 48 additions & 0 deletions Sources/KeyboardObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// KeyboardObserver.swift
// Toaster
//
// Created by SeongHo Hong on 28/08/2019.
// Copyright © 2019 Suyeol Jeon. All rights reserved.
//

import UIKit

final class KeyboardObserver {

static let shared = KeyboardObserver()

private(set) var didKeyboardShow: Bool = false

init() {
#if swift(>=4.2)
let keyboardWillShowName = UIWindow.keyboardWillShowNotification
let keyboardDidHideName = UIWindow.keyboardDidHideNotification
#else
let keyboardWillShowName = NSNotification.Name.UIKeyboardWillShow
let keyboardDidHideName = NSNotification.Name.UIKeyboardDidHide
#endif
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: keyboardWillShowName,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardDidHide),
name: keyboardDidHideName,
object: nil
)
}

@objc private func keyboardWillShow() {
didKeyboardShow = true
}

@objc private func keyboardDidHide() {
didKeyboardShow = false
}

}

5 changes: 1 addition & 4 deletions Sources/ToastWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ open class ToastWindow: UIWindow {
/// Will not return `rootViewController` while this value is `true`. Needed for iOS 13.
private var isShowing = false

private var didKeyboardShow = false

/// Returns original subviews. `ToastWindow` overrides `addSubview()` to add a subview to the
/// top window instead itself.
private var originalSubviews = NSPointerArray.weakObjects()
Expand Down Expand Up @@ -169,7 +167,6 @@ open class ToastWindow: UIWindow {
}

@objc private func keyboardWillShow() {
didKeyboardShow = true
guard let topWindow = self.topWindow(),
let subviews = self.originalSubviews.allObjects as? [UIView] else { return }
for subview in subviews {
Expand Down Expand Up @@ -220,7 +217,7 @@ open class ToastWindow: UIWindow {
private func topWindow() -> UIWindow? {
if let window = UIApplication.shared.windows.last(where: {
// https://github.com/devxoul/Toaster/issues/152
didKeyboardShow || $0.isOpaque
KeyboardObserver.shared.didKeyboardShow || $0.isOpaque
}), window !== self {
return window
}
Expand Down
22 changes: 22 additions & 0 deletions Sources/UIApplication+Load.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIApplication+Load.swift
// Toaster
//
// Created by SeongHo Hong on 28/08/2019.
// Copyright © 2019 Suyeol Jeon. All rights reserved.
//

import Foundation

extension UIApplication {

open override var next: UIResponder? {
UIApplication.runOnce
return super.next
}

private static let runOnce: Void = {
_ = KeyboardObserver.shared
}()

}
12 changes: 11 additions & 1 deletion Toaster.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
3719A73F2255991B009EAFCD /* Toaster.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 03A75E2B1BCF2066002E46C4 /* Toaster.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C06FC9D41F95A8CC00782082 /* ToasterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C06FC9D31F95A8CC00782082 /* ToasterTests.swift */; };
C06FC9D61F95A8CC00782082 /* Toaster.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A75E2B1BCF2066002E46C4 /* Toaster.framework */; };
C1C0178D23163F5000446CED /* KeyboardObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1C0178C23163F4F00446CED /* KeyboardObserver.swift */; };
C1C017922316419900446CED /* UIApplication+Load.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1C017912316419900446CED /* UIApplication+Load.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -104,6 +106,8 @@
C06FC9D11F95A8CC00782082 /* ToasterTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ToasterTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
C06FC9D31F95A8CC00782082 /* ToasterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToasterTests.swift; sourceTree = "<group>"; };
C06FC9D51F95A8CC00782082 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C1C0178C23163F4F00446CED /* KeyboardObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardObserver.swift; sourceTree = "<group>"; };
C1C017912316419900446CED /* UIApplication+Load.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Load.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -161,6 +165,8 @@
0306DBA71D85D62A007AE314 /* ToastCenter.swift */,
0306DBA81D85D62A007AE314 /* ToastView.swift */,
0306DBA91D85D62A007AE314 /* ToastWindow.swift */,
C1C0178C23163F4F00446CED /* KeyboardObserver.swift */,
C1C017912316419900446CED /* UIApplication+Load.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -320,7 +326,7 @@
TargetAttributes = {
03A75E2A1BCF2066002E46C4 = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1030;
};
03A75E4E1BCF20D4002E46C4 = {
CreatedOnToolsVersion = 7.0.1;
Expand Down Expand Up @@ -399,7 +405,9 @@
files = (
0306DBAC1D85D62A007AE314 /* ToastCenter.swift in Sources */,
0306DBAE1D85D62A007AE314 /* ToastWindow.swift in Sources */,
C1C017922316419900446CED /* UIApplication+Load.swift in Sources */,
0306DBAB1D85D62A007AE314 /* Toast.swift in Sources */,
C1C0178D23163F5000446CED /* KeyboardObserver.swift in Sources */,
0306DBAD1D85D62A007AE314 /* ToastView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -603,6 +611,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -624,6 +633,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down

0 comments on commit 59a189c

Please sign in to comment.