Implemented the DateAdapter and the SizeAdapter adapter for the Browse module.
This commit is contained in:
parent
74b40ae5c3
commit
6b1089235a
49
Modules/Sources/Browse/Logic/Adapters/DateAdapter.swift
Normal file
49
Modules/Sources/Browse/Logic/Adapters/DateAdapter.swift
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
//
|
||||||
|
// DateAdapter.swift
|
||||||
|
// Browse
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 15/12/2022.
|
||||||
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct DateAdapter {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
private let dateFormatter: DateFormatter = .dateTimeFormatter
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
func callAsFunction(value: Date?) -> String {
|
||||||
|
if let value {
|
||||||
|
return dateFormatter.string(from: value)
|
||||||
|
} else {
|
||||||
|
return .Constants.noValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - DateFormatter+Formats
|
||||||
|
|
||||||
|
private extension DateFormatter {
|
||||||
|
static let dateTimeFormatter = {
|
||||||
|
let formatter = DateFormatter()
|
||||||
|
|
||||||
|
formatter.dateStyle = .long
|
||||||
|
formatter.timeStyle = .short
|
||||||
|
formatter.locale = .current
|
||||||
|
|
||||||
|
return formatter
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - String+Constants
|
||||||
|
|
||||||
|
private extension String {
|
||||||
|
enum Constants {
|
||||||
|
static let noValue = "-"
|
||||||
|
}
|
||||||
|
}
|
54
Modules/Sources/Browse/Logic/Adapters/SizeAdapter.swift
Normal file
54
Modules/Sources/Browse/Logic/Adapters/SizeAdapter.swift
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
//
|
||||||
|
// 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 }
|
||||||
|
|
||||||
|
var sizeInBytes = Measurement(
|
||||||
|
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 = "-"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user