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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#import "MWMOpeningHours.h"
#import "MWMOpeningHoursCommon.h"
#include "3party/opening_hours/opening_hours.hpp"
#include "editor/opening_hours_ui.hpp"
#include "editor/ui2oh.hpp"
using namespace editor;
using namespace osmoh;
namespace
{
NSString * stringFromTimeSpan(Timespan const & timeSpan)
{
return [NSString stringWithFormat:@"%@ - %@", stringFromTime(timeSpan.GetStart()),
stringFromTime(timeSpan.GetEnd())];
}
NSString * breaksFromClosedTime(TTimespans const & closedTimes)
{
NSMutableString * breaks = [@"" mutableCopy];
for (auto & ct : closedTimes)
{
[breaks appendString:@"\n"];
[breaks appendString:stringFromTimeSpan(ct)];
}
return breaks.copy;
}
void addToday(ui::TimeTable const & tt, vector<Day> & allDays)
{
NSString * workingDays;
NSString * workingTimes;
NSString * breaks;
if (tt.IsTwentyFourHours())
{
workingDays = L(@"twentyfour_seven");
workingTimes = @"";
breaks = @"";
}
else
{
BOOL const everyDay = (tt.GetOpeningDays().size() == 7);
workingDays = everyDay ? L(@"daily") : L(@"today");
workingTimes = stringFromTimeSpan(tt.GetOpeningTime());
breaks = breaksFromClosedTime(tt.GetExcludeTime());
}
allDays.emplace(allDays.begin(), workingDays, workingTimes, breaks);
}
void addClosedToday(vector<Day> & allDays)
{
allDays.emplace(allDays.begin(), L(@"day_off_today"));
}
void addDay(ui::TimeTable const & tt, vector<Day> & allDays)
{
NSString * workingDays = stringFromOpeningDays(tt.GetOpeningDays());
NSString * workingTimes;
NSString * breaks;
if (tt.IsTwentyFourHours())
{
workingTimes = L(@"twentyfour_seven");
}
else
{
workingTimes = stringFromTimeSpan(tt.GetOpeningTime());
breaks = breaksFromClosedTime(tt.GetExcludeTime());
}
allDays.emplace_back(workingDays, workingTimes, breaks);
}
void addUnhandledDays(ui::TOpeningDays const & days, vector<Day> & allDays)
{
if (days.empty())
return;
allDays.emplace_back(stringFromOpeningDays(days));
}
} // namespace
@implementation MWMOpeningHours
+ (vector<Day>)processRawString:(NSString *)str
{
ui::TimeTableSet timeTableSet;
osmoh::OpeningHours oh(str.UTF8String);
if (!MakeTimeTableSet(oh, timeTableSet))
return {};
vector<Day> days;
NSCalendar * cal = [NSCalendar currentCalendar];
cal.locale = [NSLocale currentLocale];
auto const timeTablesSize = timeTableSet.Size();
auto const today = static_cast<Weekday>([cal components:NSCalendarUnitWeekday fromDate:[NSDate date]].weekday);
auto const unhandledDays = timeTableSet.GetUnhandledDays();
/// Schedule contains more than one rule for all days or unhandled days.
BOOL const isExtendedSchedule = timeTablesSize != 1 || !unhandledDays.empty();
BOOL hasCurrentDay = NO;
for (auto const & tt : timeTableSet)
{
ui::TOpeningDays const & workingDays = tt.GetOpeningDays();
if (workingDays.find(today) != workingDays.end())
{
hasCurrentDay = YES;
addToday(tt, days);
}
if (isExtendedSchedule)
addDay(tt, days);
}
if (!hasCurrentDay)
addClosedToday(days);
addUnhandledDays(unhandledDays, days);
return days;
}
@end
|