|
@@ -12,6 +12,8 @@ class CircleQuestionsAnswersViewController: BaseViewController {
|
|
|
|
|
|
var circleId : Int?
|
|
|
var communityCircleQuestionsAnswersModel : CommunityCircleQuestionsAnswersModel?
|
|
|
+ var array = Array<CustomAnswer>()
|
|
|
+ var isSuccess : Bool = false
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
@@ -25,6 +27,7 @@ class CircleQuestionsAnswersViewController: BaseViewController {
|
|
|
navigationBar.title = "加入小组测试题"
|
|
|
navigationBar.addSubview(subtitleLabel)
|
|
|
view.addSubview(tableView)
|
|
|
+ view.addSubview(submitButton)
|
|
|
|
|
|
}
|
|
|
|
|
@@ -37,10 +40,19 @@ class CircleQuestionsAnswersViewController: BaseViewController {
|
|
|
make.top.equalTo(navigationBar.titleLabel.snp.bottom)
|
|
|
make.centerX.equalToSuperview()
|
|
|
}
|
|
|
+ submitButton.snp.makeConstraints { (make) in
|
|
|
+ make.bottom.equalTo(-(kSafeTabBarHeight + 16))
|
|
|
+ make.left.equalTo(25)
|
|
|
+ make.right.equalTo(-25)
|
|
|
+ make.height.equalTo(44)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override func setupData() {
|
|
|
communityCircleQuestionApi()
|
|
|
+ submitButton.rx.tap.subscribe(onNext: {[weak self] (_) in
|
|
|
+ self?.communityCircleJoinPostApi()
|
|
|
+ }).disposed(by: disposeBag)
|
|
|
}
|
|
|
|
|
|
lazy var subtitleLabel : UILabel = {
|
|
@@ -51,12 +63,26 @@ class CircleQuestionsAnswersViewController: BaseViewController {
|
|
|
return subtitleLabel
|
|
|
}()
|
|
|
|
|
|
+ lazy var submitButton: UIButton = {
|
|
|
+ let submitButton = UIButton()
|
|
|
+ submitButton.backgroundColor = kThemeColor
|
|
|
+ submitButton.setTitle("提交", for: UIControl.State.normal)
|
|
|
+ submitButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
|
|
|
+ submitButton.cornerRadius = 22
|
|
|
+ submitButton.masksToBounds = true
|
|
|
+ submitButton.isHidden = true
|
|
|
+ return submitButton
|
|
|
+ }()
|
|
|
+
|
|
|
lazy var tableView: UITableView = {
|
|
|
let tableView = UITableView(frame: CGRect(x: 0, y: kNavBarTotalHeight, width: kScreenWidth, height: kScreenHeight - kNavBarTotalHeight - 76 - kSafeTabBarHeight), style: UITableView.Style.grouped)
|
|
|
tableView.separatorStyle = .none
|
|
|
tableView.backgroundColor = UIColor.white
|
|
|
tableView.dataSource = self
|
|
|
tableView.delegate = self
|
|
|
+ let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 10))
|
|
|
+ headerView.backgroundColor = kf7f8faColor
|
|
|
+ tableView.tableHeaderView = headerView
|
|
|
return tableView
|
|
|
}()
|
|
|
|
|
@@ -65,12 +91,73 @@ class CircleQuestionsAnswersViewController: BaseViewController {
|
|
|
[weak self] (communityCircleQuestionsAnswersModel) -> (Void) in
|
|
|
self?.communityCircleQuestionsAnswersModel = communityCircleQuestionsAnswersModel as? CommunityCircleQuestionsAnswersModel
|
|
|
self?.subtitleLabel.text = "今日剩余 \( self?.communityCircleQuestionsAnswersModel?.answerCount ?? 0) 次答题机会"
|
|
|
+ self?.submitButton.isHidden = false
|
|
|
self?.tableView.reloadData()
|
|
|
}) { (loadingStatus) in
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ typealias JoinSuccess = () -> Void
|
|
|
+ var joinSuccess : JoinSuccess?
|
|
|
+
|
|
|
+ typealias JoinFail = (Int) -> Void
|
|
|
+ var joinFail : JoinFail?
|
|
|
+ /// 加入圈子
|
|
|
+ func communityCircleJoinPostApi() {
|
|
|
+
|
|
|
+ array.removeAll()
|
|
|
+ for circleJoinQuestionModel in (communityCircleQuestionsAnswersModel?.joinQuestion)! {
|
|
|
+ if circleJoinQuestionModel.customAnswer != nil {
|
|
|
+ array.append(circleJoinQuestionModel.customAnswer!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let answerStr = array.toJSONString()
|
|
|
+ if array.count == communityCircleQuestionsAnswersModel?.joinQuestion?.count {
|
|
|
+ SwiftMoyaNetWorkServiceCommunity.shared().communityCircleJoinPostApi(circleId: circleId ?? 0, answer: answerStr) { [weak self] (rootModel) -> (Void) in
|
|
|
+ let rootModel = rootModel as? RootModel
|
|
|
+ if rootModel?.code == 0 {
|
|
|
+ self?.isSuccess = true
|
|
|
+ self?.navigationController?.popViewController(animated: true)
|
|
|
+ if let joinSuccess = self?.joinSuccess {
|
|
|
+ joinSuccess()
|
|
|
+ }
|
|
|
+ } else if rootModel?.code == 1 {
|
|
|
+
|
|
|
+ let circleSubmitAnswerModel = CircleSubmitAnswerModel(JSONString: rootModel?.data ?? "")
|
|
|
+ if circleSubmitAnswerModel?.answerCount == 0 {
|
|
|
+ self?.isSuccess = false
|
|
|
+ self?.communityCircleQuestionsAnswersModel?.answerCount = circleSubmitAnswerModel?.answerCount ?? 0
|
|
|
+ SwiftProgressHUD.shared().showText(rootModel?.msg ?? "")
|
|
|
+ self?.navigationController?.popViewController(animated: true)
|
|
|
+ }else {
|
|
|
+ AlertSheetView.alert(title: "真遗憾,没有全部答对哦~", cancelTitle: "放弃了", sureTitle: "再来一次", cancelBlock: { [weak self] (popupView, index, string) in
|
|
|
+ self?.isSuccess = false
|
|
|
+ self?.communityCircleQuestionsAnswersModel?.answerCount = circleSubmitAnswerModel?.answerCount ?? 0
|
|
|
+ self?.navigationController?.popViewController(animated: true)
|
|
|
+ }, confirmBlock: { (popupView, index, string) in
|
|
|
+ self?.communityCircleQuestionsAnswersModel?.answerCount = circleSubmitAnswerModel?.answerCount ?? 0
|
|
|
+ self?.subtitleLabel.text = "今日剩余 \( self?.communityCircleQuestionsAnswersModel?.answerCount ?? 0) 次答题机会"
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ SwiftProgressHUD.shared().showText("请回答所有问题")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override func didMove(toParent parent: UIViewController?) {
|
|
|
+ if !(parent != nil) {
|
|
|
+ if !isSuccess {
|
|
|
+ if let joinFail = self.joinFail {
|
|
|
+ joinFail(communityCircleQuestionsAnswersModel?.answerCount ?? 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
extension CircleQuestionsAnswersViewController : UITableViewDelegate, UITableViewDataSource {
|
|
@@ -97,12 +184,43 @@ extension CircleQuestionsAnswersViewController : UITableViewDelegate, UITableVie
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
+ let circleJoinQuestionModel = communityCircleQuestionsAnswersModel?.joinQuestion?[indexPath.section]
|
|
|
+
|
|
|
+ for (i,circleAnswerModel) in (communityCircleQuestionsAnswersModel?.joinQuestion?[indexPath.section].answer?.enumerated())! {
|
|
|
+ if i == indexPath.row {
|
|
|
+ let customAnswer = CustomAnswer()
|
|
|
+ customAnswer.questionId = circleJoinQuestionModel?.id
|
|
|
+ customAnswer.answer = [circleAnswerModel.id] as? [Int]
|
|
|
+ circleJoinQuestionModel?.customAnswer = customAnswer
|
|
|
+ circleAnswerModel.isSelected = true
|
|
|
+ }else {
|
|
|
+ circleAnswerModel.isSelected = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
|
|
|
+ }
|
|
|
+
|
|
|
+ func questionHeight(section: Int,circleJoinQuestionModel:CircleJoinQuestionModel?) -> CGFloat {
|
|
|
+ let numberStr = "第\(section + 1)题:"
|
|
|
+ let numberWidth = numberStr.widthForComment(font: kMediumFont14!)
|
|
|
+
|
|
|
+ let questionHeight = circleJoinQuestionModel?.question?.heightForComment(font: kRegularFont14!, width: kScreenWidth - 28 - numberWidth) ?? 0.0
|
|
|
+ return questionHeight
|
|
|
+ }
|
|
|
+
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
- return 0.000001
|
|
|
+ let circleJoinQuestionModel = communityCircleQuestionsAnswersModel?.joinQuestion?[section]
|
|
|
+ circleJoinQuestionModel?.questionHeight = questionHeight(section: section, circleJoinQuestionModel: circleJoinQuestionModel)
|
|
|
+ return ((circleJoinQuestionModel?.questionHeight ?? 0) + 30)
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
|
- return nil
|
|
|
+ let circleJoinQuestionModel = communityCircleQuestionsAnswersModel?.joinQuestion?[section]
|
|
|
+ let view = CircleQuestionsAnswersSectionHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: ((circleJoinQuestionModel?.questionHeight ?? 0) + 30)))
|
|
|
+ view.index = section
|
|
|
+ view.circleJoinQuestionModel = circleJoinQuestionModel
|
|
|
+ return view
|
|
|
}
|
|
|
|
|
|
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
|