This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BGScrobbleDecisionManager.m
162 lines (132 loc) · 4.96 KB
/
BGScrobbleDecisionManager.m
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
//
// BGScrobbleDecisionManager.m
// ScrobblePod
//
// Created by Ben Gummer on 15/05/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGScrobbleDecisionManager.h"
#import "Defines.h"
#import "iPodWatcher.h"
#import "GrowlHub.h"
@implementation BGScrobbleDecisionManager
static BGScrobbleDecisionManager *sharedDecisionManager = nil;
+(BGScrobbleDecisionManager *)sharedManager {
@synchronized(self) {
if (sharedDecisionManager == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedDecisionManager;
}
+(id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedDecisionManager == nil) {
sharedDecisionManager = [super allocWithZone:zone];
return sharedDecisionManager; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
-(id)copyWithZone:(NSZone *)zone {
return self;
}
-(id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
-(void)release {
//do nothing
}
-(id)autorelease {
return self;
}
-(id)init {
self = [super init];
if (self != nil) {
firstRefresh = YES;
self.isDecisionMadeAutomtically = YES;
self.usersManualChoice = NO;
[self shouldScrobble];
[self startRefreshTimer];
}
return self;
}
- (void) dealloc
{
[self stopRefreshTimer];
[super dealloc];
}
#pragma mark Decision Making
-(BOOL)shouldScrobble {
if ([[NSUserDefaults standardUserDefaults] boolForKey:BGPrefUsePodFreshnessInterval]) {
self.cachedOverallDecision = (self.isDecisionMadeAutomtically ? [self shouldScrobbleAuto] : self.usersManualChoice );
} else {
self.cachedOverallDecision = YES;
}
return self.cachedOverallDecision;
}
-(BOOL)shouldScrobbleAuto {
BOOL shouldScrobbleAuto = YES;
if ([[NSUserDefaults standardUserDefaults] boolForKey:BGPrefUsePodFreshnessInterval]) {
NSDate *testDate = [[NSDate alloc] initWithTimeIntervalSinceNow: ([[NSUserDefaults standardUserDefaults] integerForKey:BGPrefPodFreshnessInterval]*-3600) ];
shouldScrobbleAuto = [[iPodWatcher sharedManager] iPodDisconnectedSinceDate:testDate];
[testDate release];
}
self.cachedOverallDecision = shouldScrobbleAuto;
return self.cachedOverallDecision;
}
#pragma mark Refreshing Cache
@synthesize cachedOverallDecision;
@synthesize isDecisionMadeAutomtically;
@synthesize usersManualChoice;
-(void)refreshDecisionAndNotifyIfChanged:(BOOL)notify {
BOOL oldDecision = self.cachedOverallDecision;
BOOL newDecision = [self shouldScrobble];
if (notify && (firstRefresh || oldDecision != newDecision)) {
NSString *descriptionString;
if (newDecision == YES) {
// if user enables the 3-hour (default) time limit, then work out how long there is left until scrobbling is disabled
if ([[NSUserDefaults standardUserDefaults] boolForKey:BGPrefUsePodFreshnessInterval]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *lastSyncDate = [defaults objectForKey:BGLastSyncDate];
NSCalendarDate *lastSyncCalDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:[lastSyncDate timeIntervalSinceReferenceDate]];
int minutesToAdd = (int)([[defaults stringForKey:BGPrefPodFreshnessInterval] floatValue]*60.0);
NSCalendarDate *cutoffDate = [lastSyncCalDate dateByAddingYears:0 months:0 days:0 hours:0 minutes:minutesToAdd seconds:0];
NSString *dateDescription = [cutoffDate descriptionWithCalendarFormat:@"%I:%M%p"];
descriptionString = [NSString stringWithFormat:@"Tracks played in iTunes will be scrobbled until %@",dateDescription];
} else { // decision is yes, and is always on. we will always scrobble tracks.
descriptionString = @"Tracks played in iTunes will be scrobbled when they are played";
}
} else { // decision is no, so we will not scrobble anything until an iPod is connect
descriptionString = @"Tracks played in iTunes will not be scrobbled until an iPod is connected";
}
[[GrowlHub sharedManager] postGrowlNotificationWithName:SP_Growl_DecisionChanged
andTitle:(newDecision ? @"Scrobbling Enabled" : @"Scrobbling Disabled")
andDescription:descriptionString
andImage:nil
andIdentifier:SP_Growl_DecisionChanged];
}
self.cachedOverallDecision = newDecision;
firstRefresh = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:BGScrobbleDecisionChangedNotification object:nil];
}
#pragma mark Timer
-(void)startRefreshTimer {
[self stopRefreshTimer];
float hoursEntered = [[[NSUserDefaults standardUserDefaults] stringForKey:BGPrefPodFreshnessInterval] floatValue];
int secondsCalculated = (int)(hoursEntered*3600.0);
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:secondsCalculated target:self selector:@selector(refreshFromTimer:) userInfo:nil repeats:YES];
}
-(void)refreshFromTimer:(NSTimer *)fromTimer {
[self refreshDecisionAndNotifyIfChanged:YES];
}
-(void)stopRefreshTimer {
if (refreshTimer) [refreshTimer invalidate];
}
-(void)resetRefreshTimer {
[self startRefreshTimer];
}
@end