Skip to content

Commit

Permalink
Replace EZConfiguration with Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverAgain11 committed Jan 18, 2024
1 parent 22303ff commit 1aed2f9
Show file tree
Hide file tree
Showing 35 changed files with 158 additions and 101 deletions.
6 changes: 3 additions & 3 deletions Easydict/App/AppDelegate+EZURLScheme.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import <JLRoutes.h>
#import "EZWindowManager.h"
#import "EZSchemeParser.h"
#import "EZConfiguration.h"
#import "Easydict-Swift.h"

@implementation AppDelegate (EZURLScheme)

Expand Down Expand Up @@ -70,8 +70,8 @@ - (void)registerRouters {

- (void)showFloatingWindowAndAutoQueryText:(NSString *)text {
EZWindowManager *windowManager = [EZWindowManager shared];
EZWindowType windowType = EZConfiguration.shared.shortcutSelectTranslateWindowType;

EZWindowType windowType = Configuration.shared.shortcutSelectTranslateWindowType;
[windowManager showFloatingWindowType:windowType
queryText:text.trim
autoQuery:YES
Expand Down
1 change: 0 additions & 1 deletion Easydict/App/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import "MMCrash.h"
#import "EZWindowManager.h"
#import "EZLanguageManager.h"
#import "EZConfiguration.h"
#import "EZLog.h"
#import "EZSchemeParser.h"
#import "AppDelegate+EZURLScheme.h"
Expand Down
41 changes: 36 additions & 5 deletions Easydict/Feature/Configuration/NewConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Defaults
import Foundation

class Configuration: NSObject {
@objcMembers class Configuration: NSObject {
private(set) static var shared = Configuration()

var appDelegate = NSApp.delegate as? AppDelegate
Expand Down Expand Up @@ -46,7 +46,13 @@ class Configuration: NSObject {
var launchAtStartup: Bool

var automaticallyChecksForUpdates: Bool {
updater?.automaticallyChecksForUpdates ?? false
get {
updater?.automaticallyChecksForUpdates ?? false
}
set {
updater?.automaticallyChecksForUpdates = newValue
logSettings(["automatically_checks_for_updates": newValue])
}
}

@DefaultsWrapper(.hideMainWindow)
Expand Down Expand Up @@ -76,8 +82,17 @@ class Configuration: NSObject {
@DefaultsWrapper(.languageDetectOptimize)
var languageDetectOptimize: EZLanguageDetectOptimize

@DefaultsWrapper(.defaultTTSServiceType)
var defaultTTSServiceType: TTSServiceType
// @DefaultsWrapper(.defaultTTSServiceType)
// var defaultTTSServiceType: TTSServiceType

var defaultTTSServiceType: ServiceType {
get {
ServiceType(rawValue: Defaults[.defaultTTSServiceType].rawValue)
}
set {
Defaults[.defaultTTSServiceType] = TTSServiceType(rawValue: newValue.rawValue) ?? .youdao
}
}

@DefaultsWrapper(.showGoogleQuickLink)
var showGoogleQuickLink: Bool
Expand Down Expand Up @@ -134,6 +149,10 @@ class Configuration: NSObject {
@DefaultsWrapper(.enableBetaFeature)
private(set) var beta: Bool

static func destroySharedInstance() {
shared = Configuration()
}

override init() {
super.init()

Expand Down Expand Up @@ -431,7 +450,7 @@ private extension Configuration {

// MARK: Window Frame

private extension Configuration {
extension Configuration {
func windowFrameWithType(_ windowType: EZWindowType) -> CGRect {
Defaults[.windorFrame(for: windowType)]
}
Expand All @@ -453,6 +472,18 @@ extension Configuration {
}
}

// MARK: Intelligent Query Text Type of Service

extension Configuration {
func setQueryTextType(_ queryTextType: EZQueryTextType, serviceType: ServiceType) {
Defaults[.queryTextType(for: serviceType)] = queryTextType
}

func queryTextTypeForServiceType(_ serviceType: ServiceType) -> EZQueryTextType {
Defaults[.queryTextType(for: serviceType)]
}
}

// MARK: Intelligent Query Mode

extension Configuration {
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Feature/DarkMode/DarkModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (void)updateDarkMode {
BOOL isDarkMode = [self isDarkMode];
NSLog(@"%@", isDarkMode ? @"深色模式" : @"浅色模式");

AppearenceType type = (AppearenceType)EZConfiguration.shared.appearance;
AppearenceType type = Configuration.shared.appearance;
switch (type) {
case AppearenceTypeDark:
self.systemDarkMode = true;
Expand Down
11 changes: 6 additions & 5 deletions Easydict/Feature/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "EZLocalStorage.h"
#import "EZAppleScriptManager.h"
#import "EZSystemUtility.h"
#import "Easydict-Swift.h"

static CGFloat const kDismissPopButtonDelayTime = 0.1;
static NSTimeInterval const kDelayGetSelectedTextTime = 0.1;
Expand Down Expand Up @@ -300,7 +301,7 @@ - (void)getSelectedText:(BOOL)checkTextFrame completion:(void (^)(NSString *_Nul
self.selectTextType = EZSelectTextTypeAccessibility;

// Monitor CGEventTap must be required after using Accessibility successfully.
if (EZConfiguration.shared.autoSelectText) {
if (Configuration.shared.autoSelectText) {
[self monitorCGEventTap];
}

Expand Down Expand Up @@ -407,7 +408,7 @@ - (void)autoGetSelectedText:(BOOL)checkTextFrame {
}

- (BOOL)enabledAutoSelectText {
EZConfiguration *config = [EZConfiguration shared];
Configuration *config = [Configuration shared];
BOOL enabled = config.autoSelectText && !config.disabledAutoSelect;
if (!enabled) {
NSLog(@"disabled autoSelectText");
Expand Down Expand Up @@ -437,7 +438,7 @@ - (void)getSelectedTextBySimulatedKey:(void (^)(NSString *_Nullable))completion

// If playing audio, we do not silence system volume.
[EZAudioUtils isPlayingAudio:^(BOOL isPlaying) {
BOOL shouldTurnOffSoundTemporarily = EZConfiguration.shared.disableEmptyCopyBeep && !isPlaying;
BOOL shouldTurnOffSoundTemporarily = Configuration.shared.disableEmptyCopyBeep && !isPlaying;

// Set volume to 0 to avoid system alert.
if (shouldTurnOffSoundTemporarily) {
Expand Down Expand Up @@ -596,7 +597,7 @@ - (BOOL)isAccessibilityEnabled {
/// Check if should use simulation key to get selected text.
- (BOOL)shouldUseSimulatedKey:(NSString *)text error:(AXError)error {
BOOL isAutoSelectQuery = self.actionType == EZActionTypeAutoSelectQuery;
BOOL allowedForceAutoGetSelectedText = [EZConfiguration.shared forceAutoGetSelectedText];
BOOL allowedForceAutoGetSelectedText = [Configuration.shared forceAutoGetSelectedText];

NSString *easydictBundleID = [[NSBundle mainBundle] bundleIdentifier];

Expand All @@ -610,7 +611,7 @@ - (BOOL)shouldUseSimulatedKey:(NSString *)text error:(AXError)error {
FIX: https://github.com/tisfeng/Easydict/issues/192#issuecomment-1797878909
*/
if (isInEasydict && EZConfiguration.shared.isRecordingSelectTextShortcutKey) {
if (isInEasydict && Configuration.shared.isRecordingSelectTextShortcutKey) {
return NO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "EZConfiguration.h"
#import "NSImage+EZSymbolmage.h"
#import "NSImage+EZResize.h"
#import "Easydict-Swift.h"

static CGFloat const kMargin = 20;
static CGFloat const kRowHeight = 45;
Expand Down Expand Up @@ -254,7 +255,7 @@ - (void)selectApp {
[openPanel setAllowedContentTypes:allowedTypes];

// ???: Since [auto select] will cause lag when dragging select apps, I don't know why 😰
EZConfiguration.shared.disabledAutoSelect = YES;
Configuration.shared.disabledAutoSelect = YES;

NSModalResponse result = [openPanel runModal];
if (result == NSModalResponseOK) {
Expand All @@ -267,7 +268,7 @@ - (void)selectApp {
[self.tableView reloadData];
}

EZConfiguration.shared.disabledAutoSelect = NO;
Configuration.shared.disabledAutoSelect = NO;
}

- (NSArray<EZAppModel *> *)appModelsFromBundleIDDict:(NSDictionary<NSString *, NSNumber *> *)appBundleIDDict {
Expand Down
5 changes: 3 additions & 2 deletions Easydict/Feature/PerferenceWindow/EZAboutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "EZBlueTextButton.h"
#import "EZConfiguration.h"
#import "EZMenuItemManager.h"
#import "Easydict-Swift.h"

@interface EZAboutViewController ()

Expand All @@ -35,7 +36,7 @@ - (void)viewDidLoad {
[super viewDidLoad];

[self setupUI];
self.autoCheckUpdateButton.mm_isOn = EZConfiguration.shared.automaticallyChecksForUpdates;
self.autoCheckUpdateButton.mm_isOn = Configuration.shared.automaticallyChecksForUpdates;

[self updateViewSize];

Expand Down Expand Up @@ -177,7 +178,7 @@ - (void)updateViewConstraints {
#pragma mark - Actions

- (void)autoCheckUpdateButtonClicked:(NSButton *)sender {
EZConfiguration.shared.automaticallyChecksForUpdates = sender.mm_isOn;
Configuration.shared.automaticallyChecksForUpdates = sender.mm_isOn;
}

- (void)updateLatestVersion {
Expand Down
9 changes: 5 additions & 4 deletions Easydict/Feature/PerferenceWindow/EZPrivacyViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "EZConfiguration.h"
#import "NSViewController+EZWindow.h"
#import "NSImage+EZSymbolmage.h"
#import "Easydict-Swift.h"

@interface EZPrivacyViewController ()

Expand Down Expand Up @@ -61,7 +62,7 @@ - (void)setupUI {
action:@selector(analyticsButtonClicked:)];
[self.contentView addSubview:self.analyticsButton];

EZConfiguration *configuration = [EZConfiguration shared];
Configuration *configuration = [Configuration shared];
self.crashLogButton.mm_isOn = configuration.allowCrashLog;
self.analyticsButton.mm_isOn = configuration.allowAnalytics;
}
Expand Down Expand Up @@ -120,16 +121,16 @@ - (void)crashLogButtonClicked:(NSButton *)sender {
} else {
sender.mm_isOn = YES;
}
EZConfiguration.shared.allowCrashLog = sender.mm_isOn;
Configuration.shared.allowCrashLog = sender.mm_isOn;
}];
} else {
EZConfiguration.shared.allowCrashLog = YES;
Configuration.shared.allowCrashLog = YES;
}
}


- (void)analyticsButtonClicked:(NSButton *)sender {
EZConfiguration.shared.allowAnalytics = sender.mm_isOn;
Configuration.shared.allowAnalytics = sender.mm_isOn;
}

#pragma mark - MASPreferencesViewController
Expand Down
6 changes: 3 additions & 3 deletions Easydict/Feature/PerferenceWindow/EZSettingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@interface EZSettingViewController () <NSComboBoxDelegate>

@property (nonatomic, strong) EZConfiguration *config;
@property (nonatomic, strong) Configuration *config;

@property (nonatomic, strong) NSTextField *selectLabel;
@property (nonatomic, strong) NSTextField *inputLabel;
Expand Down Expand Up @@ -153,7 +153,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.

self.config = [EZConfiguration shared];
self.config = [Configuration shared];

[self setupUI];

Expand All @@ -165,7 +165,7 @@ - (void)viewDidLoad {

// Observe selectionShortcutView.recording status.
[self.KVOController observe:self.selectionShortcutView keyPath:@"recording" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew block:^(EZSettingViewController *settingVC, MASShortcutView *selectionShortcutView, NSDictionary<NSString *, id> *_Nonnull change) {
EZConfiguration.shared.isRecordingSelectTextShortcutKey = [change[NSKeyValueChangeNewKey] boolValue];
Configuration.shared.isRecordingSelectTextShortcutKey = [change[NSKeyValueChangeNewKey] boolValue];
}];
}

Expand Down
3 changes: 2 additions & 1 deletion Easydict/Feature/Service/Apple/EZAppleService.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import <CoreImage/CoreImage.h>
#import "NSString+EZUtils.h"
#import "EZAppleDictionary.h"
#import "Easydict-Swift.h"

static NSString *const kLineBreakText = @"\n";
static NSString *const kParagraphBreakText = @"\n\n";
Expand Down Expand Up @@ -607,7 +608,7 @@ - (NLLanguage)detectUnkownText:(NSString *)text {
NLLanguage language = NLLanguageEnglish;
// 729
if ([text isNumbers]) {
EZLanguage firstLanguage = EZConfiguration.shared.firstLanguage;
EZLanguage firstLanguage = Configuration.shared.firstLanguage;
language = [self appleLanguageFromLanguageEnum:firstLanguage];
}

Expand Down
3 changes: 2 additions & 1 deletion Easydict/Feature/Service/AudioPlayer/EZAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "EZServiceTypes.h"
#import "EZConfiguration.h"
#import <sys/xattr.h>
#import "Easydict-Swift.h"

static NSString *const kFileExtendedAttributes = @"NSFileExtendedAttributes";

Expand Down Expand Up @@ -128,7 +129,7 @@ - (void)setIsPlaying:(BOOL)playing {

// Note that user may change it when using, so we need to read it every time.
- (EZQueryService *)defaultTTSService {
EZServiceType defaultTTSServiceType = EZConfiguration.shared.defaultTTSServiceType;
EZServiceType defaultTTSServiceType = Configuration.shared.defaultTTSServiceType;
if (![_defaultTTSService.serviceType isEqualToString:defaultTTSServiceType]) {
EZQueryService *defaultTTSService = [EZServiceTypes.shared serviceWithType:defaultTTSServiceType];
_defaultTTSService = defaultTTSService;
Expand Down
6 changes: 4 additions & 2 deletions Easydict/Feature/Service/Baidu/EZBaiduTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "EZNetworkManager.h"
#import "EZConfiguration.h"
#import "NSString+EZRegex.h"
#import "Easydict-Swift.h"

static NSString *const kBaiduTranslateURL = @"https://fanyi.baidu.com";

Expand Down Expand Up @@ -124,15 +125,16 @@ - (EZServiceType)serviceType {

- (EZQueryTextType)queryTextType {
EZQueryTextType defaultType = EZQueryTextTypeDictionary | EZQueryTextTypeSentence | EZQueryTextTypeTranslation;
EZQueryTextType type = [EZConfiguration.shared queryTextTypeForServiceType:self.serviceType];
// EZQueryTextType type = [Configuration.shared queryTextTypeForServiceType:self.serviceType];
EZQueryTextType type = [Configuration.shared queryTextTypeForServiceType:EZServiceTypeGoogle];
if (type == 0) {
type = defaultType;
}
return type;
}

- (EZQueryTextType)intelligentQueryTextType {
EZQueryTextType type = [EZConfiguration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
EZQueryTextType type = [Configuration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
return type;
}

Expand Down
3 changes: 2 additions & 1 deletion Easydict/Feature/Service/Bing/EZBingService.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "EZBingLookupModel.h"
#import "EZConfiguration.h"
#import "NSString+EZUtils.h"
#import "Easydict-Swift.h"

@interface EZBingService ()
@property (nonatomic, strong) EZBingRequest *request;
Expand All @@ -32,7 +33,7 @@ - (instancetype)init {
#pragma mark - override

- (EZQueryTextType)intelligentQueryTextType {
EZQueryTextType type = [EZConfiguration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
EZQueryTextType type = [Configuration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
return type;
}

Expand Down
3 changes: 2 additions & 1 deletion Easydict/Feature/Service/Google/EZGoogleTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <JavaScriptCore/JavaScriptCore.h>
#import "NSString+EZUtils.h"
#import "EZConfiguration.h"
#import "Easydict-Swift.h"

static NSString *const kGoogleTranslateURL = @"https://translate.google.com";

Expand Down Expand Up @@ -115,7 +116,7 @@ - (EZQueryTextType)queryTextType {
}

- (EZQueryTextType)intelligentQueryTextType {
EZQueryTextType type = [EZConfiguration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
EZQueryTextType type = [Configuration.shared intelligentQueryTextTypeForServiceType:self.serviceType];
return type;
}

Expand Down
Loading

0 comments on commit 1aed2f9

Please sign in to comment.