From b302d7241f47c4720688cb332f63582a4de2ea06 Mon Sep 17 00:00:00 2001 From: Viktor Kushnerov Date: Wed, 26 Feb 2020 10:54:16 +0300 Subject: [PATCH] Safari plugin #15: Enable/disable Safari plugin by Alt+S --- ReaderTranslator/Model/AvailableView.swift | 6 ++++++ ReaderTranslator/Stores/Store.swift | 6 +++++- ReaderTranslator/Stores/ViewsStore.swift | 19 +++++++++++++++++-- .../ReaderView/Modes/ReaderView_Web.swift | 2 +- .../Views/ReaderView/ReaderView.swift | 2 +- .../Views/StatusBarView/StatusBarView.swift | 2 +- .../StatusBarView_Bookmarks.swift | 2 +- .../StatusBarView_ViewsEnabler.swift | 15 +++------------ ReaderTranslatorMac/Info.plist | 2 +- 9 files changed, 36 insertions(+), 20 deletions(-) diff --git a/ReaderTranslator/Model/AvailableView.swift b/ReaderTranslator/Model/AvailableView.swift index 733f12d..7ba469f 100644 --- a/ReaderTranslator/Model/AvailableView.swift +++ b/ReaderTranslator/Model/AvailableView.swift @@ -22,7 +22,9 @@ enum AvailableView: String, Codable, CaseIterable { case pdf = "PDF" case web = "Web" case safari = "Safari" +} +extension AvailableView { var text: String { rawValue } @@ -101,6 +103,10 @@ enum AvailableView: String, Codable, CaseIterable { ] return views.sorted { $0.orderInt < $1.orderInt } } + + var isEnabled: Bool { + ViewsStore.shared.enabledViews.contains(self) + } func getAction(text: String = Store.shared.translateAction.getText()) -> TranslateAction { switch self { diff --git a/ReaderTranslator/Stores/Store.swift b/ReaderTranslator/Stores/Store.swift index d5c86c0..e0b25ff 100644 --- a/ReaderTranslator/Stores/Store.swift +++ b/ReaderTranslator/Stores/Store.swift @@ -15,6 +15,10 @@ final class Store: ObservableObject { let maxViewWidth: CGFloat = 400 - @Published(key: "canSafariSendSelectedText") var canSafariSendSelectedText: Bool = true + @Published(key: "canSafariSendSelectedText") var canSafariSendSelectedText: Bool = true { + didSet { + ViewsStore.shared.enableView(view: .safari, enable: canSafariSendSelectedText) + } + } @Published var translateAction = TranslateAction() } diff --git a/ReaderTranslator/Stores/ViewsStore.swift b/ReaderTranslator/Stores/ViewsStore.swift index d451326..48502cd 100644 --- a/ReaderTranslator/Stores/ViewsStore.swift +++ b/ReaderTranslator/Stores/ViewsStore.swift @@ -11,12 +11,27 @@ import Foundation final class ViewsStore: ObservableObject { private init() {} static let shared = ViewsStore() - + static let defaultWidth: CGFloat = 500 @Published(key: "enabledViews") var enabledViews: Set = [.reverso, .gTranslator] @Published(key: "viewWidth") var viewWidth: [AvailableView: CGFloat] = [:] @Published(key: "viewOrder") var viewOrder: [AvailableView: Int] = [:] - + @Published var showSettings = false } + +extension ViewsStore { + func enableView(view: AvailableView, enable: Bool) { + if enable { + enabledViews.insert(view) + } else { + enabledViews.remove(view) + } + } + + func toggleView(view: AvailableView) { + let enabled = enabledViews.contains(view) + enableView(view: view, enable: !enabled) + } +} diff --git a/ReaderTranslator/Views/ReaderView/Modes/ReaderView_Web.swift b/ReaderTranslator/Views/ReaderView/Modes/ReaderView_Web.swift index b084cdf..e6c14b6 100644 --- a/ReaderTranslator/Views/ReaderView/Modes/ReaderView_Web.swift +++ b/ReaderTranslator/Views/ReaderView/Modes/ReaderView_Web.swift @@ -15,7 +15,7 @@ struct ReaderView_Web: View { var body: some View { VStack { - if viewsStore.enabledViews.contains(.web) { + if AvailableView.web.isEnabled { VStack { HStack { Image.sfSymbol("arrowshape.turn.up.left\(webStore.canGoBack ? ".fill" : "")") diff --git a/ReaderTranslator/Views/ReaderView/ReaderView.swift b/ReaderTranslator/Views/ReaderView/ReaderView.swift index b3a745c..947963d 100644 --- a/ReaderTranslator/Views/ReaderView/ReaderView.swift +++ b/ReaderTranslator/Views/ReaderView/ReaderView.swift @@ -21,7 +21,7 @@ struct ReaderView: View { #endif ForEach(AvailableView.resiableViews, id: \.self) { view in Group { - if self.viewsStore.enabledViews.contains(view) { view.view } + if view.isEnabled { view.view } } } }.padding(.bottom, 20) diff --git a/ReaderTranslator/Views/StatusBarView/StatusBarView.swift b/ReaderTranslator/Views/StatusBarView/StatusBarView.swift index a280538..f838dc8 100644 --- a/ReaderTranslator/Views/StatusBarView/StatusBarView.swift +++ b/ReaderTranslator/Views/StatusBarView/StatusBarView.swift @@ -41,7 +41,7 @@ struct StatusBarView: View { private var playbackRateView: some View { Group { - if viewsStore.enabledViews.contains(.safari) { + if AvailableView.safari.isEnabled { Text(String(format: "Apple: %.2f", [audioStore.playbackRate])) } } diff --git a/ReaderTranslator/Views/StatusBarView/StatusBarView_Bookmarks.swift b/ReaderTranslator/Views/StatusBarView/StatusBarView_Bookmarks.swift index aa91dd4..83a41d1 100644 --- a/ReaderTranslator/Views/StatusBarView/StatusBarView_Bookmarks.swift +++ b/ReaderTranslator/Views/StatusBarView/StatusBarView_Bookmarks.swift @@ -29,7 +29,7 @@ struct StatusBarView_Bookmarks: View { Button( action: { self.bookmarksStore.items.append(text) - if ViewsStore.shared.enabledViews.contains(.bookmarks) { + if AvailableView.bookmarks.isEnabled { self.store.translateAction.add(.bookmarks(text: text)) } }, diff --git a/ReaderTranslator/Views/StatusBarView/StatusBarView_ViewsEnabler.swift b/ReaderTranslator/Views/StatusBarView/StatusBarView_ViewsEnabler.swift index d074db6..c84cdec 100644 --- a/ReaderTranslator/Views/StatusBarView/StatusBarView_ViewsEnabler.swift +++ b/ReaderTranslator/Views/StatusBarView/StatusBarView_ViewsEnabler.swift @@ -38,27 +38,18 @@ struct StatusBarView_ViewsEnabler: View { private func buttonView(_ view: AvailableView) -> some View { Group { Text(view.rawValue) - .background(viewsStore.enabledViews.contains(view) ? Color.red : Color.clear) + .background(view.isEnabled ? Color.red : Color.clear) .onTapGesture { - self.toggle(view) + self.viewsStore.toggleView(view: view) } Image.sfSymbol("square.and.arrow.down.fill") .padding(.trailing, 5) .onTapGesture(count: 1) { self.store.translateAction.add(view.getAction()) - self.viewsStore.enabledViews.insert(view) + self.viewsStore.enableView(view: view, enable: true) } } } - - private func toggle(_ view: AvailableView) { - if viewsStore.enabledViews.contains(view) { - viewsStore.enabledViews.remove(view) - } else { - store.translateAction.add(view.getAction()) - viewsStore.enabledViews.insert(view) - } - } } struct StatusBarView_ViewsEnabler_Previews: PreviewProvider { diff --git a/ReaderTranslatorMac/Info.plist b/ReaderTranslatorMac/Info.plist index f90fb3e..139262e 100644 --- a/ReaderTranslatorMac/Info.plist +++ b/ReaderTranslatorMac/Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.9.4 + 1.9.5 CFBundleVersion 1800 LSApplicationCategoryType