PostViewController.swift 6.4 KB

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