MOTranslateSettingView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // MOTranslateSettingView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/7/28.
  6. //
  7. #import "MOTranslateSettingView.h"
  8. #import "MOTranslateSettingCell.h"
  9. #import "MOTranslateSettingTypeTwoCell.h"
  10. #import "MOTranslateChooseView.h"
  11. #import "MOLangData.h"
  12. @interface MOTranslateSettingView ()<UITableViewDelegate,UITableViewDataSource>
  13. @property (nonatomic, strong) UIView *bgView;
  14. @property (nonatomic, strong) UIButton *closeBtn;
  15. @property (nonatomic, strong) UILabel *titleLab;
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) MOTranslateChooseView *translateChooseView;//语言选择
  18. @property (nonatomic, strong) MOLangData *myLangData;//自己的语言设置
  19. @property (nonatomic, assign) BOOL theSwitchStatus;
  20. @end
  21. @implementation MOTranslateSettingView
  22. - (instancetype)init {
  23. if (self = [super init] ) {
  24. [self setupUI];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame{
  29. self = [super initWithFrame:frame];
  30. if (self){
  31. [self setupUI];
  32. }
  33. return self;
  34. }
  35. - (void)setupUI{
  36. [self addSubview:self.bgView];
  37. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.bottom.equalTo(self).offset(0);
  39. make.left.right.equalTo(self);
  40. make.height.equalTo(@(198.0));
  41. }];
  42. [self.bgView addSubview:self.titleLab];
  43. [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.left.right.top.equalTo(self.bgView);
  45. make.height.equalTo(@52.0);
  46. }];
  47. [self.bgView addSubview:self.tableView];
  48. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.equalTo(self.titleLab.mas_bottom);
  50. make.left.right.bottom.equalTo(self.bgView);
  51. }];
  52. [self addSubview:self.closeBtn];
  53. [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.top.left.right.equalTo(self);
  55. make.bottom.equalTo(self.bgView.mas_top);
  56. }];
  57. }
  58. - (void)toGetMyTranslationInfo{
  59. WEAKSELF
  60. [kHttpManager toGetTheTranslationInfoAboutMyWith:nil andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  61. if(kCode_Success){
  62. MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  63. weakSelf.myLangData = [MOLangData modelObjectWithDictionary:data[@"data"]];
  64. weakSelf.theSwitchStatus = weakSelf.myLangData.enabled;
  65. [weakSelf.tableView reloadData];
  66. }
  67. else{
  68. MOLogV(@"toGetTheTranslationInfoAboutMyWith 接口报错了");
  69. kShowNetError(data)
  70. }
  71. }];
  72. }
  73. #pragma mark - UITableViewDelegate,UITableViewDataSource
  74. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  75. return 1;
  76. }
  77. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  78. return 2;
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  81. WEAKSELF
  82. if(indexPath.row == 0){
  83. MOTranslateSettingTypeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:MOTranslateSettingTypeTwoCell_ID];
  84. if (cell == nil){
  85. cell = [[MOTranslateSettingTypeTwoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOTranslateSettingTypeTwoCell_ID];
  86. }
  87. cell.titleLab.text = NSLocalString(@"mimo_2_translate_show");
  88. cell.theSwitch.on = self.theSwitchStatus;
  89. cell.theSwitchChangeBlock = ^(UISwitch * _Nonnull theSwitch) {
  90. weakSelf.theSwitchStatus = theSwitch.on;
  91. if(weakSelf.myLangData.lang.length > 0){
  92. //设置打开或关闭
  93. [weakSelf toSetTranslationWith:weakSelf.myLangData And:theSwitch.on];
  94. }
  95. else{
  96. [weakSelf.tableView reloadData];
  97. }
  98. };
  99. return cell;
  100. }
  101. else{
  102. MOTranslateSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:MOTranslateSettingCell_ID];
  103. if (cell == nil){
  104. cell = [[MOTranslateSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOTranslateSettingCell_ID];
  105. }
  106. cell.titleLab.text = NSLocalString(@"mimo_2_translate_choose");
  107. if(self.myLangData.name.length > 0){
  108. cell.contentLab.text = self.myLangData.name;
  109. }
  110. else{
  111. cell.contentLab.text = NSLocalString(@"mimo_2_translate_choose_null");
  112. }
  113. if(self.theSwitchStatus){
  114. cell.titleLab.alpha = 1.0;
  115. cell.contentLab.alpha = 1.0;
  116. }
  117. else{
  118. cell.titleLab.alpha = 0.3;
  119. cell.contentLab.alpha = 0.3;
  120. }
  121. return cell;
  122. }
  123. }
  124. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  125. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  126. WEAKSELF
  127. if(indexPath.row == 1 && self.theSwitchStatus){
  128. //选择语言
  129. if(self.translateChooseView){
  130. self.translateChooseView.hidden = NO;
  131. if(self.myLangData.lang.length > 0){
  132. self.translateChooseView.lang = self.myLangData.lang;
  133. }
  134. [self.translateChooseView showTranslateChooseView];
  135. return;
  136. }
  137. self.translateChooseView = [[MOTranslateChooseView alloc] init];
  138. if(self.myLangData.lang.length > 0){
  139. self.translateChooseView.lang = self.myLangData.lang;
  140. }
  141. [self addSubview:self.translateChooseView];
  142. [self.translateChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.edges.equalTo(self);
  144. }];
  145. [self.translateChooseView showTranslateChooseView];
  146. self.translateChooseView.cellSelectBlock = ^(MOLangData * _Nonnull langData) {
  147. [weakSelf toSetTranslationWith:langData And:weakSelf.theSwitchStatus];
  148. };
  149. }
  150. }
  151. - (void)toSetTranslationWith:(MOLangData *)langData And:(BOOL)enabled{
  152. WEAKSELF
  153. NSString *langStr = langData.lang;
  154. if(langData.lang.length == 0){
  155. return;
  156. }
  157. NSDictionary *dict = @{@"lang":langStr,@"enabled":@(enabled)};
  158. [kHttpManager toSettingTheTranslationConfigWith:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  159. weakSelf.myLangData.lang = langStr;
  160. weakSelf.myLangData.name = langData.name;
  161. if(kCode_Success){
  162. [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_common_success")];
  163. weakSelf.myLangData.enabled = enabled;
  164. }
  165. else{
  166. MOLogV(@"toSettingTheTranslationConfigWith 接口报错了");
  167. kShowNetError(data)
  168. weakSelf.myLangData.enabled = !enabled;
  169. }
  170. weakSelf.theSwitchStatus = weakSelf.myLangData.enabled;
  171. weakSelf.theSettingChangeBlock ? weakSelf.theSettingChangeBlock(weakSelf.myLangData) : nil;
  172. [weakSelf.tableView reloadData];
  173. }];
  174. }
  175. - (void)showTranslateSettingView{
  176. [self toGetMyTranslationInfo];
  177. self.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
  178. CGRect actionViewRect = self.bgView.frame;
  179. actionViewRect.origin.y = SCREENHEIGHT;
  180. self.bgView.frame = actionViewRect;
  181. WEAKSELF
  182. [UIView animateWithDuration:0.3 animations:^{
  183. CGRect actionViewRect = weakSelf.bgView.frame;
  184. actionViewRect.origin.y = SCREENHEIGHT - 198.0;
  185. weakSelf.bgView.frame = actionViewRect;
  186. }];
  187. //不能滑动
  188. SendNotification(@"MOShowLivePagesVCCannotScroll")
  189. }
  190. - (void)dismissTranslateSettingView{
  191. //完成下移动画
  192. WEAKSELF
  193. [UIView animateWithDuration:0.3 animations:^
  194. {
  195. CGRect actionSheetViewRect = weakSelf.bgView.frame;
  196. actionSheetViewRect.origin.y = SCREENHEIGHT;
  197. weakSelf.bgView.frame = actionSheetViewRect;
  198. } completion:^(BOOL finished)
  199. {
  200. [self removeFromSuperview];
  201. }];
  202. //可以滑动
  203. SendNotification(@"MOShowLivePagesVCCanScroll")
  204. }
  205. #pragma mark - Lazy
  206. - (UIView *)bgView{
  207. if(!_bgView){
  208. _bgView = [[UIView alloc] init];
  209. _bgView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  210. _bgView.layer.cornerRadius = 16.0;
  211. _bgView.layer.masksToBounds = YES;
  212. _bgView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
  213. }
  214. return _bgView;
  215. }
  216. - (UILabel *)titleLab{
  217. if(!_titleLab)
  218. {
  219. _titleLab = [UILabel new];
  220. _titleLab.textColor = kBaseTextColor_1;
  221. _titleLab.textAlignment = NSTextAlignmentCenter;
  222. _titleLab.font = [MOTextTools semiboldFont:16.0];
  223. _titleLab.backgroundColor = [UIColor clearColor];
  224. _titleLab.text = NSLocalString(@"mimo_2_translate_set");
  225. }
  226. return _titleLab;
  227. }
  228. - (UITableView *)tableView{
  229. if (!_tableView)
  230. {
  231. _tableView= [[UITableView alloc] init];
  232. if (@available(iOS 11.0, *))
  233. {
  234. _tableView.estimatedRowHeight = 0;
  235. _tableView.estimatedSectionFooterHeight = 0;
  236. _tableView.estimatedSectionHeaderHeight = 0;
  237. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  238. }
  239. //iOS15适配
  240. if (@available(iOS 15.0, *))
  241. {
  242. self.tableView.sectionHeaderTopPadding = 0;
  243. }
  244. _tableView.backgroundColor = [UIColor clearColor];
  245. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  246. _tableView.dataSource = self;
  247. _tableView.delegate = self;
  248. _tableView.showsVerticalScrollIndicator = NO;
  249. _tableView.showsHorizontalScrollIndicator = NO;
  250. _tableView.rowHeight = 52.0;
  251. [_tableView registerClass:[MOTranslateSettingCell class] forCellReuseIdentifier:MOTranslateSettingCell_ID];
  252. [_tableView registerClass:[MOTranslateSettingTypeTwoCell class] forCellReuseIdentifier:MOTranslateSettingTypeTwoCell_ID];
  253. }
  254. return _tableView;
  255. }
  256. - (UIButton *)closeBtn{
  257. if(!_closeBtn){
  258. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  259. [_closeBtn setTitle:@"" forState:UIControlStateNormal];
  260. [_closeBtn addTarget:self action:@selector(closeBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
  261. }
  262. return _closeBtn;
  263. }
  264. - (void)closeBtnClickAction{
  265. [self dismissTranslateSettingView];
  266. }
  267. @end