PastActivityViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. Controller to show a past activity.
  24. */
  25. class PastActivityViewController: UIViewController, UIGestureRecognizerDelegate {
  26. @IBOutlet weak var barButton: UIButton!
  27. @IBOutlet weak var barTitle: UILabel!
  28. @IBOutlet weak var activityTitle: UILabel!
  29. @IBOutlet weak var activityImage: UIImageView!
  30. @IBOutlet weak var activityText: UILabel!
  31. @IBOutlet weak var activityDate: UILabel!
  32. // Activity id
  33. var id: Int = -1
  34. var delegate: AppDelegate?
  35. var passId: Int = -1
  36. /**
  37. Get the app context.
  38. :return: The app context.
  39. */
  40. func getContext () -> NSManagedObjectContext {
  41. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  42. }
  43. /**
  44. Run when the app loads.
  45. */
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. self.loadActivity(id: id)
  49. // Set button action
  50. barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  51. }
  52. /**
  53. Dismisses the controller.
  54. */
  55. @objc func back() {
  56. self.dismiss(animated: true, completion: nil)
  57. }
  58. /**
  59. Dispose of any resources that can be recreated.
  60. */
  61. override func didReceiveMemoryWarning() {
  62. super.didReceiveMemoryWarning()
  63. }
  64. /**
  65. Loads the selected activity.
  66. :param: id Activity id.
  67. */
  68. public func loadActivity(id: Int){
  69. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  70. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  71. let lang : String = getLanguage()
  72. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  73. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  74. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  75. do {
  76. let searchResults = try context.fetch(fetchRequest)
  77. var count = 0
  78. var sTitle: String
  79. var sText: String
  80. var image: String
  81. var date: NSDate
  82. for r in searchResults as [NSManagedObject] {
  83. count = count + 1
  84. sTitle = r.value(forKey: "title_\(lang)")! as! String
  85. if ((r.value(forKey: "after_\(lang)") != nil) && ((r.value(forKey: "after_\(lang)")! as! String).length != 0)){
  86. sText = r.value(forKey: "after_\(lang)")! as! String
  87. }
  88. else{
  89. sText = r.value(forKey: "text_\(lang)")! as! String
  90. }
  91. date = r.value(forKey: "date")! as! NSDate
  92. activityTitle.text = " \(sTitle.decode().stripHtml())"
  93. activityText.text = sText.decode().stripHtml()
  94. activityDate.text = formatDate(date: date, lang: lang)
  95. // Get main image
  96. image = ""
  97. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  98. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  99. let imgSortDescriptors = [imgSortDescriptor]
  100. imgFetchRequest.sortDescriptors = imgSortDescriptors
  101. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  102. imgFetchRequest.fetchLimit = 1
  103. do{
  104. let imgSearchResults = try context.fetch(imgFetchRequest)
  105. for imgR in imgSearchResults as [NSManagedObject]{
  106. image = imgR.value(forKey: "image")! as! String
  107. let path = "img/actividades/preview/\(image)"
  108. self.activityImage.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
  109. }
  110. } catch {
  111. NSLog(":PASTACTIVITYCONTROLLER:ERROR: Error getting image for past activity \(id): \(error)")
  112. }
  113. }
  114. } catch {
  115. NSLog(":PASTACTIVITYCONTROLLER:ERROR: Error loading past activity: \(error)")
  116. }
  117. }
  118. /**
  119. Formats a date to the desired language.
  120. :param: text The date.
  121. :param: lang Device language (only 'es', 'en', or 'eu').
  122. */
  123. func formatDate(date: NSDate, lang: String) -> String{
  124. let months_es = ["0index", "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]
  125. let months_en = ["0index", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  126. let months_eu = ["0index", "urtarrilaren", "otsailaren", "martxoaren", "abrilaren", "maiatzaren", "ekainaren", "ustailaren", "abustuaren", "irailaren", "urriaren", "azaroaren", "abenduaren"]
  127. let days_es = ["0index", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sábado", "Domingo"]
  128. let days_en = ["0index", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
  129. let days_eu = ["0index", "Astelehena", "Asteartea", "Asteazkena", "Osteguna", "Ostirala", "Larumbata", "Igandea"]
  130. let calendar = Calendar.current
  131. let day = calendar.component(.day, from: date as Date)
  132. let month = calendar.component(.month, from: date as Date)
  133. let weekday = calendar.component(.weekday, from: date as Date)
  134. var strDate = ""
  135. switch lang{
  136. case "en":
  137. var dayNum = ""
  138. switch day{
  139. case 1:
  140. dayNum = "1th"
  141. break
  142. case 2:
  143. dayNum = "2nd"
  144. break;
  145. case 3:
  146. dayNum = "3rd"
  147. break;
  148. default:
  149. dayNum = "\(day)th"
  150. }
  151. strDate = "\(days_en[weekday]), \(months_en[month]) \(dayNum)"
  152. break
  153. case "eu":
  154. strDate = "\(days_eu[weekday]) \(months_eu[month]) \(day)an"
  155. break
  156. default:
  157. strDate = "\(days_es[weekday]) \(day) de \(months_es[month])"
  158. }
  159. return strDate
  160. }
  161. /**
  162. Gets the device language. The only recognized languages are Spanish, English and Basque.
  163. If the device has another language, Spanish will be selected by default.
  164. :return: Two-letter language code.
  165. */
  166. func getLanguage() -> String{
  167. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  168. if(pre == "es" || pre == "en" || pre == "eu"){
  169. return pre
  170. }
  171. else{
  172. return "es"
  173. }
  174. }
  175. }