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.
 
 
 

214 lines
9.0 KiB

//
// GuideUserInfoViewController.swift
// Lookfit
//
// Created by lemo. on 2020/3/11.
// Copyright © 2020 Sheldon. All rights reserved.
//
import UIKit
import AVFoundation
class GuideUserInfoViewController: ViewController {
@IBOutlet weak var headerBtn: UIButton!
@IBOutlet weak var genderLabel: UILabel!
@IBOutlet weak var maleBtn: UIButton!
@IBOutlet weak var femaleBtn: UIButton!
@IBOutlet weak var nickNameLabel: UILabel!
@IBOutlet weak var nickNameTF: UITextField!
@IBOutlet weak var heightLabel: UILabel!
@IBOutlet weak var heightValueLabel: UILabel!
@IBOutlet weak var weightLabel: UILabel!
@IBOutlet weak var weightValueLabel: UILabel!
@IBOutlet weak var birthdayLabel: UILabel!
@IBOutlet weak var birthdayValueLabel: UILabel!
@IBOutlet weak var saveBtn: UIButton!
///
lazy var birthdayValue: Int = {
return DateClass.timeStrToTimestamp("1990-01-01", formatStr: "yyyy-MM-dd")
}()
///
var headerImage: Data?
lazy var pickerView: PickerView = {
let pickerView = PickerView()
pickerView.currenValues = { [weak self] (value) in
guard let `self` = self else { return }
switch pickerView.type {
case .height:
self.heightValueLabel.text = "\(value) cm"
case .weight:
self.weightValueLabel.text = "\(value) kg"
case .birthday:
self.birthdayValue = value
let dateStr = DateClass.timestampToStr(value, formatStr: "yyyy-MM-dd")
self.birthdayValueLabel.text = dateStr
default:
break
}
}
return pickerView
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func makeUI() {
super.makeUI()
//
for subView in view.subviews {
for constrain in subView.constraints {
constrain.constant = kScaleWidth(constrain.constant)
}
}
isTitleCenter = true
navigationTitle = MultiLanguageKey.baseInfo.localized
maleBtn.isEnabled = false
maleBtn.setBackgroundImage(kHexColor(0xFFA11D).asImage(CGSize(width: kScaleWidth(60), height: kScaleWidth(24))), for: .disabled)
maleBtn.setTitleColor(.white, for: .disabled)
femaleBtn.setTitleColor(.white, for: .disabled)
femaleBtn.setBackgroundImage(kHexColor(0xFFA11D).asImage(CGSize(width: kScaleWidth(60), height: kScaleWidth(24))), for: .disabled)
nickNameTF.basePlaceholder(str: MultiLanguageKey.nicknameTip.localized)
//
genderLabel.text = MultiLanguageKey.gender.localized
maleBtn.setTitle(MultiLanguageKey.male.localized, for: .normal)
femaleBtn.setTitle(MultiLanguageKey.female.localized, for: .normal)
nickNameLabel.text = MultiLanguageKey.nickname.localized
heightLabel.text = MultiLanguageKey.height.localized
weightLabel.text = MultiLanguageKey.weight.localized
birthdayLabel.text = MultiLanguageKey.birthday.localized
saveBtn.setTitle(MultiLanguageKey.save.localized, for: .normal)
}
override func handleEvent() {
headerBtn.rx.tap
.subscribe(onNext: { [weak self] _ in
guard let `self` = self else { return }
self.setAvator()
})
.disposed(by: rx.disposeBag)
Observable.of(maleBtn.rx.tap, femaleBtn.rx.tap)
.merge()
.subscribe(onNext: { [weak self] _ in
guard let `self` = self else { return }
self.maleBtn.isEnabled = !self.maleBtn.isEnabled
self.femaleBtn.isEnabled = !self.femaleBtn.isEnabled
})
.disposed(by: rx.disposeBag)
saveBtn.rx.tap
.subscribe(onNext: { [weak self] _ in
guard let `self` = self else { return }
//
guard let nickname = self.nickNameTF.text, nickname != "" else {
SVProgressHUD.showInfo(withStatus: MultiLanguageKey.nicknameTip.localized)
return
}
let heightCM = Int(self.heightValueLabel.text?.components(separatedBy: " ").first ?? "170") ?? 170
let weightKG = Int(self.weightValueLabel.text?.components(separatedBy: " ").first ?? "65") ?? 65
let userInfo = UserInfo(nickname: nickname, avatar: self.headerImage, gender: self.maleBtn.isEnabled, heightCM: heightCM, weightKG: weightKG, birthday: self.birthdayValueLabel.text ?? "")
UserDefaultsManager.saveUserInfo(userInfo: userInfo)
kNotificationCenter.post(name: NSNotification.Name(UserInfoUpdate), object: nil)
self.navigator.dismiss(sender: self)
})
.disposed(by: rx.disposeBag)
}
@IBAction func clickBtn(_ sender: UIButton) {
switch sender.tag {
case 0:
let height = Int(heightValueLabel.text?.components(separatedBy: " ").first ?? "170") ?? 170
pickerView.show(values: height, type: .height, view: view)
case 1:
let weight = Int(weightValueLabel.text?.components(separatedBy: " ").first ?? "65") ?? 65
pickerView.show(values: weight, type: .weight, view: view)
case 2:
pickerView.show(values: birthdayValue, type: .birthday, view: view)
default:
break
}
}
}
extension GuideUserInfoViewController {
func setAvator() {
let alertC = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
alertC.addAction(UIAlertAction.init(title: MultiLanguageKey.cancel.localized, style: UIAlertAction.Style.cancel, handler: nil))
let camera = UIAlertAction.init(title: MultiLanguageKey.camera.localized, style: UIAlertAction.Style.default, handler: {[weak self] (_) in
guard let `self` = self else { return }
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
if authStatus == .denied || authStatus == .restricted {
let alertVC = UIAlertController.init(title: MultiLanguageKey.tip.localized, message: MultiLanguageKey.carmaPower.localized, preferredStyle: .alert)
let goToAction = UIAlertAction.init(title: MultiLanguageKey.goto.localized, style: .default) { (action) in
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
}
let cancelAction = UIAlertAction(title: MultiLanguageKey.cancel.localized, style: .cancel, handler: nil)
alertVC.addAction(goToAction)
alertVC.addAction(cancelAction)
self.present(alertVC, animated: true, completion: nil)
return
}
self.cameraAction()
})
let gallery = UIAlertAction.init(title: MultiLanguageKey.photoSelect.localized, style: UIAlertAction.Style.default, handler: { [weak self](_) in
guard let `self` = self else { return }
self.galleryAction()
})
alertC.addAction(camera)
alertC.addAction(gallery)
self.present(alertC, animated: true, completion: nil)
}
func cameraAction() {
UIImagePickerController.rx.createWithParent(self, animated: true) { (picker) in
picker.allowsEditing = true
picker.sourceType = .camera
}
.flatMap {
$0.rx.didFinishPickingMediaWithInfo
}
.take(1)
.map { info in
return info[UIImagePickerController.InfoKey.editedImage.rawValue] as? UIImage
}
.subscribe(onNext: { [weak self](chooseImg) in
guard let `self` = self, let chooseImg = chooseImg else { return }
self.headerImage = chooseImg.jpegData(compressionQuality: 0.1)
self.headerBtn.setImage(chooseImg, for: .normal)
})
.disposed(by: rx.disposeBag)
}
func galleryAction() {
UIImagePickerController.rx.createWithParent(self, animated: true) { (picker) in
picker.allowsEditing = true
picker.sourceType = .photoLibrary
}
.flatMap {
$0.rx.didFinishPickingMediaWithInfo
}
.take(1)
.map { info in
return info[UIImagePickerController.InfoKey.editedImage.rawValue] as? UIImage
}
.subscribe(onNext: { [weak self](chooseImg) in
guard let `self` = self, let chooseImg = chooseImg else { return }
self.headerImage = chooseImg.jpegData(compressionQuality: 0.3)
self.headerBtn.setImage(chooseImg, for: .normal)
})
.disposed(by: rx.disposeBag)
}
}