|
//
// bindThirdAccountController.m
// ThePaperDemo
//
// Created by scar1900 on 14/12/11.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "bindThirdAccountController.h"
#import "shareUtil.h"
#define thirdButtonTag 1200
#define thirdButtonLabelTag 4200
@interface bindButton : UICollectionViewCell
@property(nonatomic, strong)UIButton *btn;
@property(nonatomic, strong)UIImageView *img;
@property(nonatomic, strong)UILabel *label;
@end
@implementation bindButton
- (void)setLayout {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.btn];
[self addSubview:self.img];
[self addSubview:self.label];
[self.btn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top);
make.left.equalTo(self.left);
make.right.equalTo(self.right);
make.height.equalTo(self.width);
}];
[self.img makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.btn.center);
make.width.and.height.mas_equalTo(@25);
}];
[self.label makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.btn.bottom);
make.centerX.equalTo(self.btn.centerX);
make.width.mas_equalTo(@50);
make.bottom.equalTo(self.bottom);
}];
}
- (UIButton*)btn {
if (!_btn) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor clearColor];
_btn.layer.cornerRadius = 4.f;
_btn.clipsToBounds = YES;
}
return _btn;
}
- (UIImageView*)img {
if (!_img) {
_img = [[UIImageView alloc] init];
_img.backgroundColor = [UIColor clearColor];
}
return _img;
}
- (UILabel*)label {
if (!_label) {
_label = [[UILabel alloc] init];
_label.textColor = [UIColor colorWithHexString:TextGray];
_label.textAlignment = NSTextAlignmentCenter;
_label.backgroundColor = [UIColor clearColor];
}
_label.font = appFont(TEXT_SIX_LEVELSIZE, NO);
return _label;
}
- (void)setBtnEventWithTarget:(id)target selector:(SEL)selector {
[self.btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
}
- (void)setImage:(UIImage*)image backgroundColor:(NSString*)backgroundColor {
self.img.image = image;
CGSize size = image.size;
[self.img remakeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.btn.centerX);
make.centerY.equalTo(self.btn.centerY);
make.width.mas_equalTo(size.width);
make.height.mas_equalTo(size.height);
}];
[self.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK])
forState:UIControlStateNormal];
[self.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:backgroundColor])
forState:UIControlStateSelected];
[self.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONSELECTBACK])
forState:UIControlStateHighlighted];
[self.btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK])
forState:UIControlStateDisabled];}
@end
static NSString *collectCellID = @"BindCollectionViewIdentifier";
@interface bindThirdAccountController() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *btnCollectionView;
@end
@implementation bindThirdAccountController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.titleStr = @"社交账号绑定";
[self.view addSubview:self.btnCollectionView];
[self.btnCollectionView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom).offset(57);
make.left.equalTo(self.view.left).offset(30);
make.right.equalTo(self.view.right).offset(-30);
make.bottom.equalTo(self.view.bottom);
}];
}
- (UICollectionView*)btnCollectionView {
if (!_btnCollectionView) {
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
layout.minimumInteritemSpacing = 30;
layout.minimumLineSpacing = 18;
CGFloat width = (self.view.frame.size.width-60-30*3)/4;
layout.itemSize = CGSizeMake(width, width+30);
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
_btnCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[_btnCollectionView registerClass:[bindButton class] forCellWithReuseIdentifier:collectCellID];
_btnCollectionView.delegate = self;
_btnCollectionView.dataSource = self;
_btnCollectionView.allowsSelection = NO;
_btnCollectionView.scrollEnabled = NO;
_btnCollectionView.delaysContentTouches = NO;
_btnCollectionView.backgroundColor = [UIColor clearColor];
}
return _btnCollectionView;
}
#pragma mark - collctionView delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
return 7;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {
bindButton *cell = (bindButton*)[collectionView dequeueReusableCellWithReuseIdentifier:collectCellID forIndexPath:indexPath];
[cell setLayout];
cell.btn.tag = thirdButtonTag + indexPath.row;
cell.label.tag = thirdButtonLabelTag + indexPath.row;
switch (indexPath.row) {
case 0:
[cell setBtnEventWithTarget:self selector:@selector(weChatBind:)];
[cell setImage:Image(@"login/shareWeChat.png") backgroundColor:@"0x77b14d"];
if (self.socialMap[@"WEIXIN"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.btn.selected = NO;
if ([shareUtil isHaveWeChatClient]) {
cell.label.text = @"点击绑定";
}
else {
cell.btn.enabled = NO;
cell.label.text = @"无法绑定";
}
}
break;
case 1:
[cell setBtnEventWithTarget:self selector:@selector(QQBind:)];
[cell setImage:Image(@"login/shareQQFriend.png") backgroundColor:@"0x5da3e9"];
if (self.socialMap[@"TENCENT"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
case 2:
[cell setBtnEventWithTarget:self selector:@selector(sinaBind:)];
[cell setImage:Image(@"login/shareSinaWeiBo.png") backgroundColor:@"0xcf803d"];
if (self.socialMap[@"SINA"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
case 3:
[cell setBtnEventWithTarget:self selector:@selector(doubanBind:)];
[cell setImage:Image(@"login/shareDouban.png") backgroundColor:@"0x77b24e"];
if (self.socialMap[@"DOUBAN"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
case 4:
[cell setBtnEventWithTarget:self selector:@selector(evernoteBind:)];
[cell setImage:Image(@"login/shareEvernote.png") backgroundColor:@"0x75b249"];
if (self.socialMap[@"YINXIANG"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
case 5:
[cell setBtnEventWithTarget:self selector:@selector(youdaoBind:)];
[cell setImage:Image(@"login/shareYouDao.png") backgroundColor:@"0x83b6d3"];
if (self.socialMap[@"YOUDAO"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
case 6:
[cell setBtnEventWithTarget:self selector:@selector(pocketBind:)];
[cell setImage:Image(@"login/sharePocket.png") backgroundColor:@"0xce4358"];
if (self.socialMap[@"POCKET"]) {
cell.label.text = @"解除绑定";
cell.btn.selected = YES;
}
else {
cell.label.text = @"点击绑定";
cell.btn.selected = NO;
}
break;
default:
break;
}
return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
UIEdgeInsets inset = {0, 0, 0, 0};
return inset;
}
#pragma mark - bind event
- (void)bindSocietyWithType:(shareAndLoginType)type withButton:(UIButton*)btn{
UILabel *label = (UILabel*)[self.view viewWithTag:btn.tag-thirdButtonTag+thirdButtonLabelTag];
[shareUtil bindSocietyAccountWithType:type completion:^(BOOL isSuccess) {
if (isSuccess) {
btn.selected = YES;
label.text = @"解除绑定";
ShowMessage(@"绑定成功", YES);
}else {
ShowMessage(@"绑定失败", NO);
}
}];
}
- (void)bindOutSocietyWithType:(shareAndLoginType)type withButton:(UIButton*)btn {
if ([self checkIfIsLoginType:type]) {
return;
}
UILabel *label = (UILabel*)[self.view viewWithTag:btn.tag-thirdButtonTag+thirdButtonLabelTag];
[shareUtil bindOutWithType:type completion:^(BOOL isSuccess) {
if (isSuccess) {
btn.selected = NO;
label.text = @"点击绑定";
if (![shareUtil isHaveWeChatClient] && type == weChatLoginType) {
btn.enabled = NO;
}else {
btn.enabled = YES;
}
ShowMessage(@"解绑成功", YES);
}
}];
}
- (void)weChatBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:weChatLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:weChatLoginType withButton:btn];
}
}
- (void)QQBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:QQLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:QQLoginType withButton:btn];
}
}
- (void)sinaBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:sinaWeiBoLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:sinaWeiBoLoginType withButton:btn];
}
}
- (void)doubanBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:doubanLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:doubanLoginType withButton:btn];
}
}
- (void)evernoteBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:evernoteLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:evernoteLoginType withButton:btn];
}
}
- (void)youdaoBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:youdaoLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:youdaoLoginType withButton:btn];
}
}
- (void)pocketBind:(UIButton*)btn {
if (!btn.selected) {
[self bindSocietyWithType:pocketLoginType withButton:btn];
}else {
[self bindOutSocietyWithType:pocketLoginType withButton:btn];
}
}
- (BOOL)checkIfIsLoginType:(shareAndLoginType)type {
userBO *user = [TPUserDefault instance].userBO;
if ([user.loginType intValue] == type) {
ShowTextMessage(@"登录账号无法解除绑定");
return YES;
}else {
return NO;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|