Files
2022-12-16 13:06:02 +03:00

87 lines
2.3 KiB
Swift

//
// AppSettingsModel.swift
// Malinka
//
// Created by Juraldinio on 12/16/22.
// Copyright © 2022 NUT.Tech. All rights reserved.
//
import Foundation
struct AppSettingsModel: Decodable {
static private var instance: AppSettingsModel?
enum ExchangeType: Decodable {
case uninmplemented(String)
case form
case url(String)
// MARK: - Decodable
private enum CodingKeys: String, CodingKey {
case wbtn
case 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"
}
private 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)
}
static func shared(with data: Data? = nil) -> Self {
if let model = Self.instance { return model }
if let data,
let model = try? JSONDecoder().decode(AppSettingsModel.self, from: data) {
Self.instance = model
return model
}
let model = AppSettingsModel()
Self.instance = model
return model
}
}