refactor: Add extension to Dictionary to simplify values retrieve

This commit is contained in:
Andrea Busi
2021-03-27 17:49:47 +01:00
parent f437c3cde6
commit 5dc64c4f77
3 changed files with 38 additions and 6 deletions
@@ -131,6 +131,7 @@
65DBFB7425E2BBF20041CBA6 /* GADTMediumTemplateView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 65DBFB6F25E2BBF20041CBA6 /* GADTMediumTemplateView.xib */; };
65DBFB7525E2BBF20041CBA6 /* GADTMediumTemplateView.m in Sources */ = {isa = PBXBuildFile; fileRef = 65DBFB7125E2BBF20041CBA6 /* GADTMediumTemplateView.m */; };
65DBFB7625E2BBF20041CBA6 /* GADTTemplateView.m in Sources */ = {isa = PBXBuildFile; fileRef = 65DBFB7225E2BBF20041CBA6 /* GADTTemplateView.m */; };
65E1B19B260F980600A0ACBA /* Dictionary+EQNExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E1B19A260F980600A0ACBA /* Dictionary+EQNExtensions.swift */; };
8C10B0B92281FE7F00125C9F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8C10B0BD2281FE7F00125C9F /* Localizable.strings */; };
8C10B0BA2281FE7F00125C9F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8C10B0BD2281FE7F00125C9F /* Localizable.strings */; };
8C10B0BB2281FE7F00125C9F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8C10B0BD2281FE7F00125C9F /* Localizable.strings */; };
@@ -402,6 +403,7 @@
65DBFB7125E2BBF20041CBA6 /* GADTMediumTemplateView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADTMediumTemplateView.m; sourceTree = "<group>"; };
65DBFB7225E2BBF20041CBA6 /* GADTTemplateView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADTTemplateView.m; sourceTree = "<group>"; };
65DBFB7325E2BBF20041CBA6 /* GADTMediumTemplateView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADTMediumTemplateView.h; sourceTree = "<group>"; };
65E1B19A260F980600A0ACBA /* Dictionary+EQNExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+EQNExtensions.swift"; sourceTree = "<group>"; };
8C10B0AF2281FBE800125C9F /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Main.strings; sourceTree = "<group>"; };
8C10B0B02281FBE800125C9F /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/LaunchScreen.strings; sourceTree = "<group>"; };
8C10B0B12281FBE800125C9F /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/MainInterface.strings; sourceTree = "<group>"; };
@@ -734,6 +736,7 @@
DC105640251E7ECE002579BB /* UIFont+Extensions.swift */,
8C7A3B65225A5EA40045B266 /* NSDictionary+EQNExtensions.h */,
8C7A3B64225A5EA30045B266 /* NSDictionary+EQNExtensions.m */,
65E1B19A260F980600A0ACBA /* Dictionary+EQNExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
@@ -1565,6 +1568,7 @@
DCBB84F0252CFC4600F12633 /* AlertsNoLocationTableViewCell.swift in Sources */,
DCD3E3C024D15576007C78D4 /* PurchaseProVersionViewController.swift in Sources */,
651901B925F5358700CAFF20 /* EQNMapAnnotationSeismic.swift in Sources */,
65E1B19B260F980600A0ACBA /* Dictionary+EQNExtensions.swift in Sources */,
DC99A50324E66E270071BC9F /* EQNCommandProtocol.swift in Sources */,
DCB45BC8250E86E100DB2D0C /* SeismicSettingsViewController.swift in Sources */,
DCF10DC624D2B8C7009F34C3 /* EQNPurchaseUtility.swift in Sources */,
@@ -62,10 +62,7 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa
let aps = notification["aps"] as? [String: Any],
let alert = aps["alert"] as? [String: Any] else { return }
var intensity = -1
if let intensityString = notification["intensity"] as? String, let anIntensity = Int(intensityString) {
intensity = anIntensity
}
let intensity = notification.eqn_intValue(for: "intensity") ?? 0
containerView.backgroundColor = color(for: intensity)
if let title = alert["loc-key"] as? String, let args = alert["loc-args"] as? [String], let arg = args.first {
@@ -74,8 +71,8 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa
// get coordinate
var coordinate: CLLocation?
if let latitudeString = notification["latitude"] as? String, let latitude = Double(latitudeString),
let longitudeString = notification["longitude"] as? String, let longitude = Double(longitudeString) {
if let latitude = notification.eqn_doubleValue(for: "latitude"),
let longitude = notification.eqn_doubleValue(for: "longitude") {
coordinate = CLLocation(latitude: latitude, longitude: longitude)
}
@@ -0,0 +1,31 @@
//
// Dictionary+EQNExtensions.swift
// Earthquake Network
//
// Created by Andrea Busi on 27/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import Foundation
extension Dictionary {
func eqn_intValue(for key: Key) -> Int? {
if let value = self[key] as? Int {
return value
} else if let stringValue = self[key] as? String, let value = Int(stringValue) {
return value
}
return nil
}
func eqn_doubleValue(for key: Key) -> Double? {
if let value = self[key] as? Double {
return value
} else if let stringValue = self[key] as? String, let value = Double(stringValue) {
return value
}
return nil
}
}