2023-01-25 21:18:34 +01:00
|
|
|
import Account
|
2023-01-27 20:36:40 +01:00
|
|
|
import AppAccount
|
2023-01-25 21:18:34 +01:00
|
|
|
import DesignSystem
|
|
|
|
import Env
|
|
|
|
import Models
|
2023-02-12 16:29:41 +01:00
|
|
|
import Network
|
2023-01-27 20:36:40 +01:00
|
|
|
import SwiftUI
|
2023-02-04 15:08:54 +01:00
|
|
|
import Timeline
|
2023-01-25 21:18:34 +01:00
|
|
|
|
|
|
|
struct AccountSettingsView: View {
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-03-03 18:21:52 +01:00
|
|
|
@Environment(\.openURL) private var openURL
|
2023-01-27 20:36:40 +01:00
|
|
|
|
2023-09-18 07:01:23 +02:00
|
|
|
@Environment(PushNotificationsService.self) private var pushNotifications
|
|
|
|
@Environment(CurrentAccount.self) private var currentAccount
|
|
|
|
@Environment(CurrentInstance.self) private var currentInstance
|
2023-09-18 21:03:52 +02:00
|
|
|
@Environment(Theme.self) private var theme
|
2023-09-18 07:01:23 +02:00
|
|
|
@Environment(AppAccountsManager.self) private var appAccountsManager
|
|
|
|
@Environment(Client.self) private var client
|
2023-01-27 20:36:40 +01:00
|
|
|
|
2023-01-25 21:18:34 +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-09-22 08:32:13 +02:00
|
|
|
@State private var timelineCache = TimelineCache()
|
2023-01-27 20:36:40 +01:00
|
|
|
|
2023-01-25 21:18:34 +01:00
|
|
|
let account: Account
|
|
|
|
let appAccount: AppAccount
|
2023-01-27 20:36:40 +01:00
|
|
|
|
2023-01-25 21:18:34 +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)
|
|
|
|
|
2023-01-25 21:18:34 +01:00
|
|
|
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)
|
2023-01-25 21:18:34 +01:00
|
|
|
}
|
2023-01-26 13:21:35 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 21:18:34 +01:00
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-02-04 17:17:38 +01:00
|
|
|
|
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 {
|
2023-09-22 08:32:13 +02:00
|
|
|
await timelineCache.clearCache(for: appAccountsManager.currentClient.id)
|
|
|
|
cachedPostsCount = await timelineCache.cachedPostsCount(for: appAccountsManager.currentClient.id)
|
2023-02-04 15:08:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-02-04 17:17:38 +01:00
|
|
|
|
2023-03-03 18:21:52 +01:00
|
|
|
Section {
|
|
|
|
Button {
|
|
|
|
openURL(URL(string: "https://\(client.server)/settings/profile")!)
|
|
|
|
} label: {
|
|
|
|
Text("account.action.more")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-03-13 13:38:28 +01:00
|
|
|
|
2023-01-25 21:18:34 +01:00
|
|
|
Section {
|
|
|
|
Button(role: .destructive) {
|
|
|
|
if let token = appAccount.oauthToken {
|
|
|
|
Task {
|
2023-02-04 21:54:41 +01:00
|
|
|
let client = Client(server: appAccount.server, oauthToken: token)
|
2023-09-22 08:32:13 +02:00
|
|
|
await timelineCache.clearCache(for: client.id)
|
2023-01-26 13:21:35 +01:00
|
|
|
if let sub = pushNotifications.subscriptions.first(where: { $0.account.token == token }) {
|
|
|
|
await sub.deleteSubscription()
|
|
|
|
}
|
2023-01-25 21:18:34 +01:00
|
|
|
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)
|
2023-01-25 21:18:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.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 {
|
2023-09-22 08:32:13 +02:00
|
|
|
cachedPostsCount = await timelineCache.cachedPostsCount(for: appAccountsManager.currentClient.id)
|
2023-02-04 15:08:54 +01:00
|
|
|
}
|
2023-01-25 21:18:34 +01:00
|
|
|
.navigationTitle(account.safeDisplayName)
|
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
.background(theme.secondaryBackgroundColor)
|
|
|
|
}
|
|
|
|
}
|