Created
April 14, 2017 03:00
-
-
Save jackyshan/4451040ed8053a057ae9c3daa52e8b46 to your computer and use it in GitHub Desktop.
RxSwift基础UI绑定实战总结
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BusDriverFeedbackViewController.swift | |
// renttravel | |
// | |
// Created by jackyshan on 2017/3/8. | |
// Copyright © 2017年 GCI. All rights reserved. | |
// | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
class BusDriverFeedbackViewController: GciBaseController { | |
// MARK: - 1、属性 | |
@IBOutlet weak var textView: GciUITextView! | |
@IBOutlet weak var listView: UIView! | |
@IBOutlet weak var listViewConstraint: NSLayoutConstraint! | |
@IBOutlet weak var submitBtn: UIButton! | |
var listData: [DriverFeedbackModel]? | |
let listCount: Variable<Bool> = Variable(false) | |
var busModel: BusModel? | |
var checkedList: [DriverFeedbackModel] = [DriverFeedbackModel]() | |
// MARK: - 2、生命周期 | |
init() { | |
super.init(nibName: "BusDriverFeedbackViewController", bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initUI() | |
initLinstener() | |
initData() | |
} | |
// MARK: 初始化ui | |
func initUI() { | |
showWriteNavController() | |
showLeftButton(AppConfig.blackBackImage) | |
self.view.backgroundColor = AppConfig.XXT_LightGray | |
self.title = "意见反馈" | |
} | |
// MARK: 初始化linstener | |
let disposeBag = DisposeBag() | |
func initLinstener() { | |
Observable.combineLatest(listCount.asObservable(), textView.rx.text.orEmpty.asObservable()){ ($0 == true || !$1.isEmpty) && $1.characters.count < 151 }.bind(to: submitBtn.rx.isEnabled).addDisposableTo(disposeBag) | |
textView.rx.text.orEmpty.asObservable().bind(onNext: { | |
[weak self] text in | |
let set = CharacterSet(charactersIn: "[\"'<>%;]") | |
self?.textView.text = text.trimmingCharacters(in: set) | |
}).addDisposableTo(disposeBag) | |
} | |
// MARK: 初始化data | |
func initData() { | |
getFeedBackList() | |
} | |
// MARK: 设置frame | |
override func didSystemAutoLayoutComplete() { | |
} | |
// MARK: - 3、代理 | |
// MARK: - 4、业务 | |
// MARK: 关闭窗口 | |
func closeVc() { | |
self.dismiss(animated: true, completion: nil) | |
} | |
// MARK: 提交 | |
@IBAction func clickSubmit(_ sender: AnyObject) { | |
submitDriverFeedback() | |
} | |
// MARK: btn点击 | |
func clickRecommandBtn(_ sender: UIButton) { | |
if sender.isSelected == false { | |
sender.layer.borderColor = AppConfig.XXT_Green.cgColor | |
sender.isSelected = true | |
if let list = listData { | |
checkedList.append(list[sender.tag]) | |
} | |
} | |
else { | |
sender.layer.borderColor = AppConfig.XXT_LightGray.cgColor | |
sender.isSelected = false | |
if let list = listData { | |
checkedList = checkedList.filter({$0 !== list[sender.tag]}) | |
} | |
} | |
listCount.value = !checkedList.isEmpty | |
} | |
// MARK: 返回tid字符串 | |
func getTidString() -> String { | |
var str:String = "" | |
str = checkedList.reduce("", {$0 + $1.id + ","}) | |
return str | |
} | |
// MARK: - 5、网络 | |
// MARK: 获取投诉建议类型 | |
func getFeedBackList() { | |
let send = SendFeedbackModel() | |
send.type = 1 | |
ManagerNetWork.This.doArrayTask(ManagerNetWork.CMD_GetUserFeedBackGetAll, data: send, controller: nil, success: {[weak self] (responseObj:[DriverFeedbackModel]?) in | |
if let list = responseObj { | |
self?.listData = list | |
var btnX:CGFloat = 8 | |
var btnY:CGFloat = 8 | |
let screenW = UIScreen.main.bounds.width | |
let btnW:CGFloat = (screenW - 32)/2 | |
let btnH:CGFloat = 40 | |
for (index, item) in list.enumerated() { | |
let btn = UIButton.init(type: .custom) | |
btn.tag = index | |
btn.setTitle(item.name, for: .normal) | |
btn.setTitle(item.name, for: .selected) | |
btn.setTitleColor(AppConfig.GrayColor, for: .normal) | |
btn.setTitleColor(AppConfig.XXT_Green, for: .selected) | |
btn.titleLabel?.font = UIFont.systemFont(ofSize: 16) | |
btn.frame = CGRect.init(x: btnX, y: btnY, width: btnW, height: btnH) | |
btn.addTarget(self, action: #selector(self?.clickRecommandBtn), for: .touchUpInside) | |
btn.layer.borderColor = AppConfig.XXT_LightGray.cgColor | |
btn.clipsToBounds = true | |
btn.layer.cornerRadius = 20 | |
btn.layer.borderWidth = 1 | |
if index == 0 { | |
btnX = btn.frame.maxX + 16 | |
} | |
else if ((index - 1)%2 == 0) { | |
btnX = 8 | |
btnY = btn.frame.maxY + 8 | |
} | |
else { | |
btnX = btn.frame.maxX + 16 | |
} | |
self?.listView.addSubview(btn) | |
} | |
self?.listViewConstraint.constant = btnY + (list.count%2 == 0 ? 0 : 48) | |
} | |
}, error: {[weak self] (err, msg) in | |
self?.noticeError("暂时无法点评,稍后再试", autoClear: true) | |
self?.navigationController?.popViewController(animated: true) | |
}, com: nil, showWait: false) | |
} | |
// MARK: 提交司机反馈 | |
func submitDriverFeedback() { | |
let send = SendFeedbackModel() | |
send.uuid = AuthManager.This.getLoginInfo()?.uuid | |
send.contact = AuthManager.This.getLoginInfo()?.tel | |
send.tid = getTidString() | |
send.content = textView.text | |
if let bus = busModel { | |
send.routeid = bus.routeId | |
send.routename = bus.routeName | |
send.employeecode = bus.employeeCode | |
send.employeename = bus.employeeName | |
send.organcode = bus.organCode | |
send.organname = bus.organName | |
send.busid = "\(bus.busId)" | |
} | |
ManagerNetWork.This.doTask(ManagerNetWork.CMD_AddDriverFeedBackGetAll, data: send, controller: self, success: {[weak self] (responseObj) in | |
SwiftNotice.showText("感谢您的宝贵意见,小如会牢牢记住的哦", autoClear: true, autoClearTime: 1) | |
self?.navigationController?.popViewController(animated: true) | |
}, error: {[weak self] (err, msg) in | |
self?.noticeError(msg, autoClear: true) | |
}, com: nil, showWait: true) | |
} | |
// MARK: - 6、其他 | |
deinit { | |
if let appIdx = self.getClassName().range(of: Tools.BundleName)?.upperBound { | |
print("销毁页面"+self.getClassName().substring(from: appIdx)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment