2022-12-16 00:09:50 +01:00
|
|
|
//
|
|
|
|
// SizeAdapter.swift
|
|
|
|
// Browse
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 15/12/2022.
|
|
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct SizeAdapter {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
private let measurementFormatter: MeasurementFormatter = .informationSizeFormatter
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func callAsFunction(value: Int?) -> String {
|
|
|
|
guard let value else { return .Constants.noValue }
|
|
|
|
|
2022-12-16 00:10:42 +01:00
|
|
|
let sizeInBytes = Measurement(
|
2022-12-16 00:09:50 +01:00
|
|
|
value: Double(value),
|
|
|
|
unit: UnitInformationStorage.bytes
|
|
|
|
)
|
|
|
|
|
|
|
|
return measurementFormatter.string(
|
|
|
|
from: sizeInBytes.converted(to: .megabytes)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - DateFormatter+Formats
|
|
|
|
|
|
|
|
private extension MeasurementFormatter {
|
|
|
|
static let informationSizeFormatter = {
|
|
|
|
let formatter = MeasurementFormatter()
|
|
|
|
|
|
|
|
formatter.unitStyle = .medium
|
|
|
|
formatter.numberFormatter.maximumFractionDigits = 2
|
|
|
|
formatter.locale = .current
|
|
|
|
|
|
|
|
return formatter
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - String+Constants
|
|
|
|
|
|
|
|
private extension String {
|
|
|
|
enum Constants {
|
|
|
|
static let noValue = "-"
|
|
|
|
}
|
|
|
|
}
|