Эх сурвалжийг харах

Merge branch 'Sync' into BaseProject

Iñigo Valentin 9 жил өмнө
parent
commit
83acbd9638

+ 5 - 0
app.xcodeproj/project.pbxproj

@@ -348,6 +348,11 @@
 						DevelopmentTeam = 9F9ER4X7TG;
 						LastSwiftMigration = 0830;
 						ProvisioningStyle = Automatic;
+						SystemCapabilities = {
+							com.apple.BackgroundModes = {
+								enabled = 1;
+							};
+						};
 					};
 				};
 			};

+ 27 - 0
app/AppDelegate.swift

@@ -32,12 +32,39 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 		// Override point for customization after application launch.
 		
+		// Google Mapas API KEY
 		GMSServices.provideAPIKey("AIzaSyBfIiBM_YSBlxybmI_Uz_fGoUFN4wacR80")
 		
+		// Background mode.
+		UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
+		
 		return true
 	}
 	
 
+	func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+		NSLog(":BACKGROUND:LOG: Start bg sync...")
+		Sync()
+		
+		let when = DispatchTime.now() + 120 // 2 minutes to perform the sync.
+		DispatchQueue.main.asyncAfter(deadline: when) {
+			NSLog(":BACKGROUND:LOG: Calling completion handler.")
+			completionHandler(UIBackgroundFetchResult.newData)
+		}
+		
+		/*if let tabBarController = window?.rootViewController as? UITabBarController, let viewControllers = tabBarController.viewControllers {
+			for viewController in viewControllers {
+				if let fetchViewController = viewController as? FetchViewController {
+					fetchViewController.fetch {
+						fetchViewController.updateUI()
+						completionHandler(.newData)
+					}
+				}
+			}
+		}*/
+	}
+
+
 	/**
 	Sent when the application is about to move from active to inactive state.
 	This can occur for certain types of temporary interruptions

+ 28 - 16
app/HomeView.swift

@@ -41,8 +41,13 @@ class HomeView: UIView {
 	@IBOutlet weak var pastActivitiesSection: Section!
 	@IBOutlet weak var socialSection: Section!
 	
+	var moc: NSManagedObjectContext? = nil
+	var delegate: AppDelegate? = nil
+	var lang: String? = nil
+	
 	override init(frame: CGRect){
 		super.init(frame: frame)
+		
 	}
 	
 	/**
@@ -69,31 +74,38 @@ class HomeView: UIView {
 		socialSection.setTitle(text: "Siguenos")
 		
 		//Get info to populate sections
-		let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
-		
-		let appDelegate = UIApplication.shared.delegate as! AppDelegate
-		moc.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
-		let lang : String = getLanguage()
-		
-		//Populate sections
-		setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection.getContentStack())
-		setUpBlog(context: moc, delegate: appDelegate, lang: lang, parent: blogSection.getContentStack())
-		setUpFutureActivities(context: moc, delegate: appDelegate, lang: lang, parent: futureActivitiesSection.getContentStack())
-		setUpSocial(parent: socialSection.getContentStack())
-		setUpGallery(context: moc, delegate: appDelegate, parent: gallerySection.getContentStack())
-		
+		self.moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
 		
-		pastActivitiesSection.expandSection()
+		self.delegate = UIApplication.shared.delegate as! AppDelegate
+		self.moc?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
+		self.lang = getLanguage()
 		
+		populate()
 		
-		//Always at the end: update scrollview
+		/*//Always at the end: update scrollview
 		var h: Int = 0
 		for view in scrollView.subviews {
+
 			//contentRect = contentRect.union(view.frame);
 			h = h + Int(view.frame.height) + 30 //Why 30?
+
 		}
 		// TODO: Calculate at the end
-		self.scrollView.contentSize.height = 1300// CGFloat(h);
+		self.scrollView.contentSize.height = 1300// CGFloat(h);*/
+	}
+	
+	func populate(){
+	
+		//Populate sections
+		setUpPastActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.pastActivitiesSection.getContentStack())
+		setUpBlog(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.blogSection.getContentStack())
+		setUpFutureActivities(context: self.moc!, delegate: self.delegate!, lang: self.lang!, parent: self.futureActivitiesSection.getContentStack())
+		setUpSocial(parent: self.socialSection.getContentStack())
+		setUpGallery(context: self.moc!, delegate: self.delegate!, parent: self.gallerySection.getContentStack())
+		
+		
+		//pastActivitiesSection.expandSection()
+
 	}
 	
 	/**

+ 4 - 0
app/Info.plist

@@ -24,6 +24,10 @@
 	<true/>
 	<key>NSLocationWhenInUseUsageDescription</key>
 	<string>Permitiendo acceso a tu ubicación, podrás conocer la distancia a la cuadrilla y a determinados eventos.</string>
+	<key>UIBackgroundModes</key>
+	<array>
+		<string>fetch</string>
+	</array>
 	<key>UILaunchStoryboardName</key>
 	<string>LaunchScreen</string>
 	<key>UIMainStoryboardFile</key>

+ 10 - 6
app/ViewController.swift

@@ -164,6 +164,12 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		}
 	}
 	
+	override func viewWillAppear(_ animated: Bool) {
+		NSLog(":CONTROLLER:DEBUG: Forcing a call to populate")
+		self.populate()
+		super.viewWillAppear(animated)
+	}
+	
 	/**
 	Run when the app loads.
 	*/
@@ -184,16 +190,14 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		fetchLocation()
 		self.locationTimer = Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(fetchLocation), userInfo: nil, repeats: true)
 				
-		NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
-		Sync()
+		//NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
+		NSLog(":CONTROLLER:DEBUG: Skyp sync")
+		//Sync()
 
 		
 		self.delegate = UIApplication.shared.delegate as? AppDelegate
 		self.delegate?.controller = self
 		
-		NSLog(":CONTROLLER:DEBUG: NOT Forcing a call to populate")
-		//self.populate()
-		
 		super.viewDidLoad()
 		
 	}
@@ -208,7 +212,7 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	*/
 	func populate(){
 		if (self.containerViewHome != nil){
-			//(self.containerViewHome as HomeView).populate2()
+			//(self.containerViewHome as HomeView).populate()
 			//let ng = self.containerViewHome.dbgTxt
 			//NSLog("AAAAA \(ng)")
 		}