Kaynağa Gözat

Hidden unrequired sections.

Iñigo Valentin 9 yıl önce
ebeveyn
işleme
7115e48455
1 değiştirilmiş dosya ile 111 ekleme ve 52 silme
  1. 111 52
      app/HomeView.swift

+ 111 - 52
app/HomeView.swift

@@ -42,12 +42,19 @@ class HomeView: UIView {
 	var moc: NSManagedObjectContext? = nil
 	var delegate: AppDelegate? = nil
 	var lang: String? = nil
+	var locationTimer: Timer? = nil
 	
+	
+	/**
+	Default constructor for the storyboard.
+	:param: frame View frame.
+	*/
 	override init(frame: CGRect){
 		super.init(frame: frame)
 		
 	}
 	
+	
 	/**
 	 Run when the view is started.
 	*/
@@ -60,16 +67,14 @@ class HomeView: UIView {
 		self.addSubview(container)
 		container.frame = self.bounds
 		
-
-		
 		//Set titles for each section
-		locationSection.setTitle(text: "Encuentranos")
+		locationSection.setTitle(text: "Encuéntranos")
 		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")
+		futureActivitiesSection.setTitle(text: "Próximas actividades")
+		blogSection.setTitle(text: "Últimos posts")
+		gallerySection.setTitle(text: "Últimas fotos")
+		pastActivitiesSection.setTitle(text: "Últimas actividades")
+		socialSection.setTitle(text: "Síguenos")
 		
 		//Get info to populate sections
 		self.moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
@@ -78,20 +83,21 @@ class HomeView: UIView {
 		self.moc?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
 		self.lang = getLanguage()
 		
+		
+		// Populate section.
 		populate()
 		
-		/*//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);*/
+		
+		// Special case: Location
+		// Set up once and start and periodically witha  timer.
+		setUpLocation()
+		Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(setUpLocation), userInfo: nil, repeats: true)
 	}
 	
+	
+	/**
+	Sets all the sections up
+	*/
 	func populate(){
 	
 		//Populate sections
@@ -100,12 +106,10 @@ class HomeView: UIView {
 		setUpFutureActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.futureActivitiesSection.getContentStack())
 		setUpSocial(parent: self.socialSection.getContentStack())
 		setUpGallery(context: self.moc!, delegate: self.delegate!, parent: self.gallerySection.getContentStack())
-		
-		
-		//pastActivitiesSection.expandSection()
-
+		setUpLablanca(context: self.moc!, delegate: self.delegate!, self.lang!)
 	}
 	
+	
 	/**
 	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.
@@ -122,8 +126,54 @@ class HomeView: UIView {
 	}
 	
 	
+	/**
+	Sets up the location section.
+	If no location is reported, it hiddes the section.
+	*/
+	func setUpLocation(){
+		let defaults = UserDefaults.standard
+		if (defaults.value(forKey: "GMLocLat") != nil && defaults.value(forKey: "GMLocLon") != nil){
+			let time = defaults.value(forKey: "GMLocTime") as! Date
+			let cTime = Date()
+			let minutes = Calendar.current.dateComponents([.minute], from: time, to: cTime).minute
+			if (minutes! < 30){
+				self.locationSection.isHidden = false
+			}
+			else{
+				self.locationSection.isHidden = true
+			}
+		}
+		else{
+			self.locationSection.isHidden = true
+		}
+	}
+	
+	
+	/**
+	Sets up the La Blanca secction.
+	Hiddes it if no current festivals.
+	:param: context App context.
+	:param: delegate App delegate.
+	:param: lang Language code (two letter code, lowercase. Only 'es', 'en' and 'eu' supported).
+	*/
+	func setUpLablanca(context : NSManagedObjectContext, delegate: AppDelegate, lang: String){
+		let defaults = UserDefaults.standard
+		if (defaults.value(forKey: "festivals") != nil){
+			let festivals = defaults.value(forKey: "festivals") as! Int
+			if festivals == 1{
+				// TODO Actualy show something
+			}
+			else{
+				self.lablancaSection.isHidden = true
+			}
+		}
+	}
+	
+	
 	/**
 	Sets up the future activities section.
+	If none, it hiddes the section.
+	:param: context App context.
 	: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.
@@ -146,41 +196,46 @@ class HomeView: UIView {
 			var text: String
 			var image: String
 			
-			for r in searchResults as [NSManagedObject] {
-				count = count + 1
+			if searchResults.count == 0{
+				self.futureActivitiesSection.isHidden = true
+			}
+			else{
+				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)
-				row.id = id
+					//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)
+					row.id = id
 				
-				// 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)
+					// 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)")
 					}
-				}
-				catch {
-					NSLog(":HOME:ERROR: Error getting image for activity \(id): \(error)")
-				}
-				
-				parent.addArrangedSubview(row)
 				
+					parent.addArrangedSubview(row)
+				}
 			}
-		} catch {
+		}
+		catch {
 			NSLog(":HOME:ERROR: Error loading future activities: \(error)")
 		}
 	}
@@ -188,6 +243,7 @@ class HomeView: UIView {
 	
 	/**
 	Sets up the blog section.
+	:param: context App context.
 	: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.
@@ -256,6 +312,7 @@ class HomeView: UIView {
 	
 	/**
 	Sets up the past activities section.
+	:param: context App context.
 	: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.
@@ -318,8 +375,10 @@ class HomeView: UIView {
 		}
 	}
 	
+	
 	/**
 	Sets up the future activities section.
+	:param: context App context.
 	:param: delegate App delegate.
 	:param: parent Stack view to load the rows in.
 	*/