Bubble/Threaded/Views/Settings/SettingsView.swift

240 lines
9.6 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import SwiftUI
2024-02-24 13:21:31 +01:00
import SwiftData
2023-12-29 11:17:37 +01:00
2024-02-15 23:39:06 +01:00
//TODO: Bring back "Privacy" with mutelist, blocklist and default visibility
2023-12-29 11:17:37 +01:00
struct SettingsView: View {
2024-02-08 08:34:36 +01:00
@Environment(UniversalNavigator.self) private var uniNav: UniversalNavigator
2024-02-24 13:21:31 +01:00
@Query private var loggedAccounts: [LoggedAccount]
2024-02-20 20:57:07 +01:00
@StateObject var navigator: Navigator
2024-02-24 13:21:31 +01:00
@State private var switched: Bool = false
2023-12-29 11:17:37 +01:00
var body: some View {
NavigationStack(path: $navigator.path) {
List {
2024-02-24 13:21:31 +01:00
if loggedAccounts.count > 0 {
Section {
ForEach(loggedAccounts) { logged in
if let app = logged.app {
SwitcherRow(app: app)
.listRowThreaded()
}
}
if AppDelegate.premium || loggedAccounts.count < 3 {
Button {
uniNav.presentedSheet = .mastodonLogin(logged: $switched)
} label: {
Label("settings.account-switcher.add", systemImage: "person.crop.circle.badge.plus")
.foregroundStyle(Color.blue)
}
.listRowThreaded()
}
}
.onChange(of: switched) { _, new in
if new == true {
// switched correctly
HapticManager.playHaptics(haptics: Haptic.success)
uniNav.selectedTab = .timeline
navigator.path = []
}
}
} else {
Section {
//MARK: Remove in later update
HStack(alignment: .center) {
Spacer()
Text("settings.account-switcher.relog")
.foregroundStyle(Color.gray)
.multilineTextAlignment(.center)
.font(.caption)
Spacer()
}
.listRowThreaded()
}
}
Spacer()
.frame(height: 30)
.listRowThreaded()
Section {
Button {
navigator.navigate(to: .about)
} label: {
Label("about", systemImage: "info.circle")
}
.listRowThreaded()
2024-03-07 23:24:44 +01:00
Button {
navigator.navigate(to: .privacy)
} label: {
Label("privacy", systemImage: "lock")
}
.listRowThreaded()
2024-02-12 00:47:39 +01:00
Button {
navigator.presentedCover = .shop
} label: {
Label(String("Threaded+"), systemImage: "plus")
}
.listRowThreaded()
2024-01-27 08:57:22 +01:00
Button {
2024-02-12 02:42:56 +01:00
navigator.navigate(to: .support)
} label: {
2024-03-07 23:06:28 +01:00
Label("setting.support", systemImage: "person.crop.circle.badge.questionmark")
}
.listRowThreaded()
Button {
navigator.navigate(to: .appearence)
} label: {
Label("setting.appearence", systemImage: "rectangle.3.group")
}
.listRowThreaded()
Button {
AppAccount.clear()
2024-01-26 23:12:43 +01:00
navigator.path = []
2024-02-11 18:44:15 +01:00
uniNav.selectedTab = .timeline
2024-02-08 08:34:36 +01:00
uniNav.presentedCover = .welcome
} label: {
Text("logout")
.foregroundStyle(.red)
}
.tint(Color.red)
.listRowThreaded()
}
}
2024-02-24 13:21:31 +01:00
.environmentObject(navigator)
.withAppRouter(navigator)
2024-02-11 18:44:15 +01:00
.withCovers(sheetDestination: $navigator.presentedCover)
.listThreaded()
.navigationTitle("settings")
.navigationBarTitleDisplayMode(.inline)
2023-12-29 11:17:37 +01:00
}
}
}
2024-02-24 13:21:31 +01:00
extension SettingsView {
struct SwitcherRow: View {
@Environment(AccountManager.self) private var accountManager: AccountManager
@Environment(UniversalNavigator.self) private var uniNav: UniversalNavigator
@EnvironmentObject private var navigator: Navigator
var app: AppAccount
@State private var account: Account? = nil
@State private var error: Bool = false
private var currentAccount: Bool {
2024-03-07 23:24:44 +01:00
let currentAccount = AccountManager.shared.forceAccount()
let currentClient = AccountManager.shared.forceClient()
let currentAcct = "\(currentAccount.acct)@\(currentClient.server)"
return currentAcct == app.accountName ?? ""
2024-02-24 13:21:31 +01:00
}
init(app: AppAccount) {
self.app = app
}
var body: some View {
ZStack {
if let acc = account {
HStack {
2024-02-29 13:33:29 +01:00
ProfilePicture(url: acc.avatar, size: 46)
2024-02-24 13:21:31 +01:00
VStack(alignment: .leading) {
Text(acc.displayName ?? "@\(acc.acct)")
.multilineTextAlignment(.leading)
Text("@\(acc.acct)")
.multilineTextAlignment(.leading)
.font(.caption)
.foregroundStyle(Color.gray)
}
Spacer()
if !currentAccount {
Button {
Task {
let c: Client = Client(server: app.server, oauthToken: app.oauthToken)
let am: AccountManager = .init(client: c)
let fetched: Account? = await am.fetchAccount()
if fetched == nil {
am.clear()
error = true
} else {
AccountManager.shared.setAccount(fetched!)
AccountManager.shared.setClient(c)
uniNav.selectedTab = .timeline
navigator.path = []
}
}
} label: {
Text("settings.account-switcher.log")
}
.buttonStyle(LargeButton(filled: true, height: 7.5, disabled: currentAccount))
.disabled(currentAccount)
} else {
Text("settings.account-switcher.current")
.foregroundStyle(Color.gray)
.font(.caption)
.padding(.horizontal)
.lineLimit(1)
}
}
} else {
Circle()
.fill(error ? Color.red.opacity(0.45) : Color.gray.opacity(0.45))
.frame(width: 54, height: 54)
VStack(alignment: .leading) {
Text(Account.placeholder().displayName ?? "@\(Account.placeholder().acct)")
.redacted(reason: .placeholder)
.multilineTextAlignment(.leading)
Text("@\(Account.placeholder().acct)")
.redacted(reason: .placeholder)
.multilineTextAlignment(.leading)
.font(.caption)
.foregroundStyle(Color.gray)
}
Spacer()
Button {
print(acct)
} label: {
Text("settings.account-switcher.log")
.redacted(reason: .placeholder)
}
.buttonStyle(LargeButton(filled: true, height: 7.5))
}
}
.task {
account = await findAccount(acct: app.accountName!)
}
}
private func findAccount(acct: String) async -> Account? {
guard let client = accountManager.getClient() else { return nil }
do {
try await Task.sleep(for: .milliseconds(250))
let results: SearchResults = try await client.get(endpoint: Search.search(query: acct, type: "accounts", offset: nil, following: nil), forceVersion: .v2)
return results.accounts.first
} catch {
print(error)
}
return nil
}
}
2023-12-29 11:17:37 +01:00
}