195 lines
8.2 KiB
Swift
195 lines
8.2 KiB
Swift
//
|
|
// MainService.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 05.03.2021.
|
|
// Copyright © 2021 AM. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Alamofire
|
|
import EosioSwift
|
|
import WalletNetwork
|
|
import class Resolver.ResolverScope
|
|
import struct Resolver.Injected
|
|
|
|
final class RuntimeConfiguration {
|
|
var serverTimeDiff: Double = 0
|
|
var currentTimestamp: Double { Date().timeIntervalSince1970 + serverTimeDiff }
|
|
var dealsActiveOnly = true
|
|
}
|
|
|
|
extension Main {
|
|
final class Service {
|
|
|
|
struct Socket2Msg: Decodable {
|
|
let payload: Socket2MsgPayload
|
|
|
|
init(json: String) throws {
|
|
self = try JSONDecoder().decode(Self.self, from: Data(json.utf8))
|
|
}
|
|
}
|
|
struct Socket2MsgPayload: Decodable {
|
|
let data: Socket2MsgPayloadData
|
|
}
|
|
struct Socket2MsgPayloadData: Decodable {
|
|
let notifications: Socket2MsgNotification
|
|
}
|
|
struct Socket2MsgNotification: Decodable {
|
|
let notificationType: String
|
|
let state: String
|
|
let payload: String
|
|
let previousUid: String
|
|
let currentUid: String
|
|
|
|
var order: P2POrder? {
|
|
try? JSONDecoder().decode(P2POrder.self, from: Data(payload.utf8))
|
|
}
|
|
var deal: P2PDeal? {
|
|
try? JSONDecoder().decode(P2PDeal.self, from: Data(payload.utf8))
|
|
}
|
|
var balance: P2PBalance? {
|
|
try? JSONDecoder().decode(P2PBalance.self, from: Data(payload.utf8))
|
|
}
|
|
}
|
|
|
|
@Injected var runtimeConfiguration: RuntimeConfiguration
|
|
@Injected var ordersService: P2POrdersService
|
|
@Injected var dealsService: P2PMyDealsService
|
|
@Injected var p2pBalancesService: P2PBalancesService
|
|
@Injected var p2pListsService: P2PListsService
|
|
|
|
init() {
|
|
self.loadServerTime()
|
|
Accounts().messageShelf.activeBook?.fetch()
|
|
ResolverScope.userSession.reset()
|
|
self.p2pListsService.fetch(completion: { _ in })
|
|
}
|
|
|
|
var previousUid: String?
|
|
|
|
func processSocket(info: [AnyHashable: Any]?) {
|
|
guard let info = info as? [String: String] else {
|
|
return
|
|
}
|
|
if let json = info["data"] {
|
|
if let socketMsg = try? Socket2Msg(json: json) {
|
|
// print(previousUid)
|
|
// print(socketMsg.payload.data.notifications.previousUid)
|
|
// print(socketMsg.payload.data.notifications.currentUid)
|
|
|
|
if let previousUid = previousUid,
|
|
previousUid != socketMsg.payload.data.notifications.previousUid {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
previousUid = socketMsg.payload.data.notifications.currentUid
|
|
|
|
switch socketMsg.payload.data.notifications.notificationType.uppercased() {
|
|
case "P2P_ORDERS":
|
|
switch socketMsg.payload.data.notifications.state.uppercased() {
|
|
case "CREATED", "UPDATED":
|
|
if !ordersService.updateOrCreate(order: socketMsg.payload.data.notifications.order) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
case "DELETED":
|
|
if !ordersService.delete(order: socketMsg.payload.data.notifications.order) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
case "P2P_DEALS":
|
|
switch socketMsg.payload.data.notifications.state.uppercased() {
|
|
case "CREATED", "UPDATED":
|
|
if !dealsService.updateOrCreate(deal: socketMsg.payload.data.notifications.deal) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
case "DELETED":
|
|
if !dealsService.delete(deal: socketMsg.payload.data.notifications.deal) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
case "P2P_I_PAID":
|
|
if !dealsService.updateOrCreate(deal: socketMsg.payload.data.notifications.deal) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
case "P2P_DEPOSITS":
|
|
if !p2pBalancesService.updateOrCreate(balance: socketMsg.payload.data.notifications.balance) {
|
|
reloadP2PData(username: Accounts().current?.name ?? "")
|
|
}
|
|
case "CHATS":
|
|
Accounts().messageShelf.activeBook?.fetch()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func reloadP2PData(username: String) {
|
|
// ordersService.fetch(username: username) { _ in }
|
|
// dealsService.fetch(username: username) { _ in }
|
|
// p2pBalancesService.fetch(username: username) { _ in }
|
|
|
|
ordersService.fetch(username: username) {
|
|
if $0 == nil {
|
|
self.dealsService.fetch(username: username) {
|
|
if $0 == nil {
|
|
self.p2pBalancesService.fetch(username: username) {
|
|
if $0 == nil {
|
|
} else {
|
|
delayed(5, { self.reloadP2PData(username: username) })
|
|
}
|
|
}
|
|
} else {
|
|
delayed(5, { self.reloadP2PData(username: username) })
|
|
}
|
|
}
|
|
} else {
|
|
delayed(5, { self.reloadP2PData(username: username) })
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadServerTime() {
|
|
let environment = ApplicationEnvironment.shared().current
|
|
|
|
guard let node = environment.node,
|
|
let url = URL(string: node) else {
|
|
return
|
|
}
|
|
|
|
EosioRpcProvider(endpoint: url, headers: environment.headers).getInfo(completion: { [weak self] result in
|
|
switch result {
|
|
case let .success(response):
|
|
// self?.runtimeConfiguration.serverTime = response.headBlockTime.asISODate() ?? Date()
|
|
self?.runtimeConfiguration.serverTimeDiff = (response.headBlockTime.asISODate() ?? Date()).timeIntervalSince1970 - Date().timeIntervalSince1970
|
|
case .failure:
|
|
break
|
|
}
|
|
})
|
|
}
|
|
|
|
func loadNews() async -> [GetNewsGraphQLRequest.News] {
|
|
let variables = GetNewsGraphQLRequest.Variables(langCode: Common.Model.Language.current.rawValue)
|
|
|
|
let environment = ApplicationEnvironment.shared().current
|
|
let news = try? await AF.request(
|
|
environment.backend.urlPath,
|
|
method: .post,
|
|
parameters: GetNewsGraphQLRequest(variables: variables),
|
|
encoder: JSONParameterEncoder.default,
|
|
headers: HTTPHeaders(environment.headers)
|
|
)
|
|
.serializingDecodable(GraphQLResponse<GetNewsGraphQLRequest.ResponseData>.self)
|
|
.value.data?.getNews ?? []
|
|
|
|
print((news ?? []).filter { $0.deviceType.lowercased() == "ios" || $0.deviceType.lowercased() == "any" })
|
|
|
|
return news?.filter({ $0.deviceType.lowercased() == "ios" || $0.deviceType.lowercased() == "any" }) ?? []
|
|
}
|
|
}
|
|
}
|