Ver código fonte

User id is now random.
Code comments and cleanup.

Iñigo Valentin 9 anos atrás
pai
commit
de1762e64c
2 arquivos alterados com 57 adições e 26 exclusões
  1. 5 3
      app/Sync.swift
  2. 52 23
      app/ViewController.swift

+ 5 - 3
app/Sync.swift

@@ -31,19 +31,21 @@ class Sync{
 	
 	
 	/**
 	/**
 	Starts the sync process.
 	Starts the sync process.
+	Always asynchronously.
 	*/
 	*/
 	init(){
 	init(){
 		let url = buildUrl();
 		let url = buildUrl();
 		sync(url: url)
 		sync(url: url)
 	}
 	}
 	
 	
+	/**
+	Starts the sync process, synchronously or asynchronously.
+	:param: synchronous True for synchronous sync, false for asynchronously
+	*/
 	init(synchronous: Bool){
 	init(synchronous: Bool){
 		if synchronous == true{
 		if synchronous == true{
 			NSLog(":SYNC:LOG: Synchronous sync started.")
 			NSLog(":SYNC:LOG: Synchronous sync started.")
 			self.initial = true
 			self.initial = true
-			// TODO: Shfow the controller
-			//let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
-			//delegate.controller?.initialSync()
 		}
 		}
 		let url = buildUrl();
 		let url = buildUrl();
 		sync(url: url)
 		sync(url: url)

+ 52 - 23
app/ViewController.swift

@@ -44,33 +44,48 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	
 	
 	var passId: Int = -1
 	var passId: Int = -1
 	
 	
-	//required init() {
-	//	delegate = UIApplication.shared.delegate as! AppDelegate
-	//	super.init()
-	//}
-	
+	/**
+	 Controller initializer.
+	*/
 	required init?(coder aDecoder: NSCoder) {
 	required init?(coder aDecoder: NSCoder) {
 		super.init(coder: aDecoder)
 		super.init(coder: aDecoder)
 	}
 	}
 	
 	
+	/**
+	 Retrieves the application context.
+	 :return: The application context.
+	*/
 	func getContext () -> NSManagedObjectContext {
 	func getContext () -> NSManagedObjectContext {
 		//let appDelegate = UIApplication.shared.delegate as! AppDelegate
 		//let appDelegate = UIApplication.shared.delegate as! AppDelegate
 		//return appDelegate.persistentContainer.viewContext
 		//return appDelegate.persistentContainer.viewContext
 		return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
 		return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
 	}
 	}
 	
 	
+	/**
+	 Shows a post by loading the controller.
+	 :param: id The post id.
+	*/
 	func showPost(id: Int){
 	func showPost(id: Int){
-		print("CONTROLLER:DEBUG: Showing Post \(id)")
+		NSLog(":CONTROLLER:DEBUG: Showing Post \(id)")
 		self.passId = id
 		self.passId = id
 		performSegue(withIdentifier: "SeguePost", sender: nil)
 		performSegue(withIdentifier: "SeguePost", sender: nil)
 	}
 	}
 	
 	
+	/**
+	 Shows a post by loading the controller.
+	 :param: id The album id.
+	*/
 	func showAlbum(id: Int){
 	func showAlbum(id: Int){
 		NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
 		NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
 		self.passId = id
 		self.passId = id
 		performSegue(withIdentifier: "SegueAlbum", sender: nil)
 		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){
 	func initialSync(showScreen: Bool){
 		if showScreen == true{
 		if showScreen == true{
 			NSLog(":CONTROLLER:DEBUG: Showing initial sync screen.")
 			NSLog(":CONTROLLER:DEBUG: Showing initial sync screen.")
@@ -83,6 +98,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		}
 		}
 	}
 	}
 	
 	
+	/**
+	 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?) {
 	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 		NSLog(":CONTROLLER:DEBUG: preparing for segue '\(segue.identifier)' with id \(self.passId)")
 		NSLog(":CONTROLLER:DEBUG: preparing for segue '\(segue.identifier)' with id \(self.passId)")
 		if segue.identifier == "SeguePost"{
 		if segue.identifier == "SeguePost"{
@@ -107,20 +128,13 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		self.containerViewActivities.alpha = 0
 		self.containerViewActivities.alpha = 0
 		self.containerViewBlog.alpha = 0
 		self.containerViewBlog.alpha = 0
 		self.containerViewGallery.alpha = 0
 		self.containerViewGallery.alpha = 0
-		
-		
-		//self.containerViewBlog.isHidden = true
 				
 				
 		NSLog(":CONTROLLER:LOG: viewDidLoad()")
 		NSLog(":CONTROLLER:LOG: viewDidLoad()")
 		NSLog(":CONTROLLER:DEBUG: Skyp sync")
 		NSLog(":CONTROLLER:DEBUG: Skyp sync")
 		//Sync()
 		//Sync()
 		
 		
-		
-		
 		super.viewDidLoad()
 		super.viewDidLoad()
-		// Do any additional setup after loading the view, typically from a nib.
 
 
-		
 		//self.containerViewBlog.setController(controller: self as ViewController)
 		//self.containerViewBlog.setController(controller: self as ViewController)
 		
 		
 		delegate = UIApplication.shared.delegate as! AppDelegate
 		delegate = UIApplication.shared.delegate as! AppDelegate
@@ -129,27 +143,42 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		
 		
 	}
 	}
 	
 	
+	/**
+	 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) {
 	override func viewDidAppear(_ animated: Bool) {
 		super.viewDidAppear(animated)
 		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{
 		if UserDefaults.standard.object(forKey: "userId") == nil{
-			let newUid = "123456789123"
+			let newUid = randomString(length: 16)
 			let defaults = UserDefaults.standard
 			let defaults = UserDefaults.standard
 			defaults.set(newUid, forKey: "userId")
 			defaults.set(newUid, forKey: "userId")
 			initialSync(showScreen: true)
 			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 = ""
 		
 		
-		// TODO: Only if neccessary
+		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
 	}
 	}
 
 
 	/**
 	/**