Pārlūkot izejas kodu

Initial sync is now performed and the app is initiated with data.

Iñigo Valentin 9 gadi atpakaļ
vecāks
revīzija
e8ffb19c7a

+ 1 - 0
app/AppDelegate.swift

@@ -29,6 +29,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 	var window: UIWindow?
 	var controller: ViewController?
 	var albumController: AlbumViewController?
+	var syncController: SyncController?
 
 	
 	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

+ 2 - 2
app/Base.lproj/Main.storyboard

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="3o1-ab-AT4">
     <device id="retina4_7" orientation="portrait">
         <adaptation id="fullscreen"/>
     </device>
@@ -198,7 +198,6 @@
                         <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="zJF-Mh-CNb" kind="show" identifier="SegueSchedule" id="A70-jo-yAP"/>
-                        <segue destination="3o1-ab-AT4" kind="show" identifier="SegueSync" id="E96-mK-gE9"/>
                         <segue destination="jfx-Q2-RtY" kind="show" identifier="SeguePastActivity" id="MmG-k2-wHn"/>
                         <segue destination="DDO-dI-c0J" kind="show" identifier="SegueFutureActivity" id="ec7-aO-4Ac"/>
                     </connections>
@@ -1029,6 +1028,7 @@
                     <connections>
                         <outlet property="lb" destination="pgU-dx-MvQ" id="ATL-SS-rxd"/>
                         <outlet property="pb" destination="VkR-DT-y3S" id="NMd-7R-Rlc"/>
+                        <segue destination="BYZ-38-t0r" kind="showDetail" identifier="SegueInit" action="showViewController:sender:" id="kSh-cP-och"/>
                     </connections>
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="fRm-1Y-Xmm" userLabel="First Responder" sceneMemberID="firstResponder"/>

+ 7 - 2
app/Sync.swift

@@ -33,7 +33,7 @@ class Sync{
 	var vGallery: Int = 0
 	var vBlanca: Int = 0
 	var vAll: Int = 0
-	
+		
 	/**
 	Starts the sync process.
 	Always asynchronously.
@@ -51,6 +51,7 @@ class Sync{
 		if synchronous == true{
 			NSLog(":SYNC:LOG: Synchronous sync started.")
 			self.initial = true
+			
 		}
 		let url = buildUrl();
 		sync(url: url)
@@ -94,6 +95,8 @@ class Sync{
 	func sync(url: URL){
 		
 		NSLog(":SYNC:LOG: Sync started.")
+		let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
+		delegate.syncController?.nowSyncing = true
 		
 		//Synchronously get data
 		let task = URLSession.shared.dataTask(with: url) { data, response, error in
@@ -198,8 +201,10 @@ class Sync{
 			// 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.syncController?.startApp()
 				let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
-				delegate.controller?.initialSync(showScreen: false)
+				delegate.syncController?.nowSyncing = false
 			}
 		}
 		

+ 72 - 5
app/SyncController.swift

@@ -26,6 +26,11 @@ The view controller of the app.
 */
 class SyncController: UIViewController{
 	
+	var delegate: AppDelegate?
+	
+	var nowSyncing: Bool = false
+	var syncTimer: Timer? = nil
+	
 	@IBOutlet weak var pb: UIProgressView!
 	@IBOutlet weak var lb: UILabel!
 	
@@ -39,18 +44,80 @@ class SyncController: UIViewController{
 	*/
 	override func viewDidLoad() {
 		
-		NSLog(":SYNC:LOG: SyncController shown.")
+		NSLog(":SYNCCONTROLLER:LOG: SyncController shown.")
+		self.delegate = UIApplication.shared.delegate as? AppDelegate
+		self.delegate?.syncController = self
 		
 		super.viewDidLoad()
 		
-		// Initial setup
-		pb.progress = 0
 		
-		// Start the timer
-		Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(progress), userInfo: nil, repeats: true)
 		
 	}
 	
+	override func viewDidAppear(_ animated: Bool) {
+		if UserDefaults.standard.object(forKey: "userId") == nil{
+			NSLog(":SYNCCONTROLLER:LOG: First run. Generating user ID and syncing...")
+			let newUid = randomString(length: 16)
+			let defaults = UserDefaults.standard
+			defaults.set(newUid, forKey: "userId")
+			// Initial setup
+			pb.progress = 0
+			
+			// Start the timer
+			Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(progress), userInfo: nil, repeats: true)
+			
+			syncTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(endSync), userInfo: nil, repeats: true)
+			
+			initialSync()
+		}
+		else{
+			startApp()
+		}
+	}
+	
+	func endSync(){
+		if self.nowSyncing == false{
+			syncTimer?.invalidate()
+			startApp()
+		}
+	}
+	
+	func startApp(){
+		performSegue(withIdentifier: "SegueInit", 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(){
+		//performSegue(withIdentifier: "SegueSync", sender: nil)
+		Sync(synchronous: 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
+	}
+	
 	
 	/**
 	Signal to update the progressbar and, maybe, the tag.

+ 7 - 6
app/ViewController.swift

@@ -60,6 +60,7 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	:param: coder Coder.
 	*/
 	required init?(coder aDecoder: NSCoder) {
+		NSLog(":CONTROLLER:DEBUG: Init.")
 		super.init(coder: aDecoder)
 		
 	}
@@ -146,14 +147,14 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	}
 	
 	
-	/** m
+	/** 
 	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{
-			performSegue(withIdentifier: "SegueSync", sender: nil)
+			//performSegue(withIdentifier: "SegueSync", sender: nil)
 			Sync(synchronous: true)
 		}
 		else{
@@ -227,8 +228,8 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 				
 		NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
 		//NSLog(":CONTROLLER:DEBUG: Skyp sync")
-		Notifications()
-		Sync()
+		//Notifications()
+		//Sync()
 
 		
 		self.delegate = UIApplication.shared.delegate as? AppDelegate
@@ -273,12 +274,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	*/
 	override func viewDidAppear(_ animated: Bool) {
 		super.viewDidAppear(animated)
-		if UserDefaults.standard.object(forKey: "userId") == nil{
+		/*if UserDefaults.standard.object(forKey: "userId") == nil{
 			let newUid = randomString(length: 16)
 			let defaults = UserDefaults.standard
 			defaults.set(newUid, forKey: "userId")
 			initialSync(showScreen: true)
-		}
+		}*/
 		
 	}