Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #437

Merged
merged 7 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class AppCoordinator: AppCoordinatorProtocol {
}

func start() {
guard stateMachine.state == .initial else {
MXLog.error("Received a start request when already started")
return
}

stateMachine.processEvent(userSessionStore.hasSessions ? .startWithExistingSession : .startWithAuthentication)
}

Expand Down Expand Up @@ -255,21 +260,27 @@ class AppCoordinator: AppCoordinatorProtocol {

deobserveUserSessionChanges()

if !isSoftLogout {
Task {
// first log out from the server
_ = await userSession.clientProxy.logout()

// regardless of the result, clear user data
userSessionStore.logout(userSession: userSession)
userSession = nil
notificationManager?.delegate = nil
notificationManager = nil
}
guard !isSoftLogout else {
stateMachine.processEvent(.completedSigningOut)
return
}

// complete logging out
stateMachine.processEvent(.completedSigningOut)
Task {
showLoadingIndicator()

// first log out from the server
_ = await userSession.clientProxy.logout()

// regardless of the result, clear user data
userSessionStore.logout(userSession: userSession)
userSession = nil
notificationManager?.delegate = nil
notificationManager = nil

stateMachine.processEvent(.completedSigningOut)

hideLoadingIndicator()
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
}
}

private func presentSplashScreen(isSoftLogout: Bool = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class AppCoordinatorStateMachine {

private let stateMachine: StateMachine<State, Event>

var state: AppCoordinatorStateMachine.State {
stateMachine.state
}

init() {
stateMachine = StateMachine(state: .initial)
configure()
Expand Down
41 changes: 15 additions & 26 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct HomeScreenCoordinatorParameters {
}

enum HomeScreenCoordinatorAction {
case presentRoomScreen(roomIdentifier: String)
case presentRoom(roomIdentifier: String)
case presentSettingsScreen
case presentFeedbackScreen
case presentSessionVerificationScreen
Expand All @@ -50,48 +50,37 @@ final class HomeScreenCoordinator: CoordinatorProtocol {
guard let self else { return }

switch action {
case .selectRoom(let roomIdentifier):
self.callback?(.presentRoomScreen(roomIdentifier: roomIdentifier))
case .userMenu(let action):
self.processUserMenuAction(action)
case .verifySession:
case .presentRoom(let roomIdentifier):
self.callback?(.presentRoom(roomIdentifier: roomIdentifier))
case .presentFeedbackScreen:
self.callback?(.presentFeedbackScreen)
case .presentSettingsScreen:
self.callback?(.presentSettingsScreen)
case .presentInviteFriendsScreen:
self.presentInviteFriends()
case .presentSessionVerificationScreen:
self.callback?(.presentSessionVerificationScreen)
case .signOut:
self.callback?(.signOut)
}
}
}

// MARK: - Public

func start() {
#if !DEBUG
if parameters.bugReportService.crashedLastRun {
viewModel.presentAlert(
AlertInfo(id: UUID(),
title: ElementL10n.sendBugReportAppCrashed,
primaryButton: .init(title: ElementL10n.no, action: nil),
secondaryButton: .init(title: ElementL10n.yes) { [weak self] in
self?.callback?(.presentFeedbackScreen)
}))
viewModel.presentCrashedLastRunAlert()
}
#endif
}

func toPresentable() -> AnyView {
AnyView(HomeScreen(context: viewModel.context))
}

// MARK: - Private

private func processUserMenuAction(_ action: HomeScreenViewUserMenuAction) {
switch action {
case .settings:
callback?(.presentSettingsScreen)
case .inviteFriends:
presentInviteFriends()
case .feedback:
callback?(.presentFeedbackScreen)
case .signOut:
callback?(.signOut)
}
}

private func presentInviteFriends() {
parameters.navigationStackCoordinator.setSheetCoordinator(InviteFriendsCoordinator(userId: parameters.userSession.userID))
Expand Down
9 changes: 6 additions & 3 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import Foundation
import UIKit

enum HomeScreenViewModelAction {
case selectRoom(roomIdentifier: String)
case userMenu(action: HomeScreenViewUserMenuAction)
case verifySession
case presentRoom(roomIdentifier: String)
case presentSessionVerificationScreen
case presentSettingsScreen
case presentInviteFriendsScreen
case presentFeedbackScreen
case signOut
}

enum HomeScreenViewUserMenuAction {
Expand Down
25 changes: 20 additions & 5 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,42 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol

// MARK: - Public

// swiftlint:disable:next cyclomatic_complexity
override func process(viewAction: HomeScreenViewAction) async {
switch viewAction {
case .loadRoomData(let roomIdentifier):
if state.roomListMode != .skeletons {
loadDataForRoomIdentifier(roomIdentifier)
}
case .selectRoom(let roomIdentifier):
callback?(.selectRoom(roomIdentifier: roomIdentifier))
callback?(.presentRoom(roomIdentifier: roomIdentifier))
case .userMenu(let action):
callback?(.userMenu(action: action))
switch action {
case .feedback:
callback?(.presentFeedbackScreen)
case .settings:
callback?(.presentSettingsScreen)
case .inviteFriends:
callback?(.presentInviteFriendsScreen)
case .signOut:
callback?(.signOut)
}
case .verifySession:
callback?(.verifySession)
callback?(.presentSessionVerificationScreen)
case .skipSessionVerification:
state.showSessionVerificationBanner = false
case .updatedVisibleItemIdentifiers(let identifiers):
updateVisibleRange(visibleItemIdentifiers: identifiers)
}
}

func presentAlert(_ alertInfo: AlertInfo<UUID>) {
state.bindings.alertInfo = alertInfo
func presentCrashedLastRunAlert() {
state.bindings.alertInfo = AlertInfo(id: UUID(),
title: ElementL10n.sendBugReportAppCrashed,
primaryButton: .init(title: ElementL10n.no, action: nil),
secondaryButton: .init(title: ElementL10n.yes) { [weak self] in
self?.callback?(.presentFeedbackScreen)
})
}

// MARK: - Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ protocol HomeScreenViewModelProtocol {

var context: HomeScreenViewModelType.Context { get }

func presentAlert(_ alert: AlertInfo<UUID>)
func presentCrashedLastRunAlert()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ struct RoomSummaryDetails {
let lastMessageTimestamp: Date?
let unreadNotificationCount: UInt
}

extension RoomSummaryDetails: CustomStringConvertible {
var description: String {
"id: \"\(id)\", isDirect: \"\(isDirect)\", unreadNotificationCount: \"\(unreadNotificationCount)\""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
guard let self else { return }

switch action {
case .presentRoomScreen(let roomIdentifier):
case .presentRoom(let roomIdentifier):
self.stateMachine.processEvent(.selectRoom(roomId: roomIdentifier))
case .presentSettingsScreen:
self.stateMachine.processEvent(.showSettingsScreen)
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/Sources/HomeScreenViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class HomeScreenViewModelTests: XCTestCase {
var selectedRoomId = ""
viewModel.callback = { result in
switch result {
case .selectRoom(let roomId):
case .presentRoom(let roomId):
correctResult = true
selectedRoomId = roomId
default:
Expand All @@ -53,8 +53,8 @@ class HomeScreenViewModelTests: XCTestCase {
var correctResult = false
viewModel.callback = { result in
switch result {
case .userMenu(let action):
correctResult = action == .settings
case .presentSettingsScreen:
correctResult = true
default:
break
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/340.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wait for logout confirmation before changing the app state
1 change: 1 addition & 0 deletions changelog.d/pr-437.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent crash popups when force quitting the application