This repository was archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathXSourceNoteModel.m
More file actions
139 lines (115 loc) · 4 KB
/
Copy pathXSourceNoteModel.m
File metadata and controls
139 lines (115 loc) · 4 KB
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
//
// XSourceNoteModel.m
// XSourceNote
//
// Created by everettjf on 10/2/15.
// Copyright © 2015 everettjf. All rights reserved.
//
#import "XSourceNoteModel.h"
#import "XSourceNoteUtil.h"
#import "XSourceNoteStorage.h"
NSString * const XSourceNoteModelLineNotesChanged = @"XSourceNoteModelLineNotesChanged";
static inline NSString* XSourceNote_HashLine(NSString *source,NSUInteger line){
return [NSString stringWithFormat:@"%lu/%lu",line,[source hash]];
}
@interface XSourceNoteModel ()
// for fast check
@property (nonatomic,strong) NSMutableSet<NSString*> *markset;
@end
@implementation XSourceNoteModel
+(XSourceNoteModel *)sharedModel{
static XSourceNoteModel *inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = [[XSourceNoteModel alloc]init];
});
return inst;
}
- (instancetype)init
{
self = [super init];
if (self) {
_markset = [NSMutableSet new];
}
return self;
}
- (BOOL)hasLineMark:(NSString *)source line:(NSUInteger)line{
BOOL has = NO;
@synchronized(_markset) {
has = [_markset containsObject:XSourceNote_HashLine(source, line)];
}
return has;
}
- (void)addLineNote:(XSourceNoteLineEntity *)note{
XSourceNoteStorage *st = [XSourceNoteStorage sharedStorage];
[st addLineNote:note];
dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
@synchronized(_markset) {
for(NSUInteger idx = note.begin; idx <= note.end; ++idx){
[_markset addObject:XSourceNote_HashLine(note.source, idx)];
}
}
[self _notifyLineNotesChanged];
});
}
- (void)removeLineNote:(XSourceNoteLineEntity *)note{
[[XSourceNoteStorage sharedStorage]removeLineNote:note.uniqueID];
dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
@synchronized(_markset) {
for(NSUInteger idx = note.begin; idx <= note.end; ++idx){
[_markset removeObject:XSourceNote_HashLine(note.source, idx)];
}
}
[self _notifyLineNotesChanged];
});
}
- (void)fetchAllNotes:(XSourceNoteModelFetchAllNotesBlock)completion{
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *notes = [[XSourceNoteStorage sharedStorage]fetchAllLineNotes];
NSMutableArray *entities = [NSMutableArray new];
for (XSNote* note in notes) {
XSourceNoteLineEntity *entity = [XSourceNoteLineEntity new];
entity.uniqueID = note.uniqueID;
entity.source = note.source;
entity.begin = note.begin.unsignedIntegerValue;
entity.end = note.end.unsignedIntegerValue;
if(note.content){
entity.content = [[NSString alloc]initWithString:note.content];
if(entity.content.length > 30){
entity.content = [entity.content substringToIndex:30];
}
}else{
entity.content = @"";
}
entity.code = note.code;
entity.localPath = note.localPath;
[entities addObject:entity];
}
// rehash the map
@synchronized(_markset) {
for (XSourceNoteLineEntity *note in entities) {
for(NSUInteger idx = note.begin;
idx <= note.end;
++idx){
[_markset addObject:XSourceNote_HashLine(note.source, idx)];
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
!completion?:completion(entities);
});
});
}
- (void)_notifyLineNotesChanged{
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]postNotificationName:XSourceNoteModelLineNotesChanged object:nil];
});
}
- (void)ensureInit{
[[XSourceNoteStorage sharedStorage]ensureDB];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self fetchAllNotes:nil];
});
}
@end