Browse Source

HTML decoding and small fixes for the Past activities sections in the home screen.

Iñigo Valentin 9 years ago
parent
commit
fc9c5862da

+ 2 - 2
app.xcodeproj/project.pbxproj

@@ -101,11 +101,11 @@
 				93294FF71E336FA000FDAF50 /* Entry.swift */,
 				93294FF71E336FA000FDAF50 /* Entry.swift */,
 				93D9EA6B1DE3498500089002 /* HomeView.xib */,
 				93D9EA6B1DE3498500089002 /* HomeView.xib */,
 				93D9EA691DE3491100089002 /* HomeView.swift */,
 				93D9EA691DE3491100089002 /* HomeView.swift */,
-				93D9EA671DE342AC00089002 /* UIView.swift */,
 				93D9EA751DE3ACBB00089002 /* Sync.swift */,
 				93D9EA751DE3ACBB00089002 /* Sync.swift */,
-				931AA5881E1AFF8C003A2290 /* String.swift */,
 				931AA5AF1E219006003A2290 /* RowHomePastActivities.xib */,
 				931AA5AF1E219006003A2290 /* RowHomePastActivities.xib */,
 				93294FF91E337D0D00FDAF50 /* RowHomePastActivities.swift */,
 				93294FF91E337D0D00FDAF50 /* RowHomePastActivities.swift */,
+				931AA5881E1AFF8C003A2290 /* String.swift */,
+				93D9EA671DE342AC00089002 /* UIView.swift */,
 			);
 			);
 			path = app;
 			path = app;
 			sourceTree = "<group>";
 			sourceTree = "<group>";

BIN
app.xcodeproj/project.xcworkspace/xcuserdata/seavenois.xcuserdatad/UserInterfaceState.xcuserstate


+ 4 - 29
app/RowHomePastActivities.swift

@@ -71,10 +71,6 @@ Extension of UIView to be formatted as sections.
 
 
 		container.frame = v.bounds
 		container.frame = v.bounds
 		v.addSubview(container)
 		v.addSubview(container)
-		
-		
-		//v.sizeToFit()
-		
 	}
 	}
 	
 	
 	/**
 	/**
@@ -85,21 +81,19 @@ Extension of UIView to be formatted as sections.
 	}
 	}
 	
 	
 	/**
 	/**
-	Changes the title of the activity.
+	Changes the title of the activity. Decodes HTML.
 	:param: text The new title.
 	:param: text The new title.
 	*/
 	*/
 	func setTitle(text: String){
 	func setTitle(text: String){
-		print("Setting title: \(text)")
-		title.text = text
-		//descrip.text = text
+		title.text = text.stripHtml()
 	}
 	}
 	
 	
 	/**
 	/**
-	Changes the description of the activity.
+	Changes the description of the activity. Decodes HTML.
 	:param: text The new text.
 	:param: text The new text.
 	*/
 	*/
 	func setText(text: String){
 	func setText(text: String){
-		descrip.text = text
+		descrip.text = text.stripHtml()
 	}
 	}
 	
 	
 	/**
 	/**
@@ -110,23 +104,4 @@ Extension of UIView to be formatted as sections.
 	func setImage(path: String){
 	func setImage(path: String){
 		//TODO
 		//TODO
 	}
 	}
-	
-	
-	
-	
-	/*var view:UIView!
-	
-	func setup() {
-		view = loadViewFromNib()
-		view.frame = bounds
-		view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeightaddSubview(view)
-	}
-	
-	func loadViewFromNib() -> UIView {
-		let bundle = Bundle(for:type(of: self))
-		let nib = UINib(nibName: "RowHomePastActivities", bundle: bundle)
-		let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
-		
-		return view
-	}*/
 }
 }

+ 66 - 5
app/String.swift

@@ -1,18 +1,43 @@
+// Copyright (C) 2016 Inigo Valentin
 //
 //
-//  String.swift
-//  app
+// This file is part of the Gasteizko Margolariak IOS app.
 //
 //
-//  Created by Iñigo Valentin on 2/1/17.
-//  Copyright © 2017 Margolariak. All rights reserved.
+// 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 Foundation
+import UIKit
 
 
+/**
+Extension of String to include custom utilities.
+*/
 extension String {
 extension String {
+	
+	/**
+	The length of the string.
+	*/
 	var length:Int {
 	var length:Int {
 		return self.characters.count
 		return self.characters.count
 	}
 	}
 	
 	
+	/**
+	Finds the first position of a substring.
+	:param: target The substring to look for.
+	:return: The index of the first occurrence of the substring.
+	*/
 	func indexOf(target: String) -> Int? {
 	func indexOf(target: String) -> Int? {
 		
 		
 		let range = (self as NSString).range(of: target)
 		let range = (self as NSString).range(of: target)
@@ -24,6 +49,12 @@ extension String {
 		return range.location
 		return range.location
 		
 		
 	}
 	}
+	
+	/**
+	Finds the last position of a substring.
+	:param: target The substring to look for.
+	:return: The index of the las ocurrence of the substring.
+	*/
 	func lastIndexOf(target: String) -> Int? {
 	func lastIndexOf(target: String) -> Int? {
 		
 		
 		
 		
@@ -37,16 +68,46 @@ extension String {
 		return self.length - range.location - 1
 		return self.length - range.location - 1
 		
 		
 	}
 	}
+	
+	/**
+	Checks for a substring
+	:param: target Ths substring to look for.
+	:return: True if the substring is contained in the string, false otherwise.
+	*/
 	func contains(s: String) -> Bool {
 	func contains(s: String) -> Bool {
 		return (self.range(of: s) != nil) ? true : false
 		return (self.range(of: s) != nil) ? true : false
 	}
 	}
 	
 	
+	/**
+	Gets a subset of the string.
+	:param: start The beggining index of the substring.
+	:param: start The ending index of the substring.
+	:return: The substring.
+	*/
 	func subStr(start: Int, end: Int) -> String{
 	func subStr(start: Int, end: Int) -> String{
 		
 		
 		let st = self.index(self.startIndex, offsetBy: start)
 		let st = self.index(self.startIndex, offsetBy: start)
-		var ed = self.index(self.startIndex, offsetBy: end)
+		let ed = self.index(self.startIndex, offsetBy: end)
 		
 		
 		let range = st...ed
 		let range = st...ed
 		return self[range]
 		return self[range]
 	}
 	}
+	
+	/**
+	Removes HTML symbolf from the string. It also decodes HTML symbols.
+	:return: The string eithout the HTML symbols.
+	*/
+	func stripHtml() -> String {
+		let d = data(using: String.Encoding.utf8)
+		do{
+			return try NSAttributedString(data: d!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil).string
+		} catch let error as NSError {
+			print(error.localizedDescription)
+			return self
+		}
+		
+		// TODO implement
+		//return self
+		
+	}
 }
 }

+ 4 - 9
app/Sync.swift

@@ -58,7 +58,7 @@ class Sync{
 		//Synchronously get data
 		//Synchronously get data
 		let task = URLSession.shared.dataTask(with: url) { data, response, error in
 		let task = URLSession.shared.dataTask(with: url) { data, response, error in
 			guard error == nil else {
 			guard error == nil else {
-				print("error")
+				print("Sync error")
 				return
 				return
 			}
 			}
 			guard let data = data else {
 			guard let data = data else {
@@ -114,13 +114,9 @@ class Sync{
 				//go get the results
 				//go get the results
 				let searchResults = try context.fetch(fetchRequest)
 				let searchResults = try context.fetch(fetchRequest)
 				
 				
-				//I like to check the size of the returned results!
-				print ("num of results = \(searchResults.count)")
-				
-				//You need to convert to NSManagedObject to use 'for' loops
-				for trans in searchResults as [NSManagedObject] {
-					//get the Key Value pairs (although there may be a better way to do that...
-					print("\(trans.value(forKey: "id"))")
+				print ("Num of results = \(searchResults.count)")
+				for r in searchResults as [NSManagedObject] {
+					print("\(r.value(forKey: "id"))")
 				}
 				}
 			} catch {
 			} catch {
 				print("Error with request: \(error)")
 				print("Error with request: \(error)")
@@ -248,7 +244,6 @@ class Sync{
 			//Get price
 			//Get price
 			str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
 			str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
 			let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
 			let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
-			//print("Price: \(price)")
 			
 			
 			//Get inscription
 			//Get inscription
 			str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
 			str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)