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

Merge branch 'sync' into BaseProject

Iñigo Valentin 9 лет назад
Родитель
Сommit
a9aa97c9a8
3 измененных файлов с 171 добавлено и 10 удалено
  1. 57 1
      app/Base.lproj/Main.storyboard
  2. 24 0
      app/Sync.swift
  3. 90 9
      app/ViewController.swift

+ 57 - 1
app/Base.lproj/Main.storyboard

@@ -64,7 +64,7 @@
                                 </collectionViewFlowLayout>
                                 <cells>
                                     <collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="cell" id="a4j-DF-epb" customClass="MenuCollectionViewCell" customModule="app" customModuleProvider="target">
-                                        <rect key="frame" x="0.0" y="-3" width="100" height="50"/>
+                                        <rect key="frame" x="0.0" y="-2.5" width="100" height="50"/>
                                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                         <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
                                             <rect key="frame" x="0.0" y="0.0" width="100" height="50"/>
@@ -187,6 +187,7 @@
                         <outlet property="containerViewLocation" destination="T4H-Rn-I4y" id="EFa-Km-WuR"/>
                         <segue destination="UkS-wI-Yhx" kind="show" identifier="SeguePost" id="gs2-jd-cPW"/>
                         <segue destination="k0M-Sk-DZM" kind="show" identifier="SegueAlbum" id="Puc-tv-vaD"/>
+                        <segue destination="3o1-ab-AT4" kind="show" identifier="SegueSync" id="E96-mK-gE9"/>
                     </connections>
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
@@ -689,8 +690,63 @@
             </objects>
             <point key="canvasLocation" x="-1978" y="1477"/>
         </scene>
+        <!--Sync-->
+        <scene sceneID="cqg-BI-2xm">
+            <objects>
+                <viewController title="Sync" id="3o1-ab-AT4" sceneMemberID="viewController">
+                    <layoutGuides>
+                        <viewControllerLayoutGuide type="top" id="RYy-ax-Nya"/>
+                        <viewControllerLayoutGuide type="bottom" id="rXD-89-8EQ"/>
+                    </layoutGuides>
+                    <view key="view" contentMode="scaleToFill" id="aKm-vb-AZR">
+                        <rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
+                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
+                        <subviews>
+                            <imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Logo" translatesAutoresizingMaskIntoConstraints="NO" id="dxh-Z0-luU">
+                                <rect key="frame" x="48" y="242.5" width="224" height="83"/>
+                            </imageView>
+                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Sincronizando..." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="up0-1V-tTd">
+                                <rect key="frame" x="0.0" y="335.5" width="320" height="30"/>
+                                <constraints>
+                                    <constraint firstAttribute="height" constant="30" id="mIj-oN-tq7"/>
+                                </constraints>
+                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
+                                <nil key="textColor"/>
+                                <nil key="highlightedColor"/>
+                            </label>
+                            <progressView opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="750" progress="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="VkR-DT-y3S">
+                                <rect key="frame" x="48" y="375.5" width="224" height="2"/>
+                            </progressView>
+                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Message" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pgU-dx-MvQ">
+                                <rect key="frame" x="48" y="385.5" width="224" height="18"/>
+                                <fontDescription key="fontDescription" type="italicSystem" pointSize="15"/>
+                                <color key="textColor" red="0.31238102791878175" green="0.31238102791878175" blue="0.31238102791878175" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
+                                <nil key="highlightedColor"/>
+                            </label>
+                        </subviews>
+                        <color key="backgroundColor" red="0.70980392160000005" green="0.82745098039999998" blue="0.8980392157" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
+                        <constraints>
+                            <constraint firstItem="VkR-DT-y3S" firstAttribute="top" secondItem="up0-1V-tTd" secondAttribute="bottom" constant="10" id="2jT-Qj-SnX"/>
+                            <constraint firstItem="VkR-DT-y3S" firstAttribute="width" secondItem="dxh-Z0-luU" secondAttribute="width" id="5pk-5G-ix0"/>
+                            <constraint firstItem="up0-1V-tTd" firstAttribute="centerX" secondItem="dxh-Z0-luU" secondAttribute="centerX" id="C1h-cG-8Sx"/>
+                            <constraint firstItem="pgU-dx-MvQ" firstAttribute="centerX" secondItem="VkR-DT-y3S" secondAttribute="centerX" id="QB4-zO-roz"/>
+                            <constraint firstItem="dxh-Z0-luU" firstAttribute="width" secondItem="aKm-vb-AZR" secondAttribute="width" multiplier="0.7" id="UXD-fj-qLn"/>
+                            <constraint firstItem="dxh-Z0-luU" firstAttribute="centerY" secondItem="aKm-vb-AZR" secondAttribute="centerY" id="aC5-zC-5I4"/>
+                            <constraint firstItem="up0-1V-tTd" firstAttribute="width" secondItem="aKm-vb-AZR" secondAttribute="width" id="c7R-MO-YhC"/>
+                            <constraint firstItem="pgU-dx-MvQ" firstAttribute="width" secondItem="VkR-DT-y3S" secondAttribute="width" id="dUS-Vx-M16"/>
+                            <constraint firstItem="VkR-DT-y3S" firstAttribute="centerX" secondItem="dxh-Z0-luU" secondAttribute="centerX" id="dfj-BN-WnD"/>
+                            <constraint firstItem="dxh-Z0-luU" firstAttribute="centerX" secondItem="aKm-vb-AZR" secondAttribute="centerX" id="uFb-fC-wRh"/>
+                            <constraint firstItem="up0-1V-tTd" firstAttribute="top" secondItem="dxh-Z0-luU" secondAttribute="bottom" constant="10" id="wY9-12-hcE"/>
+                            <constraint firstItem="pgU-dx-MvQ" firstAttribute="top" secondItem="VkR-DT-y3S" secondAttribute="bottom" constant="8" id="wts-Yk-LsB"/>
+                        </constraints>
+                    </view>
+                </viewController>
+                <placeholder placeholderIdentifier="IBFirstResponder" id="fRm-1Y-Xmm" userLabel="First Responder" sceneMemberID="firstResponder"/>
+            </objects>
+        </scene>
     </scenes>
     <resources>
+        <image name="Logo" width="83" height="83"/>
         <image name="PhotoPlaceholder" width="170" height="108"/>
     </resources>
 </document>

+ 24 - 0
app/Sync.swift

@@ -27,14 +27,30 @@ Class to handle server sync.
 */
 class Sync{
 	
+	var initial: Bool = false
+	
 	/**
 	Starts the sync process.
+	Always asynchronously.
 	*/
 	init(){
 		let url = buildUrl();
 		sync(url: url)
 	}
 	
+	/**
+	Starts the sync process, synchronously or asynchronously.
+	:param: synchronous True for synchronous sync, false for asynchronously
+	*/
+	init(synchronous: Bool){
+		if synchronous == true{
+			NSLog(":SYNC:LOG: Synchronous sync started.")
+			self.initial = true
+		}
+		let url = buildUrl();
+		sync(url: url)
+	}
+	
 	/**
 	Builds the URL to perform the sync against.
 	:returns: The URL.
@@ -141,6 +157,14 @@ class Sync{
 				NSLog(":SYNC:DEBUG: Error with request: \(error).")
 			}*/
 			//End tester
+			
+			// If it's the initial sync, hide the segue
+			if self.initial == true{
+				NSLog(":SYNC:LOG: Finishing synchronous sync.")
+				let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
+				delegate.controller?.initialSync(showScreen: false)
+				
+			}
 		}
 		
 		task.resume()

+ 90 - 9
app/ViewController.swift

@@ -30,6 +30,9 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	
 	//The menu collection.
 	var sectionCollection: UICollectionView!
+	
+	// Need to save the sync segue.
+	var syncSegue: UIStoryboardSegue?
 
 	//Each of the sections of the app.
 	@IBOutlet var containerViewGallery: GalleryView!
@@ -41,25 +44,66 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	
 	var passId: Int = -1
 	
+	/**
+	 Controller initializer.
+	*/
+	required init?(coder aDecoder: NSCoder) {
+		super.init(coder: aDecoder)
+	}
+	
+	/**
+	 Retrieves the application context.
+	 :return: The application context.
+	*/
 	func getContext () -> NSManagedObjectContext {
 		//let appDelegate = UIApplication.shared.delegate as! AppDelegate
 		//return appDelegate.persistentContainer.viewContext
 		return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
 	}
 	
+	/**
+	 Shows a post by loading the controller.
+	 :param: id The post id.
+	*/
 	func showPost(id: Int){
-		print("CONTROLLER:DEBUG: Showing Post \(id)")
+		NSLog(":CONTROLLER:DEBUG: Showing Post \(id)")
 		self.passId = id
 		performSegue(withIdentifier: "SeguePost", sender: nil)
 	}
 	
+	/**
+	 Shows a post by loading the controller.
+	 :param: id The album id.
+	*/
 	func showAlbum(id: Int){
 		NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
 		self.passId = id
-		// TODO: Uncomment when ready
 		performSegue(withIdentifier: "SegueAlbum", sender: nil)
 	}
 	
+	/**
+	 Handles the initial sync process.
+	 It can start it, showing the sync screen, or finish it, hidding the screen.
+	 :param: showScreen True to start the sync, false to end it.
+	*/
+	func initialSync(showScreen: Bool){
+		if showScreen == true{
+			NSLog(":CONTROLLER:DEBUG: Showing initial sync screen.")
+			performSegue(withIdentifier: "SegueSync", sender: nil)
+			Sync(synchronous: true)
+		}
+		else{
+			NSLog(":CONTROLLER:DEBUG: Hidding initial sync screen.")
+			syncSegue?.destination.dismiss(animated: true, completion: nil)
+		}
+	}
+	
+	/**
+	 Run before performing a segue.
+	 Assigns id if neccessary.
+	 :param: segue The segue to perform.
+	 :sender: The calling view.
+	*/
 	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 		NSLog(":CONTROLLER:DEBUG: preparing for segue '\(segue.identifier)' with id \(self.passId)")
 		if segue.identifier == "SeguePost"{
@@ -68,6 +112,9 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		if segue.identifier == "SegueAlbum"{
 			(segue.destination as! AlbumViewController).id = passId
 		}
+		if segue.identifier == "SegueSync"{
+			self.syncSegue = segue
+		}
 	}
 	
 	/**
@@ -81,23 +128,57 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		self.containerViewActivities.alpha = 0
 		self.containerViewBlog.alpha = 0
 		self.containerViewGallery.alpha = 0
-		
-		
-		//self.containerViewBlog.isHidden = true
 				
-		print("CONTROLLER:LOG: viewDidLoad()")
-		print("CONTROLLER:DEBUG: Skyp sync")
+		NSLog(":CONTROLLER:LOG: viewDidLoad()")
+		NSLog(":CONTROLLER:DEBUG: Skyp sync")
 		//Sync()
 		
 		super.viewDidLoad()
-		// Do any additional setup after loading the view, typically from a nib.
 
-		
 		//self.containerViewBlog.setController(controller: self as ViewController)
 		
 		delegate = UIApplication.shared.delegate as! AppDelegate
 		delegate?.controller = self
 		
+		
+	}
+	
+	/**
+	 Executed when the view is actually shown.
+	 Performs the initial sync in the first run.
+	 It also generates a user id if none exists.
+	 :param: animated Wether the controller appearance must be animated or not.
+	*/
+	override func viewDidAppear(_ animated: Bool) {
+		super.viewDidAppear(animated)
+		if UserDefaults.standard.object(forKey: "userId") == nil{
+			let newUid = randomString(length: 16)
+			let defaults = UserDefaults.standard
+			defaults.set(newUid, forKey: "userId")
+			initialSync(showScreen: true)
+		}
+		
+	}
+	
+	/**
+	 Generates a random string to be used as a user identifier.
+	 :param: length The length of the generated string.
+	 :return: A random alphanumeric string with the indicated length.
+	*/
+	func randomString(length: Int) -> String {
+		
+		let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+		let len = UInt32(letters.length)
+		
+		var randomString = ""
+		
+		for _ in 0 ..< length {
+			let rand = arc4random_uniform(len)
+			var nextChar = letters.character(at: Int(rand))
+			randomString += NSString(characters: &nextChar, length: 1) as String
+		}
+		
+		return randomString
 	}
 
 	/**