IceCubesApp/IceCubesApp/App/Tabs/Settings/SettingsTab.swift

165 lines
4.4 KiB
Swift
Raw Normal View History

2022-12-01 08:05:26 +00:00
import SwiftUI
import Timeline
2022-12-22 09:53:36 +00:00
import Env
2022-12-01 08:05:26 +00:00
import Network
import Account
import Models
2022-12-24 13:55:04 +00:00
import DesignSystem
2022-12-01 08:05:26 +00:00
struct SettingsTabs: View {
@Environment(\.openURL) private var openURL
@EnvironmentObject private var client: Client
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var currentInstance: CurrentInstance
2022-12-01 08:05:26 +00:00
@EnvironmentObject private var appAccountsManager: AppAccountsManager
2022-12-24 13:55:04 +00:00
@EnvironmentObject private var theme: Theme
2022-12-01 08:05:26 +00:00
@State private var signInInProgress = false
@State private var signInServer = IceCubesApp.defaultServer
var body: some View {
NavigationStack {
Form {
2022-12-04 08:50:25 +00:00
appSection
accountSection
2022-12-24 13:55:04 +00:00
themeSection
2022-12-04 08:50:25 +00:00
instanceSection
2022-12-01 08:05:26 +00:00
}
.onOpenURL(perform: { url in
Task {
await continueSignIn(url: url)
}
})
.navigationTitle(Text("Settings"))
.navigationBarTitleDisplayMode(.inline)
}
.task {
if appAccountsManager.currentAccount.oauthToken != nil {
signInInProgress = true
await currentAccount.fetchCurrentAccount()
await currentInstance.fetchCurrentInstance()
2022-12-01 08:05:26 +00:00
signInInProgress = false
}
}
}
2022-12-04 08:50:25 +00:00
private var accountSection: some View {
Section("Account") {
if let accountData = currentAccount.account {
2022-12-04 08:50:25 +00:00
VStack(alignment: .leading) {
Text(appAccountsManager.currentAccount.server)
.font(.headline)
Text(accountData.displayName)
Text(accountData.username)
.font(.footnote)
.foregroundColor(.gray)
}
signOutButton
} else {
TextField("Mastodon server", text: $signInServer)
signInButton
}
}
}
2022-12-24 13:55:04 +00:00
private var themeSection: some View {
Section("Theme") {
ColorPicker("Tint color", selection: $theme.tintColor)
Button {
theme.tintColor = .brand
theme.labelColor = .label
} label: {
Text("Restore default")
}
}
}
2022-12-04 08:50:25 +00:00
@ViewBuilder
private var instanceSection: some View {
if let instanceData = currentInstance.instance {
2022-12-04 08:50:25 +00:00
Section("Instance info") {
LabeledContent("Name", value: instanceData.title)
Text(instanceData.shortDescription)
LabeledContent("Email", value: instanceData.email)
LabeledContent("Version", value: instanceData.version)
LabeledContent("Users", value: "\(instanceData.stats.userCount)")
2022-12-27 09:04:39 +00:00
LabeledContent("Posts", value: "\(instanceData.stats.statusCount)")
2022-12-04 08:50:25 +00:00
LabeledContent("Domains", value: "\(instanceData.stats.domainCount)")
}
Section("Instance rules") {
ForEach(instanceData.rules) { rule in
Text(rule.text)
}
}
2022-12-04 08:50:25 +00:00
}
}
private var appSection: some View {
Section("App") {
NavigationLink(destination: IconSelectorView()) {
Label {
Text("Icon selector")
} icon: {
2022-12-27 20:35:41 +00:00
if let icon = IconSelectorView.Icon(rawValue: UIApplication.shared.alternateIconName ?? "AppIcon") {
Image(uiImage: .init(named: icon.iconName)!)
.resizable()
.frame(width: 25, height: 25)
.cornerRadius(4)
}
2022-12-04 08:50:25 +00:00
}
}
2022-12-23 15:21:31 +00:00
Link(destination: URL(string: "https://github.com/Dimillian/IceCubesApp")!) {
Text("https://github.com/Dimillian/IceCubesApp")
}
2022-12-04 08:50:25 +00:00
}
}
2022-12-01 08:05:26 +00:00
private var signInButton: some View {
Button {
signInInProgress = true
Task {
await signIn()
}
} label: {
if signInInProgress {
ProgressView()
} else {
Text("Sign in")
}
}
}
private var signOutButton: some View {
Button {
appAccountsManager.delete(account: appAccountsManager.currentAccount)
} label: {
Text("Sign out").foregroundColor(.red)
}
}
private func signIn() async {
do {
client.server = signInServer
let oauthURL = try await client.oauthURL()
openURL(oauthURL)
} catch {
signInInProgress = false
}
}
private func continueSignIn(url: URL) async {
do {
let oauthToken = try await client.continueOauthFlow(url: url)
appAccountsManager.add(account: AppAccount(server: client.server, oauthToken: oauthToken))
await currentAccount.fetchCurrentAccount()
await currentInstance.fetchCurrentInstance()
2022-12-01 08:05:26 +00:00
signInInProgress = false
} catch {
signInInProgress = false
}
}
}