-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] rewrite EZConfiguration using Swift #335
Conversation
I wrote a property wrapper for you. Have a try. @propertyWrapper
struct DefaultsWrapper<T: Defaults.Serializable> {
var wrappedValue: T {
get {
Defaults[key]
} set {
Defaults[key] = newValue
}
}
/// Initialize a property binding to Defaults value.
/// - Parameters:
/// - key: key in Defaults
/// - onUpdated: the function called on value changes
/// - callInitial: the function called on initialize the property
init(_ key: Defaults.Key<T>, onUpdated: @escaping (_ oldValue: T, _ newValue: T) -> Void, onInitial: ((_ value: T) -> Void)?) {
self.key = key
if let onInitial {
onInitial(Defaults[key])
}
observer = Defaults.observe(key) { change in
onUpdated(change.oldValue, change.newValue)
}
}
private let observer: Defaults.Observation
let key: Defaults.Key<T>
}
@DefaultsWrapper(
.firstLanguage,
onUpdated: { _, newValue in
logSettings(["first_language": newValue])
},
onInitial: { logSettings(["first_language": $0]) }
)
var firstLanguage |
I implement extension Defaults.Keys {
static func intelligentQueryTextType(for serviceType: ServiceType) -> Key<EZQueryTextType> {
let key = EZConstKey.constkey("IntelligentQueryTextType", serviceType: serviceType)
return .init(key, default: EZQueryTextType(rawValue: 7))
}
}
extension EZQueryTextType: Defaults.Serializable {
public static var bridge: Bridge = Bridge()
public struct Bridge: Defaults.Bridge {
public func serialize(_ value: EZQueryTextType?) -> String? {
guard let value else { return "7" }
return "\(value.rawValue)"
}
public func deserialize(_ object: String?) -> EZQueryTextType? {
guard let object else { return nil }
return EZQueryTextType(rawValue: UInt(object) ?? 7)
}
public typealias Value = EZQueryTextType
public typealias Serializable = String
}
} Use with Defaults[.intelligentQueryTextType(for: .ali)] Not tested. Please check if it works. I think it should work.) |
@CanglongCl |
Try make |
Defaults.observe already meet the need for observation. |
20dda82
to
3e248a5
Compare
@NeverAgain11 Please resolve conflicts. |
@tisfeng Done. |
@NeverAgain11 This code looks fine, but we need to test their functionality in real use. After rewriting EZConfiguration, you should try to completely replace the previous objc EZConfiguration with the new Swift Configuration and make sure that both the new SwiftUI settings page and the previous settings page work properly. In addition, I noticed that both |
@tisfeng The property in the swift configuration are using Struct. this is not accessible by the objc class. |
I'm not sure exactly what you are referring to, can you elaborate using code? Is it possible to use If some of the Swift properties really don't work in objc, we can start by replacing |
I tried that, and it's probably not really a good idea to completely replace objc |
I don't think we really need a |
@tisfeng I've resolved the issue. It seems like an issue cause by Now I replaced the old code with Defaults.publisher(.hideMenuBarIcon)
.removeDuplicates()
.sink { [weak self] _ in
self?.didSetHideMenuBarIcon()
} where cc @NeverAgain11 Please review the changes. |
It's works fine. |
Can we remove
|
Again, can we merge |
Thank you all, the new settings page is mostly working now, with probably a few minor issues, hopefully we can finalize and polish it up soon and get it out to users. |
if windowType == .mini { | ||
return true | ||
} | ||
return UserDefaults.standard.string(forKey: key) ?? defaultValue == "1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面代码有问题,会导致 mini 窗口一直开启智能查询模式,且无法手动关闭 #502 (comment) 。
以下是之前功能的 objc 代码。
- (BOOL)intelligentQueryModeForWindowType:(EZWindowType)windowType {
NSString *key = [EZConstKey constkey:EZIntelligentQueryModeKey windowType:windowType];
NSString *defaultValue = @"0";
// Turn on intelligent query mode by default in mini window.
if (windowType == EZWindowTypeMini) {
defaultValue = @"1";
}
NSString *stringValue = [NSUserDefaults mm_readString:key defaultValue:defaultValue];
return [stringValue boolValue];
}
#284