mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-26 22:07:12 +00:00
Code optimization for better readability
This commit is contained in:
1 parent
3871af07c5
commit
f8bbb741ff
3 files changed
+98
-79
No files matched your search
@@ -14,9 +14,6 @@ import AVFoundation
|
||||
|
||||
func showCheckInAlert(sender: CheckInController, alertController: UIAlertController)
|
||||
|
||||
optional func checkInDidStart(sender: CheckInController)
|
||||
optional func checkInDidEnd(sender: CheckInController, withSuccess succeed: Bool)
|
||||
|
||||
}
|
||||
|
||||
class CheckInController : NSObject {
|
||||
@@ -27,50 +24,64 @@ class CheckInController : NSObject {
|
||||
|
||||
// MARK: - CheckIn
|
||||
|
||||
func checkIn(regattaData: RegattaData) {
|
||||
checkInDidStart()
|
||||
func checkIn(regattaData: RegattaData, completion: (withSuccess: Bool) -> Void) {
|
||||
SVProgressHUD.show()
|
||||
requestManager = RequestManager(baseURLString: regattaData.serverURL)
|
||||
requestManager.getRegattaData(regattaData,
|
||||
success: { (regattaData) in self.checkInSuccess(regattaData) },
|
||||
failure: { (title, error) in self.checkInFailure(title, error: error) }
|
||||
success:
|
||||
{ (regattaData) in
|
||||
SVProgressHUD.popActivity()
|
||||
self.checkInSuccess(regattaData, completion: completion)
|
||||
}, failure: { (title, error) in
|
||||
SVProgressHUD.popActivity()
|
||||
self.checkInFailure(title, error: error, completion: completion)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private func checkInSuccess(regattaData: RegattaData) {
|
||||
private func checkInSuccess(regattaData: RegattaData, completion: (withSuccess: Bool) -> Void) {
|
||||
let alertController = UIAlertController(title: regattaData.welcomeString, message: nil, preferredStyle: .Alert)
|
||||
let okTitle = NSLocalizedString("OK", comment: "")
|
||||
let okAction = UIAlertAction(title: okTitle, style: .Default) { (action) in
|
||||
self.postCheckIn(regattaData)
|
||||
self.postCheckIn(regattaData, completion: completion)
|
||||
}
|
||||
let cancelTitle = NSLocalizedString("Cancel", comment: "")
|
||||
let cancelAction = UIAlertAction(title: cancelTitle, style: .Cancel) { (action) in
|
||||
self.checkInDidEnd(withSuccess: false)
|
||||
self.checkInDidFinish(withSuccess: false, completion: completion)
|
||||
}
|
||||
alertController.addAction(okAction)
|
||||
alertController.addAction(cancelAction)
|
||||
showCheckInAlert(alertController)
|
||||
}
|
||||
|
||||
private func checkInFailure(title: String, error: NSError) {
|
||||
private func checkInFailure(title: String, error: NSError, completion: (withSuccess: Bool) -> Void) {
|
||||
let alertController = UIAlertController(title: title, message: error.localizedDescription, preferredStyle: .Alert)
|
||||
let cancelTitle = NSLocalizedString("Cancel", comment: "")
|
||||
let cancelAction = UIAlertAction(title: cancelTitle, style: .Cancel) { (action) in
|
||||
self.checkInDidEnd(withSuccess: false)
|
||||
self.checkInDidFinish(withSuccess: false, completion: completion)
|
||||
}
|
||||
alertController.addAction(cancelAction)
|
||||
showCheckInAlert(alertController)
|
||||
}
|
||||
|
||||
private func postCheckIn(regattaData: RegattaData) {
|
||||
// MARK: - PostCheckIn
|
||||
|
||||
private func postCheckIn(regattaData: RegattaData, completion: (withSuccess: Bool) -> Void) {
|
||||
SVProgressHUD.show()
|
||||
requestManager.postCheckIn(regattaData.leaderboardData.name,
|
||||
competitorID: regattaData.competitorData.competitorID,
|
||||
success: { (operation, responseObject) -> Void in self.postCheckInSuccess(regattaData) },
|
||||
failure: { (operation, error) -> Void in self.postCheckInFailure(regattaData, error: error) }
|
||||
success:
|
||||
{ (operation, responseObject) -> Void in
|
||||
SVProgressHUD.popActivity()
|
||||
self.postCheckInSuccess(regattaData, completion: completion)
|
||||
}, failure: { (operation, error) -> Void in
|
||||
SVProgressHUD.popActivity()
|
||||
self.postCheckInFailure(regattaData, error: error, completion: completion)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private func postCheckInSuccess(regattaData: RegattaData) {
|
||||
private func postCheckInSuccess(regattaData: RegattaData, completion: (withSuccess: Bool) -> Void) {
|
||||
let regatta = CoreDataManager.sharedManager.fetchRegatta(regattaData) ?? CoreDataManager.sharedManager.newRegatta()
|
||||
regatta.updateWirhRegattaData(regattaData)
|
||||
let event = regatta.event ?? CoreDataManager.sharedManager.newEvent(regatta)
|
||||
@@ -80,34 +91,27 @@ class CheckInController : NSObject {
|
||||
let competitor = regatta.competitor ?? CoreDataManager.sharedManager.newCompetitor(regatta)
|
||||
competitor.updateWithCompetitorData(regattaData.competitorData)
|
||||
CoreDataManager.sharedManager.saveContext()
|
||||
checkInDidEnd(withSuccess: true)
|
||||
checkInDidFinish(withSuccess: true, completion: completion)
|
||||
}
|
||||
|
||||
private func postCheckInFailure(regattaData: RegattaData, error: AnyObject) {
|
||||
private func postCheckInFailure(regattaData: RegattaData, error: AnyObject, completion: (withSuccess: Bool) -> Void) {
|
||||
let alertTitle = String(format:NSLocalizedString("Couldn't check-in to %@", comment: ""), regattaData.leaderboardName)
|
||||
let alertController = UIAlertController(title: alertTitle, message: error.localizedDescription, preferredStyle: .Alert)
|
||||
let cancelTitle = NSLocalizedString("Cancel", comment: "")
|
||||
let cancelAction = UIAlertAction(title: cancelTitle, style: .Cancel) { (action) in
|
||||
self.checkInDidEnd(withSuccess: false)
|
||||
self.checkInDidFinish(withSuccess: false, completion: completion)
|
||||
}
|
||||
alertController.addAction(cancelAction)
|
||||
showCheckInAlert(alertController)
|
||||
}
|
||||
|
||||
private func checkInDidFinish(withSuccess success: Bool, completion: (withSuccess: Bool) -> Void) {
|
||||
completion(withSuccess: success)
|
||||
}
|
||||
|
||||
// MARK: - Controller
|
||||
|
||||
private func checkInDidStart() {
|
||||
SVProgressHUD.show()
|
||||
self.delegate?.checkInDidStart?(self)
|
||||
}
|
||||
|
||||
private func checkInDidEnd(withSuccess succeed: Bool) {
|
||||
SVProgressHUD.popActivity()
|
||||
self.delegate?.checkInDidEnd?(self, withSuccess: succeed)
|
||||
}
|
||||
|
||||
private func showCheckInAlert(alertController: UIAlertController) {
|
||||
SVProgressHUD.popActivity()
|
||||
self.delegate?.showCheckInAlert(self, alertController: alertController)
|
||||
}
|
||||
|
||||
|
||||
@@ -69,62 +69,75 @@ class HomeViewController: UIViewController {
|
||||
|
||||
// MARK: - Reviews
|
||||
|
||||
func reviews() {
|
||||
if !Preferences.termsAccepted {
|
||||
reviewTerms()
|
||||
} else {
|
||||
reviewsAfterTermsAccepted()
|
||||
}
|
||||
private func reviews() {
|
||||
self.reviewTerms({
|
||||
print("Review terms done.")
|
||||
self.reviewGPSFixes({
|
||||
print("Review GPS fixes done.")
|
||||
self.reviewNewCheckIn({
|
||||
print("Review new check-in done.")
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func reviewTerms() {
|
||||
// MARK: - 1. Review Terms
|
||||
|
||||
private func reviewTerms(completion: () -> Void) {
|
||||
guard Preferences.termsAccepted == false else { completion(); return }
|
||||
let alertTitle = NSLocalizedString("EULA_title", comment: "")
|
||||
let alertMessage = NSLocalizedString("EULA_content", comment: "")
|
||||
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .Alert)
|
||||
let viewTitle = NSLocalizedString("EULA_view", comment: "")
|
||||
let viewAction = UIAlertAction(title: viewTitle, style: .Cancel, handler: { action in
|
||||
UIApplication.sharedApplication().openURL(URLs.EULA)
|
||||
self.reviewTerms() // Reopen alert again until user accepted terms
|
||||
self.reviewTerms(completion) // Review terms until user accepted terms
|
||||
})
|
||||
let confirmTitle = NSLocalizedString("EULA_confirm", comment: "")
|
||||
let confirmAction = UIAlertAction(title: confirmTitle, style: .Default, handler: { action in
|
||||
Preferences.termsAccepted = true
|
||||
self.reviewsAfterTermsAccepted()
|
||||
completion() // Terms accepted
|
||||
})
|
||||
alertController.addAction(viewAction)
|
||||
alertController.addAction(confirmAction)
|
||||
presentViewController(alertController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func reviewsAfterTermsAccepted() {
|
||||
reviewGPSFixes()
|
||||
}
|
||||
// MARK: 2. Review GPS Fixes
|
||||
|
||||
func reviewGPSFixes() {
|
||||
private func reviewGPSFixes(completion: () -> Void) {
|
||||
SVProgressHUD.show()
|
||||
fetchedResultsController.delegate = nil
|
||||
var regattas = CoreDataManager.sharedManager.fetchRegattas() ?? []
|
||||
reviewGPSFixes(®attas)
|
||||
}
|
||||
|
||||
func reviewGPSFixes(inout regattas: [Regatta]) {
|
||||
if let regatta = regattas.popLast() {
|
||||
let gpsFixController = GPSFixController.init(regatta: regatta)
|
||||
gpsFixController.sendAll({ (withSuccess) in
|
||||
self.reviewGPSFixes(®attas)
|
||||
})
|
||||
} else {
|
||||
fetchedResultsController.delegate = self
|
||||
SVProgressHUD.popActivity()
|
||||
reviewNewCheckIn()
|
||||
reviewGPSFixes(®attas) {
|
||||
self.reviewGPSFixesCompleted(completion)
|
||||
}
|
||||
}
|
||||
|
||||
func reviewNewCheckIn() {
|
||||
guard Preferences.termsAccepted else { return }
|
||||
guard let urlString = Preferences.newCheckInURL else { return }
|
||||
guard let regattaData = RegattaData(urlString: urlString) else { return }
|
||||
checkInController.checkIn(regattaData)
|
||||
private func reviewGPSFixes(inout regattas: [Regatta], completion: () -> Void) {
|
||||
guard let regatta = regattas.popLast() else { completion(); return }
|
||||
let gpsFixController = GPSFixController.init(regatta: regatta)
|
||||
gpsFixController.sendAll({ (withSuccess) in
|
||||
self.reviewGPSFixes(®attas, completion: completion)
|
||||
})
|
||||
}
|
||||
|
||||
private func reviewGPSFixesCompleted(completion: () -> Void) {
|
||||
SVProgressHUD.popActivity()
|
||||
fetchedResultsController.delegate = self
|
||||
completion()
|
||||
}
|
||||
|
||||
// MARK: 3. Review New Check-In
|
||||
|
||||
private func reviewNewCheckIn(completion: () -> Void) {
|
||||
guard Preferences.termsAccepted else { completion(); return }
|
||||
guard let urlString = Preferences.newCheckInURL else { completion(); return }
|
||||
guard let regattaData = RegattaData(urlString: urlString) else { completion(); return }
|
||||
checkInController.checkIn(regattaData, completion: { (withSuccess) in
|
||||
Preferences.newCheckInURL = nil
|
||||
completion()
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: - Notifications
|
||||
@@ -140,9 +153,11 @@ class HomeViewController: UIViewController {
|
||||
NSNotificationCenter.defaultCenter().removeObserver(self)
|
||||
}
|
||||
|
||||
func newCheckInURLNotification(notification: NSNotification) {
|
||||
@objc private func newCheckInURLNotification(notification: NSNotification) {
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
self.reviewNewCheckIn()
|
||||
self.reviewNewCheckIn({
|
||||
print("Review new check-in done.")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -202,13 +217,13 @@ class HomeViewController: UIViewController {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
lazy var checkInController: CheckInController = {
|
||||
private lazy var checkInController: CheckInController = {
|
||||
let checkInController = CheckInController()
|
||||
checkInController.delegate = self
|
||||
return checkInController
|
||||
}()
|
||||
|
||||
lazy var fetchedResultsController: NSFetchedResultsController = {
|
||||
private lazy var fetchedResultsController: NSFetchedResultsController = {
|
||||
let fetchedResultsController = CoreDataManager.sharedManager.regattaFetchedResultsController()
|
||||
fetchedResultsController.delegate = self
|
||||
return fetchedResultsController
|
||||
@@ -292,8 +307,4 @@ extension HomeViewController: CheckInControllerDelegate {
|
||||
presentViewController(alertController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func checkInDidEnd(sender: CheckInController, withSuccess succeed: Bool) {
|
||||
Preferences.newCheckInURL = nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -92,7 +92,7 @@ class ScanViewController: UIViewController {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
lazy var checkInController: CheckInController = {
|
||||
private lazy var checkInController: CheckInController = {
|
||||
let checkInController = CheckInController()
|
||||
checkInController.delegate = self
|
||||
return checkInController
|
||||
@@ -120,14 +120,6 @@ extension ScanViewController: CheckInControllerDelegate {
|
||||
presentViewController(alertController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func checkInDidEnd(sender: CheckInController, withSuccess succeed: Bool) {
|
||||
if succeed {
|
||||
navigationController?.popViewControllerAnimated(true)
|
||||
} else {
|
||||
startScanning()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - AVCaptureMetadataOutputObjectsDelegate
|
||||
@@ -142,7 +134,9 @@ extension ScanViewController: AVCaptureMetadataOutputObjectsDelegate {
|
||||
let metadataObject: AVMetadataMachineReadableCodeObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
|
||||
if let regattaData = RegattaData(urlString: metadataObject.stringValue) {
|
||||
stopScanning()
|
||||
checkInController.checkIn(regattaData)
|
||||
dispatch_async(dispatch_get_main_queue(), {
|
||||
self.captureOutputSuccess(regattaData)
|
||||
})
|
||||
} else {
|
||||
let alertTitle = NSLocalizedString("Incorrect QR Code", comment: "")
|
||||
let alertController = UIAlertController(title: alertTitle, message: "", preferredStyle: .Alert)
|
||||
@@ -154,4 +148,14 @@ extension ScanViewController: AVCaptureMetadataOutputObjectsDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
private func captureOutputSuccess(regattaData: RegattaData) {
|
||||
checkInController.checkIn(regattaData, completion: { (withSuccess) in
|
||||
if withSuccess {
|
||||
self.navigationController?.popViewControllerAnimated(true)
|
||||
} else {
|
||||
self.startScanning()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user