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

107 lines
3.5 KiB
Swift
Raw Normal View History

import Account
2023-01-27 20:36:40 +01:00
import AppAccount
import DesignSystem
import Env
import Models
2023-01-27 20:36:40 +01:00
import SwiftUI
2023-02-04 15:08:54 +01:00
import Timeline
struct AccountSettingsView: View {
@Environment(\.dismiss) private var dismiss
2023-01-27 20:36:40 +01:00
@EnvironmentObject private var pushNotifications: PushNotificationsService
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var currentInstance: CurrentInstance
@EnvironmentObject private var theme: Theme
@EnvironmentObject private var appAccountsManager: AppAccountsManager
2023-01-27 20:36:40 +01:00
@State private var isEditingAccount: Bool = false
@State private var isEditingFilters: Bool = false
2023-02-04 15:08:54 +01:00
@State private var cachedPostsCount: Int = 0
2023-01-27 20:36:40 +01:00
let account: Account
let appAccount: AppAccount
2023-01-27 20:36:40 +01:00
var body: some View {
Form {
Section {
2023-02-02 06:34:12 +01: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 06:34:12 +01: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 15:08:54 +01:00
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)
cachedPostsCount = await TimelineCache.shared.cachedPostsCount(for: appAccountsManager.currentClient)
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
Section {
Button(role: .destructive) {
if let token = appAccount.oauthToken {
Task {
if let sub = pushNotifications.subscriptions.first(where: { $0.account.token == token }) {
await sub.deleteSubscription()
}
appAccountsManager.delete(account: appAccount)
dismiss()
}
}
} label: {
2023-01-27 07:47:52 +01:00
Text("account.action.logout")
2023-02-02 06:34:12 +01:00
.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)
}
}
}
2023-02-04 15:08:54 +01:00
.task {
cachedPostsCount = await TimelineCache.shared.cachedPostsCount(for: appAccountsManager.currentClient)
}
.navigationTitle(account.safeDisplayName)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
}
}