Files

58 lines
1.2 KiB
Swift

//
// TrafficUsage.swift
// PrivadoVPN
//
// Created by Juraldinio on 4/19/21.
// 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"
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)
}
}
let bytesCountEmitter = Emitter<UInt64>()
// MARK: - Init
static func createTrafficUsage() -> TrafficUsage { return TrafficUsage() }
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
}
}