MOSayHiModel.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MOSayHiModel.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/3/14.
  6. //
  7. #import "MOSayHiModel.h"
  8. NSString *const kMOSayHiModelJoined = @"joined";
  9. NSString *const kMOSayHiModelFollow = @"follow";
  10. NSString *const kMOSayHiModelGiftAmount = @"giftAmount";
  11. NSString *const kMOSayHiModelGift = @"gift";
  12. NSString *const kMOSayHiModelFollowText = @"followText";
  13. NSString *const kMOSayHiModelGiftText = @"giftText";
  14. NSString *const kMOSayHiModelJoinedText = @"joinedText";
  15. @interface MOSayHiModel ()
  16. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  17. @end
  18. @implementation MOSayHiModel
  19. @synthesize joined = _joined;
  20. @synthesize follow = _follow;
  21. @synthesize giftAmount = _giftAmount;
  22. @synthesize gift = _gift;
  23. @synthesize followText = _followText;
  24. @synthesize giftText = _giftText;
  25. @synthesize joinedText = _joinedText;
  26. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  27. return [[self alloc] initWithDictionary:dict];
  28. }
  29. - (instancetype)init {
  30. if (self = [super init]) {
  31. _follow = YES;
  32. _followText = NSLocalString(@"mimo_greeting_edit_hint_follow");
  33. _gift = YES;
  34. _giftAmount = 100;
  35. _giftText = NSLocalString(@"mimo_greeting_edit_hint_gift");
  36. _joined = YES;
  37. _joinedText = NSLocalString(@"mimo_greeting_edit_hint_enter");
  38. }
  39. return self;
  40. }
  41. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  42. self = [super init];
  43. // This check serves to make sure that a non-NSDictionary object
  44. // passed into the model class doesn't break the parsing.
  45. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  46. self.joined = [[self objectOrNilForKey:kMOSayHiModelJoined fromDictionary:dict] boolValue];
  47. self.follow = [[self objectOrNilForKey:kMOSayHiModelFollow fromDictionary:dict] boolValue];
  48. self.giftAmount = [[self objectOrNilForKey:kMOSayHiModelGiftAmount fromDictionary:dict] intValue];
  49. self.gift = [[self objectOrNilForKey:kMOSayHiModelGift fromDictionary:dict] boolValue];
  50. self.followText = [self objectOrNilForKey:kMOSayHiModelFollowText fromDictionary:dict];
  51. self.giftText = [self objectOrNilForKey:kMOSayHiModelGiftText fromDictionary:dict];
  52. self.joinedText = [self objectOrNilForKey:kMOSayHiModelJoinedText fromDictionary:dict];
  53. }
  54. return self;
  55. }
  56. - (NSDictionary *)dictionaryRepresentation {
  57. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  58. [mutableDict setValue:[NSNumber numberWithBool:self.joined] forKey:kMOSayHiModelJoined];
  59. [mutableDict setValue:[NSNumber numberWithBool:self.follow] forKey:kMOSayHiModelFollow];
  60. [mutableDict setValue:[NSNumber numberWithInt:self.giftAmount] forKey:kMOSayHiModelGiftAmount];
  61. [mutableDict setValue:[NSNumber numberWithBool:self.gift] forKey:kMOSayHiModelGift];
  62. [mutableDict setValue:self.followText forKey:kMOSayHiModelFollowText];
  63. [mutableDict setValue:self.giftText forKey:kMOSayHiModelGiftText];
  64. [mutableDict setValue:self.joinedText forKey:kMOSayHiModelJoinedText];
  65. return [NSDictionary dictionaryWithDictionary:mutableDict];
  66. }
  67. - (NSString *)description {
  68. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  69. }
  70. #pragma mark - Helper Method
  71. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  72. id object = [dict objectForKey:aKey];
  73. return [object isEqual:[NSNull null]] ? nil : object;
  74. }
  75. #pragma mark - NSCoding Methods
  76. - (id)initWithCoder:(NSCoder *)aDecoder {
  77. self = [super init];
  78. self.joined = [aDecoder decodeBoolForKey:kMOSayHiModelJoined];
  79. self.follow = [aDecoder decodeBoolForKey:kMOSayHiModelFollow];
  80. self.giftAmount = [aDecoder decodeIntForKey:kMOSayHiModelGiftAmount];
  81. self.gift = [aDecoder decodeBoolForKey:kMOSayHiModelGift];
  82. self.followText = [aDecoder decodeObjectForKey:kMOSayHiModelFollowText];
  83. self.giftText = [aDecoder decodeObjectForKey:kMOSayHiModelGiftText];
  84. self.joinedText = [aDecoder decodeObjectForKey:kMOSayHiModelJoinedText];
  85. return self;
  86. }
  87. - (void)encodeWithCoder:(NSCoder *)aCoder
  88. {
  89. [aCoder encodeBool:_joined forKey:kMOSayHiModelJoined];
  90. [aCoder encodeBool:_follow forKey:kMOSayHiModelFollow];
  91. [aCoder encodeInt:_giftAmount forKey:kMOSayHiModelGiftAmount];
  92. [aCoder encodeBool:_gift forKey:kMOSayHiModelGift];
  93. [aCoder encodeObject:_followText forKey:kMOSayHiModelFollowText];
  94. [aCoder encodeObject:_giftText forKey:kMOSayHiModelGiftText];
  95. [aCoder encodeObject:_joinedText forKey:kMOSayHiModelJoinedText];
  96. }
  97. - (id)copyWithZone:(NSZone *)zone {
  98. MOSayHiModel *copy = [[MOSayHiModel alloc] init];
  99. if (copy) {
  100. copy.joined = self.joined;
  101. copy.follow = self.follow;
  102. copy.giftAmount = self.giftAmount;
  103. copy.gift = self.gift;
  104. copy.followText = [self.followText copyWithZone:zone];
  105. copy.giftText = [self.giftText copyWithZone:zone];
  106. copy.joinedText = [self.joinedText copyWithZone:zone];
  107. }
  108. return copy;
  109. }
  110. @end