-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarImageCreator.swift
138 lines (112 loc) · 3.56 KB
/
CarImageCreator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// CarImageCreator.swift
//
// Created by Ramin Rezaiifar on 11/16/20.
//
import Foundation
import TeslaSwift
protocol CarImageCreator {
func loadVehicleImage(vehicleInfo: VehicleExtended) -> URL?
}
extension CarImageCreator {
func loadVehicleImage(vehicleInfo: VehicleExtended) -> URL? {
let config = vehicleInfo.vehicleConfig!
let urlStr = "https://static-assets.tesla.com" + "/v1/compositor/?model=\(carTypeCode(config.carType))&view=STUD_3QTR&size=400&bkba_opt=1&options=\(colorCode(config.exteriorColor)),\(wheelCode(config))" + additionalOptions(carType: config.carType)
let url = URL(string: urlStr)
print("url for image: \(urlStr)")
return url
}
func additionalOptions(carType: String?) -> String {
guard carType != nil else {return ""}
if carType == "models2" {
return ",MI01"
}
if carType == "models3" {
return ",MI02"
}
return ""
}
func carTypeCode(_ carType: String?) -> String {
guard carType != nil else {return "m3"}
if carType!.hasPrefix("model3") { return "m3" }
if carType!.hasPrefix("modely") { return "my" }
if carType!.hasPrefix("models") { return "ms" }
if carType!.hasPrefix("modelx") { return "mx" }
return "m3"
}
func wheelCode(_ config: VehicleConfig) -> String {
let wheelType = config.wheelType
var defaultWheel: String = "W38B"
if config.carType!.hasPrefix("model3") {
defaultWheel = "W38B"
}
if config.carType!.hasPrefix("models") {
defaultWheel = "WTTB"
}
if config.carType!.hasPrefix("modelx") {
defaultWheel = "WTHX"
}
if config.carType!.hasPrefix("modelx") {
defaultWheel = "WTHX"
}
var wheel: String
switch wheelType {
case "Pinwheel18":
wheel = "W38B"
case "AeroTurbine20": // MODEL X
wheel = "WT20"
case "Sportwheel19", "Stiletto19":
wheel = "W39B"
case "AeroTurbine19":
wheel = "WTAS"
case "Turbine19":
wheel = "WTTB"
case "Arachnid21Grey":
wheel = "WTAB"
// FIXME: Too dark.
case "Performancewheel20", "Stiletto20":
wheel = "W32P"
case "AeroTurbine22":
wheel = "WT22"
case "Super21Gray", "TwinTurbine21Carbon":
wheel = "WTSG"
default:
wheel = defaultWheel
}
return wheel
}
func colorCode(_ colorName: String?) -> String {
var color: String
switch colorName {
case "ObsidianBlack", "SolidBlack", "MetallicBlack":
color = "PMBL"
case "DeepBlueMetallic", "DeepBlue":
color = "PPSB"
case "RedMulticoat", "Red":
color = "PPMR"
case "MidnightSilverMetallic", "MidnightSilver", "SteelGrey", "SilverMetallic":
color = "PMNG"
case "MetallicBrown", "Brown":
color = "PMAB"
case "Silver":
color = "PMSS"
case "TitaniumCopper":
color = "PPTI"
case "DolphinGrey":
color = "PMTG"
case "Green", "MetallicGreen":
color = "PMSG"
case "PearlWhiteMulticoat", "PearlWhite", "Pearl":
color = "PPSW"
case "SolidWhite", "White":
color = "PBCW"
case "SignatureBlue", "MetallicBlue":
color = "PMMB"
case "SignatureRed":
color = "PPSR"
default:
color = "PBSB"
}
return color
}
}