瀏覽代碼

Sync screen shown only during the initial sync.

Iñigo Valentin 9 年之前
父節點
當前提交
e615d7341e
共有 3 個文件被更改,包括 78 次插入3 次删除
  1. 1 0
      app/Base.lproj/Main.storyboard
  2. 22 0
      app/Sync.swift
  3. 55 3
      app/ViewController.swift

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

@@ -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"/>

+ 22 - 0
app/Sync.swift

@@ -27,6 +27,8 @@ Class to handle server sync.
 */
 class Sync{
 	
+	var initial: Bool = false
+	
 	/**
 	Starts the sync process.
 	*/
@@ -35,6 +37,18 @@ class Sync{
 		sync(url: url)
 	}
 	
+	init(synchronous: Bool){
+		if synchronous == true{
+			NSLog(":SYNC:LOG: Synchronous sync started.")
+			self.initial = true
+			// TODO: Shfow the controller
+			//let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
+			//delegate.controller?.initialSync()
+		}
+		let url = buildUrl();
+		sync(url: url)
+	}
+	
 	/**
 	Builds the URL to perform the sync against.
 	:returns: The URL.
@@ -141,6 +155,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()

+ 55 - 3
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,6 +44,15 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	
 	var passId: Int = -1
 	
+	//required init() {
+	//	delegate = UIApplication.shared.delegate as! AppDelegate
+	//	super.init()
+	//}
+	
+	required init?(coder aDecoder: NSCoder) {
+		super.init(coder: aDecoder)
+	}
+	
 	func getContext () -> NSManagedObjectContext {
 		//let appDelegate = UIApplication.shared.delegate as! AppDelegate
 		//return appDelegate.persistentContainer.viewContext
@@ -56,10 +68,21 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	func showAlbum(id: Int){
 		NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
 		self.passId = id
-		// TODO: Uncomment when ready
 		performSegue(withIdentifier: "SegueAlbum", sender: nil)
 	}
 	
+	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)
+		}
+	}
+	
 	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 +91,9 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		if segue.identifier == "SegueAlbum"{
 			(segue.destination as! AlbumViewController).id = passId
 		}
+		if segue.identifier == "SegueSync"{
+			self.syncSegue = segue
+		}
 	}
 	
 	/**
@@ -85,10 +111,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		
 		//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.
 
@@ -98,6 +126,30 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		delegate = UIApplication.shared.delegate as! AppDelegate
 		delegate?.controller = self
 		
+		
+	}
+	
+	override func viewDidAppear(_ animated: Bool) {
+		super.viewDidAppear(animated)
+		
+		// Try to read user id
+		//let defaults = UserDefaults.standard
+		//let uid: String? = defaults.string(forKey: "userId")
+		
+		//NSLog(":CONTROLLER:DEBUG: userId \(uid)")
+		
+		//if uid! == nil{
+		if UserDefaults.standard.object(forKey: "userId") == nil{
+			let newUid = "123456789123"
+			let defaults = UserDefaults.standard
+			defaults.set(newUid, forKey: "userId")
+			initialSync(showScreen: true)
+		}
+		
+		
+		
+		// TODO: Only if neccessary
+		
 	}
 
 	/**