Skip to content

Commit

Permalink
feat: make sure we call applicationState from the main thread
Browse files Browse the repository at this point in the history
feat: use ApplicationStatable where possible
  • Loading branch information
adrien-coye committed Jul 4, 2023
1 parent 06cf460 commit f337a69
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Mail/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

@LazyInjectService private var orientationManager: OrientationManageable
@LazyInjectService private var accountManager: AccountManager
@LazyInjectService private var applicationState: ApplicationStatable

/// Making sure the DI is registered at a very early stage of the app launch.
private let dependencyInjectionHook = EarlyDIHook()
Expand All @@ -42,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
Logging.initLogging()

DDLogInfo("Application starting in foreground ? \(UIApplication.shared.applicationState != .background)")
DDLogInfo("Application starting in foreground ? \(applicationState.applicationState != .background)")
ApiFetcher.decoder.dateDecodingStrategy = .iso8601

UNUserNotificationCenter.current().delegate = notificationCenterDelegate
Expand Down
2 changes: 1 addition & 1 deletion MailCore/Utils/ApplicationStatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ import UIKit

/// Something that reads the application state if available
public protocol ApplicationStatable {
var applicationState: UIApplication.State? { get }
@MainActor var applicationState: UIApplication.State? { get }
}
28 changes: 15 additions & 13 deletions MailCore/Utils/MessagePresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,22 @@ public final class MessagePresenter: MessagePresentable {
// MARK: - private

private func showInContext(message: String, action: MessageAction?) {
// check not in extension mode
guard !Bundle.main.isExtension else {
presentInLocalNotification(message: message, action: action)
return
Task { @MainActor in
// check not in extension mode
guard !Bundle.main.isExtension else {
presentInLocalNotification(message: message, action: action)
return
}

// if app not in foreground, we use the local notifications
guard applicationState.applicationState == .active else {
presentInLocalNotification(message: message, action: action)
return
}

// Present the message as we are in foreground app context
presentInSnackbar(message: message, action: action)
}

// if app not in foreground, we use the local notifications
guard applicationState.applicationState == .active else {
presentInLocalNotification(message: message, action: action)
return
}

// Present the message as we are in foreground app context
presentInSnackbar(message: message, action: action)
}

// MARK: Private
Expand Down
3 changes: 2 additions & 1 deletion MailCore/Utils/NotificationsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public enum NotificationsHelper {
private static func sendImmediately(notification: UNMutableNotificationContent, id: String,
action: IKSnackBar.Action? = nil) {
DispatchQueue.main.async {
let isInBackground = Bundle.main.isExtension || UIApplication.shared.applicationState != .active
@LazyInjectService var applicationState: ApplicationStatable
let isInBackground = Bundle.main.isExtension || applicationState.applicationState != .active

if isInBackground {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
Expand Down

0 comments on commit f337a69

Please sign in to comment.