Skip to content

Commit

Permalink
Issue #394: Supla client initialization removed from ChannelCell
Browse files Browse the repository at this point in the history
  • Loading branch information
mipolansk committed Jul 15, 2024
1 parent ea22b66 commit d49a18e
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 21 deletions.
26 changes: 17 additions & 9 deletions SUPLA/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
@Singleton private var dateProvider: DateProvider

private var clientStopWork: DispatchWorkItem? = nil
private var wasInBackground = true

private var backgroundUnlockedTime: Double {
#if DEBUG
Expand Down Expand Up @@ -71,29 +72,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
return true
}

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

disconnectUseCase.invokeSynchronous()
suplaAppStateHolder.handle(event: .finish(reason: .appInBackground))
}

func applicationDidBecomeActive(_ application: UIApplication) {
SALog.debug("Application did become active")

#if DEBUG
if (ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil) {
return
}
#endif

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

wasInBackground = false
}

func applicationDidEnterBackground(_ application: UIApplication) {
SALog.debug("Application did enter background")
wasInBackground = true

var settings = settings
settings.backgroundEntryTime = dateProvider.currentTimestamp()

disconnectUseCase.invokeSynchronous()
suplaAppStateHolder.handle(event: .finish(reason: .appInBackground))
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Expand Down
23 changes: 23 additions & 0 deletions SUPLA/Core/Navigation/SuplaAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ final class SuplaAppCoordinatorImpl: NSObject, SuplaAppCoordinator {
switch ($0) {
case .initialization, .connecting(_), .finished:
self.navigateToStatusView()
case .locked:
self.navigationController.viewControllers.last?.presentedViewController?.dismiss(animated: false)
self.popToViewController(ofClass: StatusFeature.ViewController.self)
self.navigateToLockScreen(unlockAction: .authorizeApplication)
default:
break
}
Expand Down Expand Up @@ -115,6 +119,8 @@ final class SuplaAppCoordinatorImpl: NSObject, SuplaAppCoordinator {
}

func navigateToAddWizard() {
navigationController.viewControllers.last?.presentedViewController?.dismiss(animated: false)

let avc = SAAddWizardVC(nibName: "AddWizardVC", bundle: nil)
avc.modalPresentationStyle = .fullScreen
avc.modalTransitionStyle = .crossDissolve
Expand Down Expand Up @@ -227,6 +233,7 @@ final class SuplaAppCoordinatorImpl: NSObject, SuplaAppCoordinator {
if (navigationController.viewControllers.isEmpty) {
navigateTo(StatusFeature.ViewController.create())
} else if (navigationToStatusAllowed()) {
navigationController.viewControllers.last?.presentedViewController?.dismiss(animated: false)
popToViewController(ofClass: StatusFeature.ViewController.self)
}
}
Expand All @@ -240,6 +247,10 @@ final class SuplaAppCoordinatorImpl: NSObject, SuplaAppCoordinator {
return subcontroller.screenTakeoverAllowed()
}

if let subcontroller = navigationController.viewControllers.last?.presentedViewController as? NavigationSubcontroller {
return subcontroller.screenTakeoverAllowed()
}

return true
}
}
Expand All @@ -252,6 +263,8 @@ final class SuplaAppNavigationController: UINavigationController {

override func pushViewController(_ viewController: UIViewController, animated: Bool) {
statusBarStyle = viewController.preferredStatusBarStyle
SALog.debug("[PUSH] \(String(describing: viewController))")

if let navBarController = viewController as? NavigationBarVisibilityController {
SALog.debug("[PUSH] Setting navigation bar hidden: \(navBarController.navigationBarHidden)")
navigationBarHiddenOverride = navBarController.navigationBarHidden
Expand All @@ -267,6 +280,8 @@ final class SuplaAppNavigationController: UINavigationController {

override func popViewController(animated: Bool) -> UIViewController? {
let viewController = super.popViewController(animated: animated)
SALog.debug("[POP] \(String(describing: viewController))")

statusBarStyle = viewControllers.last?.preferredStatusBarStyle ?? .lightContent
if let navBarController = viewControllers.last as? NavigationBarVisibilityController {
SALog.debug("[POP] Setting navigation bar hidden: \(navBarController.navigationBarHidden)")
Expand All @@ -278,6 +293,8 @@ final class SuplaAppNavigationController: UINavigationController {

override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
let viewControllers = super.popToViewController(viewController, animated: animated)
SALog.debug("[POP] \(String(describing: viewController))")

statusBarStyle = viewController.preferredStatusBarStyle
if let navBarController = viewController as? NavigationBarVisibilityController {
SALog.debug("[POP] Setting navigation bar hidden: \(navBarController.navigationBarHidden)")
Expand Down Expand Up @@ -318,4 +335,10 @@ final class SuplaAppCoordinatorLegacyWrapper: NSObject {
@Singleton<SuplaAppCoordinator> var coordinator
coordinator.navigateTo(viewController)
}

@objc
static func present(_ viewController: UIViewController) {
@Singleton<SuplaAppCoordinator> var coordinator
coordinator.present(viewController)
}
}
2 changes: 1 addition & 1 deletion SUPLA/Features/PinSetup/PinSetupVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension PinSetupFeature {
contentView = PinSetupFeature.View(
viewState: state,
onPinChange: { viewModel.onPinChange($0) },
onSave: { viewModel.onSaveClick(.application) },
onSave: { viewModel.onSaveClick(scope) },
onAppear: { viewModel.onAppear() }
)
}
Expand Down
2 changes: 1 addition & 1 deletion SUPLA/Features/Status/StatusVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension StatusFeature {
self?.state.viewType = .connecting
self?.state.stateText = .disconnecting
case .locked:
self?.coordinator.navigateToLockScreen(unlockAction: .authorizeApplication)
break // Logic for locked placed in SuplaAppCoordinator
}
})
.disposedWhenDisappear(by: self)
Expand Down
3 changes: 2 additions & 1 deletion SUPLA/SAChannelStateExtendedValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ -(NSString *)lightSourceOperatingTimeString {
}

-(NSDate*) countdownEndsAt {
int timeDiff = [SAApp SuplaClient]->serverTimeDiffInSec;
SASuplaClient* client = [[SAApp instance] optionalSuplaClient];
int timeDiff = client == nil ? 0 : client->serverTimeDiffInSec;
return [NSDate dateWithTimeIntervalSince1970: _tsev.CountdownEndsAt + timeDiff];
}

Expand Down
5 changes: 4 additions & 1 deletion SUPLA/SAChannelStatePopup.m
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,10 @@ -(void)makeChannelStateRequest:(NSTimer *)timer {
if (_channel == nil) {
return;
}
[[SAApp SuplaClient] channelStateRequestWithChannelId:_channel.remote_id];
SASuplaClient* client = [[SAApp instance] optionalSuplaClient];
if (client != nil) {
[client channelStateRequestWithChannelId:_channel.remote_id];
}
}

-(void)setChannel:(SAChannel *)channel {
Expand Down
10 changes: 2 additions & 8 deletions SUPLA/SADialog.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,9 @@ + (BOOL)viewControllerIsPresented:(UIViewController*)vc {
}

+ (void)showModal:(SADialog*)dialogVC {
UIViewController *rootVC = [SuplaAppCoordinatorLegacyWrapper currentViewController];
dialogVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

if (@available(iOS 13.0, *)) {
dialogVC.modalInPresentation = YES;
[rootVC presentViewController:dialogVC animated:NO completion:nil];
} else {
[rootVC presentModalViewController:dialogVC animated:NO];
}
dialogVC.modalInPresentation = YES;
[SuplaAppCoordinatorLegacyWrapper present:dialogVC];
}

- (void)keyboardDidShow:(NSNotification*)notification {
Expand Down
1 change: 1 addition & 0 deletions SuplaApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ NS_ASSUME_NONNULL_BEGIN
-(void) unregisterRestApiClientTask:(SARestApiClientTask *)task;
-(void) cancelAllRestApiClientTasks;
-(bool) isClientWorking;
-(SASuplaClient *) optionalSuplaClient;
@end

extern NSString *kSADataChangedNotification;
Expand Down
11 changes: 11 additions & 0 deletions SuplaApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ -(BOOL) isClientWorking {
return result;
}

-(SASuplaClient *) optionalSuplaClient {
@synchronized(self) {
if ( _SuplaClient != nil) {
return _SuplaClient;
}
}

return nil;

}

-(BOOL) isClientAuthorized {
BOOL result = NO;
@synchronized(self) {
Expand Down

0 comments on commit d49a18e

Please sign in to comment.