Skip to content

Commit 7cb0a45

Browse files
authored
Add logic to handle no birthday in profile (google#134)
* Add logic to handle no birthday in profile
1 parent 0dc783f commit 7cb0a45

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

Samples/Swift/DaysUntilBirthday/Shared/Models/Birthday.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Foundation
1818

1919
/// A model type representing the current user's birthday.
2020
struct Birthday: Decodable {
21-
let integerDate: Birthday.IntegerDate
21+
fileprivate let integerDate: Birthday.IntegerDate
2222

2323
/// The birthday as a `Date`.
2424
var date: Date? {
@@ -30,7 +30,7 @@ struct Birthday: Decodable {
3030
year: currentYear.year,
3131
month: integerDate.month,
3232
day: integerDate.day)
33-
guard let d = comps.date else { return nil }
33+
guard let d = comps.date, comps.isValidDate else { return nil }
3434
if d < now {
3535
var nextYearComponent = DateComponents()
3636
nextYearComponent.year = 1
@@ -39,12 +39,20 @@ struct Birthday: Decodable {
3939
} else {
4040
return d
4141
}
42-
}
42+
}
4343

4444
init(from decoder: Decoder) throws {
4545
let container = try decoder.container(keyedBy: CodingKeys.self)
4646
self.integerDate = try container.decode(IntegerDate.self, forKey: .integerDate)
4747
}
48+
49+
init(integerDate: IntegerDate) {
50+
self.integerDate = integerDate
51+
}
52+
53+
static var noBirthday: Birthday? {
54+
return Birthday(integerDate: IntegerDate(month: .min, day: .min))
55+
}
4856
}
4957

5058
extension Birthday {
@@ -64,7 +72,7 @@ extension Birthday {
6472
extension Birthday: CustomStringConvertible {
6573
/// Converts the instances `date` to a `String`.
6674
var description: String {
67-
return date?.description ?? "NA"
75+
return date?.description ?? "No birthday"
6876
}
6977
}
7078

Samples/Swift/DaysUntilBirthday/Shared/ViewModels/BirthdayViewModel.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ final class BirthdayViewModel: ObservableObject {
2424
@Published private(set) var birthday: Birthday?
2525
/// Computed property calculating the number of days until the current user's birthday.
2626
var daysUntilBirthday: String {
27-
guard let bday = birthday?.date else { return "NA" }
27+
guard let bday = birthday?.date else {
28+
return NSLocalizedString("No birthday", comment: "User has no birthday")
29+
}
2830
let now = Date()
2931
let calendar = Calendar.autoupdatingCurrent
3032
let dayComps = calendar.dateComponents([.day], from: now, to: bday)
31-
guard let days = dayComps.day else { return "NA" }
33+
guard let days = dayComps.day else {
34+
return NSLocalizedString("No birthday", comment: "User has no birthday")
35+
}
3236
return String(days)
3337
}
3438
private var cancellable: AnyCancellable?
@@ -42,6 +46,7 @@ final class BirthdayViewModel: ObservableObject {
4246
case .finished:
4347
break
4448
case .failure(let error):
49+
self.birthday = Birthday.noBirthday
4550
print("Error retrieving birthday: \(error)")
4651
}
4752
} receiveValue: { birthday in

0 commit comments

Comments
 (0)