SyncController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import UIKit
  21. import CoreData
  22. /**
  23. The view controller of the app.
  24. */
  25. class SyncController: UIViewController{
  26. var delegate: AppDelegate?
  27. var nowSyncing: Bool = false
  28. var syncTimer: Timer? = nil
  29. @IBOutlet weak var pb: UIProgressView!
  30. @IBOutlet weak var lb: UILabel!
  31. @IBOutlet weak var lbTitle: UILabel!
  32. var completion: Float = 0.0
  33. let messages: [String] = ["Contactando con Don Margolo...", "Arrancando la furgo...", "Preparando kalimotxos...",
  34. "Tú eres el piloto", "Afinando la tuba...", "Margolari camina pa'lante, margolari camina pa'trás.", "Ari ari ari!", "Llegando tarde...", "¿Cabe más gente en el Foro Margolari?", "Arreglando el tirador de cerveza...", "Descorchando botellas...", "Repartiendo pulseras...", "Pintando las fiestas...", "Desplegando pancarta..."]
  35. /**
  36. Run when the app loads.
  37. */
  38. override func viewDidLoad() {
  39. NSLog(":SYNCCONTROLLER:LOG: SyncController shown.")
  40. self.delegate = UIApplication.shared.delegate as? AppDelegate
  41. self.delegate?.syncController = self
  42. super.viewDidLoad()
  43. }
  44. override func viewDidAppear(_ animated: Bool) {
  45. if UserDefaults.standard.object(forKey: "userId") == nil{
  46. NSLog(":SYNCCONTROLLER:LOG: First run. Generating user ID and syncing...")
  47. let newUid = randomString(length: 16)
  48. let defaults = UserDefaults.standard
  49. defaults.set(newUid, forKey: "userId")
  50. // Initial setup
  51. pb.isHidden = false
  52. lbTitle.isHidden = false
  53. lb.isHidden = false
  54. pb.progress = 0
  55. // Start the timer
  56. Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(progress), userInfo: nil, repeats: true)
  57. syncTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(endSync), userInfo: nil, repeats: true)
  58. initialSync()
  59. }
  60. else{
  61. startApp()
  62. }
  63. }
  64. @objc func endSync(){
  65. if self.nowSyncing == false{
  66. syncTimer?.invalidate()
  67. startApp()
  68. }
  69. }
  70. func startApp(){
  71. performSegue(withIdentifier: "SegueInit", sender: nil)
  72. }
  73. /**
  74. Handles the initial sync process.
  75. It can start it, showing the sync screen, or finish it, hidding the screen.
  76. :param: showScreen True to start the sync, false to end it.
  77. */
  78. func initialSync(){
  79. //performSegue(withIdentifier: "SegueSync", sender: nil)
  80. Sync(synchronous: true)
  81. }
  82. /**
  83. Generates a random string to be used as a user identifier.
  84. :param: length The length of the generated string.
  85. :return: A random alphanumeric string with the indicated length.
  86. */
  87. func randomString(length: Int) -> String {
  88. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  89. let len = UInt32(letters.length)
  90. var randomString = ""
  91. for _ in 0 ..< length {
  92. let rand = arc4random_uniform(len)
  93. var nextChar = letters.character(at: Int(rand))
  94. randomString += NSString(characters: &nextChar, length: 1) as String
  95. }
  96. return randomString
  97. }
  98. /**
  99. Signal to update the progressbar and, maybe, the tag.
  100. */
  101. @objc func progress(){
  102. if (Int(completion * 100) % 20) == 0 {
  103. let rnd = Int(arc4random_uniform(UInt32(messages.count)));
  104. lb.text = messages[rnd]
  105. }
  106. completion = completion + 0.02
  107. if completion >= 1.0{
  108. completion = 0.0
  109. }
  110. pb.progress = completion
  111. }
  112. }