Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RouteState.mm « RouteState « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13eee66d9eca7517185ae6a82fb3c749f6172a12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#import "RouteState.h"

#include "Framework.h"

static NSString * const kEndPointKey = @"endPoint";
static NSString * const kETAKey = @"eta";

@interface RouteState()

@property (nonatomic) NSDate * eta;

@end

@implementation RouteState

+ (instancetype)savedState
{
  RouteState * const state = [[super alloc] init];
  if (state)
  {
    NSDictionary * const stateDict = [NSDictionary dictionaryWithContentsOfURL:[RouteState stateFileURL]];
    if (stateDict)
    {
      m2::PointD point;
      NSUInteger size;
      NSGetSizeAndAlignment(@encode(m2::PointD), &size, NULL);
      NSData const * const endPointData = stateDict[kEndPointKey];
      NSDate * const eta = stateDict[kETAKey];
      if (endPointData && eta)
      {
        [endPointData getBytes:&point length:size];
        state.endPoint = point;
        state.eta = eta;
      }
    }
  }
  return state;
}

+ (void)save
{
  Framework & f = GetFramework();
  if (!f.IsOnRoute())
    return;
  location::FollowingInfo routeInfo;
  f.GetRouteFollowingInfo(routeInfo);
  m2::PointD const endPoint = f.GetRouteEndPoint();
  NSMutableDictionary * const stateDict = [NSMutableDictionary dictionary];
  NSUInteger size;
  NSGetSizeAndAlignment(@encode(m2::PointD), &size, nullptr);
  stateDict[kEndPointKey] = [NSData dataWithBytes:&endPoint length:size];
  stateDict[kETAKey] = [NSDate dateWithTimeIntervalSinceNow:routeInfo.m_time];
  [stateDict writeToURL:[RouteState stateFileURL] atomically:YES];
}

+ (void)remove
{
  [[NSFileManager defaultManager] removeItemAtURL:[RouteState stateFileURL] error:nil];
}

+ (NSURL *)stateFileURL
{
  return [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"route_info.ini"]];
}

#pragma mark - Properties

- (BOOL)hasActualRoute
{
  return [self.eta compare:[NSDate date]] == NSOrderedDescending;
}

@end