haren724 / wallpaper-player-mac

A powerful open source live wallpaper app for mac
https://docc.haren724.top
GNU General Public License v3.0
383 stars 34 forks source link

[Pr Required (急需提交)] Modify the Structs to Conform OptionSet Protocol (修改一些结构体以遵从OptionSet协议) #5

Closed haren724 closed 1 year ago

haren724 commented 1 year ago

Discussed in https://github.com/haren724/open-wallpaper-engine-mac/discussions/4

Originally posted by **haren724** August 7, 2023 Now I have some trouble with Filter Result ViewModel. The classes like 现在我在Filter Result ViewModel上遇到了一些问题。比如这种class ```Swift class FRWidescreenResolution: FilterResultsModel where ObjectType: ObservableObject { weak var parent: ObjectType? required init() {} @AppStorage("StandardDefinition") public var standardDefinition = true @AppStorage("1280x720") public var resolution1280x720 = true @AppStorage("1920x1080-FullHD") public var resolution1920x1080 = true @AppStorage("2560x1440") public var resolution2560x1440 = true @AppStorage("3840x2160-4K") public var resolution3840x2160 = true } ``` need to be converted into 需要被转换成 ```Swift struct FRWidescreenResolution: OptionSet { let rawValue: Int static let allOptions = [ "StandardDefinition", "1280x720", "Mature", "1920x1080-FullHD", "2560x1440", "3840x2160-4K" ] static let standardDefinition = Self.init(rawValue: 1 << 0) static let resolution1280x720 = Self.init(rawValue: 1 << 1) static let resolution1920x1080 = Self.init(rawValue: 1 << 2) static let resolution2560x1440 = Self.init(rawValue: 1 << 3) static let resolution3840x2160 = Self.init(rawValue: 1 << 4) static let all: Self = [.standardDefinition, resolution1280x720, resolution1920x1080, .resolution2560x1440, .resolution3840x2160] static let none: Self = [] } ``` And then it comes to UI code. Make a full use of the `allOptions` created above to the `ForEach` 然后,对于UI代码的更改,充分利用上面创建的“allOptions”到“ForEach” ```Swift ForEach(Array(zip(FRWidescreenResolution.allOptions.indices, FRWidescreenResolution.allOptions)), id: \.0) { (i, option) in Toggle(option, isOn: Binding(get: { viewModel.widescreenResolution.contains(FRWidescreenResolution(rawValue: 1 << i)) }, set: { if $0 { viewModel.widescreenResolution.insert(FRWidescreenResolution(rawValue: 1 << i)) } else { viewModel.widescreenResolution.remove(FRWidescreenResolution(rawValue: 1 << i)) } print(String(describing: viewModel.widescreenResolution)) })) } ``` Last. The declaration of struct's variable needs to be modified. And because there's no methods in newly created struct anymore, so the `.reset()` call should be changed into `= .all` 最后一点,需要修改一下结构体变量的声明方法。而且因为新创建的结构体中不再有内部方法,所以`.reset()`调用应该更改为`= .all` ```Swift class FilterResultsViewModel: ObservableObject { // ... some codes @AppStorage("FRWidescreenResolution") public var widescreenResolution = FRWidescreenResolution.all // ... some codes public func reset() { // ... some codes self.widescreenResolution = .all // ... some codes } } ```
haren724 commented 1 year ago

Thanks @KeriaDaring, this issue was solved at 26ccb9101a19c50372f398f328fe03d9883620dd.