MOEffect.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // MOEffect.m
  3. //
  4. // Created by SuperCabbage on 2023/11/24
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOEffect.h"
  8. #import "MOAttributes.h"
  9. NSString *const kMOEffectPath = @"path";
  10. NSString *const kMOEffectCode = @"code";
  11. NSString *const kMOEffectSource = @"source";
  12. NSString *const kMOEffectName = @"name";
  13. NSString *const kMOEffectDuration = @"duration";
  14. NSString *const kMOEffectId = @"id";
  15. NSString *const kMOEffectEffectType = @"effectType";
  16. NSString *const kMOEffectAnimation = @"animation";
  17. NSString *const kMOEffectImage = @"image";
  18. NSString *const kMOEffectResFile = @"resFile";
  19. NSString *const kMOEffectEffectFile = @"effectFile";
  20. NSString *const kMOEffectLock = @"lock";
  21. NSString *const kMOEffectAttributes = @"attributes";
  22. @interface MOEffect ()
  23. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  24. @end
  25. @implementation MOEffect
  26. @synthesize path = _path;
  27. @synthesize code = _code;
  28. @synthesize source = _source;
  29. @synthesize name = _name;
  30. @synthesize duration = _duration;
  31. @synthesize id = _id;
  32. @synthesize effectType = _effectType;
  33. @synthesize animation = _animation;
  34. @synthesize image = _image;
  35. @synthesize resFile = _resFile;
  36. @synthesize effectFile = _effectFile;
  37. @synthesize lock = _lock;
  38. @synthesize attributes = _attributes;
  39. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  40. return [[self alloc] initWithDictionary:dict];
  41. }
  42. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  43. self = [super init];
  44. // This check serves to make sure that a non-NSDictionary object
  45. // passed into the model class doesn't break the parsing.
  46. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  47. self.path = [self objectOrNilForKey:kMOEffectPath fromDictionary:dict];
  48. self.code = [[self objectOrNilForKey:kMOEffectCode fromDictionary:dict] doubleValue];
  49. self.source = [self objectOrNilForKey:kMOEffectSource fromDictionary:dict];
  50. self.name = [self objectOrNilForKey:kMOEffectName fromDictionary:dict];
  51. self.duration = [[self objectOrNilForKey:kMOEffectDuration fromDictionary:dict] doubleValue];
  52. self.id = [self objectOrNilForKey:kMOEffectId fromDictionary:dict];
  53. self.effectType = [[self objectOrNilForKey:kMOEffectEffectType fromDictionary:dict] doubleValue];
  54. self.animation = [[self objectOrNilForKey:kMOEffectAnimation fromDictionary:dict] boolValue];
  55. self.image = [self objectOrNilForKey:kMOEffectImage fromDictionary:dict];
  56. self.resFile = [self objectOrNilForKey:kMOEffectResFile fromDictionary:dict];
  57. self.effectFile = [self objectOrNilForKey:kMOEffectEffectFile fromDictionary:dict];
  58. self.lock = [[self objectOrNilForKey:kMOEffectLock fromDictionary:dict] boolValue];
  59. self.attributes = [MOAttributes modelObjectWithDictionary:[dict objectForKey:kMOEffectAttributes]];
  60. }
  61. return self;
  62. }
  63. - (NSDictionary *)dictionaryRepresentation {
  64. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  65. [mutableDict setValue:self.path forKey:kMOEffectPath];
  66. [mutableDict setValue:[NSNumber numberWithDouble:self.code] forKey:kMOEffectCode];
  67. [mutableDict setValue:self.source forKey:kMOEffectSource];
  68. [mutableDict setValue:self.name forKey:kMOEffectName];
  69. [mutableDict setValue:[NSNumber numberWithDouble:self.duration] forKey:kMOEffectDuration];
  70. [mutableDict setValue:self.id forKey:kMOEffectId];
  71. [mutableDict setValue:[NSNumber numberWithDouble:self.effectType] forKey:kMOEffectEffectType];
  72. [mutableDict setValue:[NSNumber numberWithBool:self.animation] forKey:kMOEffectAnimation];
  73. [mutableDict setValue:self.image forKey:kMOEffectImage];
  74. [mutableDict setValue:self.resFile forKey:kMOEffectResFile];
  75. [mutableDict setValue:self.effectFile forKey:kMOEffectEffectFile];
  76. [mutableDict setValue:[NSNumber numberWithBool:self.lock] forKey:kMOEffectLock];
  77. [mutableDict setValue:[self.attributes dictionaryRepresentation] forKey:kMOEffectAttributes];
  78. return [NSDictionary dictionaryWithDictionary:mutableDict];
  79. }
  80. - (NSString *)description {
  81. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  82. }
  83. #pragma mark - Helper Method
  84. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  85. id object = [dict objectForKey:aKey];
  86. return [object isEqual:[NSNull null]] ? nil : object;
  87. }
  88. #pragma mark - NSCoding Methods
  89. - (id)initWithCoder:(NSCoder *)aDecoder {
  90. self = [super init];
  91. self.path = [aDecoder decodeObjectForKey:kMOEffectPath];
  92. self.code = [aDecoder decodeDoubleForKey:kMOEffectCode];
  93. self.source = [aDecoder decodeObjectForKey:kMOEffectSource];
  94. self.name = [aDecoder decodeObjectForKey:kMOEffectName];
  95. self.duration = [aDecoder decodeDoubleForKey:kMOEffectDuration];
  96. self.id = [aDecoder decodeObjectForKey:kMOEffectId];
  97. self.effectType = [aDecoder decodeDoubleForKey:kMOEffectEffectType];
  98. self.animation = [aDecoder decodeBoolForKey:kMOEffectAnimation];
  99. self.image = [aDecoder decodeObjectForKey:kMOEffectImage];
  100. self.resFile = [aDecoder decodeObjectForKey:kMOEffectResFile];
  101. self.effectFile = [aDecoder decodeObjectForKey:kMOEffectEffectFile];
  102. self.lock = [aDecoder decodeBoolForKey:kMOEffectLock];
  103. self.attributes = [aDecoder decodeObjectForKey:kMOEffectAttributes];
  104. return self;
  105. }
  106. - (void)encodeWithCoder:(NSCoder *)aCoder
  107. {
  108. [aCoder encodeObject:_path forKey:kMOEffectPath];
  109. [aCoder encodeDouble:_code forKey:kMOEffectCode];
  110. [aCoder encodeObject:_source forKey:kMOEffectSource];
  111. [aCoder encodeObject:_name forKey:kMOEffectName];
  112. [aCoder encodeDouble:_duration forKey:kMOEffectDuration];
  113. [aCoder encodeObject:_id forKey:kMOEffectId];
  114. [aCoder encodeDouble:_effectType forKey:kMOEffectEffectType];
  115. [aCoder encodeBool:_animation forKey:kMOEffectAnimation];
  116. [aCoder encodeObject:_image forKey:kMOEffectImage];
  117. [aCoder encodeObject:_resFile forKey:kMOEffectResFile];
  118. [aCoder encodeObject:_effectFile forKey:kMOEffectEffectFile];
  119. [aCoder encodeBool:_lock forKey:kMOEffectLock];
  120. [aCoder encodeObject:_attributes forKey:kMOEffectAttributes];
  121. }
  122. - (id)copyWithZone:(NSZone *)zone {
  123. MOEffect *copy = [[MOEffect alloc] init];
  124. if (copy) {
  125. copy.path = [self.path copyWithZone:zone];
  126. copy.code = self.code;
  127. copy.source = [self.source copyWithZone:zone];
  128. copy.name = [self.name copyWithZone:zone];
  129. copy.duration = self.duration;
  130. copy.id = [self.id copyWithZone:zone];
  131. copy.effectType = self.effectType;
  132. copy.animation = self.animation;
  133. copy.image = [self.image copyWithZone:zone];
  134. copy.resFile = [self.resFile copyWithZone:zone];
  135. copy.effectFile = [self.effectFile copyWithZone:zone];
  136. copy.lock = self.lock;
  137. copy.attributes = [self.attributes copyWithZone:zone];
  138. }
  139. return copy;
  140. }
  141. @end