✨ Able to clear status on watchOS
🐛 Fix some bugs in status on watchOS
This commit is contained in:
@@ -148,9 +148,9 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createOrUpdateStatus(attitude: Int, isInvisible: Bool, isNotDisturb: Bool, label: String?, token: String, serverUrl: String) async throws -> SnAccountStatus {
|
func createOrUpdateStatus(attitude: Int, isInvisible: Bool, isNotDisturb: Bool, label: String?, token: String, serverUrl: String) async throws -> SnAccountStatus {
|
||||||
// First check if status exists
|
// Check if there's already a customized status
|
||||||
let existingStatus = try? await fetchAccountStatus(token: token, serverUrl: serverUrl)
|
let existingStatus = try? await fetchAccountStatus(token: token, serverUrl: serverUrl)
|
||||||
let method = existingStatus == nil ? "POST" : "PATCH"
|
let method = (existingStatus?.isCustomized == true) ? "PATCH" : "POST"
|
||||||
|
|
||||||
guard let baseURL = URL(string: serverUrl) else {
|
guard let baseURL = URL(string: serverUrl) else {
|
||||||
throw URLError(.badURL)
|
throw URLError(.badURL)
|
||||||
|
|||||||
@@ -13,10 +13,11 @@ struct AccountView: View {
|
|||||||
@State private var status: SnAccountStatus?
|
@State private var status: SnAccountStatus?
|
||||||
@State private var isLoading = false
|
@State private var isLoading = false
|
||||||
@State private var error: Error?
|
@State private var error: Error?
|
||||||
|
@State private var showingClearConfirmation = false
|
||||||
|
|
||||||
@StateObject private var profileImageLoader = ImageLoader()
|
@StateObject private var profileImageLoader = ImageLoader()
|
||||||
@StateObject private var bannerImageLoader = ImageLoader()
|
@StateObject private var bannerImageLoader = ImageLoader()
|
||||||
|
|
||||||
private let networkService = NetworkService()
|
private let networkService = NetworkService()
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@@ -110,8 +111,23 @@ struct AccountView: View {
|
|||||||
.font(.subheadline)
|
.font(.subheadline)
|
||||||
.foregroundColor(.secondary)
|
.foregroundColor(.secondary)
|
||||||
Spacer()
|
Spacer()
|
||||||
|
if status?.isCustomized == true {
|
||||||
|
Button(action: {
|
||||||
|
showingClearConfirmation = true
|
||||||
|
}) {
|
||||||
|
ZStack {
|
||||||
|
Circle()
|
||||||
|
.fill(Color.red.opacity(0.1))
|
||||||
|
.frame(width: 28, height: 28)
|
||||||
|
Image(systemName: "trash")
|
||||||
|
.foregroundColor(.red)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
.frame(width: 28, height: 28)
|
||||||
|
}
|
||||||
NavigationLink(
|
NavigationLink(
|
||||||
destination: StatusCreationView(initialStatus: status)
|
destination: StatusCreationView(initialStatus: status?.isCustomized == true ? status : nil)
|
||||||
.environmentObject(appState)
|
.environmentObject(appState)
|
||||||
) {
|
) {
|
||||||
ZStack {
|
ZStack {
|
||||||
@@ -210,6 +226,16 @@ struct AccountView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("Account")
|
.navigationTitle("Account")
|
||||||
|
.confirmationDialog("Clear Status", isPresented: $showingClearConfirmation) {
|
||||||
|
Button("Clear Status", role: .destructive) {
|
||||||
|
Task {
|
||||||
|
await clearStatus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Button("Cancel", role: .cancel) {}
|
||||||
|
} message: {
|
||||||
|
Text("Are you sure you want to clear your status? This action cannot be undone.")
|
||||||
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
Task.detached {
|
Task.detached {
|
||||||
await loadUserProfile()
|
await loadUserProfile()
|
||||||
@@ -222,19 +248,34 @@ struct AccountView: View {
|
|||||||
error = NSError(domain: "AccountView", code: 1, userInfo: [NSLocalizedDescriptionKey: "Authentication not available"])
|
error = NSError(domain: "AccountView", code: 1, userInfo: [NSLocalizedDescriptionKey: "Authentication not available"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isLoading = true
|
isLoading = true
|
||||||
error = nil
|
error = nil
|
||||||
|
|
||||||
do {
|
do {
|
||||||
user = try await networkService.fetchUserProfile(token: token, serverUrl: serverUrl)
|
user = try await networkService.fetchUserProfile(token: token, serverUrl: serverUrl)
|
||||||
status = try await networkService.fetchAccountStatus(token: token, serverUrl: serverUrl)
|
status = try await networkService.fetchAccountStatus(token: token, serverUrl: serverUrl)
|
||||||
} catch {
|
} catch {
|
||||||
self.error = error
|
self.error = error
|
||||||
}
|
}
|
||||||
|
|
||||||
isLoading = false
|
isLoading = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func clearStatus() async {
|
||||||
|
guard let token = appState.token, let serverUrl = appState.serverUrl else {
|
||||||
|
error = NSError(domain: "AccountView", code: 1, userInfo: [NSLocalizedDescriptionKey: "Authentication not available"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
try await networkService.clearStatus(token: token, serverUrl: serverUrl)
|
||||||
|
// Refresh status after clearing
|
||||||
|
status = try await networkService.fetchAccountStatus(token: token, serverUrl: serverUrl)
|
||||||
|
} catch {
|
||||||
|
self.error = error
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|||||||
Reference in New Issue
Block a user