Skip to content

Commit

Permalink
Fixes issue where Int to Float conversion would fail and would not sh…
Browse files Browse the repository at this point in the history
…ow the numbers
  • Loading branch information
marko committed Jul 20, 2021
1 parent 6c1d02b commit e6c407b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Infected.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
ADE73A36254B612900C9FBD4 /* TrendNumberView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE73A35254B612900C9FBD4 /* TrendNumberView.swift */; };
ADE73A75254D7C8300C9FBD4 /* NumbersProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD327F2B2546332E002CCE01 /* NumbersProvider.swift */; };
ADE73A8F254DDEB700C9FBD4 /* NumberRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE73A8E254DDEB700C9FBD4 /* NumberRepresentation.swift */; };
ADE7FE1526A7832D00AB3A63 /* Number.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE7FE1426A7832D00AB3A63 /* Number.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -127,6 +128,7 @@
ADE4EDE625604F530035CBE5 /* CloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloseButton.swift; sourceTree = "<group>"; };
ADE73A35254B612900C9FBD4 /* TrendNumberView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrendNumberView.swift; sourceTree = "<group>"; };
ADE73A8E254DDEB700C9FBD4 /* NumberRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NumberRepresentation.swift; sourceTree = "<group>"; };
ADE7FE1426A7832D00AB3A63 /* Number.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Number.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -236,6 +238,7 @@
ADD01CD825FE39BD0015748D /* VaccinationsInfoView.swift */,
AD288AB82648279400F9F708 /* HomeAdmissionsInfoView.swift */,
ADB726AB257C0FC800AB2DB1 /* WatchlistKeeper.swift */,
ADE7FE1426A7832D00AB3A63 /* Number.swift */,
ADD4C309251627920064A959 /* Assets.xcassets */,
AD131CC02559D393003077F3 /* Localizable.strings */,
ADD4C30B251627920064A959 /* Preview Content */,
Expand Down Expand Up @@ -434,6 +437,7 @@
AD638D8E258FAF6000EDE19B /* LinkView.swift in Sources */,
AD82EB3D25A24D0200864A78 /* ViewControllerResolver.swift in Sources */,
AD82EB3425A24CD200864A78 /* SearchBar.swift in Sources */,
ADE7FE1526A7832D00AB3A63 /* Number.swift in Sources */,
ADE10EFC254F45930080B975 /* GroupedRegionsView.swift in Sources */,
AD74CCCC255DF8B500002500 /* SectionHeader.swift in Sources */,
AD74495025A1325C00C7A658 /* HospitalAdmissionsInfoView.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions Infected/Number.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Number.swift
// Infected
//
// Created by marko on 7/21/21.
//

import Foundation

enum Number {
case integer(Int)
case decimal(Float)

var intValue: Int {
switch self {
case let .integer(value):
return value
case let .decimal(value):
return Int(value)
}
}

var floatValue: Float {
switch self {
case let .integer(value):
return Float(value)
case let .decimal(value):
return value
}
}
}
42 changes: 21 additions & 21 deletions Infected/RowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct RowView: View {
private struct DataPointView: View {

let titleKey: LocalizedStringKey
let number: Float?
let number: Number?
let trend: Int?
let numberStyle: NumberStyle
let isPositiveTrendUp: Bool
Expand Down Expand Up @@ -158,10 +158,10 @@ struct RowView: View {
return formatter
}()

let number: Float?
let number: Number?
let style: NumberStyle

init(number: Float?, style: NumberStyle) {
init(number: Number?, style: NumberStyle) {
self.number = number
self.style = style
}
Expand All @@ -172,11 +172,11 @@ struct RowView: View {
}
switch style {
case .integer, .decimal:
return Self.numberFormatter.string(for: number) ?? "--"
return Self.numberFormatter.string(for: number.intValue) ?? "--"
case .percent:
return Self.percentNumberFormatter.string(for: number) ?? "--"
return Self.percentNumberFormatter.string(for: number.floatValue) ?? "--"
case .decimalExtendedFraction:
return Self.decimalExtendedFractionFormatter.string(for: number) ?? "--"
return Self.decimalExtendedFractionFormatter.string(for: number.floatValue) ?? "--"
}
}

Expand All @@ -194,7 +194,7 @@ struct RowView: View {
HStack(spacing: 8) {
DataPointView(
titleKey: "New",
number: numbers.new.flatMap(Float.init),
number: numbers.new.flatMap { .integer($0) },
trend: numbers.trend,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -204,7 +204,7 @@ struct RowView: View {
if let per100K = numbers.per100KInhabitants {
DataPointView(
titleKey: "Per 100k",
number: per100K,
number: .decimal(per100K),
trend: nil,
numberStyle: .decimal,
isPositiveTrendUp: false
Expand All @@ -213,7 +213,7 @@ struct RowView: View {
}
DataPointView(
titleKey: "Total",
number: numbers.total.flatMap(Float.init),
number: numbers.total.flatMap { .integer($0) },
trend: nil,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -231,7 +231,7 @@ struct RowView: View {
HStack(alignment: .top) {
DataPointView(
titleKey: "New",
number: numbers.new.flatMap(Float.init),
number: numbers.new.flatMap { Number.integer($0) },
trend: numbers.trend,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -241,7 +241,7 @@ struct RowView: View {
Divider()
DataPointView(
titleKey: "Per 100k",
number: per100K,
number: .decimal(per100K),
trend: nil,
numberStyle: .decimal,
isPositiveTrendUp: false
Expand All @@ -251,7 +251,7 @@ struct RowView: View {
Divider()
DataPointView(
titleKey: "Total",
number: numbers.total.flatMap(Float.init),
number: numbers.total.flatMap { Number.integer($0) },
trend: nil,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -262,15 +262,15 @@ struct RowView: View {
HStack {
DataPointView(
titleKey: "Total",
number: numbers.total.flatMap(Float.init),
number: numbers.total.flatMap { Number.integer($0) },
trend: nil,
numberStyle: .integer,
isPositiveTrendUp: false
)
Divider()
DataPointView(
titleKey: "Reproduction Number",
number: reproductionNumber,
number: .decimal(reproductionNumber),
trend: nil,
numberStyle: .decimalExtendedFraction,
isPositiveTrendUp: false
Expand All @@ -291,7 +291,7 @@ struct RowView: View {
HStack(spacing: 8) {
DataPointView(
titleKey: "New",
number: occupancy.newAdmissions.flatMap(Float.init),
number: occupancy.newAdmissions.flatMap { Number.integer($0) },
trend: occupancy.newAdmissionsTrend,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -300,7 +300,7 @@ struct RowView: View {
Divider()
DataPointView(
titleKey: representation == .homeAdmissions ? "Active" : "Occupied Beds",
number: occupancy.currentlyOccupied.flatMap(Float.init),
number: occupancy.currentlyOccupied.flatMap { Number.integer($0) },
trend: occupancy.currentlyOccupiedTrend,
numberStyle: .integer,
isPositiveTrendUp: false
Expand All @@ -310,7 +310,7 @@ struct RowView: View {
Divider()
DataPointView(
titleKey: "Per 100k",
number: per100K,
number: .decimal(per100K),
trend: nil,
numberStyle: .decimal,
isPositiveTrendUp: false
Expand All @@ -330,7 +330,7 @@ struct RowView: View {
if let new = vaccinations.new {
DataPointView(
titleKey: "New Doses",
number: Float(new),
number: .integer(new),
trend: nil,
numberStyle: .integer,
isPositiveTrendUp: true
Expand All @@ -341,7 +341,7 @@ struct RowView: View {
if let average = vaccinations.average {
DataPointView(
titleKey: "New (7 day average)",
number: Float(average),
number: .integer(average),
trend: vaccinations.trend,
numberStyle: .integer,
isPositiveTrendUp: true
Expand All @@ -352,7 +352,7 @@ struct RowView: View {
HStack {
DataPointView(
titleKey: "Total Doses",
number: vaccinations.total.flatMap(Float.init),
number: vaccinations.total.flatMap { Number.integer($0) },
trend: nil,
numberStyle: .integer,
isPositiveTrendUp: true
Expand All @@ -361,7 +361,7 @@ struct RowView: View {
Divider()
DataPointView(
titleKey: "Coverage",
number: vaccinations.percentageOfPopulation,
number: vaccinations.percentageOfPopulation.flatMap { Number.decimal($0) },
trend: nil,
numberStyle: .percent,
isPositiveTrendUp: true
Expand Down

0 comments on commit e6c407b

Please sign in to comment.