// // TrafficUsage.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 12.02.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation final class TrafficUsage { enum Unit { static let byte: UInt64 = 1 static let kiloByte = byte * 1000 static let megaByte = kiloByte * 1000 static let gigaByte = megaByte * 1000 } static let key = "TrafficUsage" static var instance = TrafficUsage() private var bytesCount: UInt64 { get { UserDefaults.standard.value(forKey: Self.key) as? UInt64 ?? 0 } set { UserDefaults.standard.setValue(newValue, forKey: Self.key) self.bytesCountEmitter.invoke(newValue) // print("Traffic usage: \(newValue)") } } let bytesCountEmitter = Emitter() // MARK: - Init private init() { } // MARK: - Interface func get() -> UInt64 { return self.bytesCount } func add(bytes: UInt64) { self.bytesCount += bytes } func set(bytes: UInt64) { self.bytesCount = bytes } func reset() { self.bytesCount = 0 } }