xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / DeveloperServicesPlatformRequest.swift
DeveloperServicesPlatformRequest.swift96 lines · 2.4 KiBRaw
| 1 | // |
| 2 | // DeveloperServicesPlatformRequest.swift |
| 3 | // Supercharge |
| 4 | // |
| 5 | // Created by Kabir Oberai on 24/07/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | |
| 11 | public enum DeveloperServicesPlatform: Decodable, Hashable, Sendable { |
| 12 | private static let known: ([String: DeveloperServicesPlatform], [DeveloperServicesPlatform: String]) = { |
| 13 | var first: [String: DeveloperServicesPlatform] = [:] |
| 14 | var second: [DeveloperServicesPlatform: String] = [:] |
| 15 | func add(_ platform: DeveloperServicesPlatform, _ name: String? = nil) { |
| 16 | let name = name ?? "\(platform)" |
| 17 | first[name] = platform |
| 18 | second[platform] = name |
| 19 | } |
| 20 | |
| 21 | add(.macOS, "mac") |
| 22 | add(.iOS, "ios") |
| 23 | add(.watchOS, "watchOS") |
| 24 | add(.tvOS, "tvOS") |
| 25 | add(.safari) |
| 26 | |
| 27 | return (first, second) |
| 28 | }() |
| 29 | |
| 30 | case macOS |
| 31 | case iOS |
| 32 | case watchOS |
| 33 | case tvOS |
| 34 | case safari |
| 35 | case unknown(String) |
| 36 | |
| 37 | var rawValue: String { |
| 38 | switch self { |
| 39 | case .unknown(let val): |
| 40 | return val |
| 41 | default: |
| 42 | return Self.known.1[self] ?? "\(self)" |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | var os: String { |
| 47 | switch self { |
| 48 | case .iOS, .watchOS: return "ios" |
| 49 | case .tvOS: return "tvos" |
| 50 | default: return "" |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | var subPlatform: String? { |
| 55 | switch self { |
| 56 | case .iOS, .watchOS: return nil |
| 57 | case .tvOS: return "tvOS" |
| 58 | default: return "" |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public static let current: DeveloperServicesPlatform = { |
| 63 | #if os(tvOS) |
| 64 | return .tvOS |
| 65 | #elseif os(watchOS) |
| 66 | return .watchOS |
| 67 | #else |
| 68 | return .iOS |
| 69 | #endif |
| 70 | }() |
| 71 | |
| 72 | public init(from decoder: Decoder) throws { |
| 73 | let rawValue = try String(from: decoder) |
| 74 | self = Self.known.0[rawValue] ?? .unknown(rawValue) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | protocol DeveloperServicesPlatformRequest: DeveloperServicesRequest { |
| 79 | var platform: DeveloperServicesPlatform { get } |
| 80 | |
| 81 | var subAction: String { get } |
| 82 | var subParameters: [String: Any] { get } |
| 83 | } |
| 84 | |
| 85 | extension DeveloperServicesPlatformRequest { |
| 86 | |
| 87 | public var action: String { return "\(platform.os)/\(subAction)" } |
| 88 | |
| 89 | public var parameters: [String: Any] { |
| 90 | var parameters = subParameters |
| 91 | parameters["DTDK_Platform"] = platform.rawValue |
| 92 | parameters["subPlatform"] = platform.subPlatform |
| 93 | return parameters |
| 94 | } |
| 95 | |
| 96 | } |