HomeView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. var moc: NSManagedObjectContext? = nil
  40. var delegate: AppDelegate? = nil
  41. var lang: String? = nil
  42. override init(frame: CGRect){
  43. super.init(frame: frame)
  44. }
  45. /**
  46. Run when the view is started.
  47. */
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. //Load the contents of the HomeView.xib file.
  51. Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
  52. self.addSubview(container)
  53. container.frame = self.bounds
  54. //Set titles for each section
  55. locationSection.setTitle(text: "Encuentranos")
  56. lablancaSection.setTitle(text: "La Blanca")
  57. futureActivitiesSection.setTitle(text: "Proximas actividades")
  58. blogSection.setTitle(text: "Ultimos posts")
  59. gallerySection.setTitle(text: "Ultimas fotos")
  60. pastActivitiesSection.setTitle(text: "Ultimas actividades")
  61. socialSection.setTitle(text: "Siguenos")
  62. //Get info to populate sections
  63. self.moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  64. self.delegate = UIApplication.shared.delegate as! AppDelegate
  65. self.moc?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
  66. self.lang = getLanguage()
  67. populate()
  68. /*//Always at the end: update scrollview
  69. var h: Int = 0
  70. for view in scrollView.subviews {
  71. //contentRect = contentRect.union(view.frame);
  72. h = h + Int(view.frame.height) + 30 //Why 30?
  73. }
  74. // TODO: Calculate at the end
  75. self.scrollView.contentSize.height = 1300// CGFloat(h);*/
  76. }
  77. func populate(){
  78. //Populate sections
  79. setUpPastActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.pastActivitiesSection.getContentStack())
  80. setUpBlog(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.blogSection.getContentStack())
  81. setUpFutureActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.futureActivitiesSection.getContentStack())
  82. setUpSocial(parent: self.socialSection.getContentStack())
  83. setUpGallery(context: self.moc!, delegate: self.delegate!, parent: self.gallerySection.getContentStack())
  84. //pastActivitiesSection.expandSection()
  85. }
  86. /**
  87. Gets the device language. The only recognized languages are Spanish, English and Basque.
  88. If the device has another language, Spanish will be selected by default.
  89. :return: Two-letter language code.
  90. */
  91. func getLanguage() -> String{
  92. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  93. if(pre == "es" || pre == "en" || pre == "eu"){
  94. return pre
  95. }
  96. else{
  97. return "es"
  98. }
  99. }
  100. /**
  101. Sets up the future activities section.
  102. :param: delegate App delegate.
  103. :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
  104. :param: parent Stack view to load the rows in.
  105. */
  106. func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  107. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  108. let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  109. let sortDescriptors = [sortDescriptor]
  110. fetchRequest.sortDescriptors = sortDescriptors
  111. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  112. fetchRequest.fetchLimit = 2
  113. do {
  114. let searchResults = try context.fetch(fetchRequest)
  115. var row : RowHomeFutureActivities
  116. var count = 0
  117. var id: Int
  118. var title: String
  119. var text: String
  120. var image: String
  121. for r in searchResults as [NSManagedObject] {
  122. count = count + 1
  123. //Create a new row
  124. row = RowHomeFutureActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
  125. id = r.value(forKey: "id")! as! Int
  126. title = r.value(forKey: "title_\(lang)")! as! String
  127. text = r.value(forKey: "text_\(lang)")! as! String
  128. row.setTitle(text: title)
  129. row.setText(text: text.decode())
  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. // Get main image
  242. image = ""
  243. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  244. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  245. let imgSortDescriptors = [imgSortDescriptor]
  246. imgFetchRequest.sortDescriptors = imgSortDescriptors
  247. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  248. imgFetchRequest.fetchLimit = 1
  249. do{
  250. let imgSearchResults = try context.fetch(imgFetchRequest)
  251. for imgR in imgSearchResults as [NSManagedObject]{
  252. image = imgR.value(forKey: "image")! as! String
  253. row.setImage(filename: image)
  254. }
  255. }
  256. catch {
  257. NSLog(":HOME:ERROR: Error getting image for past activity \(id): \(error)")
  258. }
  259. parent.addArrangedSubview(row)
  260. }
  261. } catch {
  262. NSLog(":HOME:ERROR: Error loading past activities: \(error)")
  263. }
  264. }
  265. /**
  266. Sets up the future activities section.
  267. :param: delegate App delegate.
  268. :param: parent Stack view to load the rows in.
  269. */
  270. func setUpGallery(context : NSManagedObjectContext, delegate: AppDelegate,parent: UIStackView){
  271. // Create the row
  272. var row: RowHomeGallery
  273. row = RowHomeGallery.init(s: "rowHomeGallery", i: 0)
  274. parent.addArrangedSubview(row)
  275. // Set images
  276. let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  277. let sortDescriptor = NSSortDescriptor(key: "uploaded", ascending: false)
  278. let sortDescriptors = [sortDescriptor]
  279. fetchRequest.sortDescriptors = sortDescriptors
  280. fetchRequest.fetchLimit = 4
  281. do {
  282. let searchResults = try context.fetch(fetchRequest)
  283. var id: Int
  284. var image: String
  285. var i = 0
  286. for r in searchResults as [NSManagedObject] {
  287. image = r.value(forKey: "file")! as! String
  288. row.setImage(idx: i, filename: image)
  289. //TODO Set click listener
  290. i = i + 1
  291. }
  292. }
  293. catch{
  294. NSLog(":HOME:ERROR: Error setting gallery up: \(error)")
  295. }
  296. }
  297. /**
  298. Sets up the social section.
  299. :param: parent Stack view to load the rows in.
  300. */
  301. func setUpSocial(parent : UIStackView){
  302. //Create a new row
  303. var row : RowHomeSocial
  304. row = RowHomeSocial.init(s: "rowHomeSocial", i: 0)
  305. parent.addArrangedSubview(row)
  306. }
  307. /**
  308. Opens a post.
  309. */
  310. func openPost(_ sender:UITapGestureRecognizer? = nil){
  311. NSLog(":HOME:DEBUG: Getting delegate and showing post.")
  312. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  313. delegate.controller?.showPost(id: (sender?.view as! RowHomeBlog).id)
  314. }
  315. }