LablancaView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 LablancaView: UIView {
  27. @IBOutlet var container: UIView!
  28. // App delegate
  29. var delegate: AppDelegate? = nil
  30. // Section row list
  31. //var rows: Array<RowGallery> = []
  32. /**
  33. Run when the view is started.
  34. */
  35. override init(frame: CGRect){
  36. super.init(frame: frame)
  37. }
  38. /**
  39. Run when the view is started.
  40. */
  41. required init?(coder aDecoder: NSCoder) {
  42. super.init(coder: aDecoder)
  43. NSLog(":LABLANCA:LOG: Init lablanca section.")
  44. Bundle.main.loadNibNamed("LablancaView", owner: self, options: nil)
  45. self.addSubview(container)
  46. container.frame = self.bounds
  47. //section.setTitle(text: "Gallery")
  48. //let parent = section.getContentStack()
  49. // Get viewController from StoryBoard
  50. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  51. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  52. // Show albums
  53. /*let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  54. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  55. self.delegate = appDelegate
  56. let lang : String = getLanguage()
  57. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  58. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  59. let sortDescriptor = NSSortDescriptor(key: "time", ascending: false)
  60. let sortDescriptors = [sortDescriptor]
  61. fetchRequest.sortDescriptors = sortDescriptors
  62. do {
  63. //Get the results
  64. let searchResults = try context.fetch(fetchRequest)
  65. var row : RowGallery
  66. var count = 0
  67. var id: Int
  68. var title: String
  69. for r in searchResults as [NSManagedObject] {
  70. count = count + 1
  71. //Create a new row
  72. row = RowGallery.init(s: "rowGallery\(count)", i: count)
  73. id = r.value(forKey: "id")! as! Int
  74. title = r.value(forKey: "title_\(lang)")! as! String
  75. row.id = id
  76. row.setTitle(text: title)
  77. // Get 4 random images from the album
  78. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  79. // TODO Order random
  80. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  81. //let imgSortDescriptors = [imgSortDescriptor]
  82. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  83. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  84. imgFetchRequest.fetchLimit = 4
  85. do{
  86. let imgSearchResults = try context.fetch(imgFetchRequest)
  87. // Loop images
  88. var imageIdx = 0
  89. for imgR in imgSearchResults as [NSManagedObject]{
  90. let imageId = imgR.value(forKey: "photo")! as! Int
  91. // For each image, I need a new query to fetch from photo entity.
  92. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  93. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  94. singleImgFetchRequest.fetchLimit = 4
  95. do{
  96. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  97. // Loop image
  98. for sImgR in singleImgSearchResults as [NSManagedObject]{
  99. let image = sImgR.value(forKey: "file")! as! String
  100. row.setImage(idx: imageIdx, filename: image)
  101. }
  102. imageIdx = imageIdx + 1
  103. }
  104. catch {
  105. NSLog(":GALLERY:ERROR: Error getting image from photo entity: \(error)")
  106. }
  107. }
  108. } catch {
  109. NSLog(":GALLERY:ERROR: Error getting image for album \(id): \(error)")
  110. }
  111. parent.addArrangedSubview(row)
  112. // Add to the rows array
  113. self.rows.append(row)
  114. }
  115. } catch {
  116. NSLog(":GALLERY:ERROR: Error with request: \(error)")
  117. }
  118. //Always at the end: update scrollview
  119. var h: Int = 0
  120. for view in scrollView.subviews {
  121. //contentRect = contentRect.union(view.frame);
  122. h = h + Int(view.frame.height) + 30 //Why 30?
  123. }
  124. // TODO: Calculate at the end
  125. self.scrollView.contentSize.height = 2500//CGFloat(h);
  126. self.setUpRowsTapRecognizers()
  127. NSLog(":GALLERY:LOG: Finished loading gallery section.")*/
  128. }
  129. func getLanguage() -> String{
  130. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  131. if(pre == "es" || pre == "en" || pre == "eu"){
  132. return pre
  133. }
  134. else{
  135. return "es"
  136. }
  137. }
  138. }