GiftListManager.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // GiftListManager.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/5/20.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "GiftListManager.h"
  9. @implementation GiftListManager
  10. BogoSingletonM(Instance);
  11. - (id)init
  12. {
  13. @synchronized(self)
  14. {
  15. if (self = [super init])
  16. {
  17. //获取Documents目录
  18. NSString *docPath2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  19. //还要指定存储文件的文件名称,仍然使用字符串拼接
  20. NSString *filePath2 = [docPath2 stringByAppendingPathComponent:@"giftlist.plist"];
  21. NSDictionary *list = [NSDictionary dictionaryWithContentsOfFile:filePath2];
  22. if (list && [list isKindOfClass:[NSDictionary class]])
  23. {
  24. [self setGiftList:list];
  25. }
  26. else
  27. {
  28. self.expiry_after = 0;
  29. self.giftMArray = [NSArray array];
  30. [self loadGiftList];
  31. }
  32. }
  33. return self;
  34. }
  35. }
  36. - (void)saveGiftList:(NSDictionary *)dict
  37. {
  38. [self setGiftList:dict];
  39. NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  40. NSString *filePath = [docPath stringByAppendingPathComponent:@"giftlist.plist"];
  41. [dict writeToFile:filePath atomically:YES];
  42. }
  43. - (void)setGiftList:(NSDictionary*)list
  44. {
  45. if(list != nil)
  46. {
  47. self.giftMArray = [list objectForKey:@"list"];
  48. self.expiry_after = [[list objectForKey:@"expiry_after"] doubleValue];
  49. }
  50. }
  51. #pragma mark 重新加载礼物列表
  52. - (void)reloadGiftList
  53. {
  54. if (_expiry_after)
  55. {
  56. if (!_giftMArray || [_giftMArray count])
  57. {
  58. [self loadGiftList];
  59. }
  60. else
  61. {
  62. if ([[NSDate date] timeIntervalSince1970] > _expiry_after)
  63. {
  64. [self loadGiftList];
  65. }
  66. }
  67. }
  68. else
  69. {
  70. [self loadGiftList];
  71. }
  72. }
  73. #pragma mark 下载礼物列表数据
  74. - (void)loadGiftList
  75. {
  76. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  77. [mDict setObject:@"app" forKey:@"ctl"];
  78. [mDict setObject:@"prop" forKey:@"act"];
  79. FWWeakify(self)
  80. [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  81. FWStrongify(self)
  82. if ([responseJson toInt:@"status"] == 1)
  83. {
  84. NSMutableDictionary *tmpMdict = [NSMutableDictionary dictionaryWithDictionary:responseJson];
  85. [self saveGiftList:tmpMdict];
  86. if(self.relodComplete)
  87. {
  88. self.relodComplete();
  89. }
  90. }
  91. } FailureBlock:^(NSError *error) {
  92. }];
  93. }
  94. @end