2022-12-01 09:05:26 +01:00
|
|
|
import SwiftUI
|
|
|
|
import Timeline
|
2022-12-22 10:53:36 +01:00
|
|
|
import Env
|
2022-12-01 09:05:26 +01:00
|
|
|
import Network
|
|
|
|
import Account
|
|
|
|
import Models
|
2022-12-24 14:55:04 +01:00
|
|
|
import DesignSystem
|
2022-12-01 09:05:26 +01:00
|
|
|
|
|
|
|
struct SettingsTabs: View {
|
|
|
|
@Environment(\.openURL) private var openURL
|
|
|
|
@EnvironmentObject private var client: Client
|
|
|
|
@EnvironmentObject private var appAccountsManager: AppAccountsManager
|
2022-12-24 14:55:04 +01:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2022-12-01 09:05:26 +01:00
|
|
|
|
|
|
|
@State private var signInInProgress = false
|
|
|
|
@State private var accountData: Account?
|
2022-12-04 09:50:25 +01:00
|
|
|
@State private var instanceData: Instance?
|
2022-12-01 09:05:26 +01:00
|
|
|
@State private var signInServer = IceCubesApp.defaultServer
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationStack {
|
|
|
|
Form {
|
2022-12-04 09:50:25 +01:00
|
|
|
appSection
|
|
|
|
accountSection
|
2022-12-24 14:55:04 +01:00
|
|
|
themeSection
|
2022-12-04 09:50:25 +01:00
|
|
|
instanceSection
|
2022-12-01 09:05:26 +01:00
|
|
|
}
|
|
|
|
.onOpenURL(perform: { url in
|
|
|
|
Task {
|
|
|
|
await continueSignIn(url: url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.navigationTitle(Text("Settings"))
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
}
|
|
|
|
.task {
|
|
|
|
if appAccountsManager.currentAccount.oauthToken != nil {
|
|
|
|
signInInProgress = true
|
|
|
|
await refreshAccountInfo()
|
2022-12-04 09:50:25 +01:00
|
|
|
await refreshInstanceInfo()
|
2022-12-01 09:05:26 +01:00
|
|
|
signInInProgress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 09:50:25 +01:00
|
|
|
private var accountSection: some View {
|
|
|
|
Section("Account") {
|
|
|
|
if let accountData {
|
|
|
|
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 14:55:04 +01: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 09:50:25 +01:00
|
|
|
@ViewBuilder
|
|
|
|
private var instanceSection: some View {
|
|
|
|
if let instanceData {
|
|
|
|
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 10:04:39 +01:00
|
|
|
LabeledContent("Posts", value: "\(instanceData.stats.statusCount)")
|
2022-12-04 09:50:25 +01:00
|
|
|
LabeledContent("Domains", value: "\(instanceData.stats.domainCount)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var appSection: some View {
|
|
|
|
Section("App") {
|
|
|
|
NavigationLink(destination: IconSelectorView()) {
|
|
|
|
Label {
|
|
|
|
Text("Icon selector")
|
|
|
|
} icon: {
|
2022-12-27 21:35:41 +01: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 09:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-23 16:21:31 +01:00
|
|
|
Link(destination: URL(string: "https://github.com/Dimillian/IceCubesApp")!) {
|
|
|
|
Text("https://github.com/Dimillian/IceCubesApp")
|
|
|
|
}
|
2022-12-04 09:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:05:26 +01: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 {
|
2022-12-04 09:50:25 +01:00
|
|
|
instanceData = nil
|
2022-12-01 09:05:26 +01:00
|
|
|
accountData = nil
|
|
|
|
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 refreshAccountInfo()
|
2022-12-04 09:50:25 +01:00
|
|
|
await refreshInstanceInfo()
|
2022-12-01 09:05:26 +01:00
|
|
|
signInInProgress = false
|
|
|
|
} catch {
|
|
|
|
signInInProgress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func refreshAccountInfo() async {
|
|
|
|
accountData = try? await client.get(endpoint: Accounts.verifyCredentials)
|
|
|
|
}
|
2022-12-04 09:50:25 +01:00
|
|
|
|
|
|
|
private func refreshInstanceInfo() async {
|
|
|
|
instanceData = try? await client.get(endpoint: Instances.instance)
|
|
|
|
}
|
2022-12-01 09:05:26 +01:00
|
|
|
}
|