Skip to content

Instantly share code, notes, and snippets.

@yene
Created June 22, 2010 10:21
Show Gist options
  • Save yene/448288 to your computer and use it in GitHub Desktop.
Save yene/448288 to your computer and use it in GitHub Desktop.
//
// MyApplication.m
// IdleTimer
//
// Created by Yannick Weiss on 21.06.10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MyApplication.h"
#import "IdleTimerContainer.h"
@implementation MyApplication
- (id) init
{
self = [super init];
if (self != nil) {
[self resetValues];
idleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerInvocation) userInfo:nil repeats:YES];
timerInvocations = [[NSMutableArray array] retain];
}
return self;
}
- (void) dealloc
{
[idleTimer release];
[timerInvocations release];
[super dealloc];
}
- (void)sendEvent:(NSEvent *)anEvent;
{
[super sendEvent:anEvent];
// you can filter events here
[self resetValues];
}
- (void)timerInvocation;
{
elapsedSeconds++;
if (elapsedSeconds == 5 ) {
isUser5SecondsIdle = YES;
}
if (elapsedSeconds == 10 ) {
isUser10SecondsIdle = YES;
}
for (IdleTimerContainer *temp in timerInvocations) {
if (elapsedSeconds == temp.seconds) {
[temp perform];
}
}
NSLog(@"Timer: %i", elapsedSeconds);
}
- (void)resetValues;
{
isUserIdle = NO;
isUser5SecondsIdle = NO;
isUser10SecondsIdle = NO;
elapsedSeconds = 0;
NSLog(@"reset");
}
- (void)addIdleTimerWithSeconds:(int)seconds target:(id)target selector:(SEL)selector;
{
if (seconds <= 1) return;
IdleTimerContainer *foo = [[IdleTimerContainer alloc] initWithSeconds:seconds target:target selector:selector];
[timerInvocations addObject:foo];
[foo release];
}
- (void)removeIdleTimerForTarget:(id)target;
{
for (IdleTimerContainer *temp in timerInvocations) {
if ([target isEqual:temp.target]) {
[timerInvocations removeObject:temp];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment