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

169 lines
5.2 KiB
Swift
Raw Normal View History

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
2022-12-29 17:22:07 +01:00
struct SettingsTabs: View {
2023-01-08 14:16:43 +01:00
@EnvironmentObject private var pushNotifications: PushNotificationsService
2023-01-06 17:14:34 +01:00
@EnvironmentObject private var preferences: UserPreferences
2022-12-01 09:05:26 +01:00
@EnvironmentObject private var client: Client
@EnvironmentObject private var currentInstance: CurrentInstance
2022-12-01 09:05:26 +01:00
@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
2023-01-01 09:19:00 +01:00
@StateObject private var routeurPath = RouterPath()
2022-12-29 14:07:58 +01:00
@State private var addAccountSheetPresented = false
2022-12-01 09:05:26 +01:00
var body: some View {
NavigationStack(path: $routeurPath.path) {
2022-12-01 09:05:26 +01:00
Form {
2022-12-04 09:50:25 +01:00
appSection
2022-12-29 07:00:00 +01:00
accountsSection
2023-01-06 17:14:34 +01:00
generalSection
2022-12-01 09:05:26 +01:00
}
2022-12-29 10:39:34 +01:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2022-12-01 09:05:26 +01:00
.navigationTitle(Text("Settings"))
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(theme.primaryBackgroundColor, for: .navigationBar)
2023-01-06 17:14:34 +01:00
.withAppRouteur()
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
2022-12-01 09:05:26 +01:00
}
2023-01-01 09:19:00 +01:00
.onAppear {
routeurPath.client = client
}
2022-12-01 09:05:26 +01:00
.task {
if appAccountsManager.currentAccount.oauthToken != nil {
await currentInstance.fetchCurrentInstance()
2022-12-01 09:05:26 +01:00
}
}
.withSafariRouteur()
.environmentObject(routeurPath)
2022-12-01 09:05:26 +01:00
}
2022-12-29 07:00:00 +01:00
private var accountsSection: some View {
Section("Accounts") {
2022-12-30 08:36:22 +01:00
ForEach(appAccountsManager.availableAccounts) { account in
2022-12-29 07:00:00 +01:00
HStack {
2022-12-30 08:36:22 +01:00
AppAccountView(viewModel: .init(appAccount: account))
}
.onTapGesture {
withAnimation {
appAccountsManager.currentAccount = account
2022-12-29 07:00:00 +01:00
}
2022-12-04 09:50:25 +01:00
}
2022-12-30 08:36:22 +01:00
}
.onDelete { indexSet in
if let index = indexSet.first {
let account = appAccountsManager.availableAccounts[index]
2023-01-08 14:16:43 +01:00
if let token = account.oauthToken {
Task {
await pushNotifications.deleteSubscriptions(accounts: [.init(server: account.server, token: token)])
}
}
2022-12-30 08:36:22 +01:00
appAccountsManager.delete(account: account)
}
2022-12-04 09:50:25 +01:00
}
2022-12-29 14:07:58 +01:00
addAccountButton
2022-12-04 09:50:25 +01:00
}
2022-12-29 10:39:34 +01:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-04 09:50:25 +01:00
}
2023-01-06 17:14:34 +01:00
@ViewBuilder
private var generalSection: some View {
Section("General") {
2023-01-08 10:22:52 +01:00
NavigationLink(destination: PushNotificationsView()) {
Label("Push notifications", systemImage: "bell.and.waves.left.and.right")
}
2023-01-06 17:14:34 +01:00
if let instanceData = currentInstance.instance {
NavigationLink(destination: InstanceInfoView(instance: instanceData)) {
Label("Instance Information", systemImage: "server.rack")
}
}
2023-01-06 17:14:34 +01:00
NavigationLink(destination: DisplaySettingsView()) {
Label("Display Settings", systemImage: "paintpalette")
}
2023-01-06 17:14:34 +01:00
NavigationLink(destination: remoteLocalTimelinesView) {
Label("Remote Local Timelines", systemImage: "dot.radiowaves.right")
2022-12-24 14:55:04 +01:00
}
Picker(selection: $preferences.preferredBrowser) {
ForEach(PreferredBrowser.allCases, id: \.rawValue) { browser in
switch browser {
case .inAppSafari:
Text("In-App Safari").tag(browser)
case .safari:
Text("System Safari").tag(browser)
}
}
} label: {
Label("Browser", systemImage: "network")
}
2022-12-24 14:55:04 +01:00
}
2022-12-29 10:39:34 +01:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-24 14:55:04 +01:00
}
2022-12-04 09:50:25 +01:00
private var appSection: some View {
Section("App") {
NavigationLink(destination: IconSelectorView()) {
Label {
2023-01-06 17:14:34 +01:00
Text("App Icon")
2022-12-04 09:50:25 +01:00
} icon: {
2022-12-31 14:01:00 +01:00
if let icon = IconSelectorView.Icon(string: UIApplication.shared.alternateIconName ?? "AppIcon") {
2022-12-27 21:35:41 +01:00
Image(uiImage: .init(named: icon.iconName)!)
.resizable()
.frame(width: 25, height: 25)
.cornerRadius(4)
}
2022-12-04 09:50:25 +01:00
}
}
2023-01-07 13:44:13 +01:00
Link(destination: URL(string: "https://github.com/Dimillian/IceCubesApp")!) {
Label("Source (Github link)", systemImage: "link")
}
.tint(theme.labelColor)
2023-01-07 13:44:13 +01:00
NavigationLink(destination: SupportAppView()) {
Label("Support the app", systemImage: "wand.and.stars")
2022-12-23 16:21:31 +01:00
}
2022-12-04 09:50:25 +01:00
}
2022-12-29 10:39:34 +01:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-04 09:50:25 +01:00
}
2022-12-29 14:07:58 +01:00
private var addAccountButton: some View {
2022-12-01 09:05:26 +01:00
Button {
2022-12-29 14:07:58 +01:00
addAccountSheetPresented.toggle()
2022-12-01 09:05:26 +01:00
} label: {
2022-12-29 14:07:58 +01:00
Text("Add account")
}
.sheet(isPresented: $addAccountSheetPresented) {
AddAccountView()
2022-12-01 09:05:26 +01:00
}
}
2023-01-06 17:14:34 +01:00
private var remoteLocalTimelinesView: some View {
Form {
ForEach(preferences.remoteLocalTimelines, id: \.self) { server in
Text(server)
}.onDelete { indexes in
if let index = indexes.first {
_ = preferences.remoteLocalTimelines.remove(at: index)
}
}
.listRowBackground(theme.primaryBackgroundColor)
Button {
2023-01-06 17:14:34 +01:00
routeurPath.presentedSheet = .addRemoteLocalTimeline
} label: {
2023-01-06 17:14:34 +01:00
Label("Add a local timeline", systemImage: "badge.plus.radiowaves.right")
}
2023-01-06 17:14:34 +01:00
.listRowBackground(theme.primaryBackgroundColor)
}
2023-01-06 17:14:34 +01:00
.navigationTitle("Remote Local Timelines")
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2022-12-01 09:05:26 +01:00
}
}