JXAnnounceViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // JXAnnounceViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2018/8/17.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXAnnounceViewController.h"
  9. #import "selectProvinceVC.h"
  10. #import "selectValueVC.h"
  11. #import "ImageResize.h"
  12. #import "searchData.h"
  13. #import "JXAnnounceCell.h"
  14. #define HEIGHT 54
  15. #define STARTTIME_TAG 1
  16. #define IMGSIZE 100
  17. @interface JXAnnounceViewController ()<UITextViewDelegate>
  18. @property(nonatomic, assign) BOOL isShow;
  19. @property(nonatomic, assign) BOOL isEdit;
  20. @property (nonatomic, strong) UIView *baseView;
  21. @property (nonatomic, strong) UIView *bigView;
  22. @property (nonatomic, strong) UIView *topView;
  23. @property (nonatomic, assign) NSInteger index;
  24. @property (nonatomic, strong) JXLabel* relLabel;
  25. @end
  26. @implementation JXAnnounceViewController
  27. @synthesize delegate,didSelect,value;
  28. - (id)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. self.isGotoBack = YES;
  33. self.heightFooter = 0;
  34. self.heightHeader = JX_SCREEN_TOP;
  35. [self createHeadAndFoot];
  36. self.isShowHeaderPull = NO;
  37. self.isShowFooterPull = NO;
  38. self.tableView.backgroundColor = HEXCOLOR(0xF2F2F2);
  39. int height = 44;
  40. self.bigView = [[UIView alloc] initWithFrame:self.view.bounds];
  41. self.bigView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
  42. self.bigView.hidden = YES;
  43. [self.tableView addSubview:self.bigView];
  44. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
  45. [self.bigView addGestureRecognizer:tap];
  46. self.baseView = [[UIView alloc] initWithFrame:CGRectMake(40, JX_SCREEN_HEIGHT/4-.5, JX_SCREEN_WIDTH-80, 162)];
  47. self.baseView.backgroundColor = [UIColor whiteColor];
  48. self.baseView.layer.masksToBounds = YES;
  49. self. baseView.layer.cornerRadius = 4.0f;
  50. [self.bigView addSubview:self.baseView];
  51. int n = 20;
  52. UILabel *titLabel = [[UILabel alloc] initWithFrame:CGRectMake(INSETS, n, self.bigView.frame.size.width - INSETS*2, 20)];
  53. titLabel.textColor = HEXCOLOR(0x333333);
  54. titLabel.text = Localized(@"JX_Announcement");
  55. titLabel.font = SYSFONT(16);
  56. [self.baseView addSubview:titLabel];
  57. n = n + height;
  58. self.name = [self createTextField:self.baseView default:self.value hint:nil];
  59. self.name.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
  60. // _name.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
  61. // CGRect frame = self.name.frame;
  62. // CGSize constraintSize = CGSizeMake(frame.size.width - 20, MAXFLOAT);
  63. // CGSize size = [self.name sizeThatFits:constraintSize];
  64. self.name.textColor = HEXCOLOR(0x595959);
  65. self.name.frame = CGRectMake(10, n, self.baseView.frame.size.width - INSETS*2, 35.5);
  66. self.name.delegate = self;
  67. n = n + INSETS + height;
  68. self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, n, self.baseView.frame.size.width, 44)];
  69. [self.baseView addSubview:self.topView];
  70. // 两条线
  71. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.baseView.frame.size.width, LINE_WH)];
  72. topLine.backgroundColor = THE_LINE_COLOR;
  73. [self.topView addSubview:topLine];
  74. UIView *botLine = [[UIView alloc] initWithFrame:CGRectMake(self.baseView.frame.size.width/2, 0, LINE_WH, self.topView.frame.size.height)];
  75. botLine.backgroundColor = THE_LINE_COLOR;
  76. [self.topView addSubview:botLine];
  77. // 取消
  78. UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topLine.frame), self.baseView.frame.size.width/2, botLine.frame.size.height)];
  79. [cancelBtn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  80. [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  81. [cancelBtn.titleLabel setFont:SYSFONT(15)];
  82. [cancelBtn addTarget:self action:@selector(hideBigView) forControlEvents:UIControlEventTouchUpInside];
  83. [self.topView addSubview:cancelBtn];
  84. // 确定
  85. UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.baseView.frame.size.width/2, CGRectGetMaxY(topLine.frame), self.baseView.frame.size.width/2, botLine.frame.size.height)];
  86. [sureBtn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  87. [sureBtn setTitleColor:HEXCOLOR(0x55BEB8) forState:UIControlStateNormal];
  88. [sureBtn.titleLabel setFont:SYSFONT(15)];
  89. [sureBtn addTarget:self action:@selector(onRelease) forControlEvents:UIControlEventTouchUpInside];
  90. [self.topView addSubview:sureBtn];
  91. // 发布
  92. self.relLabel = [self createLabel:self.tableHeader default:Localized(@"JX_Publish") selector:@selector(onSave)];
  93. self.relLabel.textColor = [UIColor blackColor];
  94. self.relLabel.font = SYSFONT(15);
  95. self.relLabel.textAlignment = NSTextAlignmentRight;
  96. self.relLabel.frame = CGRectMake(JX_SCREEN_WIDTH -32-15, JX_SCREEN_TOP-30, 32, 15);
  97. }
  98. return self;
  99. }
  100. - (void)viewDidLoad
  101. {
  102. [super viewDidLoad];
  103. // Do any additional setup after loading the view.
  104. }
  105. #pragma mark - tableView data source
  106. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  107. return 1;
  108. }
  109. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  110. return self.dataArray.count;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  113. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  114. dict = self.dataArray[indexPath.row];
  115. CGSize size = [dict[@"text"] boundingRectWithSize:CGSizeMake(JX_SCREEN_WIDTH-INSETS*2-15*2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:g_factory.font16} context:nil].size;
  116. return 116 + size.height;
  117. }
  118. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  119. static NSString *identifier = @"JXAnnounceCell";
  120. JXAnnounceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  121. if (!cell) {
  122. cell = [[JXAnnounceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  123. }
  124. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  125. if (self.dataArray.count > 0) {
  126. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  127. dict = self.dataArray[indexPath.row];
  128. [g_server getHeadImageSmall:[NSString stringWithFormat:@"%@",dict[@"userId"]] userName:dict[@"nickname"] imageView:cell.icon];
  129. cell.name.text = [NSString stringWithFormat:@"%@",dict[@"nickname"]];
  130. NSTimeInterval startTime = [dict[@"time"] longLongValue];
  131. cell.time.text = [TimeUtil getTimeStrStyle1:startTime];
  132. cell.content.text = dict[@"text"];
  133. [cell setCellHeightWithText:dict[@"text"]];
  134. }
  135. return cell;
  136. }
  137. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  138. }
  139. - (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
  140. {
  141. // delete action
  142. if (self.isAdmin) { // 是群主或管理员添加删除功能
  143. UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:Localized(@"JX_Edit") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
  144. {
  145. [tableView setEditing:NO animated:YES]; // 退出编辑模式,隐藏左滑菜单
  146. self.index = indexPath.row;
  147. self.isEdit = YES;
  148. self.bigView.hidden = NO;
  149. self.relLabel.userInteractionEnabled = NO;
  150. self.name.text = self.dataArray[indexPath.row][@"text"];
  151. [self.name becomeFirstResponder];
  152. [self textViewDidChange:self.name];
  153. }];
  154. editAction.backgroundColor = [UIColor grayColor]; // 编辑按钮颜色
  155. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:Localized(@"JX_Delete") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
  156. {
  157. [tableView setEditing:NO animated:YES]; // 退出编辑模式,隐藏左滑菜单
  158. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  159. dict = self.dataArray[indexPath.row];
  160. NSString *roomId = [NSString stringWithFormat:@"%@",dict[@"roomId"]];
  161. NSString *noticeId = [NSString stringWithFormat:@"%@",dict[@"id"]];
  162. self.index = indexPath.row;
  163. [g_server roomDeleteNotice:roomId noticeId:noticeId ToView:self];
  164. }];
  165. deleteAction.backgroundColor = [UIColor redColor]; // 删除按钮颜色
  166. return @[deleteAction,editAction];
  167. }
  168. return @[];
  169. }
  170. - (void)hideBigView {
  171. [self hideKeyBoard];
  172. self.bigView.hidden = YES;
  173. self.relLabel.userInteractionEnabled = YES;
  174. [self resetBigView];
  175. }
  176. - (void)hideKeyBoard {
  177. if ([self.name isFirstResponder]) {
  178. [self.name resignFirstResponder];
  179. }
  180. }
  181. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  182. if (self.isLimit) {
  183. if (self.limitLen <= 0) {
  184. self.limitLen = 15;
  185. }
  186. if(textView.text.length > self.limitLen && ![text isEqualToString:@""]){
  187. if (!self.isShow) {
  188. self.isShow = YES;
  189. [g_App showAlert:Localized(@"JX_InputLimit")];
  190. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  191. self.isShow = NO;
  192. });
  193. }
  194. return NO;
  195. }
  196. }
  197. return YES;
  198. }
  199. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
  200. return YES;
  201. }
  202. - (void)textViewDidChange:(UITextView *)textView {
  203. int maxHeight = 66.f;
  204. CGRect frame = textView.frame;
  205. CGSize constraintSize = CGSizeMake(JX_SCREEN_WIDTH-80-INSETS*2, MAXFLOAT);
  206. CGSize size = [textView sizeThatFits:constraintSize];
  207. if (size.height >= maxHeight)
  208. {
  209. size.height = maxHeight;
  210. textView.scrollEnabled = YES; // 允许滚动
  211. }
  212. else
  213. {
  214. textView.scrollEnabled = NO; // 不允许滚动
  215. }
  216. textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
  217. self.baseView.frame = CGRectMake(40, JX_SCREEN_HEIGHT/4+35-size.height, JX_SCREEN_WIDTH-80, 162-35+size.height);
  218. self.topView.frame = CGRectMake(0, 118-35+size.height, self.baseView.frame.size.width, 40);
  219. }
  220. -(UITextView*)createTextField:(UIView*)parent default:(NSString*)s hint:(NSString*)hint{
  221. UITextView* p = [[UITextView alloc] initWithFrame:CGRectMake(0,INSETS,JX_SCREEN_WIDTH,HEIGHT)];
  222. p.delegate = self;
  223. p.autocorrectionType = UITextAutocorrectionTypeNo;
  224. p.autocapitalizationType = UITextAutocapitalizationTypeNone;
  225. p.enablesReturnKeyAutomatically = YES;
  226. p.scrollEnabled = NO;
  227. // p.borderStyle = UITextBorderStyleNone;
  228. p.returnKeyType = UIReturnKeyDone;
  229. p.showsVerticalScrollIndicator = NO;
  230. p.showsHorizontalScrollIndicator = NO;
  231. // p.clearButtonMode = UITextFieldViewModeAlways;
  232. p.textAlignment = NSTextAlignmentLeft;
  233. p.userInteractionEnabled = YES;
  234. p.backgroundColor = [UIColor whiteColor];
  235. p.text = s;
  236. // p.placeholder = hint;
  237. // p.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, HEIGHT-INSETS*2)];
  238. // p.leftViewMode = UITextFieldViewModeAlways;
  239. p.font = g_factory.font16;
  240. [parent addSubview:p];
  241. return p;
  242. }
  243. -(JXLabel*)createLabel:(UIView*)parent default:(NSString*)s selector:(SEL)selector{
  244. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH/2,INSETS,JX_SCREEN_WIDTH/2 -20,HEIGHT-INSETS*2)];
  245. p.userInteractionEnabled = NO;
  246. p.text = s;
  247. p.font = g_factory.font14;
  248. p.textAlignment = NSTextAlignmentLeft;
  249. p.didTouch = selector;
  250. p.delegate = self;
  251. [parent addSubview:p];
  252. return p;
  253. }
  254. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  255. {
  256. [self.view endEditing:YES];
  257. return YES;
  258. }
  259. - (void)onRelease {
  260. if([_name.text isEqualToString:@""]){
  261. [g_App showAlert:Localized(@"JX_AnnouncementNoNull")];
  262. return;
  263. }
  264. [self hideBigView];
  265. if (self.isEdit) {
  266. NSString *roomId = [NSString stringWithFormat:@"%@",self.dataArray[self.index][@"roomId"]];
  267. NSString *noticeId = [NSString stringWithFormat:@"%@",self.dataArray[self.index][@"id"]];
  268. [g_server updateNotice:roomId noticeId:noticeId noticeContent:self.name.text toView:self];
  269. }else {
  270. self.room.note = _name.text;
  271. [g_server updateRoomNotify:self.room toView:self];
  272. }
  273. }
  274. -(void)onSave{
  275. if (!self.isAdmin) {
  276. [g_App showAlert:Localized(@"JXRoomMemberVC_NotAdminCannotDoThis")];
  277. return;
  278. }
  279. self.isEdit = NO;
  280. self.name.text = nil;
  281. self.bigView.hidden = NO;
  282. self.relLabel.userInteractionEnabled = NO;
  283. [self.name becomeFirstResponder];
  284. }
  285. - (void)resetBigView {
  286. self.name.frame = CGRectMake(10, 64, self.baseView.frame.size.width - INSETS*2, 35.5);
  287. self.baseView.frame = CGRectMake(40, JX_SCREEN_HEIGHT/4-.5, JX_SCREEN_WIDTH-80, 162);
  288. self.topView.frame = CGRectMake(0, 118, self.baseView.frame.size.width, 40);
  289. }
  290. -(void)didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  291. [_wait stop];
  292. if( [aDownload.action isEqualToString:act_roomDeleteNotice]){
  293. [self.dataArray removeObjectAtIndex:self.index];
  294. [self.tableView reloadData];
  295. self.value = [(NSDictionary *)self.dataArray.firstObject objectForKey:@"text"];
  296. if (delegate && [self.delegate respondsToSelector:didSelect]) {
  297. [delegate performSelectorOnMainThread:didSelect withObject:self waitUntilDone:NO];
  298. }
  299. }
  300. if( [aDownload.action isEqualToString:act_roomSet]){
  301. NSMutableDictionary *noticeDict = [[NSMutableDictionary alloc] init];
  302. NSNumber *time = [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];
  303. [noticeDict setObject:self.room.roomId forKey:@"roomId"];
  304. [noticeDict setObject:self.room.roomId forKey:@"id"];
  305. [noticeDict setObject:MY_USER_ID forKey:@"userId"];
  306. [noticeDict setObject:self.name.text forKey:@"text"];
  307. [noticeDict setObject:time forKey:@"time"];
  308. [noticeDict setObject:MY_USER_NAME forKey:@"nickname"];
  309. [noticeDict setObject:[dict objectForKey:@"noticeId"] forKey:@"id"];
  310. [self.dataArray insertObject:noticeDict atIndex:0];
  311. [self.tableView reloadData];
  312. // [g_server getRoom:self.room.roomId toView:self];
  313. if (delegate && [self.delegate respondsToSelector:didSelect]) {
  314. [delegate performSelectorOnMainThread:didSelect withObject:self waitUntilDone:NO];
  315. }
  316. }
  317. if( [aDownload.action isEqualToString:act_roomGet]){
  318. // [self.dataArray removeAllObjects];
  319. // NSArray *arr = [dict objectForKey:@"notices"];
  320. // [self.dataArray addObjectsFromArray:arr];
  321. // [self.tableView reloadData];
  322. }
  323. if ([aDownload.action isEqualToString:act_updateNotice]) {
  324. [self.dataArray[self.index] setValue:self.name.text forKey:@"text"];
  325. self.value = self.name.text;
  326. [g_server showMsg:Localized(@"JXAlert_UpdateOK")];
  327. [self.tableView reloadData];
  328. if (delegate && [self.delegate respondsToSelector:didSelect]) {
  329. [delegate performSelectorOnMainThread:didSelect withObject:self waitUntilDone:NO];
  330. }
  331. }
  332. }
  333. -(int)didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  334. [_wait stop];
  335. return show_error;
  336. }
  337. -(int)didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{
  338. [_wait stop];
  339. return show_error;
  340. }
  341. -(void)didServerConnectStart:(JXConnection*)aDownload{
  342. [_wait start];
  343. }
  344. @end