HomeView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle the home view.
  25. */
  26. class HomeView: UIView {
  27. //The main scroll view.
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. //The container of the view.
  30. @IBOutlet weak var container: UIView!
  31. //Each of the sections of the view.
  32. @IBOutlet weak var locationSection: Section!
  33. @IBOutlet weak var lablancaSection: Section!
  34. @IBOutlet weak var futureActivitiesSection: Section!
  35. @IBOutlet weak var blogSection: Section!
  36. @IBOutlet weak var gallerySection: Section!
  37. @IBOutlet weak var pastActivitiesSection: Section!
  38. @IBOutlet weak var socialSection: Section!
  39. override init(frame: CGRect){
  40. super.init(frame: frame)
  41. }
  42. /**
  43. Run when the view is started.
  44. */
  45. required init?(coder aDecoder: NSCoder) {
  46. super.init(coder: aDecoder)
  47. //Load the contents of the HomeView.xib file.
  48. Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
  49. self.addSubview(container)
  50. container.frame = self.bounds
  51. //Set titles for each section
  52. locationSection.setTitle(text: "Encuentranos")
  53. lablancaSection.setTitle(text: "La Blanca")
  54. futureActivitiesSection.setTitle(text: "Proximas actividades")
  55. blogSection.setTitle(text: "Ultimos posts")
  56. gallerySection.setTitle(text: "Ultimas fotos")
  57. pastActivitiesSection.setTitle(text: "Ultimas actividades")
  58. socialSection.setTitle(text: "Siguenos")
  59. //Get info to populate sections
  60. let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  61. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  62. moc.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  63. let lang : String = getLanguage()
  64. //Populate sections
  65. setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection.getContentStack())
  66. setUpBlog(context: moc, delegate: appDelegate, lang: lang, parent: blogSection.getContentStack())
  67. setUpFutureActivities(context: moc, delegate: appDelegate, lang: lang, parent: futureActivitiesSection.getContentStack())
  68. setUpSocial(parent: socialSection.getContentStack())
  69. setUpGallery(context: moc, delegate: appDelegate, parent: gallerySection.getContentStack())
  70. pastActivitiesSection.expandSection()
  71. //Always at the end: update scrollview
  72. var h: Int = 0
  73. for view in scrollView.subviews {
  74. //contentRect = contentRect.union(view.frame);
  75. h = h + Int(view.frame.height) + 30 //Why 30?
  76. }
  77. // TODO: Calculate at the end
  78. self.scrollView.contentSize.height = 1300// CGFloat(h);
  79. }
  80. /**
  81. Gets the device language. The only recognized languages are Spanish, English and Basque.
  82. If the device has another language, Spanish will be selected by default.
  83. :return: Two-letter language code.
  84. */
  85. func getLanguage() -> String{
  86. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  87. if(pre == "es" || pre == "en" || pre == "eu"){
  88. return pre
  89. }
  90. else{
  91. return "es"
  92. }
  93. }
  94. /**
  95. Sets up the future activities section.
  96. :param: delegate App delegate.
  97. :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
  98. :param: parent Stack view to load the rows in.
  99. */
  100. func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  101. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  102. let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  103. let sortDescriptors = [sortDescriptor]
  104. fetchRequest.sortDescriptors = sortDescriptors
  105. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  106. fetchRequest.fetchLimit = 2
  107. do {
  108. let searchResults = try context.fetch(fetchRequest)
  109. var row : RowHomeFutureActivities
  110. var count = 0
  111. var id: Int
  112. var title: String
  113. var text: String
  114. var image: String
  115. for r in searchResults as [NSManagedObject] {
  116. count = count + 1
  117. //Create a new row
  118. row = RowHomeFutureActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
  119. id = r.value(forKey: "id")! as! Int
  120. title = r.value(forKey: "title_\(lang)")! as! String
  121. text = r.value(forKey: "text_\(lang)")! as! String
  122. row.setTitle(text: title)
  123. row.setText(text: text.decode())
  124. // Get main image
  125. image = ""
  126. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  127. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  128. let imgSortDescriptors = [imgSortDescriptor]
  129. imgFetchRequest.sortDescriptors = imgSortDescriptors
  130. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  131. imgFetchRequest.fetchLimit = 1
  132. do{
  133. let imgSearchResults = try context.fetch(imgFetchRequest)
  134. for imgR in imgSearchResults as [NSManagedObject]{
  135. image = imgR.value(forKey: "image")! as! String
  136. row.setImage(filename: image)
  137. }
  138. }
  139. catch {
  140. NSLog(":HOME:ERROR: Error getting image for activity \(id): \(error)")
  141. }
  142. parent.addArrangedSubview(row)
  143. }
  144. } catch {
  145. NSLog(":HOME:ERROR: Error loading future activities: \(error)")
  146. }
  147. }
  148. /**
  149. Sets up the blog section.
  150. :param: delegate App delegate.
  151. :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
  152. :param: parent Stack view to load the rows in.
  153. */
  154. func setUpBlog(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  155. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  156. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  157. let sortDescriptors = [sortDescriptor]
  158. fetchRequest.sortDescriptors = sortDescriptors
  159. fetchRequest.fetchLimit = 2
  160. do {
  161. let searchResults = try context.fetch(fetchRequest)
  162. var row : RowHomeBlog
  163. var count = 0
  164. var id: Int
  165. var title: String
  166. var text: String
  167. var image: String
  168. for r in searchResults as [NSManagedObject] {
  169. count = count + 1
  170. //Create a new row
  171. row = RowHomeBlog.init(s: "rowHomeBlog\(count)", i: count)
  172. id = r.value(forKey: "id")! as! Int
  173. title = r.value(forKey: "title_\(lang)")! as! String
  174. text = r.value(forKey: "text_\(lang)")! as! String
  175. row.id = id
  176. row.setTitle(text: title)
  177. row.setText(text: text)
  178. // Get main image
  179. image = ""
  180. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  181. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  182. let imgSortDescriptors = [imgSortDescriptor]
  183. imgFetchRequest.sortDescriptors = imgSortDescriptors
  184. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  185. imgFetchRequest.fetchLimit = 1
  186. do{
  187. let imgSearchResults = try context.fetch(imgFetchRequest)
  188. for imgR in imgSearchResults as [NSManagedObject]{
  189. image = imgR.value(forKey: "image")! as! String
  190. row.setImage(filename: image)
  191. }
  192. } catch {
  193. NSLog(":HOME:ERROR: Error getting image for post \(id): \(error)")
  194. }
  195. parent.addArrangedSubview(row)
  196. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  197. row.isUserInteractionEnabled = true
  198. row.addGestureRecognizer(tapRecognizer)
  199. }
  200. }
  201. catch {
  202. NSLog(":HOME:ERROR: Error loading the blog section: \(error)")
  203. }
  204. }
  205. /**
  206. Sets up the past activities section.
  207. :param: delegate App delegate.
  208. :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
  209. :param: parent Stack view to load the rows in.
  210. */
  211. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  212. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  213. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  214. let sortDescriptors = [sortDescriptor]
  215. fetchRequest.sortDescriptors = sortDescriptors
  216. fetchRequest.predicate = NSPredicate(format: "date <= %@", NSDate())
  217. fetchRequest.fetchLimit = 2
  218. do {
  219. let searchResults = try context.fetch(fetchRequest)
  220. var row : RowHomePastActivities
  221. var count = 0
  222. var id: Int
  223. var title: String
  224. var text: String
  225. var image: String
  226. for r in searchResults as [NSManagedObject] {
  227. count = count + 1
  228. //Create a new row
  229. row = RowHomePastActivities.init(s: "rowHomePastActivities\(count)", i: count)
  230. id = r.value(forKey: "id")! as! Int
  231. title = r.value(forKey: "title_\(lang)")! as! String
  232. text = r.value(forKey: "text_\(lang)")! as! String
  233. row.setTitle(text: title)
  234. row.setText(text: text)
  235. // Get main image
  236. image = ""
  237. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  238. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  239. let imgSortDescriptors = [imgSortDescriptor]
  240. imgFetchRequest.sortDescriptors = imgSortDescriptors
  241. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  242. imgFetchRequest.fetchLimit = 1
  243. do{
  244. let imgSearchResults = try context.fetch(imgFetchRequest)
  245. for imgR in imgSearchResults as [NSManagedObject]{
  246. image = imgR.value(forKey: "image")! as! String
  247. row.setImage(filename: image)
  248. }
  249. }
  250. catch {
  251. NSLog(":HOME:ERROR: Error getting image for past activity \(id): \(error)")
  252. }
  253. parent.addArrangedSubview(row)
  254. }
  255. } catch {
  256. NSLog(":HOME:ERROR: Error loading past activities: \(error)")
  257. }
  258. }
  259. /**
  260. Sets up the future activities section.
  261. :param: delegate App delegate.
  262. :param: parent Stack view to load the rows in.
  263. */
  264. func setUpGallery(context : NSManagedObjectContext, delegate: AppDelegate,parent: UIStackView){
  265. // Create the row
  266. var row: RowHomeGallery
  267. row = RowHomeGallery.init(s: "rowHomeGallery", i: 0)
  268. parent.addArrangedSubview(row)
  269. // Set images
  270. let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  271. let sortDescriptor = NSSortDescriptor(key: "uploaded", ascending: false)
  272. let sortDescriptors = [sortDescriptor]
  273. fetchRequest.sortDescriptors = sortDescriptors
  274. fetchRequest.fetchLimit = 4
  275. do {
  276. let searchResults = try context.fetch(fetchRequest)
  277. var id: Int
  278. var image: String
  279. var i = 0
  280. for r in searchResults as [NSManagedObject] {
  281. image = r.value(forKey: "file")! as! String
  282. row.setImage(idx: i, filename: image)
  283. //TODO Set click listener
  284. i = i + 1
  285. }
  286. }
  287. catch{
  288. NSLog(":HOME:ERROR: Error setting gallery up: \(error)")
  289. }
  290. }
  291. /**
  292. Sets up the social section.
  293. :param: parent Stack view to load the rows in.
  294. */
  295. func setUpSocial(parent : UIStackView){
  296. //Create a new row
  297. var row : RowHomeSocial
  298. row = RowHomeSocial.init(s: "rowHomeSocial", i: 0)
  299. parent.addArrangedSubview(row)
  300. }
  301. /**
  302. Opens a post.
  303. */
  304. func openPost(_ sender:UITapGestureRecognizer? = nil){
  305. NSLog(":HOME:DEBUG: Getting delegate and showing post.")
  306. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  307. delegate.controller?.showPost(id: (sender?.view as! RowHomeBlog).id)
  308. }
  309. }