| 1 |
masaki |
120 |
// |
| 2 |
|
|
// XspfMRuleEditorDelegate.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/28. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMRuleEditorDelegate.h" |
| 10 |
|
|
|
| 11 |
|
|
#import "XspfMRuleEditorRow.h" |
| 12 |
|
|
|
| 13 |
|
|
@implementation XspfMRuleEditorDelegate |
| 14 |
masaki |
125 |
|
| 15 |
|
|
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 16 |
|
|
|
| 17 |
masaki |
124 |
- (NSDictionary *)buildRows:(NSArray *)template |
| 18 |
|
|
{ |
| 19 |
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary]; |
| 20 |
|
|
for(id row in template) { |
| 21 |
masaki |
159 |
id name = [row valueForKey:@"name"]; |
| 22 |
masaki |
152 |
id rule = [XspfMRule ruleWithPlist:row]; |
| 23 |
|
|
[result setObject:rule forKey:name]; |
| 24 |
masaki |
124 |
} |
| 25 |
masaki |
152 |
rowTemplate = [result retain]; |
| 26 |
masaki |
124 |
return result; |
| 27 |
|
|
} |
| 28 |
masaki |
126 |
- (id)criteriaWithKeyPath:(NSString *)keypath |
| 29 |
|
|
{ |
| 30 |
|
|
NSString *key = nil; |
| 31 |
|
|
if([keypath isEqualToString:@"title"]) { |
| 32 |
|
|
key = @"String"; |
| 33 |
|
|
} else if([keypath isEqualToString:@"rating"]) { |
| 34 |
|
|
key = @"Rate"; |
| 35 |
masaki |
152 |
} else if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) { |
| 36 |
|
|
key = @"AbDate"; |
| 37 |
masaki |
126 |
} |
| 38 |
|
|
if(key) { |
| 39 |
|
|
id row = [rowTemplate valueForKey:key]; |
| 40 |
masaki |
152 |
id c = [[[row childAtIndex:0] copy] autorelease]; |
| 41 |
|
|
[c setValue:keypath]; |
| 42 |
masaki |
126 |
return [NSArray arrayWithObject:c]; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
return nil; |
| 46 |
|
|
} |
| 47 |
masaki |
120 |
- (void)awakeFromNib |
| 48 |
|
|
{ |
| 49 |
|
|
if(!compound) { |
| 50 |
masaki |
143 |
compound = [[XspfMCompound alloc] init]; |
| 51 |
masaki |
120 |
} |
| 52 |
masaki |
152 |
|
| 53 |
masaki |
124 |
NSBundle *m = [NSBundle mainBundle]; |
| 54 |
|
|
NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 55 |
|
|
NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path]; |
| 56 |
|
|
if(!rowsTemplate) { |
| 57 |
|
|
exit(12345); |
| 58 |
masaki |
120 |
} |
| 59 |
masaki |
124 |
|
| 60 |
masaki |
152 |
[self buildRows:rowsTemplate]; |
| 61 |
|
|
|
| 62 |
masaki |
124 |
NSMutableArray *newRows = [NSMutableArray array]; |
| 63 |
|
|
|
| 64 |
masaki |
126 |
id c = [self criteriaWithKeyPath:@"title"]; |
| 65 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 66 |
masaki |
124 |
|
| 67 |
|
|
for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) { |
| 68 |
masaki |
126 |
c = [self criteriaWithKeyPath:keyPath]; |
| 69 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 70 |
masaki |
124 |
} |
| 71 |
|
|
|
| 72 |
masaki |
126 |
c = [self criteriaWithKeyPath:@"rating"]; |
| 73 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 74 |
masaki |
124 |
|
| 75 |
masaki |
152 |
simples = [newRows retain]; |
| 76 |
|
|
|
| 77 |
masaki |
125 |
//// |
| 78 |
|
|
predicateRows = [[NSMutableArray alloc] init]; |
| 79 |
masaki |
159 |
[ruleEditor bind:@"rows" toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 80 |
masaki |
120 |
} |
| 81 |
|
|
|
| 82 |
masaki |
150 |
- (void)resolveExpression:(id)exp |
| 83 |
|
|
{ |
| 84 |
|
|
NSString *message = nil; |
| 85 |
|
|
|
| 86 |
|
|
switch([exp expressionType]) { |
| 87 |
|
|
case NSConstantValueExpressionType: |
| 88 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 89 |
|
|
break; |
| 90 |
|
|
case NSEvaluatedObjectExpressionType: |
| 91 |
|
|
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 92 |
|
|
break; |
| 93 |
|
|
case NSVariableExpressionType: |
| 94 |
|
|
message = [NSString stringWithFormat:@"variable -> %@", [exp variable]]; |
| 95 |
|
|
break; |
| 96 |
|
|
case NSKeyPathExpressionType: |
| 97 |
|
|
message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]]; |
| 98 |
|
|
break; |
| 99 |
|
|
case NSFunctionExpressionType: |
| 100 |
masaki |
154 |
message = [NSString stringWithFormat:@"oprand -> %@(%@), function -> %@, arguments -> %@", |
| 101 |
masaki |
150 |
[exp operand], NSStringFromClass([[exp operand] class]), |
| 102 |
|
|
[exp function], [exp arguments]]; |
| 103 |
|
|
break; |
| 104 |
|
|
case NSAggregateExpressionType: |
| 105 |
|
|
message = [NSString stringWithFormat:@"collection -> %@", [exp collection]]; |
| 106 |
|
|
break; |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
fprintf(stderr, "%s\n", [message UTF8String]); |
| 110 |
|
|
} |
| 111 |
|
|
- (void)resolvePredicate:(id)predicate |
| 112 |
|
|
{ |
| 113 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 114 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 115 |
|
|
for(id p in sub) { |
| 116 |
|
|
[self resolvePredicate:p]; |
| 117 |
|
|
} |
| 118 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 119 |
|
|
id left = [predicate leftExpression]; |
| 120 |
|
|
id right = [predicate rightExpression]; |
| 121 |
|
|
SEL sel = Nil; |
| 122 |
|
|
if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) { |
| 123 |
|
|
sel = [predicate customSelector]; |
| 124 |
|
|
} |
| 125 |
|
|
fprintf(stderr, "left ->\t"); |
| 126 |
|
|
[self resolveExpression:left]; |
| 127 |
|
|
if(sel) { |
| 128 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]); |
| 129 |
|
|
} else { |
| 130 |
|
|
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]); |
| 131 |
|
|
} |
| 132 |
|
|
fprintf(stderr, "right ->\t"); |
| 133 |
|
|
[self resolveExpression:right]; |
| 134 |
|
|
fprintf(stderr, "end resolve.\n"); |
| 135 |
|
|
} |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
masaki |
123 |
- (void)setPredicate:(id)predicate |
| 139 |
|
|
{ |
| 140 |
masaki |
150 |
NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate); |
| 141 |
|
|
[self resolvePredicate:predicate]; |
| 142 |
masaki |
123 |
|
| 143 |
masaki |
153 |
id new = [XspfMRule ruleEditorRowsFromPredicate:predicate withRowTemplate:rowTemplate]; |
| 144 |
masaki |
125 |
|
| 145 |
|
|
[self willChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 146 |
|
|
[predicateRows release]; |
| 147 |
|
|
predicateRows = [new retain]; |
| 148 |
|
|
[self didChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 149 |
masaki |
146 |
// [ruleEditor reloadCriteria]; |
| 150 |
masaki |
123 |
} |
| 151 |
masaki |
125 |
- (void)setPredicateRows:(id)p |
| 152 |
|
|
{ |
| 153 |
masaki |
147 |
// NSLog(@"new -> %@", p); |
| 154 |
masaki |
125 |
[predicateRows release]; |
| 155 |
|
|
predicateRows = [p retain]; |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
|
|
#pragma mark#### NSRleEditor Delegate #### |
| 159 |
masaki |
152 |
|
| 160 |
masaki |
124 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 161 |
|
|
numberOfChildrenForCriterion:(id)criterion |
| 162 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 163 |
|
|
{ |
| 164 |
|
|
NSInteger result = 0; |
| 165 |
|
|
|
| 166 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 167 |
|
|
return [compound numberOfChildrenForChild:criterion]; |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
masaki |
124 |
if(!criterion) { |
| 171 |
masaki |
152 |
result = [simples count]; |
| 172 |
masaki |
143 |
} else { |
| 173 |
masaki |
152 |
result = [criterion numberOfChildren]; |
| 174 |
masaki |
124 |
} |
| 175 |
|
|
|
| 176 |
masaki |
152 |
// NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result); |
| 177 |
masaki |
124 |
|
| 178 |
|
|
return result; |
| 179 |
|
|
} |
| 180 |
|
|
|
| 181 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 182 |
|
|
child:(NSInteger)index |
| 183 |
|
|
forCriterion:(id)criterion |
| 184 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 185 |
|
|
{ |
| 186 |
|
|
id result = nil; |
| 187 |
|
|
|
| 188 |
masaki |
143 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 189 |
|
|
return [compound childForChild:criterion atIndex:index]; |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
masaki |
124 |
if(!criterion) { |
| 193 |
masaki |
152 |
result = [simples objectAtIndex:index]; |
| 194 |
masaki |
143 |
} else { |
| 195 |
masaki |
152 |
result = [criterion childAtIndex:index]; |
| 196 |
masaki |
124 |
} |
| 197 |
|
|
|
| 198 |
masaki |
152 |
// NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result); |
| 199 |
masaki |
124 |
|
| 200 |
|
|
return result; |
| 201 |
|
|
} |
| 202 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 203 |
|
|
displayValueForCriterion:(id)criterion |
| 204 |
|
|
inRow:(NSInteger)row |
| 205 |
|
|
{ |
| 206 |
|
|
id result = nil; |
| 207 |
masaki |
143 |
|
| 208 |
|
|
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 209 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 210 |
|
|
return [compound displayValueForChild:criterion]; |
| 211 |
|
|
} |
| 212 |
|
|
|
| 213 |
|
|
if(!criterion) { |
| 214 |
|
|
// |
| 215 |
|
|
} else { |
| 216 |
masaki |
152 |
result = [criterion displayValueForRuleEditor:editor inRow:row]; |
| 217 |
masaki |
143 |
} |
| 218 |
|
|
|
| 219 |
masaki |
152 |
// NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result); |
| 220 |
masaki |
143 |
|
| 221 |
masaki |
124 |
return result; |
| 222 |
|
|
} |
| 223 |
|
|
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 224 |
|
|
predicatePartsForCriterion:(id)criterion |
| 225 |
|
|
withDisplayValue:(id)displayValue |
| 226 |
|
|
inRow:(NSInteger)row |
| 227 |
|
|
{ |
| 228 |
|
|
id result = nil; |
| 229 |
|
|
|
| 230 |
masaki |
143 |
NSRuleEditorRowType rowType = [editor rowTypeForRow:row]; |
| 231 |
|
|
if(rowType == NSRuleEditorRowTypeCompound) { |
| 232 |
|
|
return [compound predicateForChild:criterion withDisplayValue:displayValue]; |
| 233 |
|
|
} |
| 234 |
|
|
|
| 235 |
masaki |
152 |
result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row]; |
| 236 |
masaki |
153 |
// NSLog(@"predicate\tresult -> %@", result); |
| 237 |
masaki |
143 |
|
| 238 |
masaki |
152 |
// NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result); |
| 239 |
masaki |
143 |
|
| 240 |
masaki |
124 |
return result; |
| 241 |
|
|
} |
| 242 |
|
|
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 243 |
|
|
{ |
| 244 |
|
|
// |
| 245 |
|
|
} |
| 246 |
masaki |
150 |
@end |
| 247 |
masaki |
124 |
|
| 248 |
masaki |
150 |
@implementation NSString (XspfMTest) |
| 249 |
|
|
- (NSArray *)rangeOfToday |
| 250 |
|
|
{ |
| 251 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 252 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 253 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 254 |
|
|
fromDate:now]; |
| 255 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 256 |
|
|
|
| 257 |
|
|
id result = [NSArray arrayWithObjects:startOfToday, now, nil]; |
| 258 |
|
|
return result; |
| 259 |
|
|
} |
| 260 |
|
|
- (NSArray *)rangeOfYesterday |
| 261 |
|
|
{ |
| 262 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 263 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 264 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 265 |
|
|
fromDate:now]; |
| 266 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 267 |
|
|
|
| 268 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 269 |
|
|
[comp setDay:-1]; |
| 270 |
|
|
NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 271 |
|
|
|
| 272 |
|
|
id result = [NSArray arrayWithObjects:startOfYesterday, startOfToday, nil]; |
| 273 |
|
|
return result; |
| 274 |
|
|
} |
| 275 |
|
|
- (NSArray *)rangeOfThisWeek |
| 276 |
|
|
{ |
| 277 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 278 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 279 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 280 |
|
|
fromDate:now]; |
| 281 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 282 |
|
|
|
| 283 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 284 |
|
|
|
| 285 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 286 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 287 |
|
|
|
| 288 |
|
|
id result = [NSArray arrayWithObjects:startOfThisWeek, now, nil]; |
| 289 |
|
|
return result; |
| 290 |
|
|
} |
| 291 |
|
|
- (NSArray *)rangeOfLastWeek |
| 292 |
|
|
{ |
| 293 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 294 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 295 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 296 |
|
|
fromDate:now]; |
| 297 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 298 |
|
|
|
| 299 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 300 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 301 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 302 |
|
|
|
| 303 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 304 |
|
|
[comp setWeek:-1]; |
| 305 |
|
|
NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 306 |
|
|
|
| 307 |
|
|
id result = [NSArray arrayWithObjects:startOfLastWeek, startOfThisWeek, nil]; |
| 308 |
|
|
return result; |
| 309 |
|
|
} |
| 310 |
masaki |
154 |
|
| 311 |
masaki |
156 |
- (NSArray *)dateRangeByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue |
| 312 |
masaki |
154 |
{ |
| 313 |
|
|
NSInteger number = [numberValue integerValue]; |
| 314 |
|
|
NSInteger unit = [unitValue integerValue]; |
| 315 |
|
|
|
| 316 |
|
|
NSDateComponents *comp01 = [[NSDateComponents alloc] init]; |
| 317 |
|
|
NSDateComponents *comp02 = [[NSDateComponents alloc] init]; |
| 318 |
|
|
NSUInteger unitFlag = 0; |
| 319 |
|
|
switch(unit) { |
| 320 |
|
|
case 0: |
| 321 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; |
| 322 |
|
|
[comp01 setDay:-number]; |
| 323 |
|
|
[comp02 setDay:-number+1]; |
| 324 |
|
|
break; |
| 325 |
|
|
case 1: |
| 326 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit; |
| 327 |
|
|
[comp01 setWeek:-number]; |
| 328 |
|
|
[comp02 setDay:-1]; |
| 329 |
|
|
break; |
| 330 |
|
|
case 2: |
| 331 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit; |
| 332 |
|
|
[comp01 setMonth:-number]; |
| 333 |
|
|
[comp02 setDay:-1]; |
| 334 |
|
|
break; |
| 335 |
|
|
case 3: |
| 336 |
|
|
unitFlag = NSYearCalendarUnit; |
| 337 |
|
|
[comp01 setYear:-number]; |
| 338 |
|
|
[comp02 setDay:-1]; |
| 339 |
|
|
break; |
| 340 |
|
|
} |
| 341 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 342 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 343 |
|
|
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 344 |
|
|
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 345 |
|
|
|
| 346 |
|
|
NSDate *pastDay01 = [aCalendar dateByAddingComponents:comp01 toDate:aDay options:0]; |
| 347 |
|
|
NSDate *pastDay02 = [aCalendar dateByAddingComponents:comp02 toDate:aDay options:0]; |
| 348 |
|
|
|
| 349 |
|
|
id result = [NSArray arrayWithObjects:pastDay01, pastDay02, nil]; |
| 350 |
|
|
return result; |
| 351 |
|
|
} |
| 352 |
masaki |
155 |
- (NSDate *)dateByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue |
| 353 |
|
|
{ |
| 354 |
|
|
NSInteger number = [numberValue integerValue]; |
| 355 |
|
|
NSInteger unit = [unitValue integerValue]; |
| 356 |
|
|
|
| 357 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 358 |
|
|
NSUInteger unitFlag = 0; |
| 359 |
|
|
switch(unit) { |
| 360 |
|
|
case 0: |
| 361 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; |
| 362 |
|
|
[comp setDay:-number]; |
| 363 |
|
|
break; |
| 364 |
|
|
case 1: |
| 365 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit; |
| 366 |
|
|
[comp setWeek:-number]; |
| 367 |
|
|
break; |
| 368 |
|
|
case 2: |
| 369 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit; |
| 370 |
|
|
[comp setMonth:-number]; |
| 371 |
|
|
break; |
| 372 |
|
|
case 3: |
| 373 |
|
|
unitFlag = NSYearCalendarUnit; |
| 374 |
|
|
[comp setYear:-number]; |
| 375 |
|
|
break; |
| 376 |
|
|
} |
| 377 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 378 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 379 |
|
|
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 380 |
|
|
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 381 |
|
|
|
| 382 |
|
|
NSDate *pastDay = [aCalendar dateByAddingComponents:comp toDate:aDay options:0]; |
| 383 |
|
|
|
| 384 |
|
|
return pastDay; |
| 385 |
|
|
} |
| 386 |
masaki |
156 |
- (NSArray *)rangeDateByNumber:(NSNumber *)numberValue toNumber:(NSNumber *)number02Value unit:(NSNumber *)unitValue |
| 387 |
|
|
{ |
| 388 |
|
|
NSInteger number = [numberValue integerValue]; |
| 389 |
|
|
NSInteger number02 = [number02Value integerValue]; |
| 390 |
|
|
NSInteger unit = [unitValue integerValue]; |
| 391 |
|
|
|
| 392 |
|
|
NSDateComponents *comp01 = [[NSDateComponents alloc] init]; |
| 393 |
|
|
NSDateComponents *comp02 = [[NSDateComponents alloc] init]; |
| 394 |
|
|
NSUInteger unitFlag = 0; |
| 395 |
|
|
switch(unit) { |
| 396 |
|
|
case 0: |
| 397 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; |
| 398 |
|
|
[comp01 setDay:-number]; |
| 399 |
|
|
[comp02 setDay:-number02]; |
| 400 |
|
|
break; |
| 401 |
|
|
case 1: |
| 402 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit; |
| 403 |
|
|
[comp01 setWeek:-number+1]; |
| 404 |
|
|
[comp01 setDay:-1]; |
| 405 |
|
|
[comp02 setWeek:-number02]; |
| 406 |
|
|
break; |
| 407 |
|
|
case 2: |
| 408 |
|
|
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit; |
| 409 |
|
|
[comp01 setMonth:-number+1]; |
| 410 |
|
|
[comp01 setDay:-1]; |
| 411 |
|
|
[comp02 setMonth:-number02]; |
| 412 |
|
|
break; |
| 413 |
|
|
case 3: |
| 414 |
|
|
unitFlag = NSYearCalendarUnit; |
| 415 |
|
|
[comp01 setYear:-number+1]; |
| 416 |
|
|
[comp01 setDay:-1]; |
| 417 |
|
|
[comp02 setYear:-number02]; |
| 418 |
|
|
break; |
| 419 |
|
|
} |
| 420 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 421 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 422 |
|
|
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 423 |
|
|
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 424 |
|
|
|
| 425 |
|
|
NSDate *pastDay01 = [aCalendar dateByAddingComponents:comp01 toDate:aDay options:0]; |
| 426 |
|
|
NSDate *pastDay02 = [aCalendar dateByAddingComponents:comp02 toDate:aDay options:0]; |
| 427 |
|
|
|
| 428 |
|
|
id result = [NSArray arrayWithObjects:pastDay01, pastDay02, nil]; |
| 429 |
|
|
return result; |
| 430 |
|
|
} |
| 431 |
masaki |
150 |
- (NSArray *)dateRangeFromVariable:(NSString *)date |
| 432 |
|
|
{ |
| 433 |
|
|
NSLog(@"In function argument is %@", date); |
| 434 |
|
|
|
| 435 |
|
|
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 436 |
|
|
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 437 |
|
|
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 438 |
|
|
fromDate:now]; |
| 439 |
|
|
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 440 |
|
|
|
| 441 |
|
|
NSDateComponents *comp = [[NSDateComponents alloc] init]; |
| 442 |
|
|
[comp setDay:-1]; |
| 443 |
|
|
NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 444 |
|
|
|
| 445 |
|
|
[comp setDay:0]; |
| 446 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 447 |
|
|
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 448 |
|
|
|
| 449 |
|
|
[comp setWeekday:-[nowComp weekday]+1]; |
| 450 |
|
|
[comp setWeek:-1]; |
| 451 |
|
|
NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 452 |
|
|
|
| 453 |
|
|
NSLog(@"now -> %@\ntoday -> %@\nyesterday -> %@\nthisweek -> %@\nlastweek -> %@", |
| 454 |
|
|
now, startOfToday, startOfYesterday, startOfThisWeek, startOfLastWeek); |
| 455 |
|
|
|
| 456 |
|
|
id result = [NSArray arrayWithObjects:now, startOfToday, nil]; |
| 457 |
|
|
return result; |
| 458 |
|
|
} |
| 459 |
masaki |
120 |
@end |