HomeView.swift 12 KB

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