213 lines
9.0 KiB
Swift
213 lines
9.0 KiB
Swift
import SwiftUI
|
|
|
|
@main
|
|
struct ItemAtlasApp: App {
|
|
@State private var account = AtlasAccount()
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
Group {
|
|
if account.signedIn {
|
|
SpacesView(account: account)
|
|
} else {
|
|
VStack(spacing: 18) {
|
|
Spacer()
|
|
Image("BrandMark")
|
|
.resizable().frame(width: 76, height: 76)
|
|
.clipShape(.rect(cornerRadius: 17))
|
|
.accessibilityHidden(true)
|
|
Text("见物").font(.largeTitle.bold())
|
|
Text("记下家里的东西和它们的位置。").foregroundStyle(.secondary)
|
|
Spacer()
|
|
Button {
|
|
Task { await account.signIn() }
|
|
} label: {
|
|
if account.working { ProgressView().frame(maxWidth: .infinity) }
|
|
else { Text("登录").frame(maxWidth: .infinity) }
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.controlSize(.large)
|
|
.disabled(account.working)
|
|
if let error = account.error {
|
|
Text(error).font(.footnote).foregroundStyle(.red)
|
|
}
|
|
}
|
|
.padding(28)
|
|
}
|
|
}
|
|
.tint(.atlasBlue)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct SpacesView: View {
|
|
let account: AtlasAccount
|
|
@State private var spaces: [Space] = []
|
|
@State private var selected: Space?
|
|
@State private var showingCreate = false
|
|
@State private var showingJoin = false
|
|
@State private var loaded = false
|
|
@State private var error: String?
|
|
|
|
var body: some View {
|
|
Group {
|
|
if let selected {
|
|
SpaceTabs(account: account, space: selected, openSpaces: { self.selected = nil })
|
|
.id(selected)
|
|
} else {
|
|
NavigationStack {
|
|
List {
|
|
if !loaded && error == nil { ProgressView("正在加载空间") }
|
|
if loaded && spaces.isEmpty {
|
|
ContentUnavailableView("还没有空间", systemImage: "house", description: Text("新建自己的空间,或用邀请码加入家人的空间。"))
|
|
}
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
ForEach(spaces) { space in
|
|
Button { selected = space } label: { Label(space.name, systemImage: "house") }
|
|
}
|
|
Button("新建空间") { showingCreate = true }.disabled(!loaded)
|
|
Button("使用邀请码") { showingJoin = true }.disabled(!loaded)
|
|
}
|
|
.task { await load(chooseSingle: !loaded) }
|
|
.refreshable { await load() }
|
|
.navigationTitle("我的空间")
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Button("退出登录") { Task { await account.signOut() } }
|
|
}
|
|
}
|
|
.sheet(isPresented: $showingCreate) {
|
|
SpaceEditor(account: account, joining: false) { selected = $0 }
|
|
}
|
|
.sheet(isPresented: $showingJoin) {
|
|
SpaceEditor(account: account, joining: true) { selected = $0 }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func load(chooseSingle: Bool = false) async {
|
|
do {
|
|
let bytes = try await account.request("/api/households")
|
|
try Task.checkCancellation()
|
|
spaces = try JSONDecoder().decode([Space].self, from: bytes)
|
|
loaded = true
|
|
error = nil
|
|
if chooseSingle && spaces.count == 1 { selected = spaces[0] }
|
|
} catch where !Task.isCancelled { self.error = error.localizedDescription }
|
|
catch {}
|
|
}
|
|
}
|
|
|
|
private struct SpaceTabs: View {
|
|
@State private var store: InventoryStore
|
|
let openSpaces: () -> Void
|
|
|
|
init(account: AtlasAccount, space: Space, openSpaces: @escaping () -> Void) {
|
|
_store = State(initialValue: InventoryStore(account: account, space: space))
|
|
self.openSpaces = openSpaces
|
|
}
|
|
|
|
var body: some View {
|
|
TabView {
|
|
InventoryView(store: store).tabItem { Label("找物", systemImage: "magnifyingglass") }
|
|
LocationView(store: store).tabItem { Label("位置", systemImage: "square.stack") }
|
|
IntakeView(store: store).tabItem { Label("录入", systemImage: "square.and.pencil") }
|
|
InventoryView(store: store, favoritesOnly: true).tabItem { Label("收藏", systemImage: "star") }
|
|
SpaceSettings(store: store, openSpaces: openSpaces).tabItem { Label("空间", systemImage: "house") }
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct SpaceEditor: View {
|
|
let account: AtlasAccount
|
|
let joining: Bool
|
|
let saved: (Space) -> Void
|
|
@Environment(\.dismiss) private var dismiss
|
|
@FocusState private var focused: Field?
|
|
private enum Field { case name, displayName }
|
|
@State private var name = ""
|
|
@State private var displayName = ""
|
|
@State private var busy = false
|
|
@State private var error: String?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
TextField(joining ? "邀请码" : "空间名称", text: $name).focused($focused, equals: .name)
|
|
.textInputAutocapitalization(.never)
|
|
TextField("我在空间里的称呼", text: $displayName).focused($focused, equals: .displayName)
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.dismissibleKeyboard(active: focused != nil) { focused = nil }
|
|
.navigationTitle(joining ? "加入空间" : "新建空间")
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) { Button("取消") { dismiss() }.disabled(busy) }
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button(joining ? "加入" : "新建") { Task { await save() } }
|
|
.disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || (joining && displayName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) || busy)
|
|
}
|
|
}
|
|
}
|
|
.interactiveDismissDisabled(busy)
|
|
}
|
|
|
|
private func save() async {
|
|
busy = true
|
|
defer { busy = false }
|
|
let entry = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let fields = [joining ? "code" : "name": entry, "displayName": displayName.trimmingCharacters(in: .whitespacesAndNewlines)]
|
|
do {
|
|
let bytes = try await account.request(joining ? "/api/households/invitations/redeem" : "/api/households",
|
|
method: "POST", data: JSONEncoder().encode(fields))
|
|
let space = try JSONDecoder().decode(Space.self, from: bytes)
|
|
dismiss()
|
|
saved(space)
|
|
} catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|
|
|
|
private struct SpaceSettings: View {
|
|
let store: InventoryStore
|
|
let openSpaces: () -> Void
|
|
@State private var confirming = false
|
|
@State private var busy = false
|
|
@State private var error: String?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
LabeledContent("当前空间", value: store.space.name)
|
|
ShareLink(item: store.space.id.uuidString) { Label("分享空间 ID", systemImage: "square.and.arrow.up") }
|
|
Button("切换空间", action: openSpaces).disabled(busy)
|
|
if store.space.role == "OWNER" {
|
|
Section {
|
|
Button("删除空间", role: .destructive) { confirming = true }.disabled(busy)
|
|
}
|
|
}
|
|
Button("退出登录", role: .destructive) { Task { await store.account.signOut() } }.disabled(busy)
|
|
if let error { Text(error).foregroundStyle(.red) }
|
|
}
|
|
.navigationTitle("空间")
|
|
.confirmationDialog("删除「\(store.space.name)」?", isPresented: $confirming, titleVisibility: .visible) {
|
|
Button("删除空间和图片", role: .destructive) {
|
|
Task {
|
|
busy = true
|
|
defer { busy = false }
|
|
do { try await store.delete(store.root); openSpaces() }
|
|
catch { self.error = error.localizedDescription }
|
|
}
|
|
}
|
|
} message: {
|
|
Text("此空间里的物品、位置和图片都会删除,空间成员会失去访问权限。")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Color {
|
|
static let atlasNavy = Color(red: 0.09, green: 0.15, blue: 0.28)
|
|
static let atlasBlue = Color(red: 0.19, green: 0.32, blue: 0.81)
|
|
}
|