Skip to content

Commit

Permalink
make the signout button not crash the app
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Jul 13, 2024
1 parent 5ff44f6 commit e1c47a0
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 48 deletions.
8 changes: 8 additions & 0 deletions Revolt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
172F2D052C22ED8500948C00 /* CheckboxStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 172F2D042C22ED8100948C00 /* CheckboxStyle.swift */; };
173190032B754D4700B6DA49 /* EmojiPickerContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 173190022B754D4700B6DA49 /* EmojiPickerContent.swift */; };
1739DB962B08E53B00D23DAD /* MessageBadge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1739DB952B08E53B00D23DAD /* MessageBadge.swift */; };
173A336A2C422EF800B8A58E /* DeveloperSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 173A33692C422EF800B8A58E /* DeveloperSettings.swift */; };
173F16272C3C22580009FDF9 /* ServerInfoSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 173F16262C3C22540009FDF9 /* ServerInfoSheet.swift */; };
17429D972B4C91170036105A /* ChannelSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17429D962B4C91170036105A /* ChannelSettings.swift */; };
17429D992B4C931D0036105A /* ChannelOverviewSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17429D982B4C931D0036105A /* ChannelOverviewSettings.swift */; };
Expand Down Expand Up @@ -179,6 +180,7 @@
172F2D042C22ED8100948C00 /* CheckboxStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxStyle.swift; sourceTree = "<group>"; };
173190022B754D4700B6DA49 /* EmojiPickerContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiPickerContent.swift; sourceTree = "<group>"; };
1739DB952B08E53B00D23DAD /* MessageBadge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageBadge.swift; sourceTree = "<group>"; };
173A33692C422EF800B8A58E /* DeveloperSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperSettings.swift; sourceTree = "<group>"; };
173F16262C3C22540009FDF9 /* ServerInfoSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerInfoSheet.swift; sourceTree = "<group>"; };
17429D962B4C91170036105A /* ChannelSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelSettings.swift; sourceTree = "<group>"; };
17429D982B4C931D0036105A /* ChannelOverviewSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelOverviewSettings.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -485,6 +487,7 @@
isa = PBXGroup;
children = (
170C23CF2C224B6D0057E399 /* ExperimentsSettings.swift */,
173A33692C422EF800B8A58E /* DeveloperSettings.swift */,
170C23D02C224B6D0057E399 /* NotificationSettings.swift */,
170C23D12C224B6D0057E399 /* SettingsCommon.swift */,
170C23D22C224B6D0057E399 /* UserSettings.swift */,
Expand Down Expand Up @@ -834,6 +837,7 @@
1750EF6E2B0585B300DD3EB3 /* ResendEmail.swift in Sources */,
17E019D72AF1998300AB4663 /* MessageAttachment.swift in Sources */,
DAAA4BFB29F2518900F41E52 /* MessageView.swift in Sources */,
173A336A2C422EF800B8A58E /* DeveloperSettings.swift in Sources */,
1759C39A2B291A75006E6BBE /* MessageContentsView.swift in Sources */,
17E019D12AF14EC000AB4663 /* ServerIcon.swift in Sources */,
172F2D012C22ED4D00948C00 /* IteratorProtocol.swift in Sources */,
Expand Down Expand Up @@ -880,6 +884,10 @@
/* Begin PBXTargetDependency section */
175465D02C42147B0076B393 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
platformFilters = (
ios,
maccatalyst,
);
target = 175465C92C42147B0076B393 /* NotificationService */;
targetProxy = 175465CF2C42147B0076B393 /* PBXContainerItemProxy */;
};
Expand Down
4 changes: 4 additions & 0 deletions Revolt/Api/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ struct HTTPClient {
func fetchSelf() async -> Result<User, RevoltError> {
await req(method: .get, route: "/users/@me")
}

func signout() async -> Result<EmptyResponse, RevoltError> {
await req(method: .post, route: "/auth/session/logout")
}

func fetchApiInfo() async -> Result<ApiInfo, RevoltError> {
await req(method: .get, route: "/")
Expand Down
30 changes: 27 additions & 3 deletions Revolt/Api/UserSettingsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import Observation
import OSLog
import Sentry
import Types

let logger = Logger(subsystem: "chat.revolt.app", category: "settingsStore")
Expand All @@ -32,7 +33,7 @@ struct UserSettingsAccountData: Codable {

@Observable
class UserSettingsStore: Codable {
var user: User?
var user: Types.User?
var accountData: UserSettingsAccountData?

/// This is null when we havent asked for permission yet
Expand Down Expand Up @@ -100,11 +101,15 @@ class UserSettingsData {
Task(priority: .medium, operation: self.fetchFromApi)
}

@Sendable func fetchFromApi() async {

func fetchFromApi() async {
while viewState == nil {
try! await Task.sleep(for: .seconds(1))
}
let state = viewState!
if await state.state == .signedOut {
return
}

do {
self.store.user = try await state.http.fetchSelf().get()
Expand All @@ -117,7 +122,26 @@ class UserSettingsData {
writeCacheToFile()
} catch {
self.dataState = .failed
logger.error("An error occurred while fetching user settings: \(error.localizedDescription)")
let error = error as! RevoltError
switch error {
case .Alamofire(let afErr):
if afErr.responseCode == 401 {
await state.setSignedOutState()
} else {
SentrySDK.capture(error: error)
}
case .HTTPError(_, let status):
if status == 401 {
await state.setSignedOutState()
} else {
SentrySDK.capture(error: error)
}
default:
#if DEBUG
logger.error("An error occurred while fetching user settings: \(error.localizedDescription)")
#endif
SentrySDK.capture(error: error)
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions Revolt/Pages/Settings/DeveloperSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DeveloperSettings.swift
// Revolt
//
// Created by Angelo Manca on 2024-07-12.
//

import SwiftUI

struct DeveloperSettings: View {
@EnvironmentObject var viewState: ViewState

var body: some View {
Button(action: {
Task {
await viewState.promptForNotifications()
}
}) {
Text("Force remote notification upload")
}
}
}
24 changes: 22 additions & 2 deletions Revolt/Pages/Settings/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum CurrentSettingsPage: Hashable {

struct Settings: View {
@EnvironmentObject var viewState: ViewState

@State var presentLogoutDialog = false

var body: some View {
List {
Expand Down Expand Up @@ -60,12 +62,18 @@ struct Settings: View {
Image(systemName: "info.circle.fill")
Text("About")
}
NavigationLink(destination: ExperimentsSettings()) {
NavigationLink(destination: { ExperimentsSettings() }) {
Image(systemName: "flask.fill")
Text("Experiments")
}
#if DEBUG
NavigationLink(destination: { DeveloperSettings() }) {
Image(systemName: "face.smiling")
Text("Developer")
}
#endif
Button {
viewState.logout()
presentLogoutDialog = true
} label: {
HStack {
Image(systemName: "arrow.left.square")
Expand All @@ -87,6 +95,18 @@ struct Settings: View {
}
.toolbarBackground(viewState.theme.topBar.color, for: .automatic)
.background(viewState.theme.background)
.confirmationDialog("Are you sure?", isPresented: $presentLogoutDialog, titleVisibility: .visible) {
Button("Yes", role: .destructive) {
Task {
await viewState.signOut()
}
}
.keyboardShortcut(.defaultAction)
Button("Wait!", role: .cancel) {
presentLogoutDialog = false
}
.keyboardShortcut(.cancelAction)
}
}
}

Expand Down
31 changes: 28 additions & 3 deletions Revolt/RevoltApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ struct ApplicationSwitcher: View {
@Environment(\.colorScheme) var colorScheme
@EnvironmentObject var viewState: ViewState

@ViewBuilder
var body: some View {
if viewState.sessionToken != nil && !viewState.isOnboarding {
if viewState.state != .signedOut && !viewState.isOnboarding {
InnerApp()
.transition(.slide)
.task {
await viewState.backgroundWsTask()
if viewState.state != .signedOut {
withAnimation {
viewState.state = .connecting
}
}
}
.onChange(of: colorScheme) { before, after in
// automatically switch the color scheme if the user pressed "auto" in the preferences menu
Expand All @@ -96,6 +101,13 @@ struct ApplicationSwitcher: View {
}
} else {
Welcome()
.transition(.slide)
.onAppear {
if viewState.state == .signedOut && viewState.sessionToken != nil { // signging out
viewState.sessionToken = nil
viewState.destroyCache()
}
}
}
}
}
Expand All @@ -106,8 +118,21 @@ struct InnerApp: View {
var body: some View {
NavigationStack(path: $viewState.path) {
switch viewState.state {
case .connecting:
case .signedOut:
Text("Signed out... How did you get here?")
case .connecting, .notConnecting, .reconnecting:
VStack {
Text("Connecting...")
#if DEBUG
Button(action: {
viewState.destroyCache()
viewState.sessionToken = nil
viewState.state = .signedOut
}) {
Text("Developer: Nuke everything and force welcome screen")
}
#endif
}
case .connected:
HomeRewritten(
currentSelection: $viewState.currentServer,
Expand Down
Loading

0 comments on commit e1c47a0

Please sign in to comment.