Skip to content

Commit

Permalink
Issue #394: Improve logging, fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mipolansk committed Jul 15, 2024
1 parent c506458 commit 8ec79f5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 19 deletions.
5 changes: 3 additions & 2 deletions SUPLA/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
#endif

if let backgroundEntryTime = settings.backgroundEntryTime,
if settings.lockScreenSettings.pinForAppRequired,
let backgroundEntryTime = settings.backgroundEntryTime,
dateProvider.currentTimestamp() - backgroundEntryTime > backgroundUnlockedTime
{
suplaAppStateHolder.handle(event: .lock)
Expand All @@ -100,8 +101,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
SALog.debug("Push token: \(token)")
#endif
var settings = settings

var settings = settings
settings.pushToken = deviceToken
UpdateTokenTask().update(token: deviceToken) { SALog.info("Token update task finished") }
}
Expand Down
102 changes: 85 additions & 17 deletions SUPLA/Core/State/SuplaAppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ enum SuplaAppState: Equatable {
}
}

enum Error: Swift.Error {
case illegalEvent(message: String)
}

func nextState(event: SuplaAppEvent) -> SuplaAppState? {
switch (self) {
case .initialization: initializationNextState(for: event)
case .initialization: try! initializationNextState(for: event)

Check notice on line 52 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L52

No calls to throwing functions occur within 'try' expression
case .locked: lockedNextState(for: event)
case .firstProfileCreation: firstProfileCreationNextState(for: event)
case .connecting(let reason): connectingNextState(for: event, previousReason: reason)
case .connected: connectedNextState(for: event)
case .disconnecting: disconnectingNextState(for: event)
case .locking: lockingNextState(for: event)
case .finished(let reason): finishedNextState(for: event, previousReason: reason)
case .firstProfileCreation: try! firstProfileCreationNextState(for: event)

Check notice on line 54 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L54

No calls to throwing functions occur within 'try' expression
case .connecting(let reason): try! connectingNextState(for: event, previousReason: reason)

Check notice on line 55 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L55

No calls to throwing functions occur within 'try' expression
case .connected: try! connectedNextState(for: event)
case .disconnecting: try! disconnectingNextState(for: event)

Check notice on line 57 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L57

No calls to throwing functions occur within 'try' expression
case .locking: try! lockingNextState(for: event)

Check notice on line 58 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L58

No calls to throwing functions occur within 'try' expression
case .finished(let reason): try! finishedNextState(for: event, previousReason: reason)

Check notice on line 59 in SUPLA/Core/State/SuplaAppState.swift

View check run for this annotation

Xcode Cloud / SUPLA | SUPLA | Test - iOS

SUPLA/Core/State/SuplaAppState.swift#L59

No calls to throwing functions occur within 'try' expression
}
}

Expand All @@ -62,7 +66,12 @@ enum SuplaAppState: Equatable {
case .lock: .locked
case .initialized: .connecting()
case .noAccount: .firstProfileCreation
default: fatalError("Unexpected event in Initialization: \(event)")
case .connecting: try! illegalConnectingEvent()
case .connected: try! illegalConnectedEvent()
case .cancel: try! illegalCancelEvent()
case .unlock: try! illegalUnlockEvent()
case .finish: try! illegalFinishEvent()
case .error: try! illegalErrorEvent()
}
}

Expand All @@ -71,15 +80,26 @@ enum SuplaAppState: Equatable {
case .lock, .onStart, .networkConnected, .finish: nil
case .unlock: .connecting()
case .noAccount: .firstProfileCreation
default: fatalError("Unexpected event in Locked: \(event)")
case .initialized: try! illegalInitializedEvent()
case .connecting: try! illegalConnectingEvent()
case .connected: try! illegalConnectedEvent()
case .cancel: try! illegalCancelEvent()
case .error: try! illegalErrorEvent()
}
}

private func firstProfileCreationNextState(for event: SuplaAppEvent) -> SuplaAppState? {
switch (event) {
case .onStart, .networkConnected: nil
case .connecting: .connecting()
default: fatalError("Unexpected event in FirstProfileCreation: \(event)")
case .connected: try! illegalConnectedEvent()
case .cancel: try! illegalCancelEvent()
case .unlock: try! illegalUnlockEvent()
case .finish: try! illegalFinishEvent()
case .error: try! illegalErrorEvent()
case .initialized: try! illegalInitializedEvent()
case .noAccount: try! illegalNoAccountEvent()
case .lock: try! illegalLockEvent()
}
}

Expand All @@ -92,19 +112,23 @@ enum SuplaAppState: Equatable {
case .networkConnected: .connecting()
case .error(let reason): .connecting(reason: reason)
case .finish(let reason): .finished(reason: reason == nil ? previousReason : reason)
default: fatalError("Unexpected event in Connecting: \(event)")
case .noAccount: try! illegalNoAccountEvent()
case .unlock: try! illegalUnlockEvent()
}
}

private func connectedNextState(for event: SuplaAppEvent) -> SuplaAppState? {
private func connectedNextState(for event: SuplaAppEvent) throws -> SuplaAppState? {
switch (event) {
case .onStart, .networkConnected: nil
case .connecting: .connecting()
case .lock: .locked
case .cancel: .disconnecting
case .finish(let reason): .finished(reason: reason)
case .error(let reason): .finished(reason: reason)
default: fatalError("Unexpected event in Connected: \(event)")
case .initialized: try! illegalInitializedEvent()
case .noAccount: try! illegalNoAccountEvent()
case .connected: try! illegalConnectedEvent()
case .unlock: try! illegalUnlockEvent()
}
}

Expand All @@ -114,15 +138,22 @@ enum SuplaAppState: Equatable {
case .lock: .locking
case .finish(let reason): .finished(reason: reason)
case .error(let reason): .finished(reason: reason)
default: fatalError("Unexpected event in Disconnecting: \(event)")
case .initialized: try! illegalInitializedEvent()
case .noAccount: try! illegalNoAccountEvent()
case .unlock: try! illegalUnlockEvent()
}
}

private func lockingNextState(for event: SuplaAppEvent) -> SuplaAppState? {
switch (event) {
case .onStart, .lock, .cancel, .networkConnected: nil
case .finish(_): .locked
default: fatalError("Unexpected event in Locking: \(event)")
case .finish: .locked
case .initialized: try! illegalInitializedEvent()
case .noAccount: try! illegalNoAccountEvent()
case .connected: try! illegalConnectedEvent()
case .unlock: try! illegalUnlockEvent()
case .connecting: try! illegalConnectingEvent()
case .error(_): try! illegalErrorEvent()
}
}

Expand All @@ -135,7 +166,44 @@ enum SuplaAppState: Equatable {
case .noAccount: .firstProfileCreation
case .error(let reason): reason != previousReason ? .finished(reason: reason) : nil
case .finish(let reason): reason != previousReason ? .finished(reason: reason ?? previousReason) : nil
default: fatalError("Unexpected event in Finished: \(event)")
case .connected: try! illegalConnectedEvent()
case .unlock: try! illegalUnlockEvent()
}
}

private func illegalInitializedEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected initialized event!")
}

private func illegalConnectingEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected connecting event!")
}

private func illegalConnectedEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected connected event!")
}

private func illegalCancelEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected cancel event!")
}

private func illegalErrorEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected error event!")
}

private func illegalUnlockEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected unlock event!")
}

private func illegalFinishEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected finish event!")
}

private func illegalNoAccountEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected no account event!")
}

private func illegalLockEvent() throws -> SuplaAppState? {
throw Error.illegalEvent(message: "Unexpected lock event!")
}
}

0 comments on commit 8ec79f5

Please sign in to comment.