Skip to content

Commit 858d64e

Browse files
committed
Merge branch 'master' into merge/hotfix_2.6.1_master
2 parents 5afa78d + ac25769 commit 858d64e

12 files changed

Lines changed: 454 additions & 354 deletions

Examples/GroceryExpress/AddressSelectionViewController.swift

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class AddressSelectionViewController: UIViewController {
1616
/// The result of the search
1717
struct Result {
1818
let placemark: MKPlacemark
19-
let formattedAddress: String
19+
let formattedAddress: String?
2020
}
2121

2222
// MARK: - Public
@@ -29,6 +29,7 @@ final class AddressSelectionViewController: UIViewController {
2929
private var resultSearchController: UISearchController? = nil
3030
private let locationManager = CLLocationManager()
3131
private var selectedItem: Result? = nil
32+
private var doneButton: UIBarButtonItem?
3233

3334
static func instantiate() -> AddressSelectionViewController {
3435
guard let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AddressSelectionViewController") as? AddressSelectionViewController else {
@@ -60,23 +61,73 @@ final class AddressSelectionViewController: UIViewController {
6061
resultSearchController?.obscuresBackgroundDuringPresentation = true
6162
definesPresentationContext = true
6263

63-
let continueButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneTapped))
64-
navigationItem.rightBarButtonItem = continueButton
65-
continueButton.isEnabled = false
64+
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneTapped))
65+
let latLongButton = UIBarButtonItem(title: "Lat/Long", style: .plain, target: self, action: #selector(latLongTapped))
66+
navigationItem.rightBarButtonItems = [latLongButton, doneButton]
67+
doneButton.isEnabled = false
68+
self.doneButton = doneButton
6669
}
6770

6871
@objc func doneTapped() {
6972
guard let selectedItem = selectedItem else { return }
7073
onAddressSelect?(selectedItem)
7174
}
75+
76+
private func showErrorAlert(title: String, message: String?) {
77+
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
78+
alertController.addAction(.init(title: "OK", style: .default, handler: nil))
79+
present(alertController, animated: true)
80+
}
81+
82+
@objc func latLongTapped() {
83+
let alertController = UIAlertController(
84+
title: "Enter Latitude/Longitude",
85+
message: "",
86+
preferredStyle: .alert
87+
)
88+
89+
alertController.addTextField { textField in
90+
textField.placeholder = "Latitude"
91+
}
92+
93+
alertController.addTextField { textField in
94+
textField.placeholder = "Longitude"
95+
}
96+
97+
let confirmAction = UIAlertAction(title: "OK", style: .default) { [weak self] _ in
98+
alertController.message = nil
99+
100+
guard let latitudeTextFieldText = alertController.textFields?.first?.text,
101+
let longitudeTextFieldText = alertController.textFields?[1].text else { return }
102+
103+
guard let latitude = CLLocationDegrees(latitudeTextFieldText) else {
104+
self?.showErrorAlert(title: "Invalid Entry", message: "Please try again and enter a valid latitude.")
105+
return
106+
}
107+
108+
guard let longitude = CLLocationDegrees(longitudeTextFieldText) else {
109+
self?.showErrorAlert(title: "Invalid Entry", message: "Please try again and enter a valid longitude.")
110+
return
111+
}
112+
113+
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
114+
let placemark = MKPlacemark(coordinate: coordinate)
115+
self?.onAddressSelect?(.init(placemark: placemark, formattedAddress: nil))
116+
}
117+
118+
alertController.addAction(confirmAction)
119+
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
120+
alertController.addAction(cancelAction)
121+
present(alertController, animated: true, completion: nil)
122+
}
72123
}
73124

74125
extension AddressSelectionViewController: LocationSearchSelectable {
75126
func didSelect(_ placemark: MKPlacemark, formattedAddress: String) {
76127
dismiss(animated: true, completion: { [weak self] in
77128
guard let self = self else { return }
78129

79-
self.navigationItem.rightBarButtonItem?.isEnabled = true
130+
self.doneButton?.isEnabled = true
80131
self.selectedItem = .init(placemark: placemark, formattedAddress: formattedAddress)
81132
self.mapView.removeAnnotations(self.mapView.annotations)
82133
let annotation = MKPointAnnotation()

Examples/GroceryExpress/AddressUpdateViewController.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import IFTTTConnectSDK
1313
/// Generates a address update request based on the fields contained in the struct.
1414
private struct AddressUpdateRequestFactory {
1515
let token: String
16-
let userId: String
1716
let entryPlacemark: MKPlacemark
1817
let entryAddress: String
1918
let entryRadius: Int
@@ -25,7 +24,7 @@ private struct AddressUpdateRequestFactory {
2524
func generate() -> URLRequest {
2625
let body = """
2726
{
28-
"user_id": \(userId),
27+
"user_id": 1,
2928
"services": [
3029
{
3130
"service_id": "location",
@@ -99,7 +98,6 @@ final class AddressUpdateViewController: UIViewController {
9998
@IBOutlet private weak var exitAddressLabel: UILabel!
10099
@IBOutlet private weak var exitRadiusTextField: UITextField!
101100
@IBOutlet private weak var updateButton: UIButton!
102-
@IBOutlet private weak var userIdTextField: UITextField!
103101
@IBOutlet private weak var activityIndicator: UIActivityIndicatorView!
104102

105103
/// The user token that comes back from the `ConnectButtonControllerDelegate` method `didFinishActivationWithResult`
@@ -131,9 +129,6 @@ final class AddressUpdateViewController: UIViewController {
131129

132130
/// The geofence exit formatted address
133131
var exitAddress: String?
134-
135-
/// The IFTTT user id for the user to update
136-
var userId: String?
137132
}
138133

139134
private var addressUpdate: AddressUpdate = .init()
@@ -176,16 +171,25 @@ final class AddressUpdateViewController: UIViewController {
176171
configureUpdateButton()
177172
}
178173

174+
private func generateText(placemark: MKPlacemark, formattedAddress: String?) -> String {
175+
var str = ""
176+
if let formattedAddress = formattedAddress {
177+
str += formattedAddress
178+
}
179+
str += ("\n" + "Lat: \(placemark.coordinate.latitude), Lon: \(placemark.coordinate.longitude)")
180+
return str
181+
}
182+
179183
private func configureLabels() {
180-
if let entry = addressUpdate.entryAddress {
181-
enterAddressLabel.text = entry
184+
if let entryPlacemark = addressUpdate.entryPlacemark {
185+
enterAddressLabel.text = generateText(placemark: entryPlacemark, formattedAddress: addressUpdate.entryAddress)
182186
enterAddressLabel.isHidden = false
183187
} else {
184188
enterAddressLabel.isHidden = true
185189
}
186190

187-
if let exit = addressUpdate.exitAddress {
188-
exitAddressLabel.text = exit
191+
if let exitPlacemark = addressUpdate.exitPlacemark {
192+
exitAddressLabel.text = generateText(placemark: exitPlacemark, formattedAddress: addressUpdate.exitAddress)
189193
exitAddressLabel.isHidden = false
190194
} else {
191195
exitAddressLabel.isHidden = true
@@ -195,7 +199,6 @@ final class AddressUpdateViewController: UIViewController {
195199
private func configureUpdateButton() {
196200
updateButton.isEnabled = addressUpdate.entryPlacemark != nil
197201
&& addressUpdate.exitPlacemark != nil
198-
&& !(addressUpdate.userId?.isEmpty ?? true)
199202
}
200203

201204
@IBAction private func updateButtonTapped(_ sender: Any) {
@@ -218,7 +221,6 @@ final class AddressUpdateViewController: UIViewController {
218221

219222
override func viewDidLoad() {
220223
super.viewDidLoad()
221-
userIdTextField.delegate = self
222224
enterRadiusTextField.delegate = self
223225
exitRadiusTextField.delegate = self
224226

@@ -266,15 +268,17 @@ final class AddressUpdateViewController: UIViewController {
266268
}
267269

268270
private func makeRequest(completion: @escaping (Data?, Error?) -> Void) {
269-
guard let userId = addressUpdate.userId,
270-
let entryPlacemark = addressUpdate.entryPlacemark,
271-
let entryAddress = addressUpdate.entryAddress?.replacingOccurrences(of: "\n", with: " "),
271+
guard let entryPlacemark = addressUpdate.entryPlacemark,
272272
let entryRadius = addressUpdate.geofenceEntryRadius,
273273
let exitPlacemark = addressUpdate.exitPlacemark,
274-
let exitAddress = addressUpdate.exitAddress?.replacingOccurrences(of: "\n", with: " "),
275-
let exitRadius = addressUpdate.geofenceExitRadius else { return }
274+
let exitRadius = addressUpdate.geofenceExitRadius else {
275+
completion(nil, nil)
276+
return
277+
}
278+
279+
let entryAddress = addressUpdate.entryAddress?.replacingOccurrences(of: "\n", with: " ") ?? ""
280+
let exitAddress = addressUpdate.exitAddress?.replacingOccurrences(of: "\n", with: " ") ?? ""
276281
let requestFactory = AddressUpdateRequestFactory(token: token,
277-
userId: userId,
278282
entryPlacemark: entryPlacemark,
279283
entryAddress: entryAddress,
280284
entryRadius: entryRadius,
@@ -300,9 +304,7 @@ extension AddressUpdateViewController: UITextFieldDelegate {
300304
guard let text = textField.text else { return true }
301305
let updatedText = (text as NSString).replacingCharacters(in: range, with: string)
302306

303-
if textField == userIdTextField {
304-
addressUpdate.userId = updatedText
305-
} else if textField == enterRadiusTextField {
307+
if textField == enterRadiusTextField {
306308
addressUpdate.geofenceEntryRadius = Int(updatedText)
307309
} else if textField == exitRadiusTextField {
308310
addressUpdate.geofenceExitRadius = Int(updatedText)

0 commit comments

Comments
 (0)