Files
2022-03-10 11:51:50 +00:00

58 lines
1.2 KiB
Swift

//
// 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<UInt64>()
// 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
}
}