IceCubes/IceCubesApp/App/Tabs/Settings/AccountSettingView.swift
Thomas Ricouard 4189a59cf6
iOS 17+ only support + migrating to Observation framework (#1571)
* Initial iOS 17 + Observable migration

* More Observation

* More observation

* Checkpoint

* Checkpoint

* Bump version to 1.8.0

* SwiftFormat

* Fix home timeline switch on login

* Fix sidebar routerPath

* Fixes on detail view

* Remove print changes

* Simply detail view

* More opt

* Migrate DisplaySettingsLocalValues

* Better post detail transition

* Status detail animation finally right

* Cleanup
2023-09-18 07:01:23 +02:00

121 lines
4.0 KiB
Swift

import Account
import AppAccount
import DesignSystem
import Env
import Models
import Network
import SwiftUI
import Timeline
struct AccountSettingsView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.openURL) private var openURL
@Environment(PushNotificationsService.self) private var pushNotifications
@Environment(CurrentAccount.self) private var currentAccount
@Environment(CurrentInstance.self) private var currentInstance
@EnvironmentObject private var theme: Theme
@Environment(AppAccountsManager.self) private var appAccountsManager
@Environment(Client.self) private var client
@State private var isEditingAccount: Bool = false
@State private var isEditingFilters: Bool = false
@State private var cachedPostsCount: Int = 0
let account: Account
let appAccount: AppAccount
var body: some View {
Form {
Section {
Button {
isEditingAccount = true
} label: {
Label("account.action.edit-info", systemImage: "pencil")
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
if currentInstance.isFiltersSupported {
Button {
isEditingFilters = true
} label: {
Label("account.action.edit-filters", systemImage: "line.3.horizontal.decrease.circle")
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
if let subscription = pushNotifications.subscriptions.first(where: { $0.account.token == appAccount.oauthToken }) {
NavigationLink(destination: PushNotificationsView(subscription: subscription)) {
Label("settings.general.push-notifications", systemImage: "bell.and.waves.left.and.right")
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
Section {
Label("settings.account.cached-posts-\(String(cachedPostsCount))", systemImage: "internaldrive")
Button("settings.account.action.delete-cache", role: .destructive) {
Task {
await TimelineCache.shared.clearCache(for: appAccountsManager.currentClient.id)
cachedPostsCount = await TimelineCache.shared.cachedPostsCount(for: appAccountsManager.currentClient.id)
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
Section {
Button {
openURL(URL(string: "https://\(client.server)/settings/profile")!)
} label: {
Text("account.action.more")
}
}
.listRowBackground(theme.primaryBackgroundColor)
Section {
Button(role: .destructive) {
if let token = appAccount.oauthToken {
Task {
let client = Client(server: appAccount.server, oauthToken: token)
await TimelineCache.shared.clearCache(for: client.id)
if let sub = pushNotifications.subscriptions.first(where: { $0.account.token == token }) {
await sub.deleteSubscription()
}
appAccountsManager.delete(account: appAccount)
dismiss()
}
}
} label: {
Text("account.action.logout")
.frame(maxWidth: .infinity)
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
.sheet(isPresented: $isEditingAccount, content: {
EditAccountView()
})
.sheet(isPresented: $isEditingFilters, content: {
FiltersListView()
})
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
AvatarView(url: account.avatar, size: .embed)
Text(account.safeDisplayName)
.font(.headline)
}
}
}
.task {
cachedPostsCount = await TimelineCache.shared.cachedPostsCount(for: appAccountsManager.currentClient.id)
}
.navigationTitle(account.safeDisplayName)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
}
}