HomeView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. print("curh: \(h)")
  77. }
  78. // TODO: Calculate at the end
  79. self.scrollView.contentSize.height = 1300// CGFloat(h);
  80. }
  81. func getLanguage() -> String{
  82. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  83. if(pre == "es" || pre == "en" || pre == "eu"){
  84. return pre
  85. }
  86. else{
  87. return "es"
  88. }
  89. }
  90. func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  91. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  92. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  93. let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  94. let sortDescriptors = [sortDescriptor]
  95. fetchRequest.sortDescriptors = sortDescriptors
  96. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  97. fetchRequest.fetchLimit = 2
  98. do {
  99. //go get the results
  100. let searchResults = try context.fetch(fetchRequest)
  101. //I like to check the size of the returned results!
  102. print ("Activities: \(searchResults.count)")
  103. var row : RowHomePastActivities
  104. var count = 0
  105. var id: Int
  106. var title: String
  107. var text: String
  108. var image: String
  109. //You need to convert to NSManagedObject to use 'for' loops
  110. for r in searchResults as [NSManagedObject] {
  111. count = count + 1
  112. //get the Key Value pairs (although there may be a better way to do that...
  113. print("Perm: \(r.value(forKey: "permalink"))")
  114. //Create a new row
  115. row = RowHomePastActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
  116. id = r.value(forKey: "id")! as! Int
  117. title = r.value(forKey: "title_\(lang)")! as! String
  118. text = r.value(forKey: "text_\(lang)")! as! String
  119. print(title)
  120. row.setTitle(text: title)
  121. row.setText(text: text)
  122. // Get main image
  123. image = ""
  124. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  125. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  126. let imgSortDescriptors = [imgSortDescriptor]
  127. imgFetchRequest.sortDescriptors = imgSortDescriptors
  128. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  129. imgFetchRequest.fetchLimit = 1
  130. do{
  131. let imgSearchResults = try context.fetch(imgFetchRequest)
  132. for imgR in imgSearchResults as [NSManagedObject]{
  133. image = imgR.value(forKey: "image")! as! String
  134. print ("IMAGE: \(image)")
  135. row.setImage(filename: image)
  136. }
  137. } catch {
  138. print("Error getting image for activity \(id): \(error)")
  139. }
  140. print("Row height: \(row.frame.height)")
  141. parent.addArrangedSubview(row)
  142. }
  143. } catch {
  144. print("Error with request: \(error)")
  145. }
  146. }
  147. func setUpBlog(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  148. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  149. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  150. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  151. let sortDescriptors = [sortDescriptor]
  152. fetchRequest.sortDescriptors = sortDescriptors
  153. fetchRequest.fetchLimit = 2
  154. do {
  155. //go get the results
  156. let searchResults = try context.fetch(fetchRequest)
  157. //I like to check the size of the returned results!
  158. print ("Post: \(searchResults.count)")
  159. var row : RowHomeBlog
  160. var count = 0
  161. var id: Int
  162. var title: String
  163. var text: String
  164. var image: String
  165. //You need to convert to NSManagedObject to use 'for' loops
  166. for r in searchResults as [NSManagedObject] {
  167. count = count + 1
  168. //get the Key Value pairs (although there may be a better way to do that...
  169. print("Perm: \(r.value(forKey: "permalink"))")
  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. print ("IMAGE: \(image)")
  191. row.setImage(filename: image)
  192. }
  193. } catch {
  194. print("Error getting image for post \(id): \(error)")
  195. }
  196. print("Row height: \(row.frame.height)")
  197. parent.addArrangedSubview(row)
  198. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  199. row.isUserInteractionEnabled = true
  200. row.addGestureRecognizer(tapRecognizer)
  201. }
  202. } catch {
  203. print("Error with request: \(error)")
  204. }
  205. }
  206. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  207. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  208. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  209. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  210. let sortDescriptors = [sortDescriptor]
  211. fetchRequest.sortDescriptors = sortDescriptors
  212. fetchRequest.predicate = NSPredicate(format: "date <= %@", NSDate())
  213. fetchRequest.fetchLimit = 2
  214. do {
  215. //go get the results
  216. let searchResults = try context.fetch(fetchRequest)
  217. //I like to check the size of the returned results!
  218. print ("Activities: \(searchResults.count)")
  219. var row : RowHomePastActivities
  220. var count = 0
  221. var id: Int
  222. var title: String
  223. var text: String
  224. var image: String
  225. //You need to convert to NSManagedObject to use 'for' loops
  226. for r in searchResults as [NSManagedObject] {
  227. count = count + 1
  228. //get the Key Value pairs (although there may be a better way to do that...
  229. print("Perm: \(r.value(forKey: "permalink"))")
  230. //Create a new row
  231. row = RowHomePastActivities.init(s: "rowHomePastActivities\(count)", i: count)
  232. id = r.value(forKey: "id")! as! Int
  233. title = r.value(forKey: "title_\(lang)")! as! String
  234. text = r.value(forKey: "text_\(lang)")! as! String
  235. row.setTitle(text: title)
  236. row.setText(text: text)
  237. // Get main image
  238. image = ""
  239. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  240. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  241. let imgSortDescriptors = [imgSortDescriptor]
  242. imgFetchRequest.sortDescriptors = imgSortDescriptors
  243. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  244. imgFetchRequest.fetchLimit = 1
  245. do{
  246. let imgSearchResults = try context.fetch(imgFetchRequest)
  247. for imgR in imgSearchResults as [NSManagedObject]{
  248. image = imgR.value(forKey: "image")! as! String
  249. print ("IMAGE: \(image)")
  250. row.setImage(filename: image)
  251. }
  252. } catch {
  253. print("Error getting image for activity \(id): \(error)")
  254. }
  255. print("Row height: \(row.frame.height)")
  256. parent.addArrangedSubview(row)
  257. }
  258. } catch {
  259. print("Error with request: \(error)")
  260. }
  261. }
  262. func setUpGallery(context : NSManagedObjectContext, delegate: AppDelegate,parent: UIStackView){
  263. print("HOME:LOG: Setting up gallery.")
  264. // Create the row
  265. var row: RowHomeGallery
  266. row = RowHomeGallery.init(s: "rowHomeGallery", i: 0)
  267. parent.addArrangedSubview(row)
  268. // Set images
  269. let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  270. let sortDescriptor = NSSortDescriptor(key: "uploaded", ascending: false)
  271. let sortDescriptors = [sortDescriptor]
  272. fetchRequest.sortDescriptors = sortDescriptors
  273. fetchRequest.fetchLimit = 4
  274. do {
  275. //go get the results
  276. let searchResults = try context.fetch(fetchRequest)
  277. var id: Int
  278. var image: String
  279. // Loop images
  280. var i = 0
  281. for r in searchResults as [NSManagedObject] {
  282. image = r.value(forKey: "file")! as! String
  283. row.setImage(idx: i, filename: image)
  284. //TODO Set click listener
  285. i = i + 1
  286. }
  287. }
  288. catch{
  289. print("HOME:ERROR: Error setting gallery up: \(error)")
  290. }
  291. }
  292. func setUpSocial(parent : UIStackView){
  293. print("SETING UP SOCIAL")
  294. //Create a new row
  295. var row : RowHomeSocial
  296. row = RowHomeSocial.init(s: "rowHomeSocial", i: 0)
  297. print("ROW CREATED")
  298. parent.addArrangedSubview(row)
  299. }
  300. func openPost(_ sender:UITapGestureRecognizer? = nil){
  301. print("HOME:DEBUG: getting delegate and showing post.")
  302. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  303. delegate.controller?.showPost(id: (sender?.view as! RowHomeBlog).id)
  304. print("HOME:DEBUG: Post should be shown.")
  305. }
  306. }