IceCubesApp/IceCubesApp/App/Tabs/Settings/AccountSettingView.swift

124 lines
4 KiB
Swift
Raw Normal View History

import Account
2023-01-27 19:36:40 +00:00
import AppAccount
import DesignSystem
import Env
import Models
2023-02-12 15:29:41 +00:00
import Network
2023-01-27 19:36:40 +00:00
import SwiftUI
2023-02-04 14:08:54 +00:00
import Timeline
struct AccountSettingsView: View {
@Environment(\.dismiss) private var dismiss
2023-03-03 17:21:52 +00:00
@Environment(\.openURL) private var openURL
2023-01-27 19:36:40 +00:00
@Environment(PushNotificationsService.self) private var pushNotifications
@Environment(CurrentAccount.self) private var currentAccount
@Environment(CurrentInstance.self) private var currentInstance
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
@Environment(AppAccountsManager.self) private var appAccountsManager
@Environment(Client.self) private var client
2023-01-27 19:36:40 +00:00
@State private var isEditingAccount: Bool = false
@State private var isEditingFilters: Bool = false
2023-02-04 14:08:54 +00:00
@State private var cachedPostsCount: Int = 0
2023-09-22 06:32:13 +00:00
@State private var timelineCache = TimelineCache()
2023-01-27 19:36:40 +00:00
let account: Account
let appAccount: AppAccount
2023-01-27 19:36:40 +00:00
var body: some View {
Form {
Section {
2023-02-02 05:34:12 +00:00
Button {
isEditingAccount = true
} label: {
Label("account.action.edit-info", systemImage: "pencil")
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
if currentInstance.isFiltersSupported {
2023-02-02 05:34:12 +00:00
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)
2023-02-04 16:17:38 +00:00
2023-02-04 14:08:54 +00:00
Section {
Label("settings.account.cached-posts-\(String(cachedPostsCount))", systemImage: "internaldrive")
Button("settings.account.action.delete-cache", role: .destructive) {
Task {
2023-09-22 06:32:13 +00:00
await timelineCache.clearCache(for: appAccountsManager.currentClient.id)
cachedPostsCount = await timelineCache.cachedPostsCount(for: appAccountsManager.currentClient.id)
2023-02-04 14:08:54 +00:00
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
2023-02-04 16:17:38 +00:00
2023-03-03 17:21:52 +00:00
Section {
Button {
openURL(URL(string: "https://\(client.server)/settings/profile")!)
} label: {
Text("account.action.more")
}
}
.listRowBackground(theme.primaryBackgroundColor)
2023-03-13 12:38:28 +00:00
Section {
Button(role: .destructive) {
if let token = appAccount.oauthToken {
Task {
2023-02-04 20:54:41 +00:00
let client = Client(server: appAccount.server, oauthToken: token)
2023-09-22 06:32:13 +00:00
await timelineCache.clearCache(for: client.id)
if let sub = pushNotifications.subscriptions.first(where: { $0.account.token == token }) {
await sub.deleteSubscription()
}
appAccountsManager.delete(account: appAccount)
dismiss()
}
}
} label: {
2023-01-27 06:47:52 +00:00
Text("account.action.logout")
2023-02-02 05:34:12 +00:00
.frame(maxWidth: .infinity)
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
.sheet(isPresented: $isEditingAccount, content: {
EditAccountView()
})
.sheet(isPresented: $isEditingFilters, content: {
FiltersListView()
})
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
AvatarView(account.avatar, config: .embed)
Text(account.safeDisplayName)
.font(.headline)
}
}
}
2023-02-04 14:08:54 +00:00
.task {
2023-09-22 06:32:13 +00:00
cachedPostsCount = await timelineCache.cachedPostsCount(for: appAccountsManager.currentClient.id)
2023-02-04 14:08:54 +00:00
}
.navigationTitle(account.safeDisplayName)
#if !os(visionOS)
2024-02-14 11:48:14 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
#endif
}
}