TextInputAlertView.m 6.3 KB
//
//  TextInputAlertView.m
//  DreamSleep
//
//  Created by peter on 2022/9/26.
//

#import "TextInputAlertView.h"
#import "MKPPlaceholderTextView.h"
#import <IQKeyboardManager/IQKeyboardManager.h>

@interface TextInputAlertView ()
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) MKPPlaceholderTextView *textView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *replyBtn;
@property (nonatomic, assign) CGFloat duration;
@property (nonatomic, copy) NSString *commentUserFlag;
@end

@implementation TextInputAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = ColorFromHexA(0x161E38, .6);
        
        [self addSubview:self.alertView];
        [self.alertView addSubview:self.cancelBtn];
        [self.alertView addSubview:self.replyBtn];
        [self.alertView addSubview:self.textView];
        
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alertView).offset(25);
            make.top.equalTo(self.alertView).offset(18);
        }];
        [self.replyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.alertView).offset(-15);
            make.centerY.equalTo(self.cancelBtn);
            make.size.mas_equalTo(CGSizeMake(44, 26));
        }];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alertView).offset(15);
            make.right.equalTo(self.alertView).offset(-15);
            make.top.equalTo(self.alertView).offset(53);
            make.bottom.equalTo(self.alertView);
        }];
        
        [[IQKeyboardManager sharedManager] setEnable:NO];
        [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
        [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:NO];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)keyboardWillAppear:(NSNotification *)noti {
    NSDictionary *info = [noti userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    self.duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGSize keyboardSize = [value CGRectValue].size;
    [UIView animateWithDuration:self.duration animations:^{
        self.alertView.y = kScreenHeight - self.alertView.height - keyboardSize.height;
    }];
}

- (void)keyboardWillDisappear:(NSNotification *)noti {
    [UIView animateWithDuration:self.duration animations:^{
        self.alertView.y = kScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - public
- (void)showTextInputAlertView {
    [DSKeyWindow addSubview:self];
    [self.textView becomeFirstResponder];
    self.replyBtn.tag = 1;
    [self.replyBtn setTitle:@"发布" forState:UIControlStateNormal];
}

- (void)showReplyTextAlertWithCommentUser:(NSString *)commentUser {
    [self showTextInputAlertView];
    self.replyBtn.tag = 2;
    [self.replyBtn setTitle:@"回复" forState:UIControlStateNormal];
    self.commentUserFlag = [NSString stringWithFormat:@"@%@,", commentUser];
    self.textView.text = self.commentUserFlag;
}

- (void)dismiss {
    [self cancelAction];
}

#pragma mark - private
- (void)cancelAction {
    [self.textView endEditing:YES];
    self.textView.text = @"";
}

- (void)replyAction:(UIButton *)sender {
    NSString *content = self.textView.text;
    if (sender.tag == 2) { // 回复
        content = [content componentsSeparatedByString:self.commentUserFlag].lastObject;
    }
    NSString *triStr = [NSString trimString:content];
    if (triStr.length == 0) {
        [DSProgressHUD showToast:@"请输入评论内容"];
        return;
    }
    if (self.tapFinishBlock) {
        self.tapFinishBlock(sender.tag, content);
    }
}

#pragma mark - lazy
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 201)];
        [_alertView setCornerRadiusRect:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:24];
        _alertView.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, AlertDarkColor, DSWhite);
    }
    return _alertView;
}

- (MKPPlaceholderTextView *)textView {
    if (!_textView) {
        _textView = [MKPPlaceholderTextView new];
        _textView.font = SysFont(14);
        [_textView cornerRadius:12];
        _textView.textContainerInset = UIEdgeInsetsMake(15, 8, 33, 8);
        _textView.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        _textView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xF0F0F0), CornerViewDarkColor, DSWhite);
    }
    return _textView;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton btnWithTitle:@"取消" font:SysFont(14)];
        [_cancelBtn dk_setTitleColorPicker:DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite) forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)replyBtn {
    if (!_replyBtn) {
        _replyBtn = [UIButton btnWithTitle:@"发布" titleColor:DSWhite font:SysFont(12)];
        _replyBtn.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, DSWhite);
        [_replyBtn cornerRadius:13];
        [_replyBtn addTarget:self action:@selector(replyAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _replyBtn;
}

@end