Skip to content

Commit

Permalink
Show toasts above the keyboard if visible.
Browse files Browse the repository at this point in the history
It appears there's no way to show toasts over the keyboard
since iOS 16.
  • Loading branch information
aforge committed Dec 3, 2022
1 parent 6e44ed0 commit c42367a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ PODS:
- Kingfisher (5.15.8):
- Kingfisher/Core (= 5.15.8)
- Kingfisher/Core (5.15.8)
- MobileVLCKit (3.4.1b9)
- MobileVLCKit (3.4.1b13)
- nanopb (2.30908.0):
- nanopb/decode (= 2.30908.0)
- nanopb/encode (= 2.30908.0)
Expand Down Expand Up @@ -165,7 +165,7 @@ SPEC CHECKSUMS:
GoogleDataTransport: 629c20a4d363167143f30ea78320d5a7eb8bd940
GoogleUtilities: 684ee790a24f73ebb2d1d966e9711c203f2a4237
Kingfisher: a3c03d702433fa6cfedabb2bddbe076fb8f2e902
MobileVLCKit: 63fd88297505b97027f23842ae9df3a7bf17b31e
MobileVLCKit: 685736e6bb92ed2107b0f083f094b61709413122
nanopb: a0ba3315591a9ae0a16a309ee504766e90db0c96
PhoneNumberKit: 441e8b26ec88d598e3591de9061eff18f5dd12e8
PromisesObjC: 68159ce6952d93e17b2dfe273b8c40907db5ba58
Expand Down
7 changes: 3 additions & 4 deletions Tinodios/MessageInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,13 @@ class MessageInteractor: DefaultComTopic.Listener, MessageBusinessLogic, Message
onFailure: { err in
Cache.log.error("sendMessage error: %@", err.localizedDescription)
if let e = err as? TinodeError {
switch e {
case .notConnected(_):
if case .notConnected(_) = e {
DispatchQueue.main.async { UiUtils.showToast(message: NSLocalizedString("You are offline.", comment: "Toast notification")) }
Cache.tinode.reconnectNow(interactively: false, reset: false)
default:
DispatchQueue.main.async { UiUtils.showToast(message: NSLocalizedString("Message not sent.", comment: "Toast notification")) }
return nil
}
}
DispatchQueue.main.async { UiUtils.showToast(message: NSLocalizedString("Message not sent.", comment: "Toast notification")) }
return nil
}
).thenFinally {
Expand Down
21 changes: 15 additions & 6 deletions Tinodios/UiUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,19 @@ class UiUtils {
public static func showToast(message: String, duration: TimeInterval = 3.0, level: ToastLevel = .error) {
// Grab the last window (instead the key window) so we can show
// the toast over they keyboard when it's presented.
guard let parent = UIApplication.shared.windows.last else {
guard let topWindow = UIApplication.shared.windows.last else {
Cache.log.error("UiUtils.showToast - parent window not set")
return
}
var parent: UIView = topWindow
if #available(iOS 16, *) {
// UIRemoteKeyboardWindow is no longer available in iOS 16 so keyboard (if presented) may
// cover the toast.
// Show toast over the input accessory view if visible.
if let inputView = UiUtils.topViewController(rootViewController: (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController)?.inputAccessoryView {
parent = inputView
}
}

let iconSize: CGFloat = 32
let spacing: CGFloat = 8
Expand Down Expand Up @@ -600,17 +609,17 @@ class UiUtils {
}

public static func topViewController(rootViewController: UIViewController?) -> UIViewController? {
guard let rootViewController = rootViewController else { return nil }
guard let presented = rootViewController.presentedViewController else {
return rootViewController
guard let controller = rootViewController else { return nil }
if let presented = controller.presentedViewController {
return topViewController(rootViewController: presented)
}
switch presented {
switch controller {
case let navigationController as UINavigationController:
return topViewController(rootViewController: navigationController.viewControllers.last)
case let tabBarController as UITabBarController:
return topViewController(rootViewController: tabBarController.selectedViewController)
default:
return topViewController(rootViewController: presented)
return controller
}
}

Expand Down

0 comments on commit c42367a

Please sign in to comment.