linking Timer/Stopwatch via delegate methods

Change-Id: I8635b79032ae9631637f67df7d0308973d11bec3
This commit is contained in:
Siqi LIU 2013-09-07 22:21:55 +02:00
parent 0c3b5abb8a
commit b88c22fc17
8 changed files with 344 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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>
@interface Timer : NSObject
#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

View File

@ -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"
@implementation Timer
@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

View File

@ -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

View File

@ -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];

View File

@ -14,7 +14,7 @@
@protocol StopWatchDelegate <NSObject>
- (void) setTitle:(NSString *) title;
- (void) setTitle:(NSString *) title sender:(id)sender;
@end

View File

@ -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