HomeView.swift 12 KB

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