From 9b25243b6a886654c20053428e6ee4e224c40fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 7 Dec 2025 11:33:11 +0800 Subject: [PATCH] Check service status before destroying working directory --- .../Views/Setting/CoreView.swift | 27 ++++++++++++++++- Localizable.xcstrings | 30 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ApplicationLibrary/Views/Setting/CoreView.swift b/ApplicationLibrary/Views/Setting/CoreView.swift index efebf5f..989c28c 100644 --- a/ApplicationLibrary/Views/Setting/CoreView.swift +++ b/ApplicationLibrary/Views/Setting/CoreView.swift @@ -4,7 +4,9 @@ import SwiftUI @MainActor public struct CoreView: View { + @EnvironmentObject private var environments: ExtensionEnvironments @State private var isLoading = true + @State private var alert: AlertState? @State private var disableDeprecatedWarnings = false @@ -42,7 +44,7 @@ public struct CoreView: View { #endif FormButton(role: .destructive) { Task { - await destroyWorkingDirectory() + await confirmDestroyWorkingDirectory() } } label: { Label("Destroy", systemImage: "trash.fill") @@ -53,6 +55,7 @@ public struct CoreView: View { } } .navigationTitle("Core") + .alert($alert) #if os(iOS) .navigationBarTitleDisplayMode(.inline) #endif @@ -84,6 +87,28 @@ public struct CoreView: View { } } + private func confirmDestroyWorkingDirectory() async { + if environments.extensionProfile?.status.isConnected == true { + alert = AlertState( + title: String(localized: "Service is Running"), + message: String(localized: "The service must be stopped before destroying the working directory."), + primaryButton: .destructive(String(localized: "Stop Service and Continue")) { [self] in + Task { + await stopServiceAndDestroy() + } + }, + secondaryButton: .cancel() + ) + } else { + await destroyWorkingDirectory() + } + } + + private func stopServiceAndDestroy() async { + try? await environments.extensionProfile?.stop() + await destroyWorkingDirectory() + } + private nonisolated func destroyWorkingDirectory() async { try? FileManager.default.removeItem(at: FilePath.workingDirectory) await MainActor.run { diff --git a/Localizable.xcstrings b/Localizable.xcstrings index 1c54c13..cba13f4 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -1629,6 +1629,16 @@ } } }, + "Service is Running" : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "服务正在运行" + } + } + } + }, "Service Log" : { "localizations" : { "zh-Hans" : { @@ -1812,6 +1822,16 @@ } } }, + "Stop Service and Continue" : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "停止服务并继续" + } + } + } + }, "System Extension" : { "localizations" : { "zh-Hans" : { @@ -1875,6 +1895,16 @@ } } }, + "The service must be stopped before destroying the working directory." : { + "localizations" : { + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "必须先停止服务才能清理工作目录。" + } + } + } + }, "This app needs to be placed under the Applications folder to work." : { "localizations" : { "zh-Hans" : {