TUIChatPopContextController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // TUIChatPopContextController.m
  3. // TUIChat
  4. //
  5. // Created by wyl on 2022/10/24.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIChatPopContextController.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import "UIImage+ImageEffects.h"
  11. #import <TUICore/TUICore.h>
  12. @interface TUIChatPopContextController ()<V2TIMAdvancedMsgListener>
  13. @property(nonatomic, strong) UIView *recentView;
  14. @property(nonatomic, strong) UIView *alertContainerView;
  15. @property(nonatomic, strong) TUIMessageCell *alertView;
  16. @property(nonatomic, strong) TUIChatPopContextExtionView *extionView;
  17. @property(nonatomic, strong) UIColor *backgroundColor; // set backgroundColor
  18. @property(nonatomic, strong) UIView *backgroundView; // you set coustom view to it
  19. @property(nonatomic, strong) UITapGestureRecognizer *singleTap;
  20. @property(nonatomic, assign) BOOL backgoundTapDismissEnable; // default NO
  21. @end
  22. @implementation TUIChatPopContextController
  23. - (instancetype)init {
  24. if (self = [super init]) {
  25. [self configureController];
  26. }
  27. return self;
  28. }
  29. - (void)configureController {
  30. self.providesPresentationContextTransitionStyle = YES;
  31. self.definesPresentationContext = YES;
  32. self.modalPresentationStyle = UIModalPresentationCustom;
  33. _backgroundColor = [UIColor clearColor];
  34. _backgoundTapDismissEnable = YES;
  35. [[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. self.view.backgroundColor = [UIColor clearColor];
  40. [self addBackgroundView];
  41. [self addSingleTapGesture];
  42. [self configureAlertView];
  43. [self configRecentView];
  44. [self configExtionView];
  45. [self.view layoutIfNeeded];
  46. [self showHapticFeedback];
  47. }
  48. - (void)viewWillAppear:(BOOL)animated {
  49. [super viewWillAppear:animated];
  50. if (_viewWillShowHandler) {
  51. _viewWillShowHandler(_alertView);
  52. }
  53. }
  54. - (void)viewDidAppear:(BOOL)animated {
  55. [super viewDidAppear:animated];
  56. if (_viewDidShowHandler) {
  57. _viewDidShowHandler(_alertView);
  58. }
  59. //
  60. // Too far to the top
  61. CGFloat moveY = 0;
  62. if (self.recentView.frame.origin.y < NavBar_Height) {
  63. CGFloat deal = NavBar_Height - self.recentView.frame.origin.y;
  64. moveY = deal + NavBar_Height + 50;
  65. }
  66. //
  67. // Too far to the right
  68. CGFloat moveX = 0;
  69. if (self.recentView.frame.origin.x + self.recentView.frame.size.width > self.view.frame.size.width) {
  70. CGFloat deal = self.recentView.frame.origin.x + self.recentView.frame.size.width - self.view.frame.size.width;
  71. moveX = deal + 5;
  72. }
  73. //
  74. // too far down
  75. if (self.extionView.frame.origin.y + self.extionView.frame.size.height > self.view.frame.size.height) {
  76. CGFloat deal = self.extionView.frame.origin.y + self.extionView.frame.size.height - self.view.frame.size.height;
  77. moveY = -deal - 50;
  78. }
  79. BOOL oneScreenCanFillCheck = NO;
  80. // Can only one screen fit
  81. if (self.recentView.frame.size.height + self.originFrame.size.height + self.extionView.frame.size.height + kScale390(100) > self.view.bounds.size.height) {
  82. oneScreenCanFillCheck = YES;
  83. }
  84. if (oneScreenCanFillCheck) {
  85. // recentView
  86. CGFloat recentViewMoveY = NavBar_Height + 50;
  87. self.recentView.frame =
  88. CGRectMake(self.recentView.frame.origin.x - moveX, recentViewMoveY, self.recentView.frame.size.width, self.recentView.frame.size.height);
  89. // alertView
  90. [UIView animateWithDuration:0.3
  91. animations:^{
  92. self.alertContainerView.frame =
  93. CGRectMake(0, self.recentView.frame.origin.y + kScale390(8) + self.recentView.frame.size.height,
  94. self.view.frame.size.width, self.originFrame.size.height);
  95. }
  96. completion:^(BOOL finished){
  97. }];
  98. // extionView
  99. CGFloat deal = self.extionView.frame.origin.y + self.extionView.frame.size.height - self.view.frame.size.height;
  100. CGFloat extionViewMoveY = -deal - 50;
  101. self.extionView.frame = CGRectMake(self.extionView.frame.origin.x - moveX, self.extionView.frame.origin.y + extionViewMoveY,
  102. self.extionView.frame.size.width, self.extionView.frame.size.height);
  103. self.extionView.transform = CGAffineTransformMakeScale(0.1, 0.1);
  104. [UIView animateWithDuration:0.5
  105. animations:^{
  106. // Bounces
  107. self.extionView.transform = CGAffineTransformMakeScale(1, 1);
  108. }
  109. completion:^(BOOL finished){
  110. }];
  111. return;
  112. } else {
  113. // When the container need a displacement change
  114. // Or do nothing
  115. if (moveY != 0) {
  116. [UIView animateWithDuration:0.3
  117. animations:^{
  118. self.alertContainerView.frame = CGRectMake(0, self.originFrame.origin.y + moveY, self.view.frame.size.width, self.originFrame.size.height);
  119. }
  120. completion:^(BOOL finished){
  121. }];
  122. }
  123. self.recentView.frame = CGRectMake(self.recentView.frame.origin.x - moveX, self.recentView.frame.origin.y, self.recentView.frame.size.width,
  124. self.recentView.frame.size.height);
  125. [UIView animateWithDuration:0.2
  126. animations:^{
  127. // When recentView needs to have displacement animation
  128. self.recentView.frame = CGRectMake(self.recentView.frame.origin.x, self.recentView.frame.origin.y + moveY,
  129. self.recentView.frame.size.width, self.recentView.frame.size.height);
  130. }
  131. completion:^(BOOL finished){
  132. }];
  133. self.extionView.frame = CGRectMake(self.extionView.frame.origin.x - moveX, self.extionView.frame.origin.y + moveY, self.extionView.frame.size.width,
  134. self.extionView.frame.size.height);
  135. self.extionView.transform = CGAffineTransformMakeScale(0.1, 0.1);
  136. [UIView animateWithDuration:0.5
  137. animations:^{
  138. // Bounces
  139. self.extionView.transform = CGAffineTransformMakeScale(1, 1);
  140. }
  141. completion:^(BOOL finished){
  142. }];
  143. }
  144. }
  145. - (void)addBackgroundView {
  146. if (_backgroundView == nil) {
  147. UIView *backgroundView = [[UIView alloc] init];
  148. backgroundView.backgroundColor = _backgroundColor;
  149. _backgroundView = backgroundView;
  150. }
  151. _backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
  152. [self.view insertSubview:_backgroundView atIndex:0];
  153. [self addConstraintToView:_backgroundView edgeInset:UIEdgeInsetsZero];
  154. }
  155. - (void)setBackgroundView:(UIView *)backgroundView {
  156. if (_backgroundView == nil) {
  157. _backgroundView = backgroundView;
  158. } else if (_backgroundView != backgroundView) {
  159. backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
  160. [self.view insertSubview:backgroundView aboveSubview:_backgroundView];
  161. [self addConstraintToView:backgroundView edgeInset:UIEdgeInsetsZero];
  162. backgroundView.alpha = 0;
  163. [UIView animateWithDuration:0.3
  164. animations:^{
  165. backgroundView.alpha = 1;
  166. }
  167. completion:^(BOOL finished) {
  168. [_backgroundView removeFromSuperview];
  169. _backgroundView = backgroundView;
  170. [self addSingleTapGesture];
  171. }];
  172. }
  173. }
  174. - (void)addSingleTapGesture {
  175. self.view.userInteractionEnabled = YES;
  176. _backgroundView.userInteractionEnabled = YES;
  177. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  178. singleTap.enabled = _backgoundTapDismissEnable;
  179. [_backgroundView addGestureRecognizer:singleTap];
  180. _singleTap = singleTap;
  181. }
  182. - (void)configureAlertView {
  183. self.alertContainerView = [[UIView alloc] init];
  184. [self.view addSubview:self.alertContainerView];
  185. _alertView = [[self.alertCellClass alloc] init];
  186. [self.alertContainerView addSubview:_alertView];
  187. _alertView.userInteractionEnabled = YES;
  188. if ([self.alertView isKindOfClass:NSClassFromString(@"TUIMergeMessageCell_Minimalist")]) {
  189. _alertView.userInteractionEnabled = NO;
  190. }
  191. [_alertView fillWithData:self.alertViewCellData];
  192. [_alertView layoutIfNeeded];
  193. self.alertContainerView.frame = CGRectMake(0, _originFrame.origin.y, self.view.frame.size.width, _originFrame.size.height);
  194. _alertView.frame = CGRectMake(0, 0, self.alertContainerView.frame.size.width, self.alertContainerView.frame.size.height);
  195. for (UIView *view in _alertView.contentView.subviews) {
  196. if(view != _alertView.container) {
  197. view.hidden = YES;
  198. }
  199. }
  200. [_alertView.container mas_remakeConstraints:^(MASConstraintMaker *make) {
  201. make.leading.mas_equalTo(_originFrame.origin.x);
  202. make.top.mas_equalTo(0);
  203. make.size.mas_equalTo(_originFrame.size);
  204. }];
  205. }
  206. - (void)configRecentView {
  207. _recentView = [[UIView alloc] init];
  208. _recentView.backgroundColor = [UIColor clearColor];
  209. [self.view addSubview:_recentView];
  210. _recentView.frame = CGRectMake(_originFrame.origin.x,
  211. _originFrame.origin.y - kScale390(8 + 40),
  212. MAX(kTIMDefaultEmojiSize.width *8,kScale390(208)),
  213. kScale390(40));
  214. NSDictionary *param = @{TUICore_TUIChatExtension_ChatPopMenuReactRecentView_Delegate : self};
  215. [TUICore raiseExtension:TUICore_TUIChatExtension_ChatPopMenuReactRecentView_MinimalistExtensionID parentView:self.recentView param:param];
  216. }
  217. - (void)configExtionView {
  218. _extionView = [[TUIChatPopContextExtionView alloc] init];
  219. _extionView.backgroundColor = [UIColor tui_colorWithHex:@"f9f9f9"];
  220. _extionView.layer.cornerRadius = kScale390(16);
  221. [self.view addSubview:_extionView];
  222. CGFloat height = [self configAndCaculateExtionHeight];
  223. _extionView.frame = CGRectMake(_originFrame.origin.x, _originFrame.origin.y + _originFrame.size.height + kScale390(8), kScale390(180), height);
  224. }
  225. - (CGFloat)configAndCaculateExtionHeight {
  226. NSMutableArray *items = self.items;
  227. CGFloat height = 0;
  228. for (int i = 0; i < items.count; i++) {
  229. height += kScale390(40);
  230. }
  231. CGFloat topMargin = kScale390(6);
  232. CGFloat bottomMargin = kScale390(6);
  233. height += topMargin;
  234. height += bottomMargin;
  235. [_extionView configUIWithItems:items topBottomMargin:topMargin];
  236. return height;
  237. }
  238. - (void)updateExtionView {
  239. CGFloat height = [self configAndCaculateExtionHeight];
  240. _extionView.frame = CGRectMake(_extionView.frame.origin.x, _extionView.frame.origin.y, _extionView.frame.size.width, height);
  241. }
  242. - (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable {
  243. _backgoundTapDismissEnable = backgoundTapDismissEnable;
  244. _singleTap.enabled = backgoundTapDismissEnable;
  245. }
  246. - (void)addConstraintToView:(UIView *)view edgeInset:(UIEdgeInsets)edgeInset {
  247. [self addConstraintWithView:view topView:self.view leftView:self.view bottomView:self.view rightView:self.view edgeInset:edgeInset];
  248. }
  249. - (void)addConstraintWithView:(UIView *)view
  250. topView:(UIView *)topView
  251. leftView:(UIView *)leftView
  252. bottomView:(UIView *)bottomView
  253. rightView:(UIView *)rightView
  254. edgeInset:(UIEdgeInsets)edgeInset {
  255. if (topView) {
  256. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
  257. attribute:NSLayoutAttributeTop
  258. relatedBy:NSLayoutRelationEqual
  259. toItem:topView
  260. attribute:NSLayoutAttributeTop
  261. multiplier:1
  262. constant:edgeInset.top]];
  263. }
  264. if (leftView) {
  265. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
  266. attribute:NSLayoutAttributeLeft
  267. relatedBy:NSLayoutRelationEqual
  268. toItem:leftView
  269. attribute:NSLayoutAttributeLeft
  270. multiplier:1
  271. constant:edgeInset.left]];
  272. }
  273. if (rightView) {
  274. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
  275. attribute:NSLayoutAttributeRight
  276. relatedBy:NSLayoutRelationEqual
  277. toItem:rightView
  278. attribute:NSLayoutAttributeRight
  279. multiplier:1
  280. constant:edgeInset.right]];
  281. }
  282. if (bottomView) {
  283. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view
  284. attribute:NSLayoutAttributeBottom
  285. relatedBy:NSLayoutRelationEqual
  286. toItem:bottomView
  287. attribute:NSLayoutAttributeBottom
  288. multiplier:1
  289. constant:edgeInset.bottom]];
  290. }
  291. }
  292. #pragma mark - public
  293. - (void)setBlurEffectWithView:(UIView *)view {
  294. [self setBlurEffectWithView:view style:BlurEffectStyleDarkEffect];
  295. }
  296. - (void)setBlurEffectWithView:(UIView *)view style:(BlurEffectStyle)blurStyle {
  297. UIImage *snapshotImage = [UIImage snapshotImageWithView:view];
  298. UIImage *blurImage = [self blurImageWithSnapshotImage:snapshotImage style:blurStyle];
  299. dispatch_async(dispatch_get_main_queue(), ^{
  300. UIImageView *blurImageView = [[UIImageView alloc] initWithImage:blurImage];
  301. self.backgroundView = blurImageView;
  302. });
  303. }
  304. - (void)setBlurEffectWithView:(UIView *)view effectTintColor:(UIColor *)effectTintColor {
  305. UIImage *snapshotImage = [UIImage snapshotImageWithView:view];
  306. UIImage *blurImage = [snapshotImage applyTintEffectWithColor:effectTintColor];
  307. UIImageView *blurImageView = [[UIImageView alloc] initWithImage:blurImage];
  308. self.backgroundView = blurImageView;
  309. }
  310. #pragma mark - private
  311. - (void)showHapticFeedback {
  312. if (@available(iOS 10.0, *)) {
  313. dispatch_async(dispatch_get_main_queue(), ^{
  314. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
  315. [generator prepare];
  316. [generator impactOccurred];
  317. });
  318. } else {
  319. // Fallback on earlier versions
  320. }
  321. }
  322. - (UIImage *)blurImageWithSnapshotImage:(UIImage *)snapshotImage style:(BlurEffectStyle)blurStyle {
  323. switch (blurStyle) {
  324. case BlurEffectStyleLight:
  325. return [snapshotImage applyLightEffect];
  326. case BlurEffectStyleDarkEffect:
  327. return [snapshotImage applyDarkEffect];
  328. case BlurEffectStyleExtraLight:
  329. return [snapshotImage applyExtraLightEffect];
  330. default:
  331. return nil;
  332. }
  333. }
  334. - (void)blurDismissViewControllerAnimated:(BOOL)animated completion:(void (^__nullable)(BOOL finished))completion {
  335. [self dismissViewControllerAnimated:animated];
  336. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  337. if (completion) {
  338. completion(YES);
  339. }
  340. });
  341. }
  342. - (void)dismissViewControllerAnimated:(BOOL)animated {
  343. [UIView animateWithDuration:0.3
  344. animations:^{
  345. self.recentView.frame = CGRectMake(self.recentView.frame.origin.x, self.originFrame.origin.y - self.recentView.frame.size.height,
  346. self.recentView.frame.size.width, self.recentView.frame.size.height);
  347. }
  348. completion:^(BOOL finished){
  349. }];
  350. [UIView animateWithDuration:0.3
  351. animations:^{
  352. self.alertContainerView.frame = CGRectMake(0, self.originFrame.origin.y , self.view.frame.size.width, self.originFrame.size.height);
  353. }
  354. completion:^(BOOL finished) {
  355. if (finished) {
  356. [self dismissViewControllerAnimated:animated completion:self.dismissComplete];
  357. }
  358. }];
  359. self.extionView.transform = CGAffineTransformMakeScale(1, 1);
  360. [UIView animateWithDuration:0.2
  361. animations:^{
  362. self.extionView.transform = CGAffineTransformMakeScale(0.1, 0.1);
  363. }
  364. completion:^(BOOL finished) {
  365. if (finished) {
  366. self.extionView.transform = CGAffineTransformMakeScale(0, 0);
  367. }
  368. }];
  369. }
  370. #pragma mark - action
  371. - (void)singleTap:(UITapGestureRecognizer *)sender {
  372. [self dismissViewControllerAnimated:NO];
  373. }
  374. // MARK: V2TIMAdvancedMsgListener
  375. - (void)onRecvMessageRevoked:(NSString *)msgID operateUser:(V2TIMUserFullInfo *)operateUser reason:(NSString *)reason {
  376. if ([msgID isEqualToString:self.alertViewCellData.msgID]) {
  377. UIViewController *controller = self;
  378. while(controller.presentingViewController != nil){
  379. controller = controller.presentingViewController;
  380. }
  381. [controller dismissViewControllerAnimated:YES completion:^{
  382. [self blurDismissViewControllerAnimated:NO completion:nil];
  383. }];
  384. }
  385. }
  386. @end