From 49685cd2ce65397ba0d6ec4cd93478e60eca996c Mon Sep 17 00:00:00 2001 From: Sharker <1548742234@qq.com> Date: Wed, 7 Feb 2024 10:19:39 +0800 Subject: [PATCH] fix: optimize code --- .../MenuView/MainMenuShortcutCommand.swift | 30 ++++++++--------- .../MainMenuShortcutCommandItem.swift | 5 ++- .../View/Shortcut/AppShortcutSetting.swift | 32 +++++++++---------- .../View/Shortcut/GlobalShortcutSetting.swift | 12 +++---- .../Tabs/View/Shortcut/KeyHolderWrapper.swift | 3 +- 5 files changed, 40 insertions(+), 42 deletions(-) diff --git a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift index 180cb45f0..82ff53423 100644 --- a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift +++ b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommand.swift @@ -11,21 +11,21 @@ import SwiftUI extension EasyDictMainMenu { struct MainMenuShortcutCommand: Commands { @State private var appShortcutCommandList = [ - MainMenuShortcutCommandDataItem(title: ShortcutType.clearInput.localizedStringKey(), type: .clearInput), - MainMenuShortcutCommandDataItem(title: ShortcutType.clearAll.localizedStringKey(), type: .clearAll), - MainMenuShortcutCommandDataItem(title: ShortcutType.copy.localizedStringKey(), type: .copy), - MainMenuShortcutCommandDataItem(title: ShortcutType.copyFirstResult.localizedStringKey(), type: .copyFirstResult), - MainMenuShortcutCommandDataItem(title: ShortcutType.focus.localizedStringKey(), type: .focus), - MainMenuShortcutCommandDataItem(title: ShortcutType.play.localizedStringKey(), type: .play), - MainMenuShortcutCommandDataItem(title: ShortcutType.retry.localizedStringKey(), type: .retry), - MainMenuShortcutCommandDataItem(title: ShortcutType.toggle.localizedStringKey(), type: .toggle), - MainMenuShortcutCommandDataItem(title: ShortcutType.pin.localizedStringKey(), type: .pin), - MainMenuShortcutCommandDataItem(title: ShortcutType.hide.localizedStringKey(), type: .hide), - MainMenuShortcutCommandDataItem(title: ShortcutType.increaseFontSize.localizedStringKey(), type: .increaseFontSize), - MainMenuShortcutCommandDataItem(title: ShortcutType.decreaseFontSize.localizedStringKey(), type: .decreaseFontSize), - MainMenuShortcutCommandDataItem(title: ShortcutType.google.localizedStringKey(), type: .google), - MainMenuShortcutCommandDataItem(title: ShortcutType.eudic.localizedStringKey(), type: .eudic), - MainMenuShortcutCommandDataItem(title: ShortcutType.appleDic.localizedStringKey(), type: .appleDic), + MainMenuShortcutCommandDataItem(type: .clearInput), + MainMenuShortcutCommandDataItem(type: .clearAll), + MainMenuShortcutCommandDataItem(type: .copy), + MainMenuShortcutCommandDataItem(type: .copyFirstResult), + MainMenuShortcutCommandDataItem(type: .focus), + MainMenuShortcutCommandDataItem(type: .play), + MainMenuShortcutCommandDataItem(type: .retry), + MainMenuShortcutCommandDataItem(type: .toggle), + MainMenuShortcutCommandDataItem(type: .pin), + MainMenuShortcutCommandDataItem(type: .hide), + MainMenuShortcutCommandDataItem(type: .increaseFontSize), + MainMenuShortcutCommandDataItem(type: .decreaseFontSize), + MainMenuShortcutCommandDataItem(type: .google), + MainMenuShortcutCommandDataItem(type: .eudic), + MainMenuShortcutCommandDataItem(type: .appleDic), ] var body: some Commands { diff --git a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift index f0d0af319..669057c9f 100644 --- a/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift +++ b/Easydict/NewApp/View/MenuView/MainMenuShortcutCommandItem.swift @@ -9,8 +9,7 @@ import SwiftUI struct MainMenuShortcutCommandDataItem: Identifiable { - public var id: String { title } - var title: String + public var id: String { type.localizedStringKey() } var type: ShortcutType } @@ -18,7 +17,7 @@ struct MainMenuShortcutCommandItem: View { public var dataItem: MainMenuShortcutCommandDataItem var body: some View { - Button(LocalizedStringKey(dataItem.title)) { + Button(LocalizedStringKey(dataItem.type.localizedStringKey())) { switch dataItem.type { case .clearInput: Shortcut.shared.clearInput() diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift index a499b54e5..62773bb6e 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/AppShortcutSetting.swift @@ -12,21 +12,21 @@ import SwiftUI extension ShortcutTab { struct AppShortcutSettingView: View { @State private var shortcutDataList = [ - KeyHolderDataItem(title: ShortcutType.clearInput.localizedStringKey(), type: .clearInput), - KeyHolderDataItem(title: ShortcutType.clearAll.localizedStringKey(), type: .clearAll), - KeyHolderDataItem(title: ShortcutType.copy.localizedStringKey(), type: .copy), - KeyHolderDataItem(title: ShortcutType.copyFirstResult.localizedStringKey(), type: .copyFirstResult), - KeyHolderDataItem(title: ShortcutType.focus.localizedStringKey(), type: .focus), - KeyHolderDataItem(title: ShortcutType.play.localizedStringKey(), type: .play), - KeyHolderDataItem(title: ShortcutType.retry.localizedStringKey(), type: .retry), - KeyHolderDataItem(title: ShortcutType.toggle.localizedStringKey(), type: .toggle), - KeyHolderDataItem(title: ShortcutType.pin.localizedStringKey(), type: .pin), - KeyHolderDataItem(title: ShortcutType.hide.localizedStringKey(), type: .hide), - KeyHolderDataItem(title: ShortcutType.increaseFontSize.localizedStringKey(), type: .increaseFontSize), - KeyHolderDataItem(title: ShortcutType.decreaseFontSize.localizedStringKey(), type: .decreaseFontSize), - KeyHolderDataItem(title: ShortcutType.google.localizedStringKey(), type: .google), - KeyHolderDataItem(title: ShortcutType.eudic.localizedStringKey(), type: .eudic), - KeyHolderDataItem(title: ShortcutType.appleDic.localizedStringKey(), type: .appleDic), + KeyHolderDataItem(type: .clearInput), + KeyHolderDataItem(type: .clearAll), + KeyHolderDataItem(type: .copy), + KeyHolderDataItem(type: .copyFirstResult), + KeyHolderDataItem(type: .focus), + KeyHolderDataItem(type: .play), + KeyHolderDataItem(type: .retry), + KeyHolderDataItem(type: .toggle), + KeyHolderDataItem(type: .pin), + KeyHolderDataItem(type: .hide), + KeyHolderDataItem(type: .increaseFontSize), + KeyHolderDataItem(type: .decreaseFontSize), + KeyHolderDataItem(type: .google), + KeyHolderDataItem(type: .eudic), + KeyHolderDataItem(type: .appleDic), ] @State var confictAlterMessage: ShortcutConfictAlertMessage = .init(title: "", message: "") var body: some View { @@ -38,7 +38,7 @@ extension ShortcutTab { ) Section { ForEach(shortcutDataList) { item in - KeyHolderRowView(title: item.title, type: item.type, confictAlterMessage: $confictAlterMessage) + KeyHolderRowView(title: item.type.localizedStringKey(), type: item.type, confictAlterMessage: $confictAlterMessage) } } header: { Text("app_shortcut_setting") diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift index c911e5c1a..0fdcb9083 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/GlobalShortcutSetting.swift @@ -13,11 +13,11 @@ extension ShortcutTab { struct GlobalShortcutSettingView: View { @State var confictAlterMessage: ShortcutConfictAlertMessage = .init(title: "", message: "") @State private var shortcutDataList = [ - KeyHolderDataItem(title: ShortcutType.inputTranslate.localizedStringKey(), type: .inputTranslate), - KeyHolderDataItem(title: ShortcutType.snipTranslate.localizedStringKey(), type: .snipTranslate), - KeyHolderDataItem(title: ShortcutType.selectTranslate.localizedStringKey(), type: .selectTranslate), - KeyHolderDataItem(title: ShortcutType.showMiniWindow.localizedStringKey(), type: .showMiniWindow), - KeyHolderDataItem(title: ShortcutType.silentScreenshotOcr.localizedStringKey(), type: .silentScreenshotOcr), + KeyHolderDataItem(type: .inputTranslate), + KeyHolderDataItem(type: .snipTranslate), + KeyHolderDataItem(type: .selectTranslate), + KeyHolderDataItem(type: .showMiniWindow), + KeyHolderDataItem(type: .silentScreenshotOcr), ] var body: some View { let showAlter = Binding( @@ -28,7 +28,7 @@ extension ShortcutTab { ) Section { ForEach(shortcutDataList) { item in - KeyHolderRowView(title: item.title, type: item.type, confictAlterMessage: $confictAlterMessage) + KeyHolderRowView(title: item.type.localizedStringKey(), type: item.type, confictAlterMessage: $confictAlterMessage) } } header: { Text("global_shortcut_setting") diff --git a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift index beec39ae5..80ef418be 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/View/Shortcut/KeyHolderWrapper.swift @@ -13,8 +13,7 @@ import Magnet import SwiftUI public struct KeyHolderDataItem: Identifiable { - public var id: String { title } - var title: String + public var id: String { type.localizedStringKey() } var type: ShortcutType }