diff options
author | Siqi LIU <me@siqi.fr> | 2013-09-07 22:21:55 +0200 |
---|---|---|
committer | Siqi LIU <me@siqi.fr> | 2013-09-07 22:28:38 +0200 |
commit | b88c22fc1775cf63fadd996c75ce859acbe18c65 (patch) | |
tree | 98cef499e5d7f56ee05de5e133ab56581866718b /ios | |
parent | 0c3b5abb8a1bd2dcc674e68244a870d62e3808dd (diff) |
linking Timer/Stopwatch via delegate methods
Change-Id: I8635b79032ae9631637f67df7d0308973d11bec3
Diffstat (limited to 'ios')
-rwxr-xr-x | ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.h | 5 | ||||
-rwxr-xr-x | ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.m | 68 | ||||
-rw-r--r-- | ios/iosremote/iosremote/Timer.h | 41 | ||||
-rw-r--r-- | ios/iosremote/iosremote/Timer.m | 181 | ||||
-rw-r--r-- | ios/iosremote/iosremote/UIViewController+LibOStyling.h | 14 | ||||
-rw-r--r-- | ios/iosremote/iosremote/UIViewController+LibOStyling.m | 38 | ||||
-rw-r--r-- | ios/iosremote/iosremote/stopWatch.h | 2 | ||||
-rw-r--r-- | ios/iosremote/iosremote/stopWatch.m | 10 |
8 files changed, 342 insertions, 17 deletions
diff --git a/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.h b/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.h index 02495863c730..dc7e144382d9 100755 --- a/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.h +++ b/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.h @@ -27,6 +27,7 @@ #import <UIKit/UIKit.h> #import "stopWatch.h" +#import "Timer.h" @class SWRevealViewController; @protocol SWRevealViewControllerDelegate; @@ -57,7 +58,7 @@ typedef enum } FrontViewPosition; -@interface SWRevealViewController : UIViewController <StopWatchDelegate> +@interface SWRevealViewController : UIViewController <StopWatchDelegate, TimerDelegate> // Object instance init and rear view setting - (id)initWithRearViewController:(UIViewController *)rearViewController frontViewController:(UIViewController *)frontViewController; @@ -172,6 +173,8 @@ typedef enum @property (strong) void(^performBlock)( SWRevealViewControllerSegue* segue, UIViewController* svc, UIViewController* dvc ); +- (void)startTimePickerwithTimer:(Timer *) timer; + @end diff --git a/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.m b/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.m index 67c48c38fa7d..922f83c2272d 100755 --- a/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.m +++ b/ios/iosremote/iosremote/SWRevealViewController/SWRevealViewController.m @@ -28,6 +28,8 @@ #import <UIKit/UIGestureRecognizerSubclass.h> #import "SWRevealViewController.h" +#import "slideShowSwipeInList_iphone.h" +#import "UIViewController+LibOStyling.h" #pragma mark - SWDirectionPanGestureRecognizer @@ -372,6 +374,72 @@ const int FrontViewPositionNone = 0xff; _animationQueue = [NSMutableArray array]; } +#pragma mark popup timer +- (void)changeDate:(UIDatePicker *)sender { + NSLog(@"Time left: %f", sender.countDownDuration); + [[(slideShowSwipeInList *) self.rearViewController timer] setSecondsLeft: sender.countDownDuration]; +} + +- (void)removeViews:(id)object { + [[self.view viewWithTag:89] removeFromSuperview]; + [[self.view viewWithTag:90] removeFromSuperview]; + [[self.view viewWithTag:91] removeFromSuperview]; +} + +- (void)dismissDatePicker:(id)sender { + CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44); + CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216); + [UIView beginAnimations:@"MoveOut" context:nil]; + [self.view viewWithTag:89].alpha = 0; + [self.view viewWithTag:90].frame = datePickerTargetFrame; + [self.view viewWithTag:91].frame = toolbarTargetFrame; + [UIView setAnimationDelegate:self]; + [UIView setAnimationDidStopSelector:@selector(removeViews:)]; + [UIView commitAnimations]; +} + +- (IBAction)callDP:(id)sender { + if ([self.view viewWithTag:89]) { + return; + } + CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44); + CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216); + + UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds]; + darkView.alpha = 0; + darkView.backgroundColor = [UIColor blackColor]; + darkView.tag = 89; + UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)]; + [darkView addGestureRecognizer:tapGesture]; + [self.view addSubview:darkView]; + + UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)]; + datePicker.tag = 90; + [datePicker setDatePickerMode:UIDatePickerModeCountDownTimer]; + [datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged]; + [self.view addSubview:datePicker]; + + UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)]; + toolBar.tag = 91; + toolBar.barStyle = UIBarStyleBlackTranslucent; + UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; + UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)]; + [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]]; + [self.view addSubview:toolBar]; + + [UIView beginAnimations:@"MoveIn" context:nil]; + toolBar.frame = toolbarTargetFrame; + datePicker.frame = datePickerTargetFrame; + darkView.alpha = 0.5; + [UIView commitAnimations]; +} + +- (void)startTimePickerwithTimer:(Timer *) timer +{ + NSLog(@"Fired by : %@", [timer class]); + [self callDP:self]; +} + #pragma mark storyboard support diff --git a/ios/iosremote/iosremote/Timer.h b/ios/iosremote/iosremote/Timer.h index 9ed22c5bb12b..4ea0f4438f13 100644 --- a/ios/iosremote/iosremote/Timer.h +++ b/ios/iosremote/iosremote/Timer.h @@ -1,13 +1,44 @@ +// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ // -// Timer.h -// iosremote -// -// Created by Siqi Liu on 9/7/13. -// Copyright (c) 2013 libreoffice. All rights reserved. +// 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 <Foundation/Foundation.h> +#define TIMER_STATE_RUNNING 0 +#define TIMER_STATE_PAUSED 1 +#define TIMER_STATE_CLEARED 2 + +@class Timer; + +@protocol TimerDelegate <NSObject> + +- (void) setTitle:(NSString *) title sender:(id)sender; +- (void) startTimePickerwithTimer:(Timer *) timer; + +@end + @interface Timer : NSObject +// Timer +@property (strong, nonatomic) NSTimer *timerTimer; +@property BOOL set; +@property (weak, nonatomic) id<TimerDelegate> delegate; + +- (Timer *) initWithStartButton:(UIButton *)startButton + ClearButton:(UIButton *)clearButton + SetTimeButton:(UIButton *)setTimeButton + TimeLabel:(UILabel *)timeLabel; +- (void) setupWithTableViewCell:(UITableViewCell *)cell; + +- (void) start; +- (void) clear; +- (void) updateStartButtonIcon; + +- (void) setSecondsLeft:(NSTimeInterval)duration; + @end + diff --git a/ios/iosremote/iosremote/Timer.m b/ios/iosremote/iosremote/Timer.m index af1ad023ce81..57c5f7a1c162 100644 --- a/ios/iosremote/iosremote/Timer.m +++ b/ios/iosremote/iosremote/Timer.m @@ -1,13 +1,184 @@ +// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ // -// Timer.m -// iosremote -// -// Created by Siqi Liu on 9/7/13. -// Copyright (c) 2013 libreoffice. All rights reserved. +// 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 "Timer.h" +@interface Timer () + +@property int state; +@property (weak, nonatomic) UIButton * startButton; +@property (weak, nonatomic) UIButton * clearButton; +@property (weak, nonatomic) UIButton * setTimeButton; +@property (weak, nonatomic) UILabel * timeLabel; + +@end + @implementation Timer +@synthesize startButton = _startButton; +@synthesize clearButton = _clearButton; +@synthesize timeLabel = _timeLabel; +@synthesize setTimeButton = _setTimeButton; + +int hours, minutes, seconds; +int secondsLeft; +int initSecondsLeft; + +- (Timer *) init +{ + self = [super init]; + self.state = TIMER_STATE_CLEARED; + self.set = NO; + secondsLeft = 30; + + return self; +} + +- (Timer *) initWithStartButton:(UIButton *)startButton + ClearButton:(UIButton *)clearButton + SetTimeButton:(UIButton *)setTimeButton + TimeLabel:(UILabel *)timeLabel +{ + self = [self init]; + + self.startButton = startButton; + self.clearButton = clearButton; + self.setTimeButton = setTimeButton; + self.timeLabel = timeLabel; + + [self setupActions]; + return self; +} + +- (void) setupWithTableViewCell:(UITableViewCell *)cell +{ + self.startButton = (UIButton *)[[cell viewWithTag:9] viewWithTag:2]; + self.clearButton = (UIButton *)[[cell viewWithTag:9] viewWithTag:3]; + self.setTimeButton = (UIButton *)[[cell viewWithTag:9] viewWithTag:4]; + self.timeLabel = (UILabel *)[[cell viewWithTag:9] viewWithTag:1]; + + [self setupActions]; +} + +- (void) setupActions +{ + [self.startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside]; + [self.clearButton addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside]; + // Sending the sender as well, so that we get a handle on the Timer itself ---> allow us to update seconds left + [self.setTimeButton addTarget:self.delegate action:@selector(startTimePickerwithTimer:) forControlEvents:UIControlEventTouchUpInside]; + self.set = YES; +} + +- (void)updateTimer +{ + // Create date from the elapsed time + if (secondsLeft > 0) { + secondsLeft--; + hours = secondsLeft / 3600; + minutes = (secondsLeft % 3600) / 60; + seconds = (secondsLeft %3600) % 60; + self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; + [self.delegate setTitle:[NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds] sender:self]; + } else { + // flash timer label in red + static BOOL disappear = NO; + if (!disappear) { + disappear = YES; + self.timeLabel.text = @""; + [self.delegate setTitle:@"" sender:self]; + } else { + disappear = NO; + self.timeLabel.text = @"00:00:00"; + [self.delegate setTitle:@"00:00:00" sender:self]; + } + } +} + + +- (void) start +{ + switch (self.state) { + case TIMER_STATE_RUNNING: + self.state = TIMER_STATE_PAUSED; + [self.timerTimer invalidate]; + break; + case TIMER_STATE_PAUSED: + self.state = TIMER_STATE_RUNNING; + self.timerTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 + target:self + selector:@selector(updateTimer) + userInfo:nil + repeats:YES]; + [[NSRunLoop currentRunLoop] addTimer:self.timerTimer forMode:NSRunLoopCommonModes]; + break; + case TIMER_STATE_CLEARED: + self.state = TIMER_STATE_RUNNING; + secondsLeft++; + [self updateTimer]; + // Create the stop watch timer that fires every 100 ms + self.timerTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 + target:self + selector:@selector(updateTimer) + userInfo:nil + repeats:YES]; + [[NSRunLoop currentRunLoop] addTimer:self.timerTimer forMode:NSRunLoopCommonModes]; + initSecondsLeft = secondsLeft; + break; + default: + break; + } + + [self updateStartButtonIcon]; +} + +- (void) updateStartButtonIcon +{ + switch (self.state) { + case TIMER_STATE_RUNNING: + [self.startButton setImage:[UIImage imageNamed:@"timer_pause_btn"] forState:UIControlStateNormal]; + break; + case TIMER_STATE_PAUSED: + [self.startButton setImage:[UIImage imageNamed:@"timer_start_btn"] forState:UIControlStateNormal]; + break; + case TIMER_STATE_CLEARED: + [self.startButton setImage:[UIImage imageNamed:@"timer_start_btn"] forState:UIControlStateNormal]; + break; + default: + break; + } +} + +- (void) clear +{ + [self.timerTimer invalidate]; + self.timerTimer = nil; + self.state = TIMER_STATE_CLEARED; + + [self.startButton setImage:[UIImage imageNamed:@"timer_start_btn"] forState:UIControlStateNormal]; + [self.delegate setTitle:@"" sender:self]; + + secondsLeft = initSecondsLeft; + + hours = secondsLeft / 3600; + minutes = (secondsLeft % 3600) / 60; + seconds = (secondsLeft %3600) % 60; + self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; +} + +- (void) setSecondsLeft:(NSTimeInterval)duration +{ + secondsLeft = (int) duration; + + hours = secondsLeft / 3600; + minutes = (secondsLeft % 3600) / 60; + seconds = (secondsLeft %3600) % 60; + self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; +} + @end + diff --git a/ios/iosremote/iosremote/UIViewController+LibOStyling.h b/ios/iosremote/iosremote/UIViewController+LibOStyling.h index cfd3d58084c5..24b5babcb91e 100644 --- a/ios/iosremote/iosremote/UIViewController+LibOStyling.h +++ b/ios/iosremote/iosremote/UIViewController+LibOStyling.h @@ -8,8 +8,22 @@ #import <UIKit/UIKit.h> +@class Timer; + +typedef enum TitleLabelOwner : NSInteger TitleLabelOwner; +enum TitleLabelOwner : NSInteger { + STOPWATCH, + TIMER +}; + +TitleLabelOwner owner; + @interface UIViewController (LibOStyling) +- (void)setTitle:(NSString *)title sender:(id)sender; - (void)setTitle:(NSString *)title; +- (void)setOwner:(TitleLabelOwner) aOwner; +- (void)startTimePickerwithTimer:(Timer *) timer; + @end diff --git a/ios/iosremote/iosremote/UIViewController+LibOStyling.m b/ios/iosremote/iosremote/UIViewController+LibOStyling.m index 272f72615f2b..193f912e0631 100644 --- a/ios/iosremote/iosremote/UIViewController+LibOStyling.m +++ b/ios/iosremote/iosremote/UIViewController+LibOStyling.m @@ -8,9 +8,13 @@ #import "UIViewController+LibOStyling.h" #import "ControlVariables.h" +#import "Timer.h" +#import "TimerCountdownTimePicker.h" +#import "stopWatch.h" @implementation UIViewController (LibOStyling) + - (void)setTitle:(NSString *)title { UILabel *titleView = (UILabel *)self.navigationItem.titleView; @@ -28,6 +32,40 @@ [titleView sizeToFit]; } +- (void)setOwner:(TitleLabelOwner) aOwner +{ + owner = aOwner; +} + +- (void)setTitle:(NSString *)title sender:(id)sender +{ + switch (owner) { + case STOPWATCH: + if (![sender isKindOfClass:[stopWatch class]]) + return; + break; + case TIMER: + if (![sender isKindOfClass:[Timer class]]) + return; + break; + default: + break; + } + UILabel *titleView = (UILabel *)self.navigationItem.titleView; + if (!titleView) { + titleView = [[UILabel alloc] initWithFrame:CGRectZero]; + titleView.backgroundColor = [UIColor clearColor]; + titleView.font = kAppTitleFont; + titleView.shadowColor = nil; + + titleView.textColor = [UIColor blackColor]; + + self.navigationItem.titleView = titleView; + } + titleView.text = title; + [titleView sizeToFit]; +} + - (void) handleBack { [self.navigationController popViewControllerAnimated:YES]; diff --git a/ios/iosremote/iosremote/stopWatch.h b/ios/iosremote/iosremote/stopWatch.h index a77f55ab7362..01f5edc44365 100644 --- a/ios/iosremote/iosremote/stopWatch.h +++ b/ios/iosremote/iosremote/stopWatch.h @@ -14,7 +14,7 @@ @protocol StopWatchDelegate <NSObject> -- (void) setTitle:(NSString *) title; +- (void) setTitle:(NSString *) title sender:(id)sender; @end diff --git a/ios/iosremote/iosremote/stopWatch.m b/ios/iosremote/iosremote/stopWatch.m index 2eea9f4dbcb8..c5c942ec95c6 100644 --- a/ios/iosremote/iosremote/stopWatch.m +++ b/ios/iosremote/iosremote/stopWatch.m @@ -51,9 +51,9 @@ - (void) setupWithTableViewCell:(UITableViewCell *)cell { - self.startButton = (UIButton *)[cell viewWithTag:2]; - self.clearButton = (UIButton *)[cell viewWithTag:3]; - self.timeLabel = (UILabel *)[cell viewWithTag:1]; + self.startButton = (UIButton *)[[cell viewWithTag:8] viewWithTag:2]; + self.clearButton = (UIButton *)[[cell viewWithTag:8] viewWithTag:3]; + self.timeLabel = (UILabel *)[[cell viewWithTag:8] viewWithTag:1]; [self setupActions]; } @@ -80,7 +80,7 @@ // Format the elapsed time and set it to the label NSString *timeString = [dateFormatter stringFromDate:timerDate]; self.timeLabel.text = timeString; - [self.delegate setTitle:timeString]; + [self.delegate setTitle:timeString sender:self]; } @@ -147,7 +147,7 @@ [self.startButton setImage:[UIImage imageNamed:@"timer_start_btn"] forState:UIControlStateNormal]; [self updateTimer]; - [self.delegate setTitle:@""]; + [self.delegate setTitle:@"" sender:self]; } @end |