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

133 lines
3.7 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
2022-12-29 16:22:07 +00:00
struct SettingsTabs: View {
2022-12-01 08:05:26 +00:00
@EnvironmentObject private var client: Client
@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
2022-12-29 13:07:58 +00:00
@State private var addAccountSheetPresented = false
2022-12-01 08:05:26 +00:00
var body: some View {
NavigationStack {
Form {
2022-12-04 08:50:25 +00:00
appSection
2022-12-29 06:00:00 +00:00
accountsSection
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
}
2022-12-29 09:39:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2022-12-01 08:05:26 +00:00
.navigationTitle(Text("Settings"))
.navigationBarTitleDisplayMode(.inline)
}
.task {
if appAccountsManager.currentAccount.oauthToken != nil {
await currentInstance.fetchCurrentInstance()
2022-12-01 08:05:26 +00:00
}
}
}
2022-12-29 06:00:00 +00:00
private var accountsSection: some View {
Section("Accounts") {
2022-12-30 07:36:22 +00:00
ForEach(appAccountsManager.availableAccounts) { account in
2022-12-29 06:00:00 +00:00
HStack {
2022-12-30 07:36:22 +00:00
AppAccountView(viewModel: .init(appAccount: account))
}
.onTapGesture {
withAnimation {
appAccountsManager.currentAccount = account
2022-12-29 06:00:00 +00:00
}
2022-12-04 08:50:25 +00:00
}
2022-12-30 07:36:22 +00:00
}
.onDelete { indexSet in
if let index = indexSet.first {
let account = appAccountsManager.availableAccounts[index]
appAccountsManager.delete(account: account)
}
2022-12-04 08:50:25 +00:00
}
2022-12-29 13:07:58 +00:00
addAccountButton
2022-12-04 08:50:25 +00:00
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-04 08:50:25 +00:00
}
2022-12-24 13:55:04 +00:00
private var themeSection: some View {
Section("Theme") {
2022-12-29 09:39:34 +00:00
Toggle("Prefer dark color scheme", isOn: .init(get: {
theme.colorScheme == "dark"
}, set: { newValue in
if newValue {
theme.colorScheme = "dark"
} else {
theme.colorScheme = "light"
}
}))
2022-12-24 13:55:04 +00:00
ColorPicker("Tint color", selection: $theme.tintColor)
2022-12-29 09:39:34 +00:00
ColorPicker("Background color", selection: $theme.primaryBackgroundColor)
ColorPicker("Secondary Background color", selection: $theme.secondaryBackgroundColor)
2022-12-24 13:55:04 +00:00
Button {
2022-12-29 09:39:34 +00:00
theme.colorScheme = "dark"
2022-12-24 13:55:04 +00:00
theme.tintColor = .brand
2022-12-29 09:39:34 +00:00
theme.primaryBackgroundColor = .primaryBackground
theme.secondaryBackgroundColor = .secondaryBackground
2022-12-24 13:55:04 +00:00
} label: {
Text("Restore default")
}
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-24 13:55:04 +00:00
}
2022-12-04 08:50:25 +00:00
@ViewBuilder
private var instanceSection: some View {
if let instanceData = currentInstance.instance {
2022-12-29 13:07:58 +00:00
InstanceInfoView(instance: instanceData)
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-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-04 08:50:25 +00:00
}
2022-12-29 13:07:58 +00:00
private var addAccountButton: some View {
2022-12-01 08:05:26 +00:00
Button {
2022-12-29 13:07:58 +00:00
addAccountSheetPresented.toggle()
2022-12-01 08:05:26 +00:00
} label: {
2022-12-29 13:07:58 +00:00
Text("Add account")
}
.sheet(isPresented: $addAccountSheetPresented) {
AddAccountView()
2022-12-01 08:05:26 +00:00
}
}
private var signOutButton: some View {
Button {
appAccountsManager.delete(account: appAccountsManager.currentAccount)
} label: {
Text("Sign out").foregroundColor(.red)
}
}
}