Skip to content

Commit

Permalink
Improve VehicleActity class
Browse files Browse the repository at this point in the history
  • Loading branch information
lutfime committed Oct 26, 2016
1 parent 496bcbe commit a7ada05
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 113 deletions.
56 changes: 4 additions & 52 deletions KatsanaAPI/Extension/JSON+Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension JSON {
func date(gmt: Float) -> Date? {
switch self.type {
case .string:
return Formatter.jsonDateFormatter(gmt: gmt).date(from: (self.object as! String))
return (self.object as! String).date(gmt: gmt)
default:
return nil
}
Expand All @@ -21,7 +21,7 @@ extension JSON {
get {
switch self.type {
case .string:
return Formatter.jsonDateFormatter.date(from: (self.object as! String))
return (self.object as! String).date
default:
return nil
}
Expand All @@ -32,7 +32,7 @@ extension JSON {
get {
switch self.type {
case .string:
return Formatter.jsonDateWithoutTimeFormatter.date(from: (self.object as! String))
return (self.object as! String).dateWithoutTime
default:
return nil
}
Expand All @@ -43,59 +43,11 @@ extension JSON {
get {
switch self.type {
case .string:
return Formatter.jsonDateTimeFormatter.date(from: self.object as! String)
return (self.object as! String).dateTime
default:
return nil
}
}
}

}

class Formatter {

private static var internalJsonDateFormatter: DateFormatter?
private static var internalJsonDateGMTFormatter: DateFormatter?
private static var internalJsonDateWithoutTimeFormatter: DateFormatter?
private static var internalJsonDateTimeFormatter: DateFormatter?

static func jsonDateFormatter(gmt: Float) -> DateFormatter {
if (internalJsonDateGMTFormatter == nil) {
internalJsonDateGMTFormatter = DateFormatter()
internalJsonDateGMTFormatter!.dateFormat = "yyyy-MM-dd HH:mm:ss"
internalJsonDateGMTFormatter?.timeZone = Foundation.TimeZone(secondsFromGMT: 0)!
// 2013-11-18 03:31:02
}
internalJsonDateGMTFormatter?.timeZone = Foundation.TimeZone(secondsFromGMT: Int(gmt * 60*60))!
return internalJsonDateGMTFormatter!
}

static var jsonDateFormatter: DateFormatter {
if (internalJsonDateFormatter == nil) {
internalJsonDateFormatter = DateFormatter()
internalJsonDateFormatter!.dateFormat = "yyyy-MM-dd HH:mm:ss"

// 2013-11-18 03:31:02
}
return internalJsonDateFormatter!
}

static var jsonDateWithoutTimeFormatter: DateFormatter {
if (internalJsonDateWithoutTimeFormatter == nil) {
internalJsonDateWithoutTimeFormatter = DateFormatter()
internalJsonDateWithoutTimeFormatter!.dateFormat = "yyyy-MM-dd"

// 2013-11-18 03:31:02
}
return internalJsonDateWithoutTimeFormatter!
}

static var jsonDateTimeFormatter: DateFormatter {
if (internalJsonDateTimeFormatter == nil) {
internalJsonDateTimeFormatter = DateFormatter()
internalJsonDateTimeFormatter!.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
}
return internalJsonDateTimeFormatter!
}

}
83 changes: 83 additions & 0 deletions KatsanaAPI/Extension/String+Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// String+Date.swift
// KatsanaSDK
//
// Created by Wan Ahmad Lutfi on 26/10/2016.
// Copyright © 2016 pixelated. All rights reserved.
//

import Foundation

extension String {

func date(gmt: Float) -> Date? {
return Formatter.jsonDateFormatter(gmt: gmt).date(from: self)
}

public var date: Date? {
get {
return Formatter.jsonDateFormatter.date(from: self)
}
}

public var dateWithoutTime: Date? {
get {
return Formatter.jsonDateWithoutTimeFormatter.date(from: self)
}
}

public var dateTime: Date? {
get {
return Formatter.jsonDateTimeFormatter.date(from: self)
}
}

}

class Formatter {

private static var internalJsonDateFormatter: DateFormatter?
private static var internalJsonDateGMTFormatter: DateFormatter?
private static var internalJsonDateWithoutTimeFormatter: DateFormatter?
private static var internalJsonDateTimeFormatter: DateFormatter?

static func jsonDateFormatter(gmt: Float) -> DateFormatter {
if (internalJsonDateGMTFormatter == nil) {
internalJsonDateGMTFormatter = DateFormatter()
internalJsonDateGMTFormatter!.dateFormat = "yyyy-MM-dd HH:mm:ss"
internalJsonDateGMTFormatter?.timeZone = Foundation.TimeZone(secondsFromGMT: 0)!
// 2013-11-18 03:31:02
}
internalJsonDateGMTFormatter?.timeZone = Foundation.TimeZone(secondsFromGMT: Int(gmt * 60*60))!
return internalJsonDateGMTFormatter!
}

static var jsonDateFormatter: DateFormatter {
if (internalJsonDateFormatter == nil) {
internalJsonDateFormatter = DateFormatter()
internalJsonDateFormatter!.dateFormat = "yyyy-MM-dd HH:mm:ss"

// 2013-11-18 03:31:02
}
return internalJsonDateFormatter!
}

static var jsonDateWithoutTimeFormatter: DateFormatter {
if (internalJsonDateWithoutTimeFormatter == nil) {
internalJsonDateWithoutTimeFormatter = DateFormatter()
internalJsonDateWithoutTimeFormatter!.dateFormat = "yyyy-MM-dd"

// 2013-11-18 03:31:02
}
return internalJsonDateWithoutTimeFormatter!
}

static var jsonDateTimeFormatter: DateFormatter {
if (internalJsonDateTimeFormatter == nil) {
internalJsonDateTimeFormatter = DateFormatter()
internalJsonDateTimeFormatter!.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
}
return internalJsonDateTimeFormatter!
}

}
9 changes: 0 additions & 9 deletions KatsanaAPI/Object/VehicleActivity+Stream.swift

This file was deleted.

29 changes: 0 additions & 29 deletions KatsanaAPI/Object/VehicleActivity+Violation.swift

This file was deleted.

46 changes: 31 additions & 15 deletions KatsanaAPI/Object/VehicleActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ public class VehicleActivity: NSObject {
}
public var address: String!

public var distance: Float!
public var duration: Float!
public var latitude: Double!
public var longitude: Double!
public var altitude: Double!
public var course: Double!
public var distance: Float = 0
public var duration: Float = 0
public var latitude: Double = 0
public var longitude: Double = 0
public var altitude: Double = 0
public var course: Double = 0

public var speed: Double!
public var maxSpeed: Float!
public var averageSpeed: Float!
public var speed: Float = 0
public var maxSpeed: Float = 0
public var averageSpeed: Float = 0

public var timeString : String!
public var startTime: Date!
public var endTime: Date!
public var startPosition: Int!
public var endPosition: Int!
public var startPosition: Int = 0
public var endPosition: Int = 0

public var identifier : String!
public var violationId: Int!
public var policyId: Int!
public var violationId: Int = 0
public var policyId: Int = 0
public var type: ActivityType = .none

/// Policy string from server
Expand Down Expand Up @@ -109,6 +109,18 @@ public class VehicleActivity: NSObject {
return ["deviceId", "message", "distance", "duration", "latitude", "longitude", "startTime", "endTime", "startPosition", "endPosition", "violationId", "policyId", "policyKey", "maxSpeed", "averageSpeed", "identifier", "altitude", "course", "speed", "timeString"]
}

public init(dictionary:[String : Any]! = nil, identifier:String! = nil) {
if dictionary != nil {
self.policyKey = dictionary["type"] as? String
self.vehicleId = dictionary["device_id"] as? String
self.message = dictionary["message"] as? String
self.timeString = dictionary["time"] as? String
self.startTime = (dictionary["time"] as? String)?.date(gmt: 0)
self.identifier = identifier
}
super.init()
}

public func coordinate() -> CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(latitude, longitude)
}
Expand All @@ -117,7 +129,7 @@ public class VehicleActivity: NSObject {
return KatsanaAPI.shared.vehicleWith(vehicleId: vehicleId)
}

public func addressWithCompletion(completion: @escaping (String?) -> Void) -> Void {
public func address(completion: @escaping (String?) -> Void) -> Void {
guard latitude == 0 || longitude == 0 else {
completion("")
return
Expand All @@ -128,7 +140,11 @@ public class VehicleActivity: NSObject {
}

public func speedString() -> String {
return KatsanaFormatter.speedStringFrom(knot: speed)
return KatsanaFormatter.speedStringFrom(knot: Double(speed))
}

public func maxSpeedString() -> String {
return KatsanaFormatter.speedStringFrom(knot: Double(maxSpeed))
}

}
12 changes: 4 additions & 8 deletions KatsanaSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
C75105621D742D4700F4C341 /* PrefixHeader.pch in Headers */ = {isa = PBXBuildFile; fileRef = C75105611D742D4700F4C341 /* PrefixHeader.pch */; };
C75105641D742D8600F4C341 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C75105631D742D8600F4C341 /* CoreLocation.framework */; };
C756108C1DC03FC700925222 /* KatsanaFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C756108B1DC03FC700925222 /* KatsanaFormatter.swift */; };
C75610971DC0615800925222 /* String+Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75610961DC0615800925222 /* String+Date.swift */; };
C7578B491DB462E400C964DB /* KatsanaAPI+Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7578B481DB462E400C964DB /* KatsanaAPI+Profile.swift */; };
C7578B4B1DB46A9A00C964DB /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7578B4A1DB46A9A00C964DB /* UIImage+Extension.swift */; };
C75945671DAE217400774FAD /* KMObject.h in Headers */ = {isa = PBXBuildFile; fileRef = C75945651DAE217400774FAD /* KMObject.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -34,8 +35,6 @@
C79ACCEC1DBF18D4000607A6 /* Siesta.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = C79ACCDE1DBF18BA000607A6 /* Siesta.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C79ACD091DBF1A0E000607A6 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79ACD081DBF1A0E000607A6 /* SwiftyJSON.swift */; };
C79ACD101DBF2AC2000607A6 /* VehicleActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79ACD0F1DBF2AC2000607A6 /* VehicleActivity.swift */; };
C79ACD141DBF5825000607A6 /* VehicleActivity+Violation.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79ACD131DBF5825000607A6 /* VehicleActivity+Violation.swift */; };
C79ACD161DBF59E2000607A6 /* VehicleActivity+Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79ACD151DBF59E2000607A6 /* VehicleActivity+Stream.swift */; };
C79C76431DADF507003A7DEF /* KatsanaAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79C76421DADF507003A7DEF /* KatsanaAPI.swift */; };
C79C76451DADF933003A7DEF /* KatsanaAPI+Login.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79C76441DADF933003A7DEF /* KatsanaAPI+Login.swift */; };
C79D9FF51D7D19CF0003E7A8 /* KMCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C79D9FCE1D7D19CF0003E7A8 /* KMCacheManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -172,6 +171,7 @@
C75105611D742D4700F4C341 /* PrefixHeader.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = "<group>"; };
C75105631D742D8600F4C341 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
C756108B1DC03FC700925222 /* KatsanaFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KatsanaFormatter.swift; sourceTree = "<group>"; };
C75610961DC0615800925222 /* String+Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Date.swift"; sourceTree = "<group>"; };
C7578B481DB462E400C964DB /* KatsanaAPI+Profile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "KatsanaAPI+Profile.swift"; sourceTree = "<group>"; };
C7578B4A1DB46A9A00C964DB /* UIImage+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; };
C75945651DAE217400774FAD /* KMObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KMObject.h; sourceTree = "<group>"; };
Expand All @@ -192,8 +192,6 @@
C79ACCD31DBF18BA000607A6 /* Siesta.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Siesta.xcodeproj; path = Libraries/Siesta/Siesta.xcodeproj; sourceTree = "<group>"; };
C79ACD081DBF1A0E000607A6 /* SwiftyJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSON.swift; sourceTree = "<group>"; };
C79ACD0F1DBF2AC2000607A6 /* VehicleActivity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VehicleActivity.swift; sourceTree = "<group>"; };
C79ACD131DBF5825000607A6 /* VehicleActivity+Violation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "VehicleActivity+Violation.swift"; sourceTree = "<group>"; };
C79ACD151DBF59E2000607A6 /* VehicleActivity+Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "VehicleActivity+Stream.swift"; sourceTree = "<group>"; };
C79C76421DADF507003A7DEF /* KatsanaAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KatsanaAPI.swift; sourceTree = "<group>"; };
C79C76441DADF933003A7DEF /* KatsanaAPI+Login.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "KatsanaAPI+Login.swift"; sourceTree = "<group>"; };
C79D9FCE1D7D19CF0003E7A8 /* KMCacheManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KMCacheManager.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -327,6 +325,7 @@
C76538C21DB08ECF001E1788 /* Date+Extension.swift */,
C7578B4A1DB46A9A00C964DB /* UIImage+Extension.swift */,
C76715ED1DB5BF1A0044D8E2 /* JSON+Date.swift */,
C75610961DC0615800925222 /* String+Date.swift */,
);
name = Extension;
path = KatsanaAPI/Extension;
Expand Down Expand Up @@ -391,8 +390,6 @@
C7F4B01A1DB5DC5100AC3838 /* Extension */,
C75945651DAE217400774FAD /* KMObject.h */,
C79ACD0F1DBF2AC2000607A6 /* VehicleActivity.swift */,
C79ACD131DBF5825000607A6 /* VehicleActivity+Violation.swift */,
C79ACD151DBF59E2000607A6 /* VehicleActivity+Stream.swift */,
C79D9FDD1D7D19CF0003E7A8 /* KMAddress.h */,
C79D9FDE1D7D19CF0003E7A8 /* KMAddress.m */,
C79D9FE51D7D19CF0003E7A8 /* KMTrip.h */,
Expand Down Expand Up @@ -695,10 +692,10 @@
C79DA00C1D7D19CF0003E7A8 /* KMUser.m in Sources */,
C76715EE1DB5BF1A0044D8E2 /* JSON+Date.swift in Sources */,
C7F4B0221DB5FA0600AC3838 /* ImageRequest.swift in Sources */,
C79ACD141DBF5825000607A6 /* VehicleActivity+Violation.swift in Sources */,
C79DA0101D7D19CF0003E7A8 /* KMTravelHistory.m in Sources */,
C79C76431DADF507003A7DEF /* KatsanaAPI.swift in Sources */,
C79DA0021D7D19CF0003E7A8 /* KMAddress.m in Sources */,
C75610971DC0615800925222 /* String+Date.swift in Sources */,
C79ACD091DBF1A0E000607A6 /* SwiftyJSON.swift in Sources */,
C79D9FF61D7D19CF0003E7A8 /* KMCacheManager.m in Sources */,
C7B0C9211DB9A5780008B60D /* APIError.swift in Sources */,
Expand All @@ -709,7 +706,6 @@
C76B86C51DAF8A48005F6751 /* KatsanaAPI+Trip.swift in Sources */,
C79DA00E1D7D19CF0003E7A8 /* KMVehicle.m in Sources */,
C76B86C31DAF569B005F6751 /* ObjectJSONTransformer.swift in Sources */,
C79ACD161DBF59E2000607A6 /* VehicleActivity+Stream.swift in Sources */,
C76538BB1DB08B2B001E1788 /* UIImage+Extension.m in Sources */,
C79C76451DADF933003A7DEF /* KatsanaAPI+Login.swift in Sources */,
C76B86B81DAF16EC005F6751 /* KatsanaAPI+Vehicle.swift in Sources */,
Expand Down

0 comments on commit a7ada05

Please sign in to comment.