From 940a4b69a8e41c26313579424ff32c0c7186fd67 Mon Sep 17 00:00:00 2001 From: life Date: Wed, 11 Nov 2015 14:56:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BC=98=E5=8C=96,=20?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E5=88=A4=E6=96=AD=E4=BB=A5=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=B8=BA=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Leanote/service/ApiService.m | 4 +- Leanote/service/NoteService.h | 7 +- Leanote/service/NoteService.m | 77 ++++++++++++++++++--- Leanote/service/SyncService.m | 20 +++++- Leanote/service/TagService.m | 4 +- Leanote/util/Common.h | 3 +- Leanote/util/Common.m | 6 ++ Leanote/view/LeaImageSliderViewController.m | 6 +- 8 files changed, 105 insertions(+), 22 deletions(-) diff --git a/Leanote/service/ApiService.m b/Leanote/service/ApiService.m index bb20fb3..0af4b38 100644 --- a/Leanote/service/ApiService.m +++ b/Leanote/service/ApiService.m @@ -247,7 +247,7 @@ } // 处理文件 - if(![Common isNull:files]) { + if(![Common isNullOrNil:files]) { int i = 0; for(File *file in files) { NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{ @@ -323,7 +323,7 @@ } // 处理文件 - if(![Common isNull:files]) { + if(![Common isNullOrNil:files]) { int i = 0; for(File *file in files) { NSMutableDictionary *obj = [NSMutableDictionary dictionaryWithDictionary:@{ diff --git a/Leanote/service/NoteService.h b/Leanote/service/NoteService.h index 8d8e25e..4927d50 100644 --- a/Leanote/service/NoteService.h +++ b/Leanote/service/NoteService.h @@ -23,7 +23,8 @@ #pragma 同步操作 - (BOOL)deleteNoteForceByServerNoteId:(NSString *)serverNoteId; - (Note *)getNoteByServerNoteId:(NSString *)serverNoteId; -- (Note *)updateNoteForce:(id)obj; +- (Note *)updateNoteForce:(id)obj + content:(NSString*) content; - (Note *)addNoteForce:(id)obj; - (void)copyNoteForConflict:(id)obj localNote:(Note *)localNote; @@ -56,6 +57,10 @@ - (void) getNoteContent:(Note *) note success:(void (^)(NSString *))success fail:(void (^)())fail; +- (void) getNoteContent:(NSString *) serverNoteId + isMarkdown:(BOOL) isMarkdown + success:(void (^)(NSString *))success + fail:(void (^)())fail; + (void) getImage:(NSString *)serverFileId success:(void (^)(NSString *))success diff --git a/Leanote/service/NoteService.m b/Leanote/service/NoteService.m index 9d94b26..0f86846 100644 --- a/Leanote/service/NoteService.m +++ b/Leanote/service/NoteService.m @@ -219,7 +219,9 @@ // 更新笔记本 // 调用者save -- (Note *)updateNoteForce:(id)obj { +// 如果传了内容, 表示是有内容的, 这里的内容已经fixed +- (Note *)updateNoteForce:(id)obj + content:(NSString*) content { NSString *serverNoteId = obj[@"NoteId"]; Note *note = [self getNoteByServerNoteId:serverNoteId]; @@ -249,13 +251,21 @@ note.usn = usn; note.isTrash = isTrash; - note.isInitSync = M_YES; // 需要重新同步笔记 + // 是否传了内容 + if([Common isNullOrNil:content]) { + note.isInitSync = M_YES; // 需要重新同步笔记 + } + else { + note.isInitSync = M_NO; + note.content = content; + } + note.isDirty = M_NO; note.localIsNew = M_NO; note.updatedTime = [Common goDate:obj[@"UpdatedTime"]]; NSString *tagsStr; - if(![Common isNull:tags]) { + if(![Common isNullOrNil:tags]) { tagsStr = [tags componentsJoinedByString:@","]; } else { @@ -275,7 +285,7 @@ // recountTag, 之前的tag, 现在的tag, 得到不一样的tag, 只分析不同的tag count [TagService recountTagNoteCountByTitles:tags inContext:self.tmpContext]; - if(![Common isNull:everTags]) { + if(![Common isNullOrNil:everTags]) { [TagService recountTagNoteCountByTitles:[everTags componentsSeparatedByString:@","] inContext:self.tmpContext]; } return note; @@ -319,7 +329,7 @@ note.userId = [UserService getCurUserId]; NSString *tagsStr; - if(![Common isNull:tags]) { + if(![Common isNullOrNil:tags]) { tagsStr = [tags componentsJoinedByString:@","]; } else { @@ -750,6 +760,22 @@ } } +- (void) getNoteContent:(NSString *) serverNoteId + isMarkdown:(BOOL) isMarkdown + success:(void (^)(NSString *))success + fail:(void (^)())fail +{ + + [ApiService getNoteContent:serverNoteId success:^(id obj) { + NSString *content = [self fixContent:obj[@"Content"] isMarkdown:isMarkdown]; + success(content); + } fail:^{ + if(fail) { + fail(); + } + }]; +} + #pragma 发送改变 @@ -877,7 +903,7 @@ } // files NSArray *files = ret[@"Files"]; - if(![Common isNull:files] && [files count] > 0) { + if(![Common isNullOrNil:files] && [files count] > 0) { for(NSDictionary *file in files) { NSString *localFileId = file[@"LocalFileId"]; NSString *serverFileId = file[@"FileId"]; @@ -924,15 +950,44 @@ // 冲突, 复制之 if(msg && [msg isEqualToString:@"conflict"]) { [ApiService getNote:note.serverNoteId success:^(id obj) { - [self copyNoteForConflict:obj localNote:note]; + // [self copyNoteForConflict:obj localNote:note]; + + // 和sync的处理逻辑一致 + // 这里, 得到内容再判断是否冲突, 如果内容一样, 则认为不是冲突的 + [self getNoteContent:note.serverNoteId isMarkdown:[note.isMarkdown boolValue] success:^(NSString * content) { + + // 内容一样, 则使用更新之 + if ([note.content isEqualToString:content]) { + [self updateNoteForce:obj content:content]; + if(success) { + success(); + } + } + // 内容不一样, 则确实是冲突的 + else { + [self copyNoteForConflict:obj localNote:note]; + if(fail) { + // 是否冲突 + fail(ret); + } + } + } fail:^{ + // 内容得不到, 就当作是冲突来处理 + [self copyNoteForConflict:obj localNote:note]; + if(fail) { + // 是否冲突 + fail(ret); + } + }]; + } fail:^{ + if(fail) { + // 是否冲突 + fail(ret); + } }]; } } - if(fail) { - // 是否冲突 - fail(ret); - } }]; } diff --git a/Leanote/service/SyncService.m b/Leanote/service/SyncService.m index b0d1f27..6ac0b9b 100644 --- a/Leanote/service/SyncService.m +++ b/Leanote/service/SyncService.m @@ -162,12 +162,28 @@ BOOL inSyncing = NO; // 本地修改了, 有冲突, 则复制一个 if ([note.isDirty boolValue]) { NSLog(@"有冲突 服务器USN=%ld 本地note:%@", (long)[usn integerValue], note); - [_noteService copyNoteForConflict:eachObj localNote:note]; + + // 这里, 得到内容再判断是否冲突, 如果内容一样, 则认为不是冲突的 + [_noteService getNoteContent:note.serverNoteId isMarkdown:[note.isMarkdown boolValue] success:^(NSString * content) { + + // 内容一样, 则使用更新之 + if ([note.content isEqualToString:content]) { + [_noteService updateNoteForce:eachObj content:content]; + } + // 内容不一样, 则确实是冲突的 + else { + [_noteService copyNoteForConflict:eachObj localNote:note]; + } + } fail:^{ + // 内容得不到, 就当作是冲突来处理 + [_noteService copyNoteForConflict:eachObj localNote:note]; + }]; + } // 本地没修改, 则用服务器的数据 else { NSLog(@" updateNoteForce %@", note); - [_noteService updateNoteForce:eachObj]; + [_noteService updateNoteForce:eachObj content:nil]; } } } diff --git a/Leanote/service/TagService.m b/Leanote/service/TagService.m index 04571df..c59a40d 100644 --- a/Leanote/service/TagService.m +++ b/Leanote/service/TagService.m @@ -139,7 +139,7 @@ + (void)recountTagNoteCountByTitlesStr:(NSString *)titlesStr inContext:(NSManagedObjectContext *)inContext { - if(![Common isNull:titlesStr]) { + if(![Common isNullOrNil:titlesStr]) { NSArray *titles = [titlesStr componentsSeparatedByString:@","]; for(NSString *title in titles) { [self.class recountTagNoteCount:title inContext:inContext]; @@ -153,7 +153,7 @@ + (void)recountTagNoteCountByTitles:(NSArray *)titles inContext:(NSManagedObjectContext *)inContext { - if(![Common isNull:titles]) { + if(![Common isNullOrNil:titles]) { for(NSString *title in titles) { [self.class recountTagNoteCount:title inContext:inContext]; } diff --git a/Leanote/util/Common.h b/Leanote/util/Common.h index 24726ce..bd68b37 100644 --- a/Leanote/util/Common.h +++ b/Leanote/util/Common.h @@ -9,7 +9,8 @@ #import @interface Common : NSObject -+ (BOOL) isNull:(id) i; +//+ (BOOL) isNull:(id) i; ++ (BOOL) isNullOrNil:(id) i; + (BOOL) isBlankString:(NSString *)string; + (NSString *) getDocPath; + (NSString *) getAbsPath:(NSString *)relatedPath; diff --git a/Leanote/util/Common.m b/Leanote/util/Common.m index 3ca8375..dfa161a 100644 --- a/Leanote/util/Common.m +++ b/Leanote/util/Common.m @@ -14,6 +14,7 @@ @implementation Common +/* + (BOOL) isNull:(id) i { if(i) { return (NSNull *)i == [NSNull null]; @@ -23,6 +24,11 @@ } // return !i || (NSNull *)i == [NSNull null]; } +*/ + ++ (BOOL) isNullOrNil:(id) i { + return !i || (NSNull *)i == [NSNull null]; +} + (BOOL) isBlankString:(NSString *)string { if (string == nil || string == NULL) { diff --git a/Leanote/view/LeaImageSliderViewController.m b/Leanote/view/LeaImageSliderViewController.m index 50efdba..1177554 100644 --- a/Leanote/view/LeaImageSliderViewController.m +++ b/Leanote/view/LeaImageSliderViewController.m @@ -142,7 +142,7 @@ return nil; } LeaImageViewController *vc = self.views[index]; - if(vc && ![Common isNull:vc]) { + if(vc && ![Common isNullOrNil:vc]) { return self.views[index]; } @@ -151,9 +151,9 @@ NSString *fileId = [Common getFileIdFromUrl:url]; UIImage *img; - if(![Common isNull:fileId]) { + if(![Common isNullOrNil:fileId]) { NSString *absPath = [FileService getFileAbsPathByFileIdOrServerFileId:fileId]; - if (![Common isNull:absPath]) { + if (![Common isNullOrNil:absPath]) { img = [[UIImage alloc] initWithContentsOfFile:absPath]; } }