GiGaMaskTime/GIGA/Modules/Mask/Controller/GIGaMaskShareViewController.m

302 lines
11 KiB
Objective-C

//
// GIGaMaskShareViewController.m
// GIGA
//
// Created by lianxiang on 2018/9/21.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GIGaMaskShareViewController.h"
#import "MaskTimeEndViewImgaeCell.h"
#import "MaskTimeTimeViewCell.h"
#import "MaskTimeEndActionViewCell.h"
#import "ShareViewController.h"
#import "MaskTimeShareActionSheet.h"
#import "GIGaUserFileHelper.h"
#import <AVFoundation/AVFoundation.h>
#import "GiGaBaseAPiRequest.h"
#import "GiGaShareModel.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "GiGaUserInfoVC.h"
@interface GIGaMaskShareViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong) GiGaShareModel *model;
@end
@implementation GIGaMaskShareViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addNavTitile:@"面膜时间"];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.sectionFooterHeight = 0;
self.tableView.estimatedSectionFooterHeight= 0;
self.tableView.estimatedSectionHeaderHeight = 0;
//self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 30)];
self.tableView.frame = CGRectMake(0,0,KMainW, self.view.frame.size.height-SAFE_NAV_HEIGHT);
[self requestshareInfo];
[self initUserAvator];
}
-(void)initUserAvator{
//rightItem
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightBtn.frame = CGRectMake(0, 0, 40, 40);
rightBtn.layer.masksToBounds = YES;
rightBtn.layer.cornerRadius = 20;
[rightBtn setImage:[UIImage imageNamed:@"nav_circle_avatar"] forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(goUserInfoView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 11.0) {
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
}
BOOL isUserLogin = [GiGaUserDefault isUserLogin];
if (isUserLogin) {
GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser];
if (user) {
if (user.headImgUrl && ![user.headImgUrl isKindOfClass:[NSNull class]]) {
NSURL *url = [NSURL URLWithString:user.headImgUrl];
[rightBtn sd_setImageWithURL:url forState:UIControlStateNormal];
}
}
}
}
-(void)goUserInfoView{
GiGaUserInfoVC *userVC= [[GiGaUserInfoVC alloc] init];
[self.navigationController pushViewController:userVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
MaskTimeEndViewImgaeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskTimeEndViewImgaeCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskTimeEndViewImgaeCell" owner:self options:nil] lastObject];
}
if (self.model) {
cell.minuteLab.text = [NSString stringWithFormat:@"%@min",self.model.minute];
cell.timeLable.text = [NSString stringWithFormat:@"这是您敷面膜的第:%@张",self.model.monthMaskNum];
if (self.model.productImgUrl && ![self.model.productImgUrl isKindOfClass:[NSNull class]]) {
[cell.holdImagView sd_setImageWithURL:[NSURL URLWithString:self.model.productImgUrl]];
}
}
return cell;
}else if (indexPath.section == 1){
MaskTimeTimeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskTimeTimeViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskTimeTimeViewCell" owner:self options:nil] lastObject];
}
if (self.model) {
cell.timeLabe.text = self.model.minute;
CGFloat percent = [self.model.exquisite floatValue];
int perrr = percent * 100;
NSString *per = [NSString stringWithFormat:@"%d",perrr];
cell.percentLabe.text = per;
cell.totalTime.text = self.model.totalMaskNum;
NSURL *url = [NSURL URLWithString:self.model.headImgUrl];
[cell.useravator sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"nav_circle_avatar"]];
}
return cell;
}else if (indexPath.section == 2){
MaskTimeEndActionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskTimeEndActionViewCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskTimeEndActionViewCell" owner:self options:nil] lastObject];
}
[cell.changeBackBtn addTarget:self action:@selector(changeBackBtnAction) forControlEvents:UIControlEventTouchUpInside];
[cell.shareBtn addTarget:self action:@selector(shareBtnAction) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 285;
}else if (indexPath.section == 1){
return 205;
}
return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{ return 0.1;
}
#pragma mark changeBackBtnAction
-(void)changeBackBtnAction{
MaskTimeShareActionSheet * sheet = [MaskTimeShareActionSheet showAtView:self];
[sheet show:^(NSUInteger index) {
switch (index) {
case 0:
{//手机
[self photosAlbum];
}
break;
case 1:
{
//系统
[GIGaUserFileHelper delectShareImage:kUSERSHAREA_IMAGENAME];
ShareViewController *share = [[ShareViewController alloc] init];
share.model = self.model;
[self presentViewController:share animated:NO completion:nil];
}
break;
case 2:
{
//自拍
[self takePhoto];
}
break;
default:
break;
}
}];
}
#pragma mark share
-(void)shareBtnAction{
ShareViewController *share = [[ShareViewController alloc] init];
share.model = self.model;
[self presentViewController:share animated:YES completion:nil];
}
#pragma mark 相册
-(void)photosAlbum{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES;
//相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//权限
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
}else{
NSString *errorStr = @"应用相册权限受限,请在设置中启用";
[self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"知道了");
//alertMaker.addActionDefaultTitle(@"去开启");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
}];
}
}
-(void)takePhoto{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//权限判断应用是否有使用相机的权限
NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
NSString *errorStr = @"应用相机权限受限,请在设置中启用";
[self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"知道了");
//alertMaker.addActionDefaultTitle(@"去开启");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
}];
return;
}
// 设置数据源
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
GILog(@"info:%@",info);
[picker dismissViewControllerAnimated:YES completion:nil]
;
UIImage*img=[info objectForKey:UIImagePickerControllerEditedImage];
UIImage *small = [GIGaUserFileHelper zipScaleWithImage:img];
[GIGaUserFileHelper savaShareImange:kUSERSHAREA_IMAGENAME image:small];
ShareViewController *share = [[ShareViewController alloc] init];
share.model = self.model;
[self presentViewController:share animated:NO completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)requestshareInfo{
if (self.maskTimeId == nil) {
return;
}
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:kApiShareAction method:RequestPostMethod parms:@{@"id":self.maskTimeId}];
[api requstDataWithResult:^(GiGaAPIResult *result){
if (result.success) {
NSDictionary *dic = result.dic;
GiGaShareModel *model = [[GiGaShareModel alloc] initWithDictionary:dic error:nil];
self.model = model;
[self.tableView reloadData];
}
}];
}
@end