Skip to content

Commit

Permalink
Issue #394: Show lock screen after 2 minutes being in background
Browse files Browse the repository at this point in the history
  • Loading branch information
mipolansk committed Jul 12, 2024
1 parent 377b0a6 commit b6f6679
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
24 changes: 22 additions & 2 deletions SUPLA/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@

import Foundation

private let BACKGROUND_UNLOCKED_TIME_DEBUG_S: Double = 10
private let BACKGROUND_UNLOCKED_TIME_S: Double = 120

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow? = nil

@Singleton private var settings: GlobalSettings
@Singleton private var insertNotificationUseCase: InsertNotificationUseCase
@Singleton private var coordinator: SuplaAppCoordinator
@Singleton private var suplaAppStateHolder: SuplaAppStateHolder
@Singleton private var disconnectUseCase: DisconnectUseCase
@Singleton private var dateProvider: DateProvider

private var clientStopWork: DispatchWorkItem? = nil

private var backgroundUnlockedTime: Double {
#if DEBUG
BACKGROUND_UNLOCKED_TIME_DEBUG_S
#else
BACKGROUND_UNLOCKED_TIME_S
#endif
}

override init() {
SALogWrapper.setup()
DiContainer.start()
Expand Down Expand Up @@ -61,6 +72,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

func applicationDidEnterBackground(_ application: UIApplication) {
var settings = settings
settings.backgroundEntryTime = dateProvider.currentTimestamp()

disconnectUseCase.invokeSynchronous()
suplaAppStateHolder.handle(event: .finish(reason: .appInBackground))
}
Expand All @@ -72,7 +86,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
#endif

suplaAppStateHolder.handle(event: .onStart)
if let backgroundEntryTime = settings.backgroundEntryTime,
dateProvider.currentTimestamp() - backgroundEntryTime > backgroundUnlockedTime
{
suplaAppStateHolder.handle(event: .lock)
} else {
suplaAppStateHolder.handle(event: .onStart)
}
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Expand Down
15 changes: 15 additions & 0 deletions SUPLA/Core/Config/GlobalSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protocol GlobalSettings {
var showOpeningPercent: Bool { get set }
var darkMode: DarkModeSetting { get set }
var lockScreenSettings: LockScreenSettings { get set }
var backgroundEntryTime: Double? { get set }
}

class GlobalSettingsImpl: GlobalSettings {
Expand Down Expand Up @@ -163,6 +164,20 @@ class GlobalSettingsImpl: GlobalSettings {
get { return LockScreenSettings.from(string: defaults.string(forKey: lockScreenKey)) }
set { defaults.set(newValue.asString(), forKey: lockScreenKey) }
}

private let backgroundEntryTimeKey = "supla_background_entry_time"
var backgroundEntryTime: Double? {
get { return exists(backgroundEntryTimeKey) ? defaults.double(forKey: backgroundEntryTimeKey) : nil }
set {
if let time = newValue {
defaults.set(time, forKey: backgroundEntryTimeKey)
}
}
}

private func exists(_ key: String) -> Bool {
defaults.object(forKey: key) != nil
}
}

@objc class GlobalSettingsLegacy: NSObject {
Expand Down
7 changes: 7 additions & 0 deletions SUPLATests/Mocks/GlobalSettingsMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ class GlobalSettingsMock: GlobalSettings {
get { lockScreenSettingsReturns }
set { lockScreenSettingsValues.append(newValue) }
}


var backgroundEntryMock: FunctionMock<Double?, Double?> = .init()
var backgroundEntryTime: Double? {
get { backgroundEntryMock.get() }
set { backgroundEntryMock.set(newValue) }
}
}
8 changes: 8 additions & 0 deletions SUPLATests/Tools/Core/FunctionMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class FunctionMock<Parameters, Returns> {
return returns.next()
}

func set(_ parameters: Parameters) {
self.parameters.append(parameters)
}

func get() -> Returns {
return returns.next()
}

func verifyCalls(_ count: Int) {
XCTAssertEqual(parameters.count, count)
}
Expand Down

0 comments on commit b6f6679

Please sign in to comment.