Skip to content

Commit 80c09cf

Browse files
authored
Merge pull request #9 from r-token/improvement/preserve-country-code
Preserve the Country Code in Phone Numbers
2 parents 4110020 + 845f0d3 commit 80c09cf

File tree

6 files changed

+65
-44
lines changed

6 files changed

+65
-44
lines changed

CatchUp-SwiftUI.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
"$(inherited)",
505505
"@executable_path/Frameworks",
506506
);
507-
MARKETING_VERSION = 3.1.2;
507+
MARKETING_VERSION = 3.1.3;
508508
PRODUCT_BUNDLE_IDENTIFIER = com.tokensolutions.CatchUp;
509509
PRODUCT_NAME = CatchUp;
510510
SUPPORTS_MACCATALYST = NO;
@@ -532,7 +532,7 @@
532532
"$(inherited)",
533533
"@executable_path/Frameworks",
534534
);
535-
MARKETING_VERSION = 3.1.2;
535+
MARKETING_VERSION = 3.1.3;
536536
PRODUCT_BUNDLE_IDENTIFIER = com.tokensolutions.CatchUp;
537537
PRODUCT_NAME = CatchUp;
538538
SUPPORTS_MACCATALYST = NO;

CatchUp-SwiftUI/Utilities/ContactHelper.swift

+3-11
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,19 @@ struct ContactHelper {
168168
var anniversaryString: String
169169

170170
let anniversary = contact.dates.filter { date -> Bool in
171-
172171
guard let label = date.label else {
173172
return false
174173
}
175-
176174
return label.contains("Anniversary")
177-
178-
} .first?.value as DateComponents?
175+
} .first?.value as DateComponents?
179176

180-
if anniversary?.date != nil {
181-
177+
if let anniversaryDate = anniversary?.date {
182178
let formatter = DateFormatter()
183179
formatter.dateFormat = "MM-dd"
184180
formatter.locale = Locale(identifier: "en_US_POSIX")
185181
formatter.timeZone = TimeZone(secondsFromGMT: 0)
186-
187-
anniversaryString = formatter.string(from: (anniversary?.date!)!)
188-
189-
let anniversaryDate = formatter.date(from: anniversaryString)!
182+
190183
anniversaryString = formatter.string(from: anniversaryDate)
191-
192184
} else {
193185
anniversaryString = ""
194186
}

CatchUp-SwiftUI/Utilities/Conversions.swift

+29-22
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ import PhoneNumberKit
1212

1313
struct Converter {
1414
// MARK: Only used in DetailScreen
15-
static func getFormattedPhoneNumber(from phoneNumber: String) -> String {
16-
let phoneNumberKit = PhoneNumberKit()
17-
15+
static func getFormattedPhoneNumber(from phoneNumber: String, with phoneNumberKit: PhoneNumberKit) -> String {
1816
print("formatting phone number: \(phoneNumber)")
1917

2018
do {
2119
let parsedPhoneNumber = try phoneNumberKit.parse(phoneNumber.trimmingCharacters(in: .whitespacesAndNewlines))
22-
let formattedPhoneNumber = phoneNumberKit.format(parsedPhoneNumber, toType: .national)
20+
let formattedPhoneNumber = phoneNumberKit.format(parsedPhoneNumber, toType: .international)
2321
return formattedPhoneNumber
24-
}
25-
catch {
26-
print("PhoneNumberKit Parse Error: \(error)")
22+
} catch {
23+
print("PhoneNumberKit parse error: \(error)")
2724
return phoneNumber
2825
}
2926
}
3027

31-
static func getTappablePhoneNumber(from phoneNumber: String) -> URL {
28+
static func getTappablePhoneNumber(from phoneNumber: String) -> URL? {
3229
print("getting tappable phone number: \(phoneNumber)")
3330

3431
let tel = "tel://"
3532
let cleanNumber = phoneNumber.replacingOccurrences(of: "[^\\d+]", with: "", options: [.regularExpression])
3633
let formattedString = tel + cleanNumber
37-
let tappableNumber = URL(string: formattedString)!
38-
39-
return tappableNumber
34+
35+
if let tappableNumber = URL(string: formattedString) {
36+
return tappableNumber
37+
} else {
38+
return nil
39+
}
4040
}
4141

4242
static func getTappableEmail(from emailAddress: String) -> URL {
@@ -97,14 +97,21 @@ struct Converter {
9797
}
9898

9999

100-
// MARK: Used in HomeScreen and DetailScreen
101-
100+
// MARK: Used in ContactPictureView and DetailScreen
101+
102102
static func getContactPicture(from string: String) -> Image {
103-
let imageData = NSData(base64Encoded: string)
104-
let uiImage = UIImage(data: imageData! as Data)!
105-
let image = Image(uiImage: uiImage)
106-
107-
return image
103+
if let imageData = NSData(base64Encoded: string) {
104+
if let uiImage = UIImage(data: imageData as Data) {
105+
let image = Image(uiImage: uiImage)
106+
return image
107+
} else {
108+
let image = Image(uiImage: UIImage(named: "DefaultPhoto")!)
109+
return image
110+
}
111+
} else {
112+
let image = Image(uiImage: UIImage(named: "DefaultPhoto")!)
113+
return image
114+
}
108115
}
109116

110117
static func convertNotificationPreferenceToString(contact: SelectedContact) -> String {
@@ -324,10 +331,10 @@ struct Converter {
324331
dateComponents.day = Int(contact.notification_preference_custom_day)
325332
dateComponents.year = Int(contact.notification_preference_custom_year)
326333

327-
let dateTime = Calendar.current.date(from: dateComponents)
328-
let dateString = formatter.string(from: dateTime!)
329-
let dayDate = formatter.date(from: dateString)!
330-
334+
guard let dateTime = Calendar.current.date(from: dateComponents) else { return "Unknown" }
335+
let dateString = formatter.string(from: dateTime)
336+
guard let dayDate = formatter.date(from: dateString) else { return "Unknown" }
337+
331338
day = formatter.string(from: dayDate)
332339
year = String(contact.notification_preference_custom_year)
333340

CatchUp-SwiftUI/Views/DetailScreen Subviews/ContactInfoView.swift

+22-6
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@
77
//
88

99
import MapKit
10+
import PhoneNumberKit
1011
import SwiftUI
1112

1213
struct ContactInfoView: View {
1314
@State private var isShowingEmailAlert = false
1415
@State private var emailString = ""
1516
@State private var emailUrlForAlert: URL?
17+
@State private var isShowingInvalidPhoneNumberAlert = false
1618

19+
let phoneNumberKit = PhoneNumberKit()
1720
let contact: SelectedContact
1821

1922
var formattedPrimaryPhoneNumber: String {
20-
Converter.getFormattedPhoneNumber(from: contact.phone)
23+
Converter.getFormattedPhoneNumber(from: contact.phone, with: phoneNumberKit)
2124
}
2225

2326
var formattedSecondaryPhoneNumber: String {
24-
Converter.getFormattedPhoneNumber(from: contact.secondary_phone)
27+
Converter.getFormattedPhoneNumber(from: contact.secondary_phone, with: phoneNumberKit)
2528
}
2629

27-
var tappablePrimaryPhoneNumber: URL {
30+
var tappablePrimaryPhoneNumber: URL? {
2831
Converter.getTappablePhoneNumber(from: contact.phone)
2932
}
3033

31-
var tappableSecondaryPhoneNumber: URL {
34+
var tappableSecondaryPhoneNumber: URL? {
3235
Converter.getTappablePhoneNumber(from: contact.secondary_phone)
3336
}
3437

@@ -47,10 +50,19 @@ struct ContactInfoView: View {
4750
.font(.caption)
4851

4952
Button(formattedPrimaryPhoneNumber) {
50-
UIApplication.shared.open(tappablePrimaryPhoneNumber)
53+
if let tappablePrimaryPhoneNumber {
54+
UIApplication.shared.open(tappablePrimaryPhoneNumber)
55+
} else {
56+
isShowingInvalidPhoneNumberAlert = true
57+
}
5158
}
5259
.foregroundStyle(.blue)
5360
}
61+
.alert("Phone number is invalid", isPresented: $isShowingInvalidPhoneNumberAlert, actions: {
62+
Button("OK", role: .cancel) {}
63+
}, message: {
64+
Text("Could not dial this phone number. Ensure the number is correct in your Contacts app.")
65+
})
5466
}
5567

5668
if contact.hasSecondaryPhone() {
@@ -59,7 +71,11 @@ struct ContactInfoView: View {
5971
.font(.caption)
6072

6173
Button(formattedSecondaryPhoneNumber) {
62-
UIApplication.shared.open(tappableSecondaryPhoneNumber)
74+
if let tappableSecondaryPhoneNumber {
75+
UIApplication.shared.open(tappableSecondaryPhoneNumber)
76+
} else {
77+
isShowingInvalidPhoneNumberAlert = true
78+
}
6379
}
6480
.foregroundStyle(.blue)
6581
}

CatchUp-SwiftUI/Views/DetailScreen Subviews/NotificationPreferenceView.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct NotificationPreferenceView: View {
2323
@State private var whatDayText = ""
2424

2525
@Bindable var contact: SelectedContact
26+
@Binding var shouldSetPreferenceViewState: Bool
2627

2728
let notificationOptions: [NotificationOption] = NotificationOption.allCases
2829
let dayOptions: [DayOption] = DayOption.allCases
@@ -82,7 +83,10 @@ struct NotificationPreferenceView: View {
8283
}
8384
}
8485
.onAppear {
85-
setInitialState()
86+
if shouldSetPreferenceViewState {
87+
shouldSetPreferenceViewState = false
88+
setInitialState()
89+
}
8690
}
8791

8892
.onChange(of: contact) {
@@ -211,5 +215,5 @@ struct NotificationPreferenceView: View {
211215
}
212216

213217
#Preview {
214-
NotificationPreferenceView(contact: SelectedContact.sampleData)
218+
NotificationPreferenceView(contact: SelectedContact.sampleData, shouldSetPreferenceViewState: .constant(true))
215219
}

CatchUp-SwiftUI/Views/DetailScreen.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ struct DetailScreen: View {
1212
@Environment(DataController.self) var dataController
1313
@Bindable var contact: SelectedContact
1414

15+
@State private var shouldSetPreferenceViewState = true
16+
1517
var nextCatchUpTime: String {
1618
print("recalculating nextCatchUpTime")
1719
return ContactHelper.getFriendlyNextCatchUpTime(for: contact, forQuarterlyPreference: false)
@@ -35,7 +37,7 @@ struct DetailScreen: View {
3537
BirthdayOrAnniversaryRow(contact: contact)
3638
}
3739
Section("Notification Preference") {
38-
NotificationPreferenceView(contact: contact)
40+
NotificationPreferenceView(contact: contact, shouldSetPreferenceViewState: $shouldSetPreferenceViewState)
3941
}
4042

4143
if contact.hasContactInfo() {

0 commit comments

Comments
 (0)