Add keyboard observer

This commit is contained in:
seongho.hong
2019-08-27 12:30:34 +09:00
parent 3bd7e2944f
commit b4f91a9e4a
5 changed files with 83 additions and 5 deletions

View File

@@ -0,0 +1,18 @@
//
// KeyboardObserver.h
// Toaster
//
// Created by SeongHo on 27/08/2019.
// Copyright © 2019 Suyeol Jeon. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ToastKeyboardObserver : NSObject
@property (nonatomic, assign) BOOL didKeyboardShow;
+ (instancetype)shared;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,51 @@
//
// KeyboardObserver.m
// Toaster
//
// Created by SeongHo on 27/08/2019.
// Copyright © 2019 Suyeol Jeon. All rights reserved.
//
#import "ToastKeyboardObserver.h"
#import <UIKit/UIKit.h>
@implementation ToastKeyboardObserver
+ (void)load {
[ToastKeyboardObserver shared];
}
+ (instancetype)shared {
static ToastKeyboardObserver *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});
return shared;
}
- (instancetype)init {
self = [super init];
if (self) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[center addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
return self;
}
- (void)keyboardWillShow:(NSNotification*)notification {
self.didKeyboardShow = YES;
}
- (void)keyboardDidHide:(NSNotification*)notification {
self.didKeyboardShow = NO;
}
@end

View File

@@ -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()
@@ -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 {
@@ -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
ToastKeyboardObserver.shared().didKeyboardShow || $0.isOpaque
}), window !== self {
return window
}

View File

@@ -2,3 +2,5 @@
FOUNDATION_EXPORT double ToasterVersionNumber;
FOUNDATION_EXPORT const unsigned char ToasterVersionString[];
#import "ToastKeyboardObserver.h"

View File

@@ -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 */; };
C104FC202314D92D0034B9AB /* ToastKeyboardObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = C104FC1E2314D92D0034B9AB /* ToastKeyboardObserver.h */; settings = {ATTRIBUTES = (Public, ); }; };
C104FC212314D92D0034B9AB /* ToastKeyboardObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = C104FC1F2314D92D0034B9AB /* ToastKeyboardObserver.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -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>"; };
C104FC1E2314D92D0034B9AB /* ToastKeyboardObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ToastKeyboardObserver.h; sourceTree = "<group>"; };
C104FC1F2314D92D0034B9AB /* ToastKeyboardObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ToastKeyboardObserver.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -161,6 +165,8 @@
0306DBA71D85D62A007AE314 /* ToastCenter.swift */,
0306DBA81D85D62A007AE314 /* ToastView.swift */,
0306DBA91D85D62A007AE314 /* ToastWindow.swift */,
C104FC1E2314D92D0034B9AB /* ToastKeyboardObserver.h */,
C104FC1F2314D92D0034B9AB /* ToastKeyboardObserver.m */,
);
path = Sources;
sourceTree = "<group>";
@@ -228,6 +234,7 @@
buildActionMask = 2147483647;
files = (
0306DBAA1D85D62A007AE314 /* Toaster.h in Headers */,
C104FC202314D92D0034B9AB /* ToastKeyboardObserver.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -320,7 +327,7 @@
TargetAttributes = {
03A75E2A1BCF2066002E46C4 = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1030;
};
03A75E4E1BCF20D4002E46C4 = {
CreatedOnToolsVersion = 7.0.1;
@@ -400,6 +407,7 @@
0306DBAC1D85D62A007AE314 /* ToastCenter.swift in Sources */,
0306DBAE1D85D62A007AE314 /* ToastWindow.swift in Sources */,
0306DBAB1D85D62A007AE314 /* Toast.swift in Sources */,
C104FC212314D92D0034B9AB /* ToastKeyboardObserver.m in Sources */,
0306DBAD1D85D62A007AE314 /* ToastView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -603,6 +611,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
@@ -624,6 +633,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 5.0;
};
name = Release;
};