-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAllArticleList.m
128 lines (122 loc) · 3.58 KB
/
AllArticleList.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
//
// AllArticleList.m
// spires
//
// Created by Yuji on 08/10/15.
// Copyright 2008 Y. Tachikawa. All rights reserved.
//
#import "AllArticleList.h"
#import "MOC.h"
#import "SpiresHelper.h"
#import "ArticleFetchOperation.h"
static AllArticleList*_allArticleList=nil;
@implementation AllArticleList
{
ArticleFetchOperation*currentFetchOperation;
}
+(AllArticleList*)allArticleListInMOC:(NSManagedObjectContext*)moc
{
NSArray* a=nil;
NSEntityDescription*authorEntity=[NSEntityDescription entityForName:@"AllArticleList" inManagedObjectContext:moc];
{
NSFetchRequest*req=[[NSFetchRequest alloc]init];
[req setEntity:authorEntity];
NSError*error=nil;
a=[moc executeFetchRequest:req error:&error];
}
if([a count]==1){
return a[0];
}else if([a count]>1){
NSLog(@"inconsistency detected ... there are more than one AllArticleLists!");
AllArticleList*max=a[0];
for(NSUInteger i=1;i<[a count];i++){
AllArticleList*al=a[i];
if([al.articles count]>[max.articles count]){
max=al;
}
}
for(AllArticleList*al in a){
if(al!=max){
[moc deleteObject:al];
}
}
return max;
}else{
return nil;
}
}
+(AllArticleList*)createAllArticleListInMOC:(NSManagedObjectContext*)moc
{
NSEntityDescription*entity=[NSEntityDescription entityForName:@"AllArticleList" inManagedObjectContext:moc];
AllArticleList* mo=(AllArticleList*)[[NSManagedObject alloc] initWithEntity:entity
insertIntoManagedObjectContext:nil];
[mo setValue:@"inspire" forKey:@"name"];
[mo setValue:@0 forKey:@"positionInView"];
mo.sortDescriptors=@[[NSSortDescriptor sortDescriptorWithKey:@"eprintForSorting" ascending:NO]];
[moc insertObject:mo];
NSEntityDescription*articleEntity=[NSEntityDescription entityForName:@"Article" inManagedObjectContext:moc];
NSFetchRequest*req=[[NSFetchRequest alloc]init];
[req setEntity:articleEntity];
[req setPredicate:[NSPredicate predicateWithValue:YES]];
[req setFetchLimit:LOADED_ENTRIES_MAX];
NSError*error=nil;
NSArray*a=[moc executeFetchRequest:req error:&error];
NSSet* s=[NSSet setWithArray:a];
[mo addArticles:s];
error=nil;
[moc save:&error];
return mo;
}
+(AllArticleList*)allArticleList
{
if(!_allArticleList){
_allArticleList=[self allArticleListInMOC:[MOC moc]];
}
if(!_allArticleList){
_allArticleList=[self createAllArticleListInMOC:[MOC moc]];
}
return _allArticleList;
}
-(NSString*)searchString
{
return [self primitiveValueForKey:@"searchString"];
}
-(void)setSearchString:(NSString *)newSearchString
{
[self willChangeValueForKey:@"searchString"];
[self setPrimitiveValue:newSearchString forKey:@"searchString"];
[self reload];
[self didChangeValueForKey:@"searchString"];
}
-(void)reload
{
NSLog(@"reloading internally:%@",self.searchString);
if(currentFetchOperation) {
[currentFetchOperation cancel];
}
if([self.articles count]>2000){
self.articles=nil;
}
currentFetchOperation=[[ArticleFetchOperation alloc] initWithQuery:self.searchString forArticleList:self];
[[OperationQueues sharedQueue] addOperation:currentFetchOperation];
}
#if TARGET_OS_IPHONE
-(UIImage*)icon
{
return [UIImage imageNamed:@"spires-blue"];
}
#else
-(NSImage*)icon
{
return [NSImage imageNamed:@"spires-blue"];
}
#endif
-(NSString*)placeholderForSearchField
{
#if TARGET_OS_IPHONE
return @"Enter something like 'a Maldacena'";
#else
return @"Enter SPIRES query and hit return. Use shift-return to search within local database";
#endif
}
@end