YH_RoomSecretViewController.m 4.94 KB
//
//  YH_RoomSecretViewController.m
//  YohoLive
//
//  Created by 盖剑秋 on 16/8/29.
//  Copyright © 2016年 YOHO!. All rights reserved.
//

#import "YH_RoomSecretViewController.h"
#import "YH_NetworkAdapter+Live.h"
#import "YH_Tool.h"

@interface YH_RoomSecretViewController()

@end

@implementation YH_RoomSecretViewController
{
    void (^completionBlock)(NSDictionary *dic, BOOL success);
    UITextField *textField;
    UIButton *startButton;
}

+ (void)showInController:(UIViewController *)aController completionBlock:(void (^)(NSDictionary *, BOOL))block {
    YH_RoomSecretViewController *ctrler = [YH_RoomSecretViewController new];
    [ctrler setCompletionBlock:block];
    [aController.view addSubview:ctrler.view];
    [aController addChildViewController:ctrler];
}

- (instancetype)init {
    if (self = [super init]) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    UIView *bg = [UIView new];
    [self.view addSubview:bg];
    bg.frame = CGRectMake(0, 0, 280, 250);
    bg.center = self.view.center;
    bg.backgroundColor = [UIColor whiteColor];
    CGRect frame = bg.frame;
    CGFloat  bgHeight = 170;
    if ([[UIScreen mainScreen] bounds].size.height < 667) {
        bgHeight = 100;
    }
    frame.origin.y = bgHeight;
    bg.frame = frame;
    bg.userInteractionEnabled = YES;
    

    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 280, 19)];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"房间秘钥";
    [bg addSubview:titleLabel];
    
    UIView *secretBg = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabel.frame)+44, 250, 40)];
    secretBg.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [bg addSubview:secretBg];
    UIImageView *keyImage = [[UIImageView alloc] initWithFrame:CGRectMake(8, 12, 15, 16)];
    keyImage.image = [UIImage imageNamed:@"home_icon_roomkey"];
    [secretBg addSubview:keyImage];
    
    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(31, 12, 0.5, 16)];
    bar.backgroundColor = [UIColor lightGrayColor];
    [secretBg addSubview:bar];
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(35, 0, 210, 40)];
    textField.font = [UIFont systemFontOfSize:14];
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    [secretBg addSubview:textField];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startButton.frame = CGRectMake(15, CGRectGetMaxY(secretBg.frame)+20, 250, 40);
    startButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    startButton.backgroundColor = [UIColor lightGrayColor];
    [bg addSubview:startButton];
    [startButton addTarget:self action:@selector(startButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [startButton setTitle:@"开始直播" forState:UIControlStateNormal];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.frame = CGRectMake(280-18-8, 8, 18, 18);
    [closeButton setImage:[UIImage imageNamed:@"home_btn_close"] forState:UIControlStateNormal];
    [bg addSubview:closeButton];
    [closeButton addTarget:self action:@selector(closeButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}

- (void)textDidChange:(NSNotification *)sender {
    UITextField *txtFild = sender.object;
    if (txtFild.text.length) {
        startButton.backgroundColor = [UIColor blackColor];
    }else {
        startButton.backgroundColor = [UIColor lightGrayColor];
    }
}

- (void)startButtonPressed {
    if (!textField.text.length) {
        return;
    }
    [[YH_NetworkAdapter adapter] getRoomWithSecret:textField.text completion:^(NSMutableDictionary *infoDict, NSError *error) {
       
        if (error) {
            [YH_Tool alertMessage:@"密码无效,请重输"];
            textField.text = @"";
        } else {
            if (completionBlock) {
                completionBlock(infoDict, YES);
            }
            [self closeButtonPressed];
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.3 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)closeButtonPressed {
    [UIView animateWithDuration:0.3 animations:^{
        self.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

- (void)setCompletionBlock:(void (^)(NSDictionary *, BOOL))block {
    completionBlock = [block copy];
}

@end