329 lines
13 KiB
Swift
329 lines
13 KiB
Swift
import SwiftUI
|
|
|
|
private struct AccountProfile: Decodable {
|
|
let username: String?
|
|
let nickname: String
|
|
let signature: String
|
|
let avatar: ProfileAvatar
|
|
|
|
var displayName: String { nickname.isEmpty ? (username ?? "我的账号") : nickname }
|
|
|
|
struct Update: Encodable {
|
|
var nickname: String
|
|
var signature: String
|
|
var avatar: ProfileAvatar
|
|
}
|
|
}
|
|
|
|
enum ProfileAvatar: String, CaseIterable, Codable, Identifiable {
|
|
case person = "PERSON"
|
|
case cat = "CAT"
|
|
case leaf = "LEAF"
|
|
case moon = "MOON"
|
|
case mountain = "MOUNTAIN"
|
|
case spark = "SPARK"
|
|
|
|
var id: Self { self }
|
|
|
|
var symbol: String {
|
|
switch self {
|
|
case .person: "person.fill"
|
|
case .cat: "cat.fill"
|
|
case .leaf: "leaf.fill"
|
|
case .moon: "moon.stars.fill"
|
|
case .mountain: "mountain.2.fill"
|
|
case .spark: "sparkle"
|
|
}
|
|
}
|
|
|
|
var title: String {
|
|
switch self {
|
|
case .person: "默认"
|
|
case .cat: "小猫"
|
|
case .leaf: "叶子"
|
|
case .moon: "夜空"
|
|
case .mountain: "山丘"
|
|
case .spark: "星光"
|
|
}
|
|
}
|
|
|
|
var color: Color {
|
|
switch self {
|
|
case .person: .atlasBlue
|
|
case .cat: .orange
|
|
case .leaf: .green
|
|
case .moon: .purple
|
|
case .mountain: .teal
|
|
case .spark: .pink
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AvatarBadge: View {
|
|
let avatar: ProfileAvatar
|
|
let size: CGFloat
|
|
|
|
var body: some View {
|
|
Image(systemName: avatar.symbol)
|
|
.font(.system(size: size * 0.40, weight: .medium))
|
|
.symbolRenderingMode(.hierarchical)
|
|
.foregroundStyle(avatar.color)
|
|
.frame(width: size, height: size)
|
|
.background(avatar.color.opacity(0.12), in: Circle())
|
|
.accessibilityHidden(true)
|
|
}
|
|
}
|
|
|
|
struct MyView: View {
|
|
let account: AtlasAccount
|
|
var store: InventoryStore? = nil
|
|
let openSpaces: () -> Void
|
|
|
|
@State private var profile: AccountProfile?
|
|
@State private var profileError: String?
|
|
@State private var inventory: AccountInventorySummary?
|
|
@State private var inventoryError: String?
|
|
@State private var showingProfile = false
|
|
@State private var confirmingSignOut = false
|
|
|
|
init(account: AtlasAccount, store: InventoryStore? = nil, openSpaces: @escaping () -> Void) {
|
|
self.account = account
|
|
self.store = store
|
|
self.openSpaces = openSpaces
|
|
_profile = State(initialValue: account.cached("/api/me/profile"))
|
|
_inventory = State(initialValue: account.cached(AccountInventorySummary.path))
|
|
}
|
|
|
|
var body: some View {
|
|
List {
|
|
Section {
|
|
if let profile {
|
|
Button { showingProfile = true } label: {
|
|
HStack(spacing: 13) {
|
|
AvatarBadge(avatar: profile.avatar, size: 52)
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(profile.displayName).font(.headline)
|
|
if !profile.nickname.isEmpty, let username = profile.username {
|
|
Text("账号 " + username).font(.caption).foregroundStyle(.secondary)
|
|
}
|
|
Text(profile.signature.isEmpty ? "添加签名" : profile.signature)
|
|
.font(.subheadline).foregroundStyle(.secondary).lineLimit(2)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
Image(systemName: "pencil")
|
|
.foregroundStyle(Color.atlasBlue)
|
|
.accessibilityHidden(true)
|
|
}
|
|
.padding(.vertical, 5)
|
|
.foregroundStyle(.primary)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.accessibilityHint("编辑头像、昵称和签名")
|
|
} else if profileError == nil {
|
|
ProgressView("正在加载资料")
|
|
}
|
|
if let profileError {
|
|
Text(profileError).font(.footnote).foregroundStyle(.secondary)
|
|
Button("重新加载资料") { Task { await loadProfile(refresh: true) } }
|
|
}
|
|
}
|
|
|
|
overview
|
|
|
|
Section("空间") {
|
|
if let store {
|
|
NavigationLink {
|
|
SpaceSettings(store: store, openSpaces: openSpaces)
|
|
} label: {
|
|
Label {
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
Text(store.space.name)
|
|
Text("空间设置").font(.caption).foregroundStyle(.secondary)
|
|
}
|
|
} icon: {
|
|
Image(systemName: "house")
|
|
}
|
|
}
|
|
Button(action: openSpaces) { Label("切换空间", systemImage: "arrow.left.arrow.right") }
|
|
} else {
|
|
Button(action: openSpaces) { Label("查看我的空间", systemImage: "house") }
|
|
}
|
|
}
|
|
|
|
Section {
|
|
NavigationLink {
|
|
CommunityFeedView(account: account, mine: true)
|
|
} label: {
|
|
Label("我公开的物品", systemImage: "bubble.left")
|
|
}
|
|
if let store {
|
|
NavigationLink {
|
|
ItemListView(store: store, favoritesOnly: true)
|
|
} label: {
|
|
Label("我的收藏", systemImage: "star")
|
|
}
|
|
}
|
|
}
|
|
|
|
Section {
|
|
Button(role: .destructive) { confirmingSignOut = true } label: {
|
|
Label("退出登录", systemImage: "rectangle.portrait.and.arrow.right")
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("我的")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.task { await loadProfile() }
|
|
.task(id: store?.revision ?? 0) { await loadInventory() }
|
|
.refreshable {
|
|
async let profile: Void = loadProfile(refresh: true)
|
|
await loadInventory(refresh: true)
|
|
await profile
|
|
}
|
|
.sheet(isPresented: $showingProfile) {
|
|
if let profile {
|
|
ProfileEditor(account: account, profile: profile) { self.profile = $0 }
|
|
}
|
|
}
|
|
.confirmationDialog("退出当前账号?", isPresented: $confirmingSignOut, titleVisibility: .visible) {
|
|
Button("退出登录", role: .destructive) { Task { await account.signOut() } }
|
|
}
|
|
}
|
|
|
|
private var overview: some View {
|
|
Section {
|
|
if let inventory {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text("购入总额").font(.subheadline).foregroundStyle(.secondary)
|
|
if inventory.pricedItems > 0 {
|
|
Text(PurchasePrice.totalDisplay(cents: inventory.totalValueCents))
|
|
.font(.title2.weight(.semibold))
|
|
.monospacedDigit()
|
|
.minimumScaleFactor(0.6)
|
|
} else {
|
|
Text("暂无已填写的价格").font(.headline)
|
|
}
|
|
}
|
|
.accessibilityElement(children: .combine)
|
|
LabeledContent("在册物品", value: "\(inventory.activeUnits) 件 · \(inventory.activeItems) 条记录")
|
|
LabeledContent("已公开", value: "\(inventory.publishedItems) 件")
|
|
LabeledContent("已删除", value: "\(inventory.archivedItems) 条")
|
|
} else if inventoryError == nil {
|
|
ProgressView("正在加载物品概览")
|
|
}
|
|
if let inventoryError {
|
|
Text(inventoryError).font(.footnote).foregroundStyle(.secondary)
|
|
Button("重新加载概览") { Task { await loadInventory(refresh: true) } }
|
|
}
|
|
} header: {
|
|
Text("物品概览")
|
|
} footer: {
|
|
Text("统计你拥有的空间。购入总额按已填写单价的在册物品计算。")
|
|
}
|
|
}
|
|
|
|
private func loadInventory(refresh: Bool = false) async {
|
|
do {
|
|
inventory = try await account.read(AccountInventorySummary.path, refresh: refresh)
|
|
inventoryError = nil
|
|
} catch is CancellationError {
|
|
} catch where !Task.isCancelled {
|
|
inventoryError = "暂时无法加载物品概览。"
|
|
} catch {}
|
|
}
|
|
|
|
private func loadProfile(refresh: Bool = false) async {
|
|
do {
|
|
profile = try await account.read("/api/me/profile", refresh: refresh)
|
|
profileError = nil
|
|
} catch is CancellationError {
|
|
} catch where !Task.isCancelled {
|
|
if (error as? AtlasError)?.resourceUnavailable == true { profile = nil }
|
|
profileError = "暂时无法加载账号资料。"
|
|
} catch {}
|
|
}
|
|
}
|
|
|
|
private struct ProfileEditor: View {
|
|
let account: AtlasAccount
|
|
let saved: (AccountProfile) -> Void
|
|
@Environment(\.dismiss) private var dismiss
|
|
@FocusState private var typing: Bool
|
|
@State private var update: AccountProfile.Update
|
|
@State private var busy = false
|
|
@State private var error: String?
|
|
|
|
init(account: AtlasAccount, profile: AccountProfile, saved: @escaping (AccountProfile) -> Void) {
|
|
self.account = account
|
|
self.saved = saved
|
|
_update = State(initialValue: AccountProfile.Update(nickname: profile.nickname, signature: profile.signature,
|
|
avatar: profile.avatar))
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section("头像") {
|
|
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 14) {
|
|
ForEach(ProfileAvatar.allCases) { avatar in
|
|
Button {
|
|
update.avatar = avatar
|
|
} label: {
|
|
VStack(spacing: 5) {
|
|
AvatarBadge(avatar: avatar, size: 48)
|
|
.padding(2)
|
|
.overlay {
|
|
Circle().strokeBorder(update.avatar == avatar ? Color.atlasBlue : .clear,
|
|
lineWidth: 2)
|
|
}
|
|
Text(avatar.title).font(.caption).foregroundStyle(.primary)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.accessibilityAddTraits(update.avatar == avatar ? .isSelected : [])
|
|
}
|
|
}
|
|
.padding(.vertical, 7)
|
|
}
|
|
Section {
|
|
TextField("昵称", text: $update.nickname).focused($typing)
|
|
TextField("签名", text: $update.signature).focused($typing)
|
|
} header: {
|
|
Text("资料")
|
|
} footer: {
|
|
if update.nickname.utf16.count > 32 || update.signature.utf16.count > 100 {
|
|
Text("昵称最多 32 字符,签名最多 100 字符。")
|
|
}
|
|
}
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.dismissibleKeyboard(active: typing) { typing = false }
|
|
.navigationTitle("编辑资料")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) }
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("保存") { typing = false; Task { await save() } }
|
|
.disabled(busy || update.nickname.utf16.count > 32 || update.signature.utf16.count > 100)
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
}
|
|
|
|
private func save() async {
|
|
guard !busy else { return }
|
|
busy = true
|
|
defer { busy = false }
|
|
error = nil
|
|
do {
|
|
let data = try await account.request("/api/me/profile", method: "PUT", data: JSONEncoder().encode(update), timeout: 12)
|
|
account.invalidateReads("/api/me/profile", discard: true)
|
|
account.invalidateReads("/api/community/")
|
|
saved(try JSONDecoder().decode(AccountProfile.self, from: data))
|
|
dismiss()
|
|
} catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|