You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
11 KiB

2 years ago
//
// AmendContactsViewController.m
// LekangGuard
//
// Created by ecell on 2022/12/2.
//
#import "AmendContactsViewController.h"
#import "SelectContactCollectionViewCell.h"
@interface AmendContactsViewController ()<WSLWaterFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
/// 关系Field
@property (nonatomic ,weak) UITextField *contactField;
/// 手机号Field
@property (nonatomic ,weak) UITextField *phoneField;
/// 号码View
@property (nonatomic ,weak) UIView *phoneView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) WSLWaterFlowLayoutStyle flowLayoutStyle;
/// 昵称Arr
@property (nonatomic ,strong) NSArray *nameArr;
/// 头像Arr
@property (nonatomic ,strong) NSArray *imgArr;
/// 关系ID
@property (nonatomic ,assign) NSInteger relationshipId;
/// 关系
@property (nonatomic ,strong) NSString *relationship;
@end
@implementation AmendContactsViewController
- (void) setConModel:(ContactsModel *)conModel
{
_conModel = conModel;
self.zx_navTitle = conModel == nil ? GJText(@"添加联系人") : GJText(@"修改联系人");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self zx_setRightBtnWithText:GJText(@"保存") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
[self setConMessage];
}];
self.imgArr = [UICommon getUnclickRelationshipImageArr:APIManager.sharedManager.deviceModel.deviceType];
self.nameArr = [UICommon getRelationshipNameArr:APIManager.sharedManager.deviceModel.deviceType];
for (int i = 0; i < 2; i++)
{
UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:RGB(245, 245, 245) cornerRadius:5 borderWidth:0 borderColor:KKClearColor];
if (i == 1) self.phoneView = bgView;
[self.view addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view).inset(15);
make.top.equalTo(self.view).offset(iPhoneX_NavHeight+Adapted(20)+Adapted(50*i)+10*i);
make.height.mas_equalTo(Adapted(50));
}];
UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(13) textColor:KKTextBlackColor text:i == 0 ? GJText(@"与我的关系") : GJText(@"我的号码") Radius:0];
CGFloat width = [(NSString *)titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontADA_(13)} context:nil].size.width;
[bgView addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(bgView).offset(10);
make.centerY.equalTo(bgView);
make.width.mas_equalTo(Adapted(width));
}];
UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(13) maxTextNum:i == 0 ? 20 : 25 placeholderColor:KKGrey143 placeholder:i == 0 ? GJText(@"请输入关系") : GJText(@"请输入号码") toMaxNum:^(UITextField *textField) {
} change:^(UITextField *textField) {
}];
textfield.textAlignment = NSTextAlignmentRight;
if (i == 0) self.contactField = textfield;
else
{
textfield.keyboardType = UIKeyboardTypePhonePad;
self.phoneField = textfield;
}
[bgView addSubview:textfield];
[textfield mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLabel.mas_right).offset(7);
make.right.equalTo(bgView).inset(10);
make.centerY.equalTo(bgView);
make.height.mas_equalTo(Adapted(44));
}];
}
UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(14) textColor:KKTextBlackColor text:GJText(@"选择头像") Radius:0];
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(15);
make.top.equalTo(self.phoneView.mas_bottom).offset(Adapted(15));
}];
WSLWaterFlowLayout *FlowLayout = [[WSLWaterFlowLayout alloc] init];
FlowLayout.delegate = self;
FlowLayout.flowLayoutStyle = self.flowLayoutStyle;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:FlowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.alwaysBounceVertical = YES;
_collectionView.allowsMultipleSelection = NO;
_collectionView.scrollEnabled = NO;
_collectionView.backgroundColor = KKClearColor;
if (@available(iOS 11.0, *))
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[_collectionView registerClass:SelectContactCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(SelectContactCollectionViewCell.class)];
[self.view addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(titleLabel.mas_bottom).offset(0);
make.bottom.equalTo(self.view.mas_bottom);
}];
kWeakSelf(self)
AfterDispatch(0.3, ^{
NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:weakself.conModel == nil ? 0 : weakself.conModel.image-1 inSection:0];
[weakself collectionView:weakself.collectionView didSelectItemAtIndexPath:indexPaths];
weakself.phoneField.text = weakself.conModel.phone;
if (weakself.conModel != nil)
weakself.contactField.text = weakself.conModel.name;
});
}
#pragma mark - WSLWaterFlowLayoutDelegate
//返回每个item大小
- (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((SCREEN_WIDTH-75)/4, (SCREEN_WIDTH-75)/4+Adapted(25));
}
/** 头视图Size */
-(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section
{
return CGSizeMake(SCREEN_WIDTH, 0.1);
}
/** 列数*/
-(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout
{
return 4;
}
/** 列间距*/
-(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout
{
return 15;
}
/** 行间距*/
-(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout
{
return 10;
}
/** 边缘之间的间距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout
{
return UIEdgeInsetsMake(0, 15, 10, 15);
}
#pragma mark - UICollectionView数据源
//组个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//组内成员个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imgArr.count;
}
//设置元素内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SelectContactCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(SelectContactCollectionViewCell.class) forIndexPath:indexPath];
cell.tag = indexPath.item+1;
cell.deviceType = APIManager.sharedManager.deviceModel.deviceType;
cell.iconImgName = self.imgArr[indexPath.row];
cell.nameStr = self.nameArr[indexPath.row];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
self.contactField.text = [UICommon getRelationshipNameWithCodeID:indexPath.item+1 deviceType:APIManager.sharedManager.deviceModel.deviceType] ;
self.relationship = self.contactField.text;
self.relationshipId = indexPath.item+1;
}
#pragma mark 修改联系人信息
/// 修改联系人信息
- (void)setConMessage
{
if (self.phoneField.text.length <= 0 )
{
[UICommon MessageErrorText:@"请输入号码"];
return;
}
if (self.contactField.text.length <= 0)
{
[UICommon MessageErrorText:@"请输入关系"];
return;
}
if (self.conModel != nil)
{
[UICommon MessageUpload:@"加载中"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:self.conModel.Id forKey:@"id"];
[parameters setValue:@(self.relationshipId) forKey:@"image"];
[parameters setValue:self.contactField.text forKey:@"relation"];
[parameters setValue:self.contactField.text forKey:@"name"];
[parameters setValue:self.phoneField.text forKey:@"phone"];
[parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"];
[[[APIManager sharedManager] APPOST:UpdateAddress_URL parameters:parameters isJson:YES resultClass:nil] subscribeNext:^(id _Nullable x) {
[UICommon HidenLoading];
[UICommon MessageSuccessText:@"修改成功"];
AfterDispatch(0.5, ^{
self.isUpdateSucceed();
[self.navigationController popViewControllerAnimated:YES];
});
} error:^(NSError * _Nullable error) {
NSDictionary *dic = error.userInfo;
[UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
}];
}
else
{
NSMutableArray *newArr = [NSMutableArray new];
[newArr addObject:@{@"name":self.contactField.text,
@"phone":self.phoneField.text,
@"addMethod":@(1),
@"image":@(self.relationshipId),
@"imei":APIManager.sharedManager.deviceModel.imei,
@"relation":[UICommon getRelationshipNameWithCodeID:self.relationshipId deviceType:APIManager.sharedManager.deviceModel.deviceType]}];
[UICommon MessageUpload:@"加载中"];
[[[APIManager sharedManager] PostBodyWithApi:AddressImport_URL json:[newArr yy_modelToJSONString]] subscribeNext:^(id _Nullable x) {
[UICommon HidenLoading];
[UICommon MessageSuccessText:@"添加成功"];
AfterDispatch(0.5, ^{
self.isUpdateSucceed();
[self.navigationController popViewControllerAnimated:YES];
});
} error:^(NSError * _Nullable error) {
NSDictionary *dic = error.userInfo;
[UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]];
}];
}
}
/*
#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