Skip to content

Commit

Permalink
Issue #394: Add option to lock app by PIN code
Browse files Browse the repository at this point in the history
  • Loading branch information
mipolansk committed Jul 12, 2024
1 parent a27acfb commit 9049233
Show file tree
Hide file tree
Showing 215 changed files with 6,784 additions and 2,940 deletions.
624 changes: 478 additions & 146 deletions SUPLA.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions SUPLA/AddWizardVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,7 @@ - (IBAction)cancelOrBackTouch:(nullable id)sender {
[self cleanUp];
[self.OpQueue cancelAllOperations];
[self savePrefs];
[[SAApp currentNavigationCoordinator] finish];
[[SAApp SuplaClient] reconnect];
[SuplaAppCoordinatorLegacyWrapper dismissWithAnimated: true];
}


Expand Down
159 changes: 0 additions & 159 deletions SUPLA/AppDelegate.m

This file was deleted.

100 changes: 99 additions & 1 deletion SUPLA/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,104 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


import Foundation

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

private var clientStopWork: DispatchWorkItem? = nil

override init() {
SALogWrapper.setup()
DiContainer.start()
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
#if DEBUG
// Short-circuit starting app if running unit tests
if (ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil) {
return true
}
#endif

window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.overrideUserInterfaceStyle = settings.darkMode.interfaceStyle
coordinator.attachToWindow(window)
coordinator.start(animated: true)
}

registerForNotifications()

DispatchQueue.global(qos: .userInitiated).async {
InitializationUseCase.invoke()
}

return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
disconnectUseCase.invokeSynchronous()
suplaAppStateHolder.handle(event: .finish(reason: .appInBackground))
}

func applicationDidBecomeActive(_ application: UIApplication) {
#if DEBUG
if (ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil) {
return
}
#endif

suplaAppStateHolder.handle(event: .onStart)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
#if DEBUG
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
SALog.debug("Push token: \(token)")
#endif
var settings = settings

settings.pushToken = deviceToken
UpdateTokenTask().update(token: deviceToken) { SALog.info("Token update task finished") }
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: any Error) {
SALog.error("Failed to register for remote notifications with error \(error)")
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
do {
try insertNotificationUseCase.invoke(userInfo: userInfo).subscribeSynchronous()
} catch {
SALog.error("Could not insert notification: \(String(describing: error))")
}
completionHandler(.newData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}

private func registerForNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { [weak self] (granted, error) in

if (granted) {
DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() }
} else {
SALog.error("Notifications not allowed \(String(describing: error))")
var settings = self?.settings
settings?.pushToken = nil
}
}
}
}
7 changes: 7 additions & 0 deletions SUPLA/Core/Config/GlobalSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protocol GlobalSettings {
var channelHeight: ChannelHeight { get set }
var showOpeningPercent: Bool { get set }
var darkMode: DarkModeSetting { get set }
var lockScreenSettings: LockScreenSettings { get set }
}

class GlobalSettingsImpl: GlobalSettings {
Expand Down Expand Up @@ -156,6 +157,12 @@ class GlobalSettingsImpl: GlobalSettings {
get { return DarkModeSetting.from(defaults.integer(forKey: darkModeKey)) }
set { defaults.set(newValue.rawValue, forKey: darkModeKey) }
}

private let lockScreenKey = "supla_config_lock_screen"
var lockScreenSettings: LockScreenSettings {
get { return LockScreenSettings.from(string: defaults.string(forKey: lockScreenKey)) }
set { defaults.set(newValue.asString(), forKey: lockScreenKey) }
}
}

@objc class GlobalSettingsLegacy: NSObject {
Expand Down
15 changes: 9 additions & 6 deletions SUPLA/Core/DI/DiContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ extension DiContainer {
@objc static func start() {
// MARK: General

register(SuplaAppCoordinator.self, SuplaAppCoordinatorImpl())
register(GlobalSettings.self, GlobalSettingsImpl())
register(RuntimeConfig.self, RuntimeConfigImpl())
register(SuplaClientProvider.self, SuplaClientProviderImpl())
register(SuplaAppWrapper.self, SuplaAppWrapperImpl())
register(SuplaAppProvider.self, SuplaAppProviderImpl())
register(VibrationService.self, VibrationServiceImpl())
register(SingleCall.self, SingleCallImpl())
register(DateProvider.self, DateProviderImpl())
Expand All @@ -77,6 +78,8 @@ extension DiContainer {
register(SessionResponseProvider.self, SessionResponseProviderImpl())
register(SuplaSchedulers.self, SuplaSchedulersImpl())
register(ThreadHandler.self, ThreadHandlerImpl())
register(SuplaAppStateHolder.self, SuplaAppStateHolderImpl())
register(DatabaseProxy.self, DatabaseProxyImpl())
// Managers
register(UpdateEventsManager.self, UpdateEventsManagerImpl())
register(ChannelConfigEventsManager.self, ChannelConfigEventsManagerImpl())
Expand Down Expand Up @@ -167,7 +170,10 @@ extension DiContainer {
register(CallSuplaClientOperationUseCase.self, CallSuplaClientOperationUseCaseImpl())
register(ExecuteRollerShutterActionUseCase.self, ExecuteRollerShutterActionUseCaseImpl())
register(AuthorizeUseCase.self, AuthorizeUseCaseImpl())
register(LoginUseCase.self, LoginUseCaseImpl())
register(ExecuteFacadeBlindActionUseCase.self, ExecuteFacadeBlindActionUseCaseImpl())
register(DisconnectUseCase.self, DisconnectUseCaseImpl())
register(ReconnectUseCase.self, ReconnectUseCaseImpl())
// Usecases - Detail
register(ProvideDetailTypeUseCase.self, ProvideDetailTypeUseCaseImpl())
// Usecases - Group
Expand Down Expand Up @@ -196,6 +202,8 @@ extension DiContainer {
// Usecases - Notification
register(InsertNotificationUseCase.self, InsertNotificationUseCaseImpl())
register(NotificationCenterWrapper.self, NotificationCenterWrapperImpl())
// Usecases - Lock
register(CheckPinUseCase.self, CheckPinUseCaseImpl())

// MARK: Not singletons

Expand All @@ -218,11 +226,6 @@ extension DiContainer {
return DiContainer.shared.resolve(type: DeviceConfigEventsManager.self)
}

@objc static func setPushToken(token: Data?) {
var settings = DiContainer.shared.resolve(type: GlobalSettings.self)
settings?.pushToken = token
}

@objc static func getPushToken() -> Data? {
DiContainer.shared.resolve(type: GlobalSettings.self)?.pushToken
}
Expand Down
6 changes: 6 additions & 0 deletions SUPLA/Core/Events/ListsEventsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protocol UpdateEventsManager: UpdateEventsManagerEmitter {
func observeChannelsUpdate() -> Observable<Void>
func observeGroupsUpdate() -> Observable<Void>
func observeScenesUpdate() -> Observable<Void>

func cleanup()
}

final class UpdateEventsManagerImpl: UpdateEventsManager {
Expand Down Expand Up @@ -127,6 +129,10 @@ final class UpdateEventsManagerImpl: UpdateEventsManager {

func observeScenesUpdate() -> Observable<Void> { sceneUpdatesSubject.asObservable() }

func cleanup() {
subjects.removeAll()
}

private func getSubjectForScene(sceneId: Int) -> BehaviorRelay<Int> {
return syncedQueue.sync(execute: {
getSubject(id: sceneId, type: .scene)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
/*
Copyright (C) AC SOFTWARE SP. Z O.O.

Expand All @@ -16,10 +17,13 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

class NotificationsLogNavigationCoordinator: BaseNavigationCoordinator {
override var viewController: UIViewController {
_viewController
}
import RxSwift

private lazy var _viewController: NotificationsLogVC = .init(navigator: self)
extension Completable {
static func complete() -> Completable {
Completable.create { completable in
completable(.completed)
return Disposables.create()
}
}
}
Loading

0 comments on commit 9049233

Please sign in to comment.