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

348 lines
11 KiB
Swift
Raw Normal View History

2022-12-01 08:05:26 +00:00
import Account
import AppAccount
2023-01-17 10:36:01 +00:00
import DesignSystem
import Env
import Foundation
2023-01-17 10:36:01 +00:00
import Models
import Network
2023-02-26 05:45:57 +00:00
import Nuke
import SwiftData
2023-01-17 10:36:01 +00:00
import SwiftUI
import Timeline
2022-12-01 08:05:26 +00:00
2023-09-19 07:18:20 +00:00
@MainActor
2022-12-29 16:22:07 +00:00
struct SettingsTabs: View {
@Environment(\.dismiss) private var dismiss
2023-09-22 10:49:25 +00:00
@Environment(\.modelContext) private var context
2023-01-27 19:36:40 +00:00
@Environment(PushNotificationsService.self) private var pushNotifications
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
@Environment(Client.self) private var client
@Environment(CurrentInstance.self) private var currentInstance
@Environment(AppAccountsManager.self) private var appAccountsManager
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2023-01-17 10:36:01 +00:00
@State private var routerPath = RouterPath()
2022-12-29 13:07:58 +00:00
@State private var addAccountSheetPresented = false
@State private var isEditingAccount = false
@State private var cachedRemoved = false
2023-09-22 06:32:13 +00:00
@State private var timelineCache = TimelineCache()
2023-01-17 10:36:01 +00:00
2023-01-10 05:58:50 +00:00
@Binding var popToRootTab: Tab
2023-09-22 10:49:25 +00:00
@Query(sort: \LocalTimeline.creationDate, order: .reverse) var localTimelines: [LocalTimeline]
2023-09-22 17:33:53 +00:00
@Query(sort: \TagGroup.creationDate, order: .reverse) var tagGroups: [TagGroup]
2023-01-17 10:36:01 +00:00
2022-12-01 08:05:26 +00:00
var body: some View {
NavigationStack(path: $routerPath.path) {
2022-12-01 08:05:26 +00:00
Form {
2022-12-04 08:50:25 +00:00
appSection
2022-12-29 06:00:00 +00:00
accountsSection
2023-01-06 16:14:34 +00:00
generalSection
otherSections
cacheSection
2022-12-01 08:05:26 +00:00
}
2022-12-29 09:39:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.navigationTitle(Text("settings.title"))
2022-12-01 08:05:26 +00:00
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .navigationBar)
.toolbar {
if UIDevice.current.userInterfaceIdiom == .phone {
ToolbarItem {
2023-02-22 21:12:10 +00:00
Button {
dismiss()
2023-02-22 21:12:10 +00:00
} label: {
Text("action.done").bold()
}
}
}
2023-09-16 12:15:03 +00:00
if UIDevice.current.userInterfaceIdiom == .pad, !preferences.showiPadSecondaryColumn {
SecondaryColumnToolbarItem()
}
}
.withAppRouter()
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
2022-12-01 08:05:26 +00:00
}
2023-01-01 08:19:00 +00:00
.onAppear {
routerPath.client = client
2023-01-01 08:19:00 +00:00
}
2022-12-01 08:05:26 +00:00
.task {
if appAccountsManager.currentAccount.oauthToken != nil {
await currentInstance.fetchCurrentInstance()
2022-12-01 08:05:26 +00:00
}
}
.withSafariRouter()
.environment(routerPath)
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .notifications {
routerPath.path = []
2023-01-10 05:58:50 +00:00
}
}
2022-12-01 08:05:26 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-29 06:00:00 +00:00
private var accountsSection: some View {
Section("settings.section.accounts") {
2022-12-30 07:36:22 +00:00
ForEach(appAccountsManager.availableAccounts) { account in
HStack {
if isEditingAccount {
Button {
Task {
await logoutAccount(account: account)
}
} label: {
Image(systemName: "trash")
.renderingMode(.template)
.tint(.red)
}
}
AppAccountView(viewModel: .init(appAccount: account))
}
2022-12-30 07:36:22 +00:00
}
.onDelete { indexSet in
if let index = indexSet.first {
let account = appAccountsManager.availableAccounts[index]
Task {
await logoutAccount(account: account)
2023-01-08 13:16:43 +00:00
}
2022-12-30 07:36:22 +00:00
}
2022-12-04 08:50:25 +00:00
}
if !appAccountsManager.availableAccounts.isEmpty {
editAccountButton
}
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
}
2023-02-18 06:26:48 +00:00
private func logoutAccount(account: AppAccount) async {
if let token = account.oauthToken,
let sub = pushNotifications.subscriptions.first(where: { $0.account.token == token })
{
let client = Client(server: account.server, oauthToken: token)
2023-09-22 06:32:13 +00:00
await timelineCache.clearCache(for: client.id)
await sub.deleteSubscription()
appAccountsManager.delete(account: account)
}
}
2023-01-17 10:36:01 +00:00
2023-01-06 16:14:34 +00:00
@ViewBuilder
private var generalSection: some View {
Section("settings.section.general") {
2023-01-06 16:14:34 +00:00
if let instanceData = currentInstance.instance {
NavigationLink(destination: InstanceInfoView(instance: instanceData)) {
Label("settings.general.instance", systemImage: "server.rack")
}
}
2023-01-06 16:14:34 +00:00
NavigationLink(destination: DisplaySettingsView()) {
Label("settings.general.display", systemImage: "paintpalette")
}
2023-02-12 15:29:41 +00:00
if HapticManager.shared.supportsHaptics {
NavigationLink(destination: HapticSettingsView()) {
Label("settings.general.haptic", systemImage: "waveform.path")
}
2023-02-12 15:29:41 +00:00
}
2023-01-06 16:14:34 +00:00
NavigationLink(destination: remoteLocalTimelinesView) {
Label("settings.general.remote-timelines", systemImage: "dot.radiowaves.right")
2022-12-24 13:55:04 +00:00
}
2023-07-19 05:46:25 +00:00
NavigationLink(destination: tagGroupsView) {
Label("timeline.filter.tag-groups", systemImage: "number")
}
NavigationLink(destination: ContentSettingsView()) {
Label("settings.general.content", systemImage: "rectangle.stack")
}
NavigationLink(destination: SwipeActionsSettingsView()) {
Label("settings.general.swipeactions", systemImage: "hand.draw")
2023-03-14 17:50:19 +00:00
}
NavigationLink(destination: TranslationSettingsView()) {
Label("settings.general.translate", systemImage: "captions.bubble")
}
Link(destination: URL(string: UIApplication.openSettingsURLString)!) {
Label("settings.system", systemImage: "gear")
}
.tint(theme.labelColor)
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-09-19 07:18:20 +00:00
@ViewBuilder
private var otherSections: some View {
2023-09-19 07:18:20 +00:00
@Bindable var preferences = preferences
Section("settings.section.other") {
2023-10-23 17:12:25 +00:00
if !ProcessInfo.processInfo.isMacCatalystApp{
2023-01-19 10:59:12 +00:00
Picker(selection: $preferences.preferredBrowser) {
ForEach(PreferredBrowser.allCases, id: \.rawValue) { browser in
switch browser {
case .inAppSafari:
Text("settings.general.browser.in-app").tag(browser)
2023-01-19 10:59:12 +00:00
case .safari:
Text("settings.general.browser.system").tag(browser)
2023-01-19 10:59:12 +00:00
}
}
2023-01-19 10:59:12 +00:00
} label: {
Label("settings.general.browser", systemImage: "network")
}
Toggle(isOn: $preferences.inAppBrowserReaderView) {
Label("settings.general.browser.in-app.readerview", systemImage: "doc.plaintext")
}
.disabled(preferences.preferredBrowser != PreferredBrowser.inAppSafari)
}
Toggle(isOn: $preferences.isOpenAIEnabled) {
Label("settings.other.hide-openai", systemImage: "faxmachine")
}
Toggle(isOn: $preferences.isSocialKeyboardEnabled) {
Label("settings.other.social-keyboard", systemImage: "keyboard")
}
2023-02-28 17:55:08 +00:00
Toggle(isOn: $preferences.soundEffectEnabled) {
Label("settings.other.sound-effect", systemImage: "hifispeaker")
}
2022-12-24 13:55:04 +00:00
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-24 13:55:04 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-04 08:50:25 +00:00
private var appSection: some View {
Section {
2023-10-23 17:12:25 +00:00
if !ProcessInfo.processInfo.isMacCatalystApp {
2023-01-17 20:08:05 +00:00
NavigationLink(destination: IconSelectorView()) {
Label {
Text("settings.app.icon")
2023-01-17 20:08:05 +00:00
} icon: {
let icon = IconSelectorView.Icon(string: UIApplication.shared.alternateIconName ?? "AppIcon")
Image(uiImage: .init(named: icon.iconName)!)
.resizable()
.frame(width: 25, height: 25)
.cornerRadius(4)
2022-12-27 20:35:41 +00:00
}
2022-12-04 08:50:25 +00:00
}
}
2023-01-17 10:36:01 +00:00
Link(destination: URL(string: "https://github.com/Dimillian/IceCubesApp")!) {
Label("settings.app.source", systemImage: "link")
}
.accessibilityRemoveTraits(.isButton)
.tint(theme.labelColor)
2023-01-17 10:36:01 +00:00
2023-01-07 12:44:13 +00:00
NavigationLink(destination: SupportAppView()) {
Label("settings.app.support", systemImage: "wand.and.stars")
2022-12-23 15:21:31 +00:00
}
2023-01-22 05:38:30 +00:00
if let reviewURL = URL(string: "https://apps.apple.com/app/id\(AppInfo.appStoreAppId)?action=write-review") {
Link(destination: reviewURL) {
Label("settings.rate", systemImage: "link")
}
.accessibilityRemoveTraits(.isButton)
.tint(theme.labelColor)
}
NavigationLink(destination: AboutView()) {
Label("settings.app.about", systemImage: "info.circle")
}
} header: {
2023-01-22 05:38:30 +00:00
Text("settings.section.app")
} footer: {
2023-01-22 05:38:30 +00:00
if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
Text("settings.section.app.footer \(appVersion)").frame(maxWidth: .infinity, alignment: .center)
2023-01-22 05:38:30 +00:00
}
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
}
2023-01-17 10:36:01 +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: {
Text("settings.account.add")
2022-12-29 13:07:58 +00:00
}
.sheet(isPresented: $addAccountSheetPresented) {
AddAccountView()
2022-12-01 08:05:26 +00:00
}
}
2023-02-18 06:26:48 +00:00
private var editAccountButton: some View {
Button(role: isEditingAccount ? .none : .destructive) {
withAnimation {
isEditingAccount.toggle()
}
} label: {
if isEditingAccount {
Text("action.done")
} else {
Text("account.action.logout")
}
}
}
2023-07-19 05:46:25 +00:00
private var tagGroupsView: some View {
Form {
2023-09-22 17:33:53 +00:00
ForEach(tagGroups) { group in
Label(group.title, systemImage: group.symbolName)
2023-08-04 10:40:21 +00:00
.onTapGesture {
routerPath.presentedSheet = .editTagGroup(tagGroup: group, onSaved: nil)
}
2023-07-19 05:46:25 +00:00
}
.onDelete { indexes in
if let index = indexes.first {
2023-09-22 17:33:53 +00:00
context.delete(tagGroups[index])
}
}
2023-07-19 05:46:25 +00:00
.listRowBackground(theme.primaryBackgroundColor)
Button {
routerPath.presentedSheet = .addTagGroup
} label: {
Label("timeline.filter.add-tag-groups", systemImage: "plus")
}
2023-07-19 05:46:25 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
.navigationTitle("timeline.filter.tag-groups")
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.toolbar {
EditButton()
}
2023-07-19 05:46:25 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-06 16:14:34 +00:00
private var remoteLocalTimelinesView: some View {
Form {
2023-09-22 10:49:25 +00:00
ForEach(localTimelines) { timeline in
Text(timeline.instance)
2023-01-06 16:14:34 +00:00
}.onDelete { indexes in
if let index = indexes.first {
2023-09-22 10:49:25 +00:00
context.delete(localTimelines[index])
2023-01-06 16:14:34 +00:00
}
}
.listRowBackground(theme.primaryBackgroundColor)
Button {
routerPath.presentedSheet = .addRemoteLocalTimeline
} label: {
Label("settings.timeline.add", systemImage: "badge.plus.radiowaves.right")
}
2023-01-06 16:14:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
.navigationTitle("settings.general.remote-timelines")
2023-01-06 16:14:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.toolbar {
2023-07-19 05:46:25 +00:00
EditButton()
}
2022-12-01 08:05:26 +00:00
}
2023-02-26 05:45:57 +00:00
private var cacheSection: some View {
Section("settings.section.cache") {
if cachedRemoved {
Text("action.done")
.transition(.move(edge: .leading))
} else {
Button("settings.cache-media.clear", role: .destructive) {
ImagePipeline.shared.cache.removeAll()
withAnimation {
cachedRemoved = true
}
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
2022-12-01 08:05:26 +00:00
}