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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#import "MWMOpeningHoursEditorViewController.h"
#import "MWMOpeningHoursAddClosedTableViewCell.h"
#import "MWMOpeningHoursAddScheduleTableViewCell.h"
#import "MWMOpeningHoursAllDayTableViewCell.h"
#import "MWMOpeningHoursClosedSpanTableViewCell.h"
#import "MWMOpeningHoursDaysSelectorTableViewCell.h"
#import "MWMOpeningHoursDeleteScheduleTableViewCell.h"
#import "MWMOpeningHoursModel.h"
#import "MWMOpeningHoursSection.h"
#import "MWMOpeningHoursTimeSelectorTableViewCell.h"
#import "MWMOpeningHoursTimeSpanTableViewCell.h"
#import "MWMTextView.h"
#import "SwiftBridge.h"
extern NSDictionary * const kMWMOpeningHoursEditorTableCells = @{
@(MWMOpeningHoursEditorDaysSelectorCell) : [MWMOpeningHoursDaysSelectorTableViewCell class],
@(MWMOpeningHoursEditorAllDayCell) : [MWMOpeningHoursAllDayTableViewCell class],
@(MWMOpeningHoursEditorTimeSpanCell) : [MWMOpeningHoursTimeSpanTableViewCell class],
@(MWMOpeningHoursEditorTimeSelectorCell) : [MWMOpeningHoursTimeSelectorTableViewCell class],
@(MWMOpeningHoursEditorClosedSpanCell) : [MWMOpeningHoursClosedSpanTableViewCell class],
@(MWMOpeningHoursEditorAddClosedCell) : [MWMOpeningHoursAddClosedTableViewCell class],
@(MWMOpeningHoursEditorDeleteScheduleCell) : [MWMOpeningHoursDeleteScheduleTableViewCell class],
@(MWMOpeningHoursEditorAddScheduleCell) : [MWMOpeningHoursAddScheduleTableViewCell class],
};
@interface MWMOpeningHoursEditorViewController ()<UITableViewDelegate, UITableViewDataSource,
UITextViewDelegate, MWMOpeningHoursModelProtocol>
@property(weak, nonatomic, readwrite) IBOutlet UITableView * tableView;
@property(weak, nonatomic, readwrite) IBOutlet UIView * advancedEditor;
@property(weak, nonatomic, readwrite) IBOutlet MWMTextView * editorView;
@property(weak, nonatomic) IBOutlet UIView * helpView;
@property(weak, nonatomic) IBOutlet UIWebView * help;
@property(weak, nonatomic, readwrite) IBOutlet NSLayoutConstraint * ohTextViewHeight;
@property(weak, nonatomic) IBOutlet UIView * exampleValuesSeparator;
@property(weak, nonatomic) IBOutlet UIImageView * exampleValuesExpandView;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * examplesButtonBottomOffset;
@property(weak, nonatomic, readwrite) IBOutlet UIButton * toggleModeButton;
@property(nonatomic) BOOL exampleExpanded;
@property(nonatomic) BOOL isSimpleMode;
@property(nonatomic) MWMOpeningHoursModel * model;
@end
@implementation MWMOpeningHoursEditorViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configNavBar];
[self configTable];
[self configAdvancedEditor];
[self configData];
}
#pragma mark - Configuration
- (void)configNavBar
{
self.title = L(@"editor_time_title").capitalizedString;
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(onDone)];
}
- (void)configTable
{
[kMWMOpeningHoursEditorTableCells
enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, Class cls, BOOL * _Nonnull stop) {
[self.tableView registerWithCellClass:cls];
}];
}
- (void)configAdvancedEditor
{
[self.editorView setTextContainerInset:{.top = 12, .left = 10, .bottom = 12, .right = 10}];
self.editorView.keyboardAppearance =
[UIColor isNightMode] ? UIKeyboardAppearanceDark : UIKeyboardAppearanceDefault;
NSString * path =
[NSBundle.mainBundle pathForResource:@"opening_hours_how_to_edit" ofType:@"html"];
NSString * html =
[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSURL * baseURL = [NSURL fileURLWithPath:path];
[self.help loadHTMLString:html baseURL:baseURL];
}
- (void)configData
{
self.model = [[MWMOpeningHoursModel alloc] initWithDelegate:self];
self.isSimpleMode = self.model.isSimpleModeCapable;
}
#pragma mark - Actions
- (void)onCancel { [self.navigationController popViewControllerAnimated:YES]; }
- (void)onDone
{
[self.model storeCachedData];
[self.model updateOpeningHours];
[self.delegate setOpeningHours:self.openingHours];
[self onCancel];
}
#pragma mark - Table
- (MWMOpeningHoursEditorCells)cellKeyForIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section < self.model.count)
return [self.model cellKeyForIndexPath:indexPath];
else
return MWMOpeningHoursEditorAddScheduleCell;
}
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
CGFloat const width = self.view.width;
if (indexPath.section < self.model.count)
return [self.model heightForIndexPath:indexPath withWidth:width];
else
return [MWMOpeningHoursAddScheduleTableViewCell height];
}
#pragma mark - Fill cells with data
- (void)fillCell:(MWMOpeningHoursTableViewCell * _Nonnull)cell
atIndexPath:(NSIndexPath * _Nonnull)indexPath
{
if (indexPath.section < self.model.count)
[self.model fillCell:cell atIndexPath:indexPath];
else if ([cell isKindOfClass:[MWMOpeningHoursAddScheduleTableViewCell class]])
((MWMOpeningHoursAddScheduleTableViewCell *)cell).model = self.model;
}
#pragma mark - UITableViewDataSource
- (UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView
cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
Class cls = kMWMOpeningHoursEditorTableCells[@([self cellKeyForIndexPath:indexPath])];
auto cell = [tableView dequeueReusableCellWithCellClass:cls indexPath:indexPath];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView * _Nonnull)tableView
{
if (!self.model.isSimpleMode)
return 0;
return self.model.count + (self.model.canAddSection ? 1 : 0);
}
- (NSInteger)tableView:(UITableView * _Nonnull)tableView numberOfRowsInSection:(NSInteger)section
{
return (section < self.model.count ? [self.model numberOfRowsInSection:section] : 1);
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView * _Nonnull)tableView
heightForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
return [self heightForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView * _Nonnull)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
return [self heightForRowAtIndexPath:indexPath];
}
- (void)tableView:(UITableView * _Nonnull)tableView
willDisplayCell:(MWMOpeningHoursTableViewCell * _Nonnull)cell
forRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
[self fillCell:cell atIndexPath:indexPath];
}
#pragma mark - Advanced mode
- (void)setExampleExpanded:(BOOL)exampleExpanded
{
_exampleExpanded = exampleExpanded;
self.help.hidden = !exampleExpanded;
self.examplesButtonBottomOffset.priority =
exampleExpanded ? UILayoutPriorityDefaultLow : UILayoutPriorityDefaultHigh;
self.exampleValuesSeparator.hidden = !exampleExpanded;
self.exampleValuesExpandView.image =
[UIImage imageNamed:exampleExpanded ? @"ic_arrow_gray_up" : @"ic_arrow_gray_down"];
if (exampleExpanded)
[self.editorView resignFirstResponder];
else
[self.editorView becomeFirstResponder];
}
- (IBAction)toggleExample { self.exampleExpanded = !self.exampleExpanded; }
- (IBAction)toggleMode { self.isSimpleMode = !self.isSimpleMode; }
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
self.openingHours = textView.text;
self.navigationItem.rightBarButtonItem.enabled = self.model.isValid;
self.toggleModeButton.enabled = self.model.isSimpleModeCapable;
}
#pragma mark - Properties
- (void)setIsSimpleMode:(BOOL)isSimpleMode
{
self.model.isSimpleMode = isSimpleMode;
if (!isSimpleMode)
self.exampleExpanded = NO;
self.toggleModeButton.enabled = self.model.isSimpleModeCapable;
}
- (BOOL)isSimpleMode { return self.model.isSimpleMode; }
@end
|