Просмотр исходного кода

Preparing the image loading and caching.

Iñigo Valentin 9 лет назад
Родитель
Сommit
c5f03e2a36

+ 4 - 0
app.xcodeproj/project.pbxproj

@@ -13,6 +13,7 @@
 		931AA5B01E219006003A2290 /* RowHomePastActivities.xib in Resources */ = {isa = PBXBuildFile; fileRef = 931AA5AF1E219006003A2290 /* RowHomePastActivities.xib */; };
 		93294FF81E336FA000FDAF50 /* Entry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93294FF71E336FA000FDAF50 /* Entry.swift */; };
 		93294FFA1E337D0D00FDAF50 /* RowHomePastActivities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93294FF91E337D0D00FDAF50 /* RowHomePastActivities.swift */; };
+		935892D01E493C2300E1CD5F /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 935892CF1E493C2300E1CD5F /* UIImageView.swift */; };
 		93B408031D9ED44C000FBC99 /* MenuCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */; };
 		93B84B9A1D9C31AB006C0F67 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B991D9C31AB006C0F67 /* AppDelegate.swift */; };
 		93B84B9C1D9C31AB006C0F67 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B9B1D9C31AB006C0F67 /* ViewController.swift */; };
@@ -36,6 +37,7 @@
 		931AA5AF1E219006003A2290 /* RowHomePastActivities.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RowHomePastActivities.xib; sourceTree = "<group>"; };
 		93294FF71E336FA000FDAF50 /* Entry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Entry.swift; sourceTree = "<group>"; };
 		93294FF91E337D0D00FDAF50 /* RowHomePastActivities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowHomePastActivities.swift; sourceTree = "<group>"; };
+		935892CF1E493C2300E1CD5F /* UIImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageView.swift; sourceTree = "<group>"; };
 		93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MenuCollectionViewCell.swift; path = ../MenuCollectionViewCell.swift; sourceTree = "<group>"; };
 		93B84B961D9C31AB006C0F67 /* app.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = app.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		93B84B991D9C31AB006C0F67 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -106,6 +108,7 @@
 				93294FF91E337D0D00FDAF50 /* RowHomePastActivities.swift */,
 				931AA5881E1AFF8C003A2290 /* String.swift */,
 				93D9EA671DE342AC00089002 /* UIView.swift */,
+				935892CF1E493C2300E1CD5F /* UIImageView.swift */,
 			);
 			path = app;
 			sourceTree = "<group>";
@@ -200,6 +203,7 @@
 				93D9EA641DE273FD00089002 /* Section.swift in Sources */,
 				93D9EA6A1DE3491100089002 /* HomeView.swift in Sources */,
 				93294FFA1E337D0D00FDAF50 /* RowHomePastActivities.swift in Sources */,
+				935892D01E493C2300E1CD5F /* UIImageView.swift in Sources */,
 				93D9EA761DE3ACBB00089002 /* Sync.swift in Sources */,
 				931AA5891E1AFF8C003A2290 /* String.swift in Sources */,
 				93B84B9C1D9C31AB006C0F67 /* ViewController.swift in Sources */,

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


+ 24 - 2
app/HomeView.swift

@@ -119,9 +119,11 @@ class HomeView: UIView {
 			print ("Activities: \(searchResults.count)")
 			
 			var row : RowHomePastActivities
-			var count = 0;
-			var title : String
+			var count = 0
+			var id: Int
+			var title: String
 			var text: String
+			var image: String
 			
 			//You need to convert to NSManagedObject to use 'for' loops
 			for r in searchResults as [NSManagedObject] {
@@ -132,12 +134,32 @@ class HomeView: UIView {
 				
 				//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
 				print(title)
 				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
+						print ("IMAGE: \(image)")
+						row.setImage(filename: image)
+					}
+				} catch {
+					print("Error getting image for activity \(id): \(error)")
+				}
+				
 				print("Row height: \(row.frame.height)")
 				
 				parent.addArrangedSubview(row)

+ 14 - 3
app/RowHomePastActivities.swift

@@ -38,9 +38,12 @@ Extension of UIView to be formatted as sections.
 	//The activity title.
 	@IBOutlet weak var title: UILabel!
 	
-	//The activity description
+	//The activity description.
 	@IBOutlet weak var descrip: UILabel!
 	
+	//The activity image.
+	@IBOutlet weak var image: UIImageView!
+	
 	/**
 	Default constructor
 	*/
@@ -101,7 +104,15 @@ Extension of UIView to be formatted as sections.
 	If null or empty, the igage is hidden.
 	:param: path The new text.
 	*/
-	func setImage(path: String){
-		//TODO
+	func setImage(filename: String){
+		if (filename == ""){
+			print("No image")
+			//TODO hide the imageview
+		}
+		else{
+			print("Set image \(filename)")
+			let path = "img/actividades/thumb/\(filename)"
+			image.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
+		}
 	}
 }

+ 1 - 0
app/RowHomePastActivities.xib

@@ -15,6 +15,7 @@
                 <outlet property="container" destination="2hI-rc-KPm" id="rWX-Vl-kOp"/>
                 <outlet property="descrip" destination="f4E-5p-Bid" id="IF5-ck-qUL"/>
                 <outlet property="entry" destination="wLM-VU-VYk" id="Gkq-yE-fdN"/>
+                <outlet property="image" destination="WSe-uz-Uhz" id="iU4-jm-UzQ"/>
                 <outlet property="title" destination="Bkz-DV-HVr" id="1ok-yI-Npt"/>
             </connections>
         </placeholder>

+ 56 - 0
app/UIImageView.swift

@@ -0,0 +1,56 @@
+// 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 UIKit
+
+/**
+Extension of UIImageView to include custom utilities.
+*/
+extension UIImageView {
+	
+	/**
+	Sets an image in the UIImageView. If the image is downloaded, it will not download it again.
+	If it isn't, it will download it.
+	:param: localPath Path where the image is or will be.
+	:param: remotePath Url of the image to download (if not cached).
+	:return: 0 if the file was set form the local path, 1 if the file was downloaded, other for error.
+	*/
+	func setImage(localPath: String, remotePath: String) -> Int{
+		//Check if the local image exists.
+		let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
+		let url = NSURL(fileURLWithPath: path)
+		let filePath = url.appendingPathComponent(localPath)?.path
+		let fileManager = FileManager.default
+		if fileManager.fileExists(atPath: filePath!) {
+			print("FILE AVAILABLE: \(filePath)")
+			//TODO Set image
+			return 0
+		} else {
+			print("FILE NOT AVAILABLE: \(filePath)")
+			print("Downloading from \(remotePath)")
+			//TODO Download
+			//TODO Set image
+			return 1
+		}
+
+	}
+	
+}