Skip to content

Commit

Permalink
fixed parsing Double values when it's mixed with integers and default…
Browse files Browse the repository at this point in the history
… value is integer, added setter
  • Loading branch information
Cyberbeni committed May 23, 2018
1 parent 7e03521 commit 50e8726
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<integer>0</integer>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
<real>3.5</real>
</array>
</dict>
<dict>
Expand Down
42 changes: 25 additions & 17 deletions Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import Foundation

struct Settings {

enum DefaultBackendServer: String {
case test = "1"
case dev = "0"
Expand All @@ -14,32 +13,41 @@ struct Settings {
private static let defaultValue: DefaultBackendServer = .test

static var selectedValue: DefaultBackendServer {
let value = UserDefaults.standard.object(forKey: key) as? String
if let value = value {
return DefaultBackendServer(rawValue: value) ?? defaultValue
} else {
return defaultValue
get {
let value = UserDefaults.standard.object(forKey: key) as? String
if let value = value {
return DefaultBackendServer(rawValue: value) ?? defaultValue
} else {
return defaultValue
}
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: key)
}
}
}

enum DefaultNavigationApp: Int {
case chooseFromAvailableApps = 0
case maps = 1
case googleMaps = 2
case waze = 3
enum DefaultNavigationApp: Double {
case chooseFromAvailableApps = 0.0
case maps = 1.0
case googleMaps = 2.0
case waze = 3.5

private static let key = "defaultNavigationApp"
private static let defaultValue: DefaultNavigationApp = .chooseFromAvailableApps

static var selectedValue: DefaultNavigationApp {
let value = UserDefaults.standard.object(forKey: key) as? Int
if let value = value {
return DefaultNavigationApp(rawValue: value) ?? defaultValue
} else {
return defaultValue
get {
let value = UserDefaults.standard.object(forKey: key) as? Double
if let value = value {
return DefaultNavigationApp(rawValue: value) ?? defaultValue
} else {
return defaultValue
}
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: key)
}
}
}

}
9 changes: 5 additions & 4 deletions SettingsGenerator/Parsers/MultiValueParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ final class MultiValueParser: Parser {
for element in multiValues {
if let dictionary = element as? NSDictionary {
print(element)
let values = dictionary[Keys.Values.rawValue]
let defaultValue = dictionary[Keys.DefaultValue.rawValue]
switch defaultValue {
case let value as Int:
switch (values, defaultValue) {
case let (_ as [Int], value as Int):
Printer.shared.add(MultiValueObject(defaultValue: value, dictionary: dictionary))
case let value as String:
case let (_ as [String], value as String):
Printer.shared.add(MultiValueObject(defaultValue: value, dictionary: dictionary))
case let value as Double:
case let (_ as [Double], value as Double):
Printer.shared.add(MultiValueObject(defaultValue: value, dictionary: dictionary))
default:
print("unsupported type")
Expand Down
15 changes: 10 additions & 5 deletions SettingsGenerator/Printable Objects/MultiValueObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ final class MultiValueObject<ValueType: CaseLineProvider & Equatable>: Printable
description.append("""
static var selectedValue: \(key.capitalizedFirstLetter) {
let value = UserDefaults.standard.object(forKey: key) as? \(ValueType.self)
if let value = value {
return \(key.capitalizedFirstLetter)(rawValue: value) ?? defaultValue
} else {
return defaultValue
get {
let value = UserDefaults.standard.object(forKey: key) as? \(ValueType.self)
if let value = value {
return \(key.capitalizedFirstLetter)(rawValue: value) ?? defaultValue
} else {
return defaultValue
}
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: key)
}
}
Expand Down
2 changes: 0 additions & 2 deletions SettingsGenerator/Printer/Printer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ final class Printer {
import Foundation
struct Settings {
"""
private var footer: String = """
}
"""

Expand Down

0 comments on commit 50e8726

Please sign in to comment.