PastActivityViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. func back() {
  56. NSLog(":FUTUREACTIVITYCONTROLLER:CONTROLLER:DEBUG: Back")
  57. self.dismiss(animated: true, completion: nil)
  58. }
  59. /**
  60. Dispose of any resources that can be recreated.
  61. */
  62. override func didReceiveMemoryWarning() {
  63. super.didReceiveMemoryWarning()
  64. }
  65. /**
  66. Loads the selected post.
  67. :param: id Post id.
  68. */
  69. public func loadActivity(id: Int){
  70. NSLog(":POSTCONTROLLER:DEBUG: Loading post \(id)")
  71. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  72. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  73. let lang : String = getLanguage()
  74. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  75. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  76. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  77. do {
  78. let searchResults = try context.fetch(fetchRequest)
  79. var count = 0
  80. var sTitle: String
  81. var sText: String
  82. var image: String
  83. for r in searchResults as [NSManagedObject] {
  84. count = count + 1
  85. sTitle = r.value(forKey: "title_\(lang)")! as! String
  86. if (r.value(forKey: "after_\(lang)")! as! String).length == 0{
  87. sText = r.value(forKey: "after_\(lang)")! as! String
  88. }
  89. else{
  90. sText = r.value(forKey: "description_\(lang)")! as! String
  91. }
  92. activityTitle.text = " \(sTitle)"
  93. activityText.text = sText.stripHtml()
  94. // Get main image
  95. image = ""
  96. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  97. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  98. let imgSortDescriptors = [imgSortDescriptor]
  99. imgFetchRequest.sortDescriptors = imgSortDescriptors
  100. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  101. imgFetchRequest.fetchLimit = 1
  102. do{
  103. let imgSearchResults = try context.fetch(imgFetchRequest)
  104. for imgR in imgSearchResults as [NSManagedObject]{
  105. image = imgR.value(forKey: "image")! as! String
  106. let path = "img/actividades/preview/\(image)"
  107. self.activityImage.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
  108. }
  109. } catch {
  110. NSLog(":POSTCONTROLLER:DEBUG: Error getting image for post \(id): \(error)")
  111. }
  112. }
  113. } catch {
  114. NSLog(":POSTCONTROLLER:DEBUG: Error with request: \(error)")
  115. }
  116. }
  117. /**
  118. Gets the device language. The only recognized languages are Spanish, English and Basque.
  119. If the device has another language, Spanish will be selected by default.
  120. :return: Two-letter language code.
  121. */
  122. func getLanguage() -> String{
  123. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  124. if(pre == "es" || pre == "en" || pre == "eu"){
  125. return pre
  126. }
  127. else{
  128. return "es"
  129. }
  130. }
  131. }