Skip to content

Commit b69c159

Browse files
committed
Fixes #914: Add multi-select to custom fields
1 parent bf820d7 commit b69c159

5 files changed

Lines changed: 142 additions & 82 deletions

File tree

src/Shared/PresetsDisplay/PresetDisplayKey.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
import UIKit
1212

1313
// A key along with information about possible values
14-
class PresetDisplayKey: NSObject, NSSecureCoding, Codable {
14+
class PresetDisplayKey: NSObject, Codable {
1515
public class var supportsSecureCoding: Bool { return true }
1616

1717
let name: String // name of the preset, e.g. Hours
@@ -50,17 +50,8 @@ class PresetDisplayKey: NSObject, NSSecureCoding, Codable {
5050
self.defaultValue = defaultValue
5151
}
5252

53-
// This is used only for user-defined keys, call from
53+
// This is used only for user-defined keys, called from
5454
// PresetKeyUserDefined() super.init()
55-
func encode(with coder: NSCoder) {
56-
coder.encode(name, forKey: "name")
57-
coder.encode(tagKey, forKey: "tagKey")
58-
coder.encode(placeholder, forKey: "placeholder")
59-
coder.encode(presetValues, forKey: "presetList")
60-
coder.encode(keyboardType.rawValue, forKey: "keyboardType")
61-
coder.encode(autocapitalizationType.rawValue, forKey: "capitalize")
62-
}
63-
6455
required init?(coder: NSCoder) {
6556
if let name = coder.decodeObject(forKey: "name") as? String,
6657
let tagKey = coder.decodeObject(forKey: "tagKey") as? String,
@@ -88,21 +79,31 @@ class PresetDisplayKey: NSObject, NSSecureCoding, Codable {
8879
case name
8980
case tagKey
9081
case presetList
82+
case presetType
9183
}
9284

85+
// This is used only for user-defined keys, called from
86+
// it's encoder
9387
func encode(to encoder: Encoder) throws {
9488
var container = encoder.container(keyedBy: CodingKeys.self)
9589
try container.encode(name, forKey: .name)
9690
try container.encode(tagKey, forKey: .tagKey)
9791
try container.encode(presetValues, forKey: .presetList)
92+
try container.encode(type, forKey: .presetType)
9893
}
9994

95+
// This is used only for user-defined keys, called from
96+
// its decoder
10097
required init(from decoder: Decoder) throws {
10198
let container = try decoder.container(keyedBy: CodingKeys.self)
10299
name = try container.decode(String.self, forKey: .name)
103100
tagKey = try container.decode(String.self, forKey: .tagKey)
104101
presetValues = try container.decode([PresetDisplayValue].self, forKey: .presetList)
105-
type = .combo
102+
// originally we didn't save 'type' so it may not exist.
103+
// If it exists then use it, otherwise infer the type from the number of presets provided
104+
type = (try? container.decode(PresetType.self, forKey: .presetType)) ??
105+
((presetValues?.count ?? 0) > 0 ? .combo : .text)
106+
106107
defaultValue = nil
107108
placeholder = PresetDisplayKey.placeholderForPresets(presetValues)
108109
?? PresetTranslations.shared.unknownForLocale

src/Shared/PresetsDisplay/PresetDisplayKeyUserDefined.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ final class PresetDisplayKeyUserDefined: PresetDisplayKey {
3131
init(appliesToKey: String, // empty string is possible
3232
appliesToValue: String, // empty string is possible
3333
name: String,
34+
type: PresetType,
3435
tagKey key: String,
3536
placeholder: String?,
3637
keyboard: UIKeyboardType,
@@ -41,7 +42,7 @@ final class PresetDisplayKeyUserDefined: PresetDisplayKey {
4142
self.appliesToKey = appliesToKey
4243
self.appliesToValue = appliesToValue
4344
super.init(name: name,
44-
type: presetValues.count > 0 ? .combo : .text,
45+
type: type,
4546
tagKey: key,
4647
defaultValue: nil,
4748
placeholder: placeholder,
@@ -51,12 +52,6 @@ final class PresetDisplayKeyUserDefined: PresetDisplayKey {
5152
presetValues: presetValues)
5253
}
5354

54-
override func encode(with coder: NSCoder) {
55-
super.encode(with: coder)
56-
coder.encode(appliesToKey, forKey: "appliesToKey")
57-
coder.encode(appliesToValue, forKey: "appliesToValue")
58-
}
59-
6055
// MARK: Codable
6156

6257
enum CodingKeys: String, CodingKey {

0 commit comments

Comments
 (0)