| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- // Copyright (C) 2016 Inigo Valentin
- //
- // This file is part of the Gasteizko Margolariak IOS app.
- //
- // The Gasteizko Margolariak IOS app is free software: you can
- // redistribute it and/or modify it under the terms of the
- // GNU General Public License as published by the Free Software
- // Foundation, either version 3 of the License, or (at your
- // option) any later version.
- //
- // The Gasteizko Margolariak IOS app is distributed in the
- // hope that it will be useful, but WITHOUT ANY WARRANTY;
- // without even the implied warranty of MERCHANTABILITY or
- // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- // Public License for more details.
- //
- // You should have received a copy of the GNU General Public
- // License along with the Gasteizko Margolariak IOS app.
- // If not, see <http://www.gnu.org/licenses/>.
- import Foundation
- import CoreData
- import UIKit
- /**
- Class to handle the home view.
- */
- class HomeView: UIView {
-
- //The main scroll view.
- @IBOutlet weak var scrollView: UIScrollView!
-
- //The container of the view.
- @IBOutlet weak var container: UIView!
-
- //Each of the sections of the view.
- @IBOutlet weak var locationSection: Section!
- @IBOutlet weak var lablancaSection: Section!
- @IBOutlet weak var futureActivitiesSection: Section!
- @IBOutlet weak var blogSection: Section!
- @IBOutlet weak var gallerySection: Section!
- @IBOutlet weak var pastActivitiesSection: Section!
- @IBOutlet weak var socialSection: Section!
-
- override init(frame: CGRect){
- super.init(frame: frame)
- }
-
- /**
- Run when the view is started.
- */
- required init?(coder aDecoder: NSCoder) {
-
- super.init(coder: aDecoder)
-
- //Load the contents of the HomeView.xib file.
- Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
- self.addSubview(container)
- container.frame = self.bounds
-
-
- //Set titles for each section
- locationSection.setTitle(text: "Encuentranos")
- lablancaSection.setTitle(text: "La Blanca")
- futureActivitiesSection.setTitle(text: "Proximas actividades")
- blogSection.setTitle(text: "Ultimos posts")
- gallerySection.setTitle(text: "Ultimas fotos")
- pastActivitiesSection.setTitle(text: "Ultimas actividades")
- socialSection.setTitle(text: "Siguenos")
-
- //Get info to populate sections
- let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- moc.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
- let lang : String = getLanguage()
-
- //Populate sections
- setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection.getContentStack())
- setUpBlog(context: moc, delegate: appDelegate, lang: lang, parent: blogSection.getContentStack())
- setUpFutureActivities(context: moc, delegate: appDelegate, lang: lang, parent: futureActivitiesSection.getContentStack())
- setUpSocial(parent: socialSection.getContentStack())
- setUpGallery(context: moc, delegate: appDelegate, parent: gallerySection.getContentStack())
-
-
- pastActivitiesSection.expandSection()
-
-
- //Always at the end: update scrollview
- var h: Int = 0
- for view in scrollView.subviews {
- //contentRect = contentRect.union(view.frame);
- h = h + Int(view.frame.height) + 30 //Why 30?
- }
- // TODO: Calculate at the end
- self.scrollView.contentSize.height = 1300// CGFloat(h);
- }
-
- /**
- Gets the device language. The only recognized languages are Spanish, English and Basque.
- If the device has another language, Spanish will be selected by default.
- :return: Two-letter language code.
- */
- func getLanguage() -> String{
- let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
- if(pre == "es" || pre == "en" || pre == "eu"){
- return pre
- }
- else{
- return "es"
- }
- }
-
-
- /**
- Sets up the future activities section.
- :param: delegate App delegate.
- :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
- :param: parent Stack view to load the rows in.
- */
- func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
- let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
- let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
- let sortDescriptors = [sortDescriptor]
- fetchRequest.sortDescriptors = sortDescriptors
- fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
- fetchRequest.fetchLimit = 2
-
- do {
- let searchResults = try context.fetch(fetchRequest)
-
- var row : RowHomeFutureActivities
- var count = 0
- var id: Int
- var title: String
- var text: String
- var image: String
-
- for r in searchResults as [NSManagedObject] {
- count = count + 1
-
- //Create a new row
- row = RowHomeFutureActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
- id = r.value(forKey: "id")! as! Int
- title = r.value(forKey: "title_\(lang)")! as! String
- text = r.value(forKey: "text_\(lang)")! as! String
- row.setTitle(text: title)
- row.setText(text: text.decode())
-
- // Get main image
- image = ""
- let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
- let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
- let imgSortDescriptors = [imgSortDescriptor]
- imgFetchRequest.sortDescriptors = imgSortDescriptors
- imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
- imgFetchRequest.fetchLimit = 1
- do{
- let imgSearchResults = try context.fetch(imgFetchRequest)
- for imgR in imgSearchResults as [NSManagedObject]{
- image = imgR.value(forKey: "image")! as! String
- row.setImage(filename: image)
- }
- }
- catch {
- NSLog(":HOME:ERROR: Error getting image for activity \(id): \(error)")
- }
-
- parent.addArrangedSubview(row)
-
- }
- } catch {
- NSLog(":HOME:ERROR: Error loading future activities: \(error)")
- }
- }
-
-
- /**
- Sets up the blog section.
- :param: delegate App delegate.
- :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
- :param: parent Stack view to load the rows in.
- */
- func setUpBlog(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
-
- let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
- let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
- let sortDescriptors = [sortDescriptor]
- fetchRequest.sortDescriptors = sortDescriptors
- fetchRequest.fetchLimit = 2
-
- do {
- let searchResults = try context.fetch(fetchRequest)
- var row : RowHomeBlog
- var count = 0
- var id: Int
- var title: String
- var text: String
- var image: String
-
- for r in searchResults as [NSManagedObject] {
- count = count + 1
-
- //Create a new row
- row = RowHomeBlog.init(s: "rowHomeBlog\(count)", i: count)
- id = r.value(forKey: "id")! as! Int
- title = r.value(forKey: "title_\(lang)")! as! String
- text = r.value(forKey: "text_\(lang)")! as! String
-
- row.id = id
- row.setTitle(text: title)
- row.setText(text: text)
-
- // Get main image
- image = ""
- let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
- let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
- let imgSortDescriptors = [imgSortDescriptor]
- imgFetchRequest.sortDescriptors = imgSortDescriptors
- imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
- imgFetchRequest.fetchLimit = 1
- do{
- let imgSearchResults = try context.fetch(imgFetchRequest)
- for imgR in imgSearchResults as [NSManagedObject]{
- image = imgR.value(forKey: "image")! as! String
- row.setImage(filename: image)
- }
- } catch {
- NSLog(":HOME:ERROR: Error getting image for post \(id): \(error)")
- }
-
- parent.addArrangedSubview(row)
-
- let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
- row.isUserInteractionEnabled = true
- row.addGestureRecognizer(tapRecognizer)
-
- }
- }
- catch {
- NSLog(":HOME:ERROR: Error loading the blog section: \(error)")
- }
- }
-
-
- /**
- Sets up the past activities section.
- :param: delegate App delegate.
- :param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
- :param: parent Stack view to load the rows in.
- */
- func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
-
- let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
- let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
- let sortDescriptors = [sortDescriptor]
- fetchRequest.sortDescriptors = sortDescriptors
- fetchRequest.predicate = NSPredicate(format: "date <= %@", NSDate())
- fetchRequest.fetchLimit = 2
-
- do {
- let searchResults = try context.fetch(fetchRequest)
-
- var row : RowHomePastActivities
- var count = 0
- var id: Int
- var title: String
- var text: String
- var image: String
-
- for r in searchResults as [NSManagedObject] {
- count = count + 1
-
- //Create a new row
- row = RowHomePastActivities.init(s: "rowHomePastActivities\(count)", i: count)
- id = r.value(forKey: "id")! as! Int
- title = r.value(forKey: "title_\(lang)")! as! String
- text = r.value(forKey: "text_\(lang)")! as! String
-
- row.setTitle(text: title)
- row.setText(text: text)
-
- // Get main image
- image = ""
- let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
- let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
- let imgSortDescriptors = [imgSortDescriptor]
- imgFetchRequest.sortDescriptors = imgSortDescriptors
- imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
- imgFetchRequest.fetchLimit = 1
- do{
- let imgSearchResults = try context.fetch(imgFetchRequest)
- for imgR in imgSearchResults as [NSManagedObject]{
- image = imgR.value(forKey: "image")! as! String
- row.setImage(filename: image)
- }
- }
- catch {
- NSLog(":HOME:ERROR: Error getting image for past activity \(id): \(error)")
- }
- parent.addArrangedSubview(row)
-
- }
- } catch {
- NSLog(":HOME:ERROR: Error loading past activities: \(error)")
- }
- }
-
- /**
- Sets up the future activities section.
- :param: delegate App delegate.
- :param: parent Stack view to load the rows in.
- */
- func setUpGallery(context : NSManagedObjectContext, delegate: AppDelegate,parent: UIStackView){
-
- // Create the row
- var row: RowHomeGallery
- row = RowHomeGallery.init(s: "rowHomeGallery", i: 0)
- parent.addArrangedSubview(row)
-
- // Set images
- let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
- let sortDescriptor = NSSortDescriptor(key: "uploaded", ascending: false)
- let sortDescriptors = [sortDescriptor]
- fetchRequest.sortDescriptors = sortDescriptors
- fetchRequest.fetchLimit = 4
-
- do {
- let searchResults = try context.fetch(fetchRequest)
-
- var id: Int
- var image: String
- var i = 0
- for r in searchResults as [NSManagedObject] {
-
- image = r.value(forKey: "file")! as! String
- row.setImage(idx: i, filename: image)
-
- //TODO Set click listener
- i = i + 1
- }
- }
- catch{
- NSLog(":HOME:ERROR: Error setting gallery up: \(error)")
- }
- }
-
-
- /**
- Sets up the social section.
- :param: parent Stack view to load the rows in.
- */
- func setUpSocial(parent : UIStackView){
- //Create a new row
- var row : RowHomeSocial
- row = RowHomeSocial.init(s: "rowHomeSocial", i: 0)
- parent.addArrangedSubview(row)
- }
-
-
- /**
- Opens a post.
- */
- func openPost(_ sender:UITapGestureRecognizer? = nil){
- NSLog(":HOME:DEBUG: Getting delegate and showing post.")
- let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
- delegate.controller?.showPost(id: (sender?.view as! RowHomeBlog).id)
- }
- }
|