94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
//
|
|
// SeismicNetworkViewModel.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Andrea Busi on 15/03/21.
|
|
// Copyright © 2021 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
|
|
struct SeismicNetworkViewModel {
|
|
|
|
let place: String
|
|
let network: String
|
|
let isPreliminary: Bool
|
|
let magnitude: String
|
|
let rawMagnitude: Double
|
|
let depth: String
|
|
let time: String
|
|
let distance: String
|
|
let coordinate: String
|
|
let population: String
|
|
let smartphones: String
|
|
let users: String
|
|
|
|
// MARK: - Init
|
|
|
|
init(seismic: EQNSisma) {
|
|
self.place = seismic.place
|
|
self.network = seismic.provider
|
|
self.rawMagnitude = seismic.magnitude.doubleValue
|
|
|
|
let isPreliminary = seismic.preliminary.intValue > 0
|
|
self.isPreliminary = isPreliminary
|
|
if isPreliminary {
|
|
let lowerValue = seismic.magnitude.doubleValue - seismic.magnitudeRange.doubleValue/2.0
|
|
let upperValue = seismic.magnitude.doubleValue + seismic.magnitudeRange.doubleValue/2.0
|
|
self.magnitude = String(format: "%.1f-%.1f\(seismic.magnitudeType)", lowerValue, upperValue)
|
|
self.depth = ""
|
|
} else {
|
|
self.magnitude = String(format: "%.1f%@", seismic.magnitude.doubleValue, seismic.magnitudeType)
|
|
self.depth = String(format: "%.1f km ↓", seismic.depth.doubleValue)
|
|
}
|
|
|
|
// we need to check agains null values, because sometimes WS returns invalid dates
|
|
if let date = seismic.date {
|
|
let formattedDate = EQNUtility.formattedDate(from: date)
|
|
let time = EQNUtility.formattedString(forTimeDifference: Int(seismic.timeDifference))
|
|
self.time = "\(formattedDate) - \(time)"
|
|
} else {
|
|
self.time = "no time available"
|
|
}
|
|
|
|
// distance
|
|
let distanceRounded = Int(round(seismic.userDistance))
|
|
self.distance = String(format: NSLocalizedString("official_distance", comment: ""), distanceRounded)
|
|
let coordinateText = EQNUtility.coordinateString(coordinate: seismic.coordinate.coordinate)
|
|
self.coordinate = "\(coordinateText)"
|
|
|
|
let population = Self.formatPopulation(seismic.population100km)
|
|
self.population = String(format: NSLocalizedString("share_radius100", comment: ""), population)
|
|
|
|
if seismic.smartphoneNumber.intValue > 0 {
|
|
self.smartphones = String(format: NSLocalizedString("official_smartphones", comment: ""), seismic.smartphoneNumber)
|
|
} else {
|
|
self.smartphones = ""
|
|
}
|
|
if seismic.userNumber.intValue > 0 {
|
|
self.users = String(format: NSLocalizedString("official_reports", comment: ""), seismic.userNumber)
|
|
} else {
|
|
self.users = ""
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
/// Format population value (ex. 1.5M, 2.4k)
|
|
private static func formatPopulation(_ population: Double) -> String {
|
|
var populationString = ""
|
|
if population > 999_999 {
|
|
let roundedPopulation = round(population / 100_000) / 10
|
|
populationString = "\(roundedPopulation)M"
|
|
} else if population > 999 {
|
|
let roundedPopulation = round(population / 100) / 10
|
|
populationString = "\(roundedPopulation)K"
|
|
} else {
|
|
let roundedPopulation = round(population)
|
|
populationString = "\(roundedPopulation)"
|
|
}
|
|
return populationString
|
|
}
|
|
}
|