Skip to content

Commit

Permalink
fix: code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Jan 20, 2024
1 parent 892a0dd commit 93e2fe6
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Combine
import SwiftUI

class DisabledAppViewModel: ObservableObject {
private class DisabledAppViewModel: ObservableObject {
@Published var appModelList: [EZAppModel] = []
@Published var selectedAppModels: Set<EZAppModel> = []
@Published var isImporting = false
Expand Down Expand Up @@ -60,7 +60,7 @@ class DisabledAppViewModel: ObservableObject {

@available(macOS 13.0, *)
struct DisabledAppTab: View {
@StateObject var disabledAppViewModel = DisabledAppViewModel()
@StateObject private var disabledAppViewModel = DisabledAppViewModel()

var listToolbar: some View {
ListToolbar()
Expand Down Expand Up @@ -133,15 +133,16 @@ private struct ListToolbar: View {
VStack(spacing: 0) {
Divider()
HStack(spacing: 0) {
ListButton(imageName: "plus", enabled: true) {
ListButton(systemName: "plus") {
disabledAppViewModel.isImporting.toggle()
}
Divider().padding(.vertical, 1)
let isNoSelectedApps = disabledAppViewModel.selectedAppModels.isEmpty
ListButton(imageName: "minus", enabled: !isNoSelectedApps) {
.environment(\.isEnabled, true)
Divider()
.padding(.vertical, 1)
ListButton(systemName: "minus") {
disabledAppViewModel.removeDisabledApp()
}
.disabled(isNoSelectedApps)
.environment(\.isEnabled, !disabledAppViewModel.selectedAppModels.isEmpty)
Spacer()
}
.padding(2)
Expand All @@ -153,24 +154,25 @@ private struct ListToolbar: View {

@available(macOS 13.0, *)
private struct ListButton: View {
var imageName: String
var enabled: Bool
@Environment(\.isEnabled) private var isEnabled: Bool
var systemName: String
var action: () -> Void

var body: some View {
Button(action: {
action()
}) {
Image(systemName: imageName)
Image(systemName: systemName)
.resizable()
.scaledToFit()
.frame(width: 10, height: 10)
.padding(.horizontal, 8)
.contentShape(Rectangle())
.foregroundStyle(enabled ? Color(.secondaryLabelColor) : Color(.tertiaryLabelColor))
.foregroundStyle(isEnabled ? Color(.secondaryLabelColor) : Color(.tertiaryLabelColor))
.font(.system(size: 14, weight: .semibold))
}
.buttonStyle(BorderlessButtonStyle())
.disabled(isEnabled)
}
}

Expand Down

0 comments on commit 93e2fe6

Please sign in to comment.