summaryrefslogtreecommitdiff
path: root/ios/iosremote/iosremote/Classes/SlideShowSwipeInList~iphone.m
blob: b4fd205bcb70a549ea1917ef882b65c8270978e5 (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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#import "SlideShowSwipeInList~iphone.h"
#import "CommunicationManager.h"
#import "CommandInterpreter.h"
#import "SlideShow.h"
#import "CommandTransmitter.h"
#import "SWRevealViewController.h"
#import "SlideShowPreviewViewController.h"
#import "StopWatch.h"
#import "Timer.h"
#import "UIImageView+setImageAnimated.h"
#import "UIView+Shadowing.h"
#import "UIViewController+LibOStyling.h"
#import <QuartzCore/CALayer.h>
#import <QuartzCore/QuartzCore.h>

@interface SlideShowSwipeInList_iphone () <UIScrollViewDelegate>

@property (nonatomic, strong) CommunicationManager *comManager;
@property (nonatomic, strong) SlideShow *slideshow;
@property NSInteger currentPage;

@end

@implementation SlideShowSwipeInList_iphone

@synthesize comManager = _comManager;
@synthesize currentPage = _currentPage;
@synthesize slideshow = _slideshow;
@synthesize timer = _timer;
@synthesize stopWatch = _stopWatch;

dispatch_queue_t backgroundQueue;


- (void) viewDidLoad
{
    [super viewDidLoad];

    self.comManager = [CommunicationManager sharedComManager];
    self.slideshow = self.comManager.interpreter.slideShow;
    self.slideshow.secondaryDelegate = self;
    
    self.clearsSelectionOnViewWillAppear = NO;
    // set stopwatch as default, users may swipe for a timer
    self.currentPage = kDefaultTimerWidget;
    [self.revealViewController setOwner:kDefaultTimerWidget];
    
    // Instanciate stopwatch and start if autostart is set
    self.stopWatch = [[StopWatch alloc] init];
    if (kStopwatchAutoStart) {
        [self.stopWatch start];
    }
    
    // Instanciate timer and start if autostart is set
    self.timer = [[Timer alloc] init];
    if (kCountDownTimerAutoStart){
        [self.timer start];
    }
    
    self.tableView.backgroundColor = [UIColor colorWithRed:.674509804 green:.729411765 blue:.760784314 alpha:1.0];
}

- (void) viewDidAppear:(BOOL)animated
{
    if (!self.stopWatch.set) {
        [self.stopWatch setupWithTableViewCell:[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
    }
    if (!self.timer.set) {
        [self.timer setupWithTableViewCell:[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
    }
    [self.stopWatch updateStartButtonIcon];
    [self.timer updateStartButtonIcon];
    if ([self.comManager.interpreter.slideShow size] > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.slideshow.currentSlide
                                                    inSection:1];
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
    }
}

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
    // configure the segue.
    // in this case we dont swap out the front view controller, which is a UINavigationController.
    // but we could..
    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] )
    {
        SWRevealViewControllerSegue* rvcs = (SWRevealViewControllerSegue*) segue;

        SWRevealViewController* rvc = self.revealViewController;
        NSAssert( rvc != nil, @"oops! must have a revealViewController" );

        NSAssert( [rvc.frontViewController isKindOfClass: [UINavigationController class]], @"oops!  for this segue we want a permanent navigation controller in the front!" );

        rvcs.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

            UINavigationController* nc = (UINavigationController*)rvc.frontViewController;
            [nc setViewControllers: @[ dvc ] animated: YES ];

            [rvc setFrontViewPosition: FrontViewPositionLeft animated: YES];
        };
    }
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Section one used for stopwatch
    if (section == 0)
        return 1;
    else
        return [self.slideshow size];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 23;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSString *sectionTitle;
    switch (section) {
        case 0:
            switch (self.currentPage) {
                case 0:
                    sectionTitle = NSLocalizedString(@"Stop Watch", @"Sidebar section header");
                    break;
                case 1:
                    sectionTitle = NSLocalizedString(@"Timer", @"Sidebar section header");
                    break;
                default:
                    break;
            }
            break;
        case 1:
            sectionTitle = NSLocalizedString(@"Slides", @"Sidebar section header");
        default:
            break;
    }
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(10, 0, 284, 23);
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:14.0];
    label.text = sectionTitle;
    label.backgroundColor = [UIColor clearColor];
    
    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 23)];
    view.backgroundColor = [UIColor colorWithRed:1.0 green:0.662745098 blue:0.074509804 alpha:0.9];

    [view addSubview:label];
    
    if (section == 0)
    {
        UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.revealViewController.rearViewRevealWidth - 20, view.frame.origin.y + 3, 40, 20)];
        pageControl.numberOfPages = 2;
        pageControl.currentPage = self.currentPage;
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
        {
            pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
            pageControl.pageIndicatorTintColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7];
        }
        pageControl.tag = -100;
        view.tag = -99;
        label.tag = -98;
        [view addSubview:pageControl];
    }
    
    return view;
}

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier: @"sw_customized_segue" sender: [tableView cellForRowAtIndexPath: indexPath]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"stopWatch";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        UIView * view = [cell viewWithTag:8];
        [view.layer setCornerRadius:5.0f];
        view.layer.masksToBounds = YES;
        view.clipsToBounds = YES;
        [view setShadow];
        
        view = [cell viewWithTag:9];
        [view.layer setCornerRadius:5.0f];
        view.layer.masksToBounds = YES;
        view.clipsToBounds = YES;
        [view setShadow];
        
        UIScrollView * scroll = (UIScrollView *) [cell viewWithTag:7];
        scroll.contentSize = CGSizeMake(412, 120);
        
        CGRect frame = scroll.frame;
        frame.origin.x = frame.size.width * self.currentPage;
        frame.origin.y = 0;
        [scroll scrollRectToVisible:frame animated:YES];
        
        cell.contentView.backgroundColor = [UIColor colorWithRed:.674509804-0.02 * indexPath.row green:.729411765-0.02 * indexPath.row blue:.760784314-0.02 * indexPath.row alpha:1.0];

        return cell;
    } else {
        static NSString *CellIdentifier = @"slide";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
        UILabel * slideNumber = (UILabel *)[cell viewWithTag:2];
        UIImageView * image = (UIImageView *)[cell viewWithTag:1];

        // Starting 20, all tags are used for thumbnails in this sidebar
        [cell setTag:20+indexPath.row];
        [self.slideshow getContentAtIndex:indexPath.row forView:cell];
        [slideNumber setText:[NSString stringWithFormat:@"%u", indexPath.row+1]];
        
        [image setShadow];
        
        cell.contentView.backgroundColor = [UIColor colorWithRed:.674509804-0.02 * indexPath.row green:.729411765-0.02 * indexPath.row blue:.760784314-0.02 * indexPath.row alpha:1.0];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    // Otherwise selection will disable background color and make slide number unreadable
    if(indexPath.section == 1 && cell.selected){
        UILabel *label = (UILabel *)[cell viewWithTag:2];
        if ([label backgroundColor]!=[UIColor lightGrayColor]) {
            [label setBackgroundColor:[UIColor lightGrayColor]];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
        return;
    [self.comManager.transmitter gotoSlide:indexPath.row];
    [[[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:2] setBackgroundColor:[UIColor lightGrayColor]];
    [self.revealViewController revealToggle: self];
}

- (void)viewDidUnload {
    [self setStopWatch:nil];
    [self setTimer:nil];
    [super viewDidUnload];
}

#pragma mark scrollview delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 7) {
        CGFloat pageWidth = scrollView.frame.size.width;
        float fractionalPage = scrollView.contentOffset.x / pageWidth;
        NSInteger page = lround(fractionalPage);
        if (self.currentPage != page) {
            UIPageControl * pageControl = (UIPageControl *) ([[self.tableView viewWithTag:-99] viewWithTag:-100]);
            UILabel * label = (UILabel *) ([[self.tableView viewWithTag:-99] viewWithTag:-98]);
            
            CATransition *animation = [CATransition animation];
            animation.duration = 0.5;
            animation.type = kCATransitionMoveIn;
            animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
            [label.layer addAnimation:animation forKey:@"changeTextTransition"];

            switch (page) {
                case 0:
                    [label setText:NSLocalizedString(@"Stop Watch", @"Sidebar section header")];
                    [self.revealViewController setTitle:@""];
                    [self.revealViewController setOwner:STOPWATCH];
                    break;
                case 1:
                    [label setText:NSLocalizedString(@"Timer", @"Sidebar section header")];
                    [self.revealViewController setTitle:@""];
                    [self.revealViewController setOwner:TIMER];
                    break;
                default:
                    break;
            }
            [pageControl setCurrentPage:page];
            self.currentPage = page;
        }
    }
}

@end