Skip to content

Commit

Permalink
Fix vehicleActivity type not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
lutfime committed Oct 26, 2016
1 parent a7ada05 commit ff7b5fb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 59 deletions.
5 changes: 5 additions & 0 deletions KatsanaAPI/Cache/KMCacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ - (NSArray*)activityObjects{
NSMutableArray *activities;
if (user.userId) {
activities = self.activities[user.userId.copy];
if ([activities.firstObject isKindOfClass:[NSDictionary class]]) {
self.activities = nil;
activities = nil;
}

}
return activities.copy;
}
Expand Down
7 changes: 4 additions & 3 deletions KatsanaAPI/Object/KMUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ - (void)addActivityObject:(VehicleActivity *)activity{
[activities addObject:activity];
}

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self"
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self.startTime"
ascending: NO];
[activities sortUsingDescriptors:@[sortOrder]];

Expand Down Expand Up @@ -188,8 +188,9 @@ - (void)updateFilterActivities{
- (NSArray*)activitiesSeparatedByDaysForActivities:(NSArray*)theActivities{
NSMutableArray *group = [NSMutableArray array];
NSMutableArray *currentDayGroup;

NSArray *activities = [[theActivities sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator].allObjects;
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self.startTime"
ascending: NO];
NSArray *activities = [theActivities sortedArrayUsingDescriptors:@[sortOrder]];
NSDate *currentDate;
for (VehicleActivity *violation in activities) {
if (!currentDate || ![[NSCalendar currentCalendar] isDate:currentDate inSameDayAsDate:violation.startTime]) {
Expand Down
20 changes: 1 addition & 19 deletions KatsanaAPI/Object/ObjectJSONTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class ObjectJSONTransformer: NSObject {
let violation = VehicleActivity()
violation.violationId = json["id"].intValue
violation.policyId = json["policy_id"].intValue
violation.message = json["description"].stringValue
violation.address = json["address"].stringValue
violation.distance = json["distance"].floatValue
violation.duration = json["duration"].floatValue
Expand All @@ -162,25 +161,8 @@ class ObjectJSONTransformer: NSObject {
violation.endPosition = json["end_position"].intValue
violation.maxSpeed = json["max_speed"].floatValue
violation.averageSpeed = json["average_speed"].floatValue
violation.message = json["description"].stringValue

return violation
}

// @{@"id": @"violationId",
// @"policy_id": @"policyId",
// @"policy_type": @"policyType",
// @"description": @"message",
// @"address": @"address",
// @"distance": @"distance",
// @"duration": @"duration",
// @"latitude": @"latitude",
// @"longitude": @"longitude",
// @"start_time": @"startTime",
// @"end_time": @"endTime",
// @"start_position": @"startPosition",
// @"end_position": @"endPosition",
// @"max_speed" : @"maxSpeed",
// @"average_speed" : @"averageSpeed",
// @"idle_duration" : @"idleDuration"
// };
}
89 changes: 52 additions & 37 deletions KatsanaAPI/Object/VehicleActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import UIKit
case harshAccelerate
case harshCorner
case speedSummary
case doorAjar
//More can be added
}

Expand All @@ -34,19 +35,22 @@ import UIKit
//@property (nonatomic, strong) NSString *serverTimeText;

public class VehicleActivity: NSObject {
internal var privateAttributedMessage: NSAttributedString!

public var vehicleId: String!
public var message: String!
public var attributedMessage: NSAttributedString!{
set{
privateAttributedMessage = newValue
}
get{
//Implement function updateAttributedMessage in extension for lazy attributed message initialization
if self.attributedMessage == nil {
if responds(to: Selector(("updateAttributedMessage"))) {
if privateAttributedMessage == nil {
if self.responds(to: Selector(("updateAttributedMessage"))) {
perform(Selector(("updateAttributedMessage")))
}
}
return self.attributedMessage
return privateAttributedMessage
}
}
public var address: String!
Expand All @@ -71,54 +75,60 @@ public class VehicleActivity: NSObject {
public var identifier : String!
public var violationId: Int = 0
public var policyId: Int = 0
public var type: ActivityType = .none
public lazy var type: ActivityType = {
var type : ActivityType = .none
switch self.policyKey {
case "speed":
type = .speed
case "movement":
type = .time
case "area":
type = .area
case "battery-cutoff":
type = .batteryCutoff
case "trip-start":
type = .tripStart
case "speed-summary":
type = .speedSummary
case "harsh-brake":
type = .harshBrake
case "harsh-accelerate":
type = .harshAccelerate
case "harsh-corner":
type = .harshCorner
case "checkpoint":
type = .checkpoint
case "door-ajar":
type = .doorAjar
default:
print("Policy " + self.policyKey + " not handled")
}
return type
}()

/// Policy string from server
var policyKey: String!{
didSet{
var type : ActivityType!
switch policyKey {
case "speed":
type = .speed
case "movement":
type = .time
case "area":
type = .area
case "battery-cutoff":
type = .batteryCutoff
case "trip-start":
type = .tripStart
case "speed-summary":
type = .speedSummary
case "harsh-brake":
type = .harshBrake
case "harsh-accelerate":
type = .harshAccelerate
case "harsh-corner":
type = .harshCorner
case "checkpoint":
type = .checkpoint
default:
print("Policy" + policyKey + "not handled")
}
self.type = type
}
}
var policyKey: String!


class func fastCodingKeys() -> [String]! {
return ["deviceId", "message", "distance", "duration", "latitude", "longitude", "startTime", "endTime", "startPosition", "endPosition", "violationId", "policyId", "policyKey", "maxSpeed", "averageSpeed", "identifier", "altitude", "course", "speed", "timeString"]
return ["vehicleId", "message", "distance", "duration", "latitude", "longitude", "startTime", "endTime", "startPosition", "endPosition", "violationId", "policyId", "policyKey", "maxSpeed", "averageSpeed", "identifier", "altitude", "course", "speed", "timeString"]
}

convenience override init() {
self.init(dictionary: nil, identifier: nil)
}

public init(dictionary:[String : Any]! = nil, identifier:String! = nil) {
super.init()
if dictionary != nil {
self.policyKey = dictionary["type"] as? String
self.vehicleId = dictionary["device_id"] as? String
self.vehicleId = (dictionary["device_id"] as? NSNumber)?.stringValue
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 {
Expand All @@ -138,6 +148,8 @@ public class VehicleActivity: NSObject {
completion(address?.optimizedAddress())
})
}

// MARK: Display

public func speedString() -> String {
return KatsanaFormatter.speedStringFrom(knot: Double(speed))
Expand All @@ -147,4 +159,7 @@ public class VehicleActivity: NSObject {
return KatsanaFormatter.speedStringFrom(knot: Double(maxSpeed))
}

// class public func allTypes() -> [String] {
// return ["speed", "movement", "area", "battery-cutoff", "trip-start", "speed-summary", "harsh-brake", "harsh-accelerate", "harsh-corner", "checkpoint"]
// }
}

0 comments on commit ff7b5fb

Please sign in to comment.