MMDraftUtil.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // MMDraftUtil.m
  3. // RichTextEditDemo
  4. //
  5. // Created by aron on 2017/7/25.
  6. // Copyright © 2017年 aron. All rights reserved.
  7. //
  8. #import "MMDraftUtil.h"
  9. #import "MMDraftModel.h"
  10. #import "MMRichTextModel.h"
  11. #import "MMRichImageModel.h"
  12. #import "MMRichTitleModel.h"
  13. #import "NSString+NSDate.h"
  14. #import "MMRichContentUtil.h"
  15. @implementation MMDraftUtil
  16. // 保存草稿数据
  17. + (void)saveDraftData:(MMDraftModel*)draftData {
  18. // 验证草稿数据
  19. [self validateDraftData:draftData];
  20. // 保存数据库
  21. [MMDraftModel insertDraft:draftData error:nil];
  22. }
  23. // 删除草稿数据
  24. + (void)delateDraftData:(MMDraftModel*)draftData {
  25. // 删除图片
  26. [self deleteImagesFromDraft:draftData];
  27. // 删除数据库记录
  28. [MMDraftModel deleteDraft:draftData error:nil];
  29. }
  30. // 获取草稿
  31. + (void)retriveDraftWithCompletion:(void (^)(NSArray *aDrafts, NSError *aError))aCompletionBlock {
  32. [MMDraftModel retriveDraftWithCompletion:aCompletionBlock];
  33. }
  34. // 数据库读取的草稿序列化
  35. + (MMDraftModel*)draftModelWithDraftDataString:(NSString*)draftDataString {
  36. MMDraftModel* draftModel = nil;
  37. NSDictionary* dic = nil;
  38. NSData* jsonData = [(NSString *)draftDataString dataUsingEncoding : NSUTF8StringEncoding];
  39. if (jsonData) {
  40. dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
  41. }
  42. if (dic) {
  43. draftModel = [MMDraftModel yy_modelWithDictionary:dic];
  44. // Title
  45. NSDictionary* titleModelDict = [dic objectForKey:@"titleModel"];
  46. if ([titleModelDict isKindOfClass:[NSDictionary class]]) {
  47. MMRichTitleModel* titleModel = [MMRichTitleModel yy_modelWithDictionary:titleModelDict];
  48. if (nil == titleModel) {
  49. titleModel = [MMRichTitleModel new];
  50. }
  51. draftModel.titleModel = titleModel;
  52. }
  53. // Contents
  54. NSMutableArray* tmpContentModels = [NSMutableArray array];
  55. NSArray* contentModelsArr = [dic objectForKey:@"contentModels"];
  56. if ([contentModelsArr isKindOfClass:[NSArray class]]) {
  57. for (NSDictionary* richContentDict in contentModelsArr) {
  58. NSNumber* richContentTypeNum = [richContentDict objectForKey:@"richContentType"];
  59. if ([richContentTypeNum respondsToSelector:@selector(integerValue)]) {
  60. MMRichContentType richContentType = [richContentTypeNum integerValue];
  61. if (richContentType == MMRichContentTypeImage) {
  62. MMRichImageModel* imageModel = [MMRichImageModel yy_modelWithDictionary:richContentDict];
  63. if (imageModel) {
  64. [tmpContentModels addObject:imageModel];
  65. }
  66. } else if (richContentType == MMRichContentTypeText) {
  67. MMRichTextModel* textModel = [MMRichTextModel yy_modelWithDictionary:richContentDict];
  68. if (textModel) {
  69. [tmpContentModels addObject:textModel];
  70. }
  71. }
  72. }
  73. }
  74. }
  75. draftModel.contentModels = tmpContentModels;
  76. }
  77. return draftModel;
  78. }
  79. // 更新草稿修改时间
  80. + (void)updateModifyTimeWithDraftData:(MMDraftModel*)draftData {
  81. draftData.modifyTimeString = [NSString yyyyMMddTHHmmssZDateStringFromDate:[NSDate date]];
  82. }
  83. // 编辑模型生产草稿模型
  84. + (MMDraftModel*)draftModelWithTitleModel:(MMBaseRichContentModel*)titleModel contents:(NSArray<MMBaseRichContentModel*>*)contents tid:(NSString*)tid draftId:(NSInteger)draftId {
  85. MMDraftModel* draftModel = [MMDraftModel new];
  86. draftModel.draftId = draftId > 0 ? draftId : [[NSDate date] timeIntervalSince1970];
  87. draftModel.createTimeString = [NSString yyyyMMddTHHmmssZDateStringFromDate:[NSDate date]];
  88. draftModel.modifyTimeString = draftModel.createTimeString;
  89. draftModel.tid = tid;
  90. draftModel.userId = @"TEST_USER_ID";
  91. draftModel.titleModel = titleModel;
  92. draftModel.contentModels = contents;
  93. return draftModel;
  94. }
  95. + (NSString*)contentFromDraftModel:(MMDraftModel*)draftModel {
  96. return [MMRichContentUtil plainContentFromRichContents:draftModel.contentModels];
  97. }
  98. #pragma mark - ......::::::: helper :::::::......
  99. + (void)deleteImagesFromDraft:(MMDraftModel*)draftModel {
  100. for (int i = 0; i< draftModel.contentModels.count; i++) {
  101. NSObject* content = draftModel.contentModels[i];
  102. if ([content isKindOfClass:[MMRichImageModel class]]) {
  103. MMRichImageModel* imgContent = (MMRichImageModel*)content;
  104. [self deleteImageContent:imgContent];
  105. }
  106. }
  107. }
  108. + (void)deleteImageContent:(MMRichImageModel*)imgContent {
  109. NSString* imgPath = [[MMRichContentUtil imageSavedLocalPath] stringByAppendingPathComponent:imgContent.localImageName];
  110. if (imgPath) {
  111. // 删除单张图片
  112. [[NSFileManager defaultManager] removeItemAtPath:imgPath error:nil];
  113. }
  114. }
  115. + (void)validateDraftData:(MMDraftModel*)draftModel {
  116. for (int i = 0; i< draftModel.contentModels.count; i++) {
  117. NSObject* content = draftModel.contentModels[i];
  118. if ([content isKindOfClass:[MMRichImageModel class]]) {
  119. MMRichImageModel* imgContent = (MMRichImageModel*)content;
  120. NSString* localImagePath = [[MMRichContentUtil imageSavedLocalPath] stringByAppendingPathComponent:imgContent.localImageName];
  121. BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:localImagePath];
  122. if (NO == isExists) {
  123. NSString* scaledImageStoreName= [MMRichContentUtil saveImageToLocal:imgContent.image];
  124. imgContent.localImageName = scaledImageStoreName;
  125. }
  126. }
  127. }
  128. }
  129. @end