فهرست منبع

Work on the data sync process.

Iñigo Valentin 9 سال پیش
والد
کامیت
fb7db13295

+ 4 - 0
app.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		931AA5891E1AFF8C003A2290 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 931AA5881E1AFF8C003A2290 /* String.swift */; };
 		93B408031D9ED44C000FBC99 /* MenuCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */; };
 		93B84B9A1D9C31AB006C0F67 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B991D9C31AB006C0F67 /* AppDelegate.swift */; };
 		93B84B9C1D9C31AB006C0F67 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B9B1D9C31AB006C0F67 /* ViewController.swift */; };
@@ -24,6 +25,7 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
+		931AA5881E1AFF8C003A2290 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
 		93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MenuCollectionViewCell.swift; path = ../MenuCollectionViewCell.swift; sourceTree = "<group>"; };
 		93B84B961D9C31AB006C0F67 /* app.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = app.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		93B84B991D9C31AB006C0F67 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -88,6 +90,7 @@
 				93D9EA691DE3491100089002 /* HomeView.swift */,
 				93D9EA671DE342AC00089002 /* UIView.swift */,
 				93D9EA751DE3ACBB00089002 /* Sync.swift */,
+				931AA5881E1AFF8C003A2290 /* String.swift */,
 			);
 			path = app;
 			sourceTree = "<group>";
@@ -178,6 +181,7 @@
 				93D9EA641DE273FD00089002 /* Section.swift in Sources */,
 				93D9EA6A1DE3491100089002 /* HomeView.swift in Sources */,
 				93D9EA761DE3ACBB00089002 /* Sync.swift in Sources */,
+				931AA5891E1AFF8C003A2290 /* String.swift in Sources */,
 				93B84B9C1D9C31AB006C0F67 /* ViewController.swift in Sources */,
 				93B84B9A1D9C31AB006C0F67 /* AppDelegate.swift in Sources */,
 				93B84BA21D9C31AB006C0F67 /* app.xcdatamodeld in Sources */,

BIN
app.xcodeproj/project.xcworkspace/xcuserdata/seavenois.xcuserdatad/UserInterfaceState.xcuserstate


+ 52 - 0
app/String.swift

@@ -0,0 +1,52 @@
+//
+//  String.swift
+//  app
+//
+//  Created by Iñigo Valentin on 2/1/17.
+//  Copyright © 2017 Margolariak. All rights reserved.
+//
+
+import Foundation
+
+extension String {
+    var length:Int {
+        return self.characters.count
+    }
+    
+    func indexOf(target: String) -> Int? {
+        
+        let range = (self as NSString).range(of: target)
+        
+        guard range.toRange() != nil else {
+            return nil
+        }
+        
+        return range.location
+        
+    }
+    func lastIndexOf(target: String) -> Int? {
+        
+        
+        
+        let range = (self as NSString).range(of: target, options: NSString.CompareOptions.backwards)
+        
+        guard range.toRange() != nil else {
+            return nil
+        }
+        
+        return self.length - range.location - 1
+        
+    }
+    func contains(s: String) -> Bool {
+        return (self.range(of: s) != nil) ? true : false
+    }
+    
+    func subStr(start: Int, end: Int) -> String{
+        
+        let st = self.index(self.startIndex, offsetBy: start)
+        var ed = self.index(self.startIndex, offsetBy: end)
+        
+        let range = st...ed
+        return self[range]
+    }
+}

+ 11 - 32
app/Sync.swift

@@ -67,27 +67,15 @@ class Sync{
             //print(json)
             print("Got the JSON")
             
+            var strData = String(data:data, encoding: String.Encoding.utf8)
             
-            getTable(data: data AS String, table: "activities")
+            let dataIdx = strData?.indexOf(target: "{\"data\"")
+            strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
             
+            //TODO: do something with versions
+            
+            self.getTable(data: strData!, table: "activity")
             
-            //TODO: Do this for ech table
-            /*
-             let data = [
-             (1, ["name": "United States of America", "short_name": "US"]),
-             (2, ["name": "United Kingdom",           "short_name": "UK"]),
-             (3, ["name": "France"])]
-             
-             let companiesTable = db["companies"] // get the table from our SQLite Database
-             db.transaction(.Deferred) { txn in
-             for (index,company) in data {
-             if companiesTable.insert(name <- company["name"]!, shortname <- company["short_name"]).statement.failed {
-             return .Rollback
-             }
-             }
-             return .Commit
-             }
-             */
         }
         
         task.resume()
@@ -95,20 +83,11 @@ class Sync{
     }
     
     func getTable(data: String, table: String){
-        //let index2 = data.index(data.startIndex, offsetBy: 2) //will call succ 2 times
-        //let lastChar: Character = data[index2] //now we can index!
-        
-        //let characterIndex2 = data.characters.index(data.characters.startIndex, offsetBy: 2)
-        //let lastChar2 = data.characters[characterIndex2] //will do the same as above
-        
-        //let range: Range<String.Index> = data.range(of: table)!
-        //let index: Int = data.distance(from: data.startIndex, to: range.lowerBound)
-        
-        //print(data)
-        
-        //if let index = find(data, table) { // Unwrap the optional
-        //    data.substringFromIndex(advance(index, 1)) // => "ABCDE"
-        //}
+        let iS : Int? = data.indexOf(target: table)
+        var str = data.subStr(start: iS!, end: data.length - 1)
+        let iE : Int? = str.indexOf(target: ";")! - iS!
+        str = str.subStr(start: 0, end: iE!)
+        print("Table \(table): \(str)")
     }
     
 }

+ 3 - 16
app/app.xcdatamodeld/app.xcdatamodel/contents

@@ -64,19 +64,6 @@
         <attribute name="title_eu" optional="YES" attributeType="String" syncable="YES"/>
     </entity>
     <entity name="Festival" representedClassName="Festival" syncable="YES" codeGenerationType="class">
-        <attribute name="attribute" optional="YES" syncable="YES"/>
-        <attribute name="attribute1" optional="YES" syncable="YES"/>
-        <attribute name="attribute2" optional="YES" syncable="YES"/>
-        <attribute name="attribute3" optional="YES" syncable="YES"/>
-        <attribute name="attribute4" optional="YES" syncable="YES"/>
-        <attribute name="attribute5" optional="YES" syncable="YES"/>
-        <attribute name="attribute6" optional="YES" syncable="YES"/>
-        <attribute name="attribute7" optional="YES" syncable="YES"/>
-        <attribute name="attribute8" optional="YES" syncable="YES"/>
-        <attribute name="attribute9" optional="YES" syncable="YES"/>
-        <attribute name="attribute10" optional="YES" syncable="YES"/>
-        <attribute name="attribute11" optional="YES" syncable="YES"/>
-        <attribute name="attribute12" optional="YES" syncable="YES"/>
         <attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
         <attribute name="img" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="summary_en" optional="YES" attributeType="String" syncable="YES"/>
@@ -136,8 +123,8 @@
         <attribute name="action" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="dtime" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
         <attribute name="duration" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
+        <attribute name="gm" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
         <attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
-        <attribute name="internal" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
         <attribute name="seen" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
         <attribute name="text_en" optional="YES" attributeType="String" syncable="YES"/>
         <attribute name="text_es" optional="YES" attributeType="String" syncable="YES"/>
@@ -238,13 +225,13 @@
         <element name="Activity_itinerary" positionX="-18" positionY="-3888" width="128" height="210"/>
         <element name="Activity_tag" positionX="0" positionY="-3852" width="128" height="75"/>
         <element name="Album" positionX="18" positionY="-3861" width="128" height="180"/>
-        <element name="Festival" positionX="36" positionY="-3852" width="128" height="375"/>
+        <element name="Festival" positionX="36" positionY="-3852" width="128" height="180"/>
         <element name="Festival_day" positionX="-9" positionY="-3906" width="128" height="135"/>
         <element name="Festival_event" positionX="0" positionY="-3897" width="128" height="225"/>
         <element name="Festival_event_image" positionX="9" positionY="-3888" width="128" height="105"/>
         <element name="Festival_offer" positionX="18" positionY="-3879" width="128" height="195"/>
         <element name="Location" positionX="27" positionY="-3870" width="128" height="90"/>
-        <element name="Notification" positionX="36" positionY="-3861" width="128" height="225"/>
+        <element name="Notification" positionX="36" positionY="-3861" width="128" height="30"/>
         <element name="Photo" positionX="-9" positionY="-3906" width="128" height="285"/>
         <element name="Photo_album" positionX="0" positionY="-3897" width="128" height="75"/>
         <element name="Place" positionX="9" positionY="-3888" width="128" height="195"/>