HaveNewVersionView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // HaveNewVersionView.m
  3. // VclustersGemini
  4. //
  5. // Created by APPLE on 2020/2/24.
  6. // Copyright © 2020 APPLE. All rights reserved.
  7. //
  8. #import "HaveNewVersionView.h"
  9. @interface HaveNewVersionView ()<UITableViewDelegate,UITableViewDataSource>
  10. @property (nonatomic,strong) NSMutableArray *dataSource;
  11. @end
  12. @implementation HaveNewVersionView
  13. @synthesize titleLabel;
  14. @synthesize detailLabel;
  15. @synthesize bgView;
  16. @synthesize deleteBtn;
  17. static HaveNewVersionView * _instance;
  18. + (instancetype)shardInstance {
  19. static dispatch_once_t HaveNewVersionViewonceToken;
  20. dispatch_once(&HaveNewVersionViewonceToken, ^{
  21. _instance = [[self alloc] init];
  22. });
  23. return _instance;
  24. }
  25. - (id)initWithFrame:(CGRect)frame
  26. {
  27. self = [super initWithFrame:frame];
  28. if (self)
  29. {
  30. //[self drawAnyView];
  31. }
  32. return self;
  33. }
  34. - (void)setVersionModel:(HWVersionModel *)versionModel
  35. {
  36. _versionModel = versionModel;
  37. [self drawAnyView];
  38. }
  39. - (void)drawAnyView
  40. {
  41. if(bgView){
  42. [bgView removeFromSuperview];
  43. }
  44. self.tag = 111111;
  45. [self setBackgroundColor:HW000000Color60];
  46. NSString *tipStr = _versionModel.versionInfo;
  47. CGFloat curHeight = [tipStr boundingRectWithSize:CGSizeMake(300 -40, 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size.height;
  48. CGFloat fullHeight = curHeight + 20;
  49. BOOL isNeedScroll = NO;
  50. curHeight += 10;
  51. if( (curHeight + 380) >= SCREEN_H){
  52. curHeight = SCREEN_H - 380 - 10;
  53. isNeedScroll = YES;
  54. }
  55. bgView = [[UIView alloc] init];
  56. [bgView setBackgroundColor:[UIColor whiteColor]];
  57. [bgView.layer setCornerRadius:8.f];
  58. [self addSubview:bgView];
  59. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.height.mas_equalTo(280.f + curHeight);
  61. make.width.mas_equalTo(300.f);
  62. make.centerX.equalTo(self.mas_centerX);
  63. make.centerY.equalTo(self.mas_centerY);
  64. }];
  65. // 图片
  66. UIImageView *topImageView = [[UIImageView alloc] init];
  67. topImageView.image = [UIImage imageNamed:@"gengxinbeijing_pic"];
  68. topImageView.contentMode = UIViewContentModeScaleAspectFit;
  69. [bgView addSubview:topImageView];
  70. [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.top.mas_equalTo(bgView.mas_top);
  72. make.left.mas_equalTo(bgView.mas_left);
  73. make.right.mas_equalTo(bgView.mas_right);
  74. make.height.mas_equalTo(141);
  75. }];
  76. /*提示文字*/
  77. UILabel *tipsLabel = [[UILabel alloc] init];
  78. //[tipsLabel setText:NSLocalizedString(@"app_update_new_version_tips",nil)];
  79. [tipsLabel setText:tipStr];
  80. [tipsLabel setTextColor:HW0A132BColor];
  81. [tipsLabel setFont:[UIFont systemFontOfSize:13.f]];
  82. //[tipsLabel setTextAlignment:(NSTextAlignmentCenter)];
  83. [tipsLabel setNumberOfLines:0];
  84. // [bgView addSubview:tipsLabel];
  85. // [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  86. // make.top.equalTo(topImageView.mas_bottom).offset(20.f);
  87. // make.left.mas_equalTo(20);
  88. // make.right.mas_equalTo(-20);
  89. // make.height.mas_equalTo(curHeight);
  90. // }];
  91. //可能文字超出
  92. UIScrollView *bgScorllV = [[UIScrollView alloc] init];
  93. //bgScorllV.showsHorizontalScrollIndicator = NO;
  94. //bgScorllV.backgroundColor = [UIColor greenColor];
  95. if(isNeedScroll){
  96. [bgView addSubview:bgScorllV];
  97. [bgScorllV mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.top.equalTo(topImageView.mas_bottom).offset(20.f);
  99. make.left.mas_equalTo(0);
  100. make.right.mas_equalTo(0);
  101. make.height.mas_equalTo(curHeight);
  102. }];
  103. [bgScorllV addSubview:tipsLabel];
  104. [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.top.mas_equalTo(0);
  106. make.left.mas_equalTo(20);
  107. //make.right.mas_equalTo(-20);
  108. make.width.mas_equalTo(260);
  109. make.height.mas_equalTo(fullHeight);
  110. }];
  111. bgScorllV.contentSize = CGSizeMake(300, fullHeight);
  112. }
  113. else{
  114. [bgView addSubview:tipsLabel];
  115. [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.top.equalTo(topImageView.mas_bottom).offset(20.f);
  117. make.left.mas_equalTo(20);
  118. make.right.mas_equalTo(-20);
  119. make.height.mas_equalTo(curHeight);
  120. }];
  121. }
  122. /*删除按钮*/
  123. deleteBtn = [[UIButton alloc] init];
  124. [deleteBtn setBackgroundColor:[UIColor clearColor]];
  125. //[deleteBtn setImage:[UIImage imageNamed:@"guanbi_bai_icon"] forState:(UIControlStateNormal)];
  126. [deleteBtn setImage:[UIImage imageNamed:@"common_del"] forState:(UIControlStateNormal)];
  127. [deleteBtn addTarget:self action:@selector(deleteBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
  128. [bgView addSubview:deleteBtn];
  129. [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.height.mas_equalTo(45.f);
  131. make.width.mas_equalTo(45.f);
  132. make.right.mas_equalTo(bgView.mas_right).mas_offset(0);
  133. make.top.mas_equalTo(bgView.mas_top).mas_offset(0);
  134. }];
  135. deleteBtn.hidden = YES;
  136. // 分割线
  137. // UIView *line = [[UIView alloc] init];
  138. // line.backgroundColor = HWCCCCCCColor;
  139. // [bgView addSubview:line];
  140. // [line mas_makeConstraints:^(MASConstraintMaker *make) {
  141. // make.left.mas_equalTo(0);
  142. // make.right.mas_equalTo(0);
  143. // make.height.mas_equalTo(1);
  144. // make.bottom.mas_equalTo(-92);
  145. // }];
  146. // gradient
  147. CAGradientLayer *gl = [CAGradientLayer layer];
  148. gl.frame = CGRectMake(0,0,160.f,40.f);
  149. gl.startPoint = CGPointMake(0, 0.5);
  150. gl.endPoint = CGPointMake(1, 0.5);
  151. gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  152. gl.locations = @[@(0), @(1.0f)];
  153. if(_versionModel.necessary){
  154. UIButton *updatekBtn = [[UIButton alloc] init];
  155. updatekBtn.frame = CGRectMake(0, 0, 160.f, 40.f);
  156. [updatekBtn.layer addSublayer:gl];
  157. [updatekBtn addTarget:self action:@selector(updateBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
  158. [updatekBtn setTitle:NSLocalizedString(@"app_update_update",nil) forState:(UIControlStateNormal)];
  159. [updatekBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  160. [updatekBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  161. [updatekBtn.layer setCornerRadius:8.f];
  162. updatekBtn.clipsToBounds = YES;
  163. [bgView addSubview:updatekBtn];
  164. [updatekBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  165. make.centerX.equalTo(bgView.mas_centerX);
  166. make.width.mas_equalTo(160.f);
  167. make.bottom.mas_equalTo(-30.f);
  168. make.height.mas_equalTo(40.f);
  169. }];
  170. }
  171. else{
  172. /*稍后再说 按钮 app_update_wait*/
  173. UIButton *waitBtn = [[UIButton alloc] init];
  174. [waitBtn setTitle:NSLocalizedString(@"app_update_wait",nil) forState:(UIControlStateNormal)];
  175. [waitBtn setTitleColor:HW666666Color forState:UIControlStateNormal];
  176. [waitBtn.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
  177. [waitBtn addTarget:self action:@selector(waitBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
  178. [bgView addSubview:waitBtn];
  179. [waitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  180. make.height.mas_equalTo(30.f);
  181. make.width.mas_equalTo(168.f);
  182. make.centerX.equalTo(bgView.mas_centerX);
  183. make.bottom.mas_equalTo(-20.f);
  184. }];
  185. UIButton *updatekBtn = [[UIButton alloc] init];
  186. updatekBtn.frame = CGRectMake(0, 0, 160.f, 40.f);
  187. [updatekBtn.layer addSublayer:gl];
  188. [updatekBtn addTarget:self action:@selector(updateBtnPressed) forControlEvents:(UIControlEventTouchUpInside)];
  189. [updatekBtn setTitle:NSLocalizedString(@"app_update_update",nil) forState:(UIControlStateNormal)];
  190. [updatekBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  191. [updatekBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  192. [updatekBtn.layer setCornerRadius:8.f];
  193. updatekBtn.clipsToBounds = YES;
  194. [bgView addSubview:updatekBtn];
  195. [updatekBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.centerX.equalTo(bgView.mas_centerX);
  197. make.width.mas_equalTo(160.f);
  198. make.bottom.equalTo(waitBtn.mas_top).offset(-10);
  199. make.height.mas_equalTo(40.f);
  200. }];
  201. }
  202. }
  203. - (void)waitBtnPressed
  204. {
  205. [self deleteBtnPressed];
  206. if(_versionModel.versionNumber){
  207. [HWDataManager setObjectWithKey:Const_did_show_version_and_click_wait value:_versionModel.versionNumber];
  208. }
  209. }
  210. - (void)deleteBtnPressed
  211. {
  212. [self removeFromSuperview];
  213. self.isShow = NO;
  214. if ([_delegate respondsToSelector:@selector(closeBtnBePressed)]) {
  215. [_delegate closeBtnBePressed];
  216. }
  217. if(_closeViewFun){
  218. _closeViewFun();
  219. }
  220. }
  221. - (void)updateBtnPressed {
  222. if (!self.versionModel.necessary) { // 非强制更新
  223. self.isShow = NO;
  224. [self removeFromSuperview];
  225. if ([_delegate respondsToSelector:@selector(closeBtnBePressed)]) {
  226. [_delegate closeBtnBePressed];
  227. }
  228. }
  229. /*跳转蒲公英*/
  230. NSURL * url = [NSURL URLWithString:self.versionModel.downUrl];
  231. HLog(@"立即更新-跳转到:%@",self.versionModel.downUrl);
  232. // NSString *url = [self.versionModel.downUrl stringByReplacingOccurrencesOfString:@" " withString:@""]; // 防止url里有空格字符导致无法跳转
  233. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
  234. ;
  235. }];
  236. }
  237. #pragma mark - 列表委托
  238. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  239. return 1;
  240. }
  241. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  242. return self.dataSource.count;
  243. }
  244. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  245. static NSString *identifier = @"newVersionInfoCellidentifier";
  246. NSInteger row = indexPath.row;
  247. NSString *tipsString = self.dataSource[row];
  248. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  249. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  250. UITableViewCell *payTypecell = [tableView dequeueReusableCellWithIdentifier:identifier];
  251. payTypecell.selectionStyle = UITableViewCellSelectionStyleNone;
  252. if (!cell)
  253. {
  254. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
  255. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  256. [cell setBackgroundColor:[UIColor clearColor]];
  257. /*文字*/
  258. [cell.textLabel setFont:[UIFont systemFontOfSize:14.f]];
  259. [cell.textLabel setTextColor:HWCFD1D4Color];
  260. cell.textLabel.numberOfLines = 0;
  261. [cell.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  262. make.left.mas_equalTo(15);
  263. make.right.mas_equalTo(-15);
  264. make.top.mas_equalTo(5);
  265. make.bottom.mas_equalTo(5);
  266. }];
  267. }
  268. cell.textLabel.text = [NSString stringWithFormat:@"·%@",tipsString];
  269. return cell;
  270. }
  271. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  272. return UITableViewAutomaticDimension;
  273. }
  274. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  275. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 37)];
  276. bgView.backgroundColor = HWF5F7FAColor;
  277. UILabel *label1 = [[UILabel alloc] init];
  278. label1.text = [NSString stringWithFormat:@"发现新版本(%@)", self.versionModel.versionNumber];
  279. label1.textColor = HWCFD1D4Color;
  280. label1.font = [UIFont boldSystemFontOfSize:14.f];
  281. [bgView addSubview:label1];
  282. [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
  283. make.left.mas_equalTo(15);
  284. make.top.mas_equalTo(10);
  285. }];
  286. UILabel *label2 = [[UILabel alloc] init];
  287. label2.text = [NSString stringWithFormat:@"大小:%@MB", self.versionModel.fileSize];
  288. label2.textColor = HWF5F7FAColor;
  289. label2.font = [UIFont systemFontOfSize:10.f];
  290. [bgView addSubview:label2];
  291. [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
  292. make.left.mas_equalTo(label1.mas_right).mas_offset(5);
  293. make.centerY.mas_equalTo(label1.mas_centerY);
  294. }];
  295. return bgView;
  296. }
  297. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  298. return 35;
  299. }
  300. - (NSMutableArray *)dataSource {
  301. if (!_dataSource) {
  302. NSArray *tips = [self.versionModel.versionInfo componentsSeparatedByString:@"&&"];
  303. _dataSource = [NSMutableArray arrayWithArray:tips];
  304. }
  305. return _dataSource;
  306. }
  307. @end