68 lines
1.8 KiB
Swift
68 lines
1.8 KiB
Swift
//
|
|
// AppSettingsModel.swift
|
|
// Malinka
|
|
//
|
|
// Created by user on 21.11.2022.
|
|
// Copyright © 2022 NUT.Tech. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct AppSettingsModel: Decodable {
|
|
|
|
enum ExchangeType: Decodable {
|
|
|
|
case uninmplemented(String)
|
|
case form
|
|
case url(String)
|
|
|
|
// MARK: - Decodable
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case wbtn = "wbtn"
|
|
case wurl = "wurl"
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
let type = try container.decodeIfPresent(String.self, forKey: .wbtn)
|
|
let url = try container.decodeIfPresent(String.self, forKey: .wurl)
|
|
|
|
guard let type else {
|
|
self = ExchangeType.form
|
|
return
|
|
}
|
|
|
|
switch type {
|
|
case "form": self = ExchangeType.form
|
|
case "url":
|
|
if let url {
|
|
self = ExchangeType.url(url)
|
|
} else {
|
|
self = ExchangeType.form
|
|
}
|
|
default: self = ExchangeType.uninmplemented(type)
|
|
}
|
|
}
|
|
}
|
|
|
|
let isSwapActive: Bool
|
|
let exchangeType: ExchangeType
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case isSwapActive = "is_swap_active"
|
|
}
|
|
|
|
init() {
|
|
self.isSwapActive = true
|
|
self.exchangeType = .form
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
self.isSwapActive = (try? container.decode(Bool.self, forKey: .isSwapActive)) ?? true
|
|
self.exchangeType = try ExchangeType(from: decoder)
|
|
}
|
|
}
|