//
|
// HomeListenFight_lesson_2_VC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/24.
|
//
|
|
import UIKit
|
import QMUIKit
|
|
class HomeListenFight_lesson_2_VC: BaseVC {
|
|
private var viewModel = FightAnswerViewModel()
|
private var listenNewModel:ListenNewModel!
|
private var page:Int!
|
var rootViewModel:HomeListenFightViewModel!
|
var teamScheduleModel:TeamScheduleModel?
|
private var tempViews = [StudyHandleView]()
|
|
private var playedIndex = Set<Int>() //已经播放过的view
|
private var voicePlayer = VoicePlayer.share()
|
private var isAnsterModel = Set<Listen1SubModel>()
|
private var studieds = 0
|
private var handleClouse:(()->Void)?
|
|
private lazy var stackView:UIStackView = {
|
let stackView = UIStackView()
|
stackView.spacing = 78
|
stackView.distribution = .equalSpacing
|
return stackView
|
}()
|
|
private lazy var collectionView:UICollectionView = {
|
let flowLayout = UICollectionViewFlowLayout()
|
let w = (JQ_ScreenW - 194 * 2 - 25) / 2
|
flowLayout.itemSize = CGSize(width: w, height: w * 0.745)
|
flowLayout.minimumLineSpacing = 25
|
flowLayout.minimumInteritemSpacing = 25
|
flowLayout.scrollDirection = .vertical
|
let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
collection.register(UINib(nibName: "ListenFight_lesson_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_lesson_1_CCell")
|
collection.isScrollEnabled = false
|
return collection
|
}()
|
|
|
required init(page:Int,listenNewModel:ListenNewModel){
|
super.init(nibName: nil, bundle: nil)
|
self.page = page
|
self.listenNewModel = listenNewModel
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
navigationItem.titleView = UIView()
|
|
|
//回传记录,始终保持答题进度
|
if let team = teamScheduleModel{
|
for teamId in team.teamIds{
|
for v in listenNewModel.subjectList[page]{
|
if team.topicIds.contains(v.id){
|
rootViewModel.insertCorrectAnswer(teamId: "\(teamId)", answerId: v.id)
|
}
|
}
|
}
|
}
|
|
rootViewModel.currentPage.subscribe(onNext: {[unowned self] cpage in
|
if cpage == self.page{
|
self.restore()
|
self.resetStackView()
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
VoicePlayer.share().delegate = self
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
VoicePlayer.share().delegate = nil
|
VoicePlayer.share().playerInterrupt()
|
}
|
|
func restore(){
|
studieds = 0
|
playedIndex.removeAll()
|
tempViews.removeAll()
|
|
for subView in view.subviews{
|
if subView is StudyHandleView{
|
subView.removeFromSuperview()
|
}
|
}
|
setUI()
|
collectionView.reloadData()
|
|
}
|
|
override func viewDidLayoutSubviews() {
|
super.viewDidLayoutSubviews()
|
let flowLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
|
let w = (self.collectionView.size.width - flowLayout.minimumLineSpacing) / 2
|
let h = (self.collectionView.size.height - flowLayout.minimumInteritemSpacing) / 2
|
|
if flowLayout.itemSize.width != w || flowLayout.itemSize.height != h{
|
flowLayout.itemSize = CGSize(width: w, height: h)
|
collectionView.reloadData()
|
}
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
viewModel.selectIndex.accept(IndexPath(row: 0, section: 0))
|
|
if !view.subviews.contains(collectionView){
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.backgroundColor = UIColor(hexStr: "#C3BFB3")
|
view.addSubview(collectionView)
|
}
|
|
collectionView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(101)
|
make.left.equalTo(194)
|
make.right.equalTo(-194)
|
make.bottom.equalToSuperview()
|
}
|
|
if !view.subviews.contains(stackView){
|
view.addSubview(stackView)
|
}
|
|
stackView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(24)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(40)
|
make.width.greaterThanOrEqualTo(125)
|
}
|
|
addStackView()
|
}
|
|
private func addStackView(){
|
|
for subV in stackView.arrangedSubviews{
|
subV.removeFromSuperview()
|
}
|
tempViews.removeAll()
|
|
for index in 0...2{
|
let handleView = StudyHandleView.jq_loadNibView()
|
handleView.listenType = .lesson2
|
handleView.view_choose.alpha = 0
|
handleView.btn_choose.tag = 10 + index
|
handleView.tag = 20 + index
|
|
let model = listenNewModel.subjectList[page][studieds]
|
|
if index == 0{
|
handleView.vioceSoundUrl = model.correct
|
}
|
|
if index == 1{
|
handleView.vioceSoundUrl = model.error.components(separatedBy: ",").first
|
}
|
|
if index == 2{
|
handleView.vioceSoundUrl = model.error.components(separatedBy: ",").last
|
}
|
|
handleView.playAt {[weak self] index in
|
self?.playedIndex.insert(index)
|
}
|
|
|
handleView.chooseClouse {[weak self] btn in
|
guard let weakSelf = self else { return }
|
// if weakSelf.playedIndex.count < 3{
|
// handleView.btn_choose.isSelected = false
|
// alertError(msg: "请听完");return
|
// }
|
handleView.view_choose.alpha = 1
|
var lessionType:Fight_lessonType = .none
|
if handleView.vioceSoundUrl == weakSelf.listenNewModel.subjectList[weakSelf.page][weakSelf.studieds].correct{
|
lessionType = .success
|
weakSelf.voicePlayer.playSuccessVoice()
|
let teamId = weakSelf.listenNewModel.data?.id.components(separatedBy: ",")[weakSelf.page]
|
let answerId = weakSelf.listenNewModel.subjectList[weakSelf.page][weakSelf.studieds].id
|
weakSelf.rootViewModel.insertCorrectAnswer(teamId: teamId, answerId: answerId)
|
}else{
|
lessionType = .fail
|
// 重置按钮至最初样式
|
weakSelf.playedIndex.removeAll()
|
for sub in weakSelf.stackView.arrangedSubviews as! [StudyHandleView]{
|
sub.btn_pay.isEnabled = true
|
sub.btn_choose.isEnabled = true
|
sub.resetView()
|
sub.view_choose.alpha = 0
|
}
|
weakSelf.voicePlayer.playFailVoice()
|
}
|
|
switch lessionType {
|
case .success:
|
weakSelf.rootViewModel.correctNum += 1
|
handleView.btn_choose.isEnabled = false
|
handleView.btn_state.setImage(UIImage(named: "icon_success_small"), for: .normal)
|
UIView.animate(withDuration: 0.5) {
|
handleView.btn_state.alpha = 1
|
}
|
|
let i = ceil(handleView.x / 300).int
|
if let cell = self?.collectionView.cellForItem(at: IndexPath(row: weakSelf.studieds, section: 0)) as? ListenFight_lesson_1_CCell{
|
self?.viewModel.answerType.accept(.success)
|
self?.answerSuccess(cell,index: i)
|
|
if let data = self?.listenNewModel.list[self!.page]{
|
Services.answerQuestion(id: data.id, status: 2).subscribe(onNext: {_ in
|
self?.handleClouse?()
|
}).disposed(by: weakSelf.disposeBag)
|
}
|
}
|
case .fail:
|
weakSelf.rootViewModel.errorNum += 1
|
handleView.btn_state.setImage(UIImage(named: "icon_waring_small"), for: .normal)
|
UIView.animate(withDuration: 0.5) {
|
handleView.btn_state.alpha = 1
|
}
|
UIView.animate(withDuration: 0.5, delay: 1.2, options: .layoutSubviews) {
|
handleView.btn_pay.alpha = 1
|
handleView.btn_voice.alpha = 1
|
handleView.btn_state.alpha = 0
|
} completion: { _ in
|
handleView.btn_choose.isSelected = false
|
}
|
|
if let data = self?.listenNewModel.list[self!.page]{
|
Services.answerQuestion(id: data.id, status: 3).subscribe(onNext: {_ in
|
self?.handleClouse?()
|
}).disposed(by: weakSelf.disposeBag)
|
}
|
|
|
default:
|
handleView.btn_state.setImage(nil, for: .normal)
|
}
|
}
|
|
handleView.snp.makeConstraints { make in
|
make.height.equalTo(52)
|
make.width.greaterThanOrEqualTo(221)
|
}
|
tempViews.append(handleView)
|
tempViews.shuffle()
|
}
|
|
stackView.addArrangedSubviews(tempViews)
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+1) {
|
let temp = self.tempViews.first
|
temp?.isplaying()
|
self.voicePlayer.playerAt(url: temp?.vioceSoundUrl ?? "")
|
self.playedIndex.insert(0)
|
self.collectionView.reloadData()
|
}
|
}
|
|
private func answerSuccess(_ cell:ListenFight_lesson_1_CCell,index:Int){
|
let studyHandleView = stackView.arrangedSubviews[index] as! StudyHandleView
|
let bounds = studyHandleView.convert(studyHandleView.bounds, to: self.view)
|
|
//copy试图放在上面进行覆盖
|
let copyHandleView = studyHandleView.copyView()
|
copyHandleView.view_choose.isHidden = true
|
copyHandleView.listenType = .lesson2
|
copyHandleView.resetView()
|
copyHandleView.alpha = 1
|
copyHandleView.vioceSoundUrl = listenNewModel.subjectList[page][self.viewModel.selectIndex.value!.row].correct
|
copyHandleView.jq_cornerRadius = 0
|
view.addSubview(copyHandleView)
|
view.layoutIfNeeded()
|
|
copyHandleView.snp.makeConstraints { make in
|
make.top.equalToSuperview().offset(bounds.origin.y)
|
make.left.equalToSuperview().offset(bounds.origin.x)
|
make.width.equalTo(159)
|
make.height.equalTo(52)
|
}
|
|
view.layoutIfNeeded()
|
|
let v = cell.view_topHandle.convert(cell.bounds, to: self.view)
|
UIView.animate(withDuration: 0.6) {
|
copyHandleView.snp.updateConstraints { make in
|
make.top.equalTo(self.view).offset(v.origin.y)
|
make.left.equalToSuperview().offset(v.origin.x)
|
make.width.equalTo(v.size.width - 10)
|
make.height.equalTo(40)
|
}
|
self.view.layoutIfNeeded()
|
}completion: { complete in
|
if complete{
|
self.studieds += 1
|
//答完题了
|
if self.studieds == 4{
|
NotificationCenter.default.post(name: NextLession_Noti, object: nil)
|
return
|
}
|
|
self.resetStackView()
|
}
|
}
|
}
|
|
private func resetStackView(){
|
playedIndex.removeAll()
|
collectionView.reloadData()
|
addStackView()
|
}
|
|
override func setRx() {
|
NotificationCenter.default.rx.notification(ResetLession_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self]_ in
|
self?.restore()
|
self?.viewDidLoad()
|
}).disposed(by: disposeBag)
|
}
|
|
func handleClouseAction(clouse:@escaping ()->Void){
|
self.handleClouse = clouse
|
}
|
}
|
|
extension HomeListenFight_lesson_2_VC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
viewModel.selectIndex.accept(indexPath)
|
}
|
}
|
|
extension HomeListenFight_lesson_2_VC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as! ListenFight_lesson_1_CCell
|
cell.jq_addShadows(shadowColor: .black.withAlphaComponent(0.31), corner: 5, radius: 5, offset: CGSize(width: 0, height: 1), opacity: 1)
|
|
if indexPath.row == studieds{
|
cell.backgroundColor = .orange
|
}else{
|
cell.backgroundColor = .white
|
}
|
cell.label_title.isHidden = true
|
|
cell.setListen1SubModel(listenNewModel.subjectList[page][indexPath.row])
|
cell.label_title.text = listenNewModel.subjectList[page][indexPath.row].name
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return listenNewModel.subjectList[page].count
|
}
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
return 1
|
}
|
}
|
|
extension HomeListenFight_lesson_2_VC:VoicePlayerDelegate{
|
func playing() {
|
view.isUserInteractionEnabled = false
|
print("正在播放")
|
//正在播放中,其他播放按钮先禁止
|
for sub in stackView.arrangedSubviews as! [StudyHandleView]{
|
sub.btn_pay.isEnabled = false
|
sub.btn_choose.isEnabled = false
|
}
|
}
|
|
func playComplete() {
|
collectionView.reloadData()
|
view.isUserInteractionEnabled = true
|
//对已经播放过的View,进行刷新
|
for sub in stackView.arrangedSubviews as! [StudyHandleView]{
|
if sub.alpha == 0{
|
sub.isplaying()
|
sub.voicePlayer.playerAt(url: sub.vioceSoundUrl)
|
return
|
}else{
|
sub.btn_pay.isEnabled = true
|
sub.btn_choose.isEnabled = true
|
sub.resetView()
|
}
|
|
// if playedIndex.contains(sub.tag){
|
// sub.view_choose.alpha = playedIndex.contains(sub.tag) ? 1:0
|
// }
|
}
|
|
|
// for sub in view.subviews{
|
// if let v = sub as? StudyHandleView{
|
// v.resetView()
|
// }
|
// }
|
|
// for v in tempViews{
|
// if v.isplayend{
|
// v.resetView()
|
// }
|
// }
|
|
// for (i,v) in tempViews.enumerated(){
|
// if v.isplayend == false{
|
// self.playedIndex.insert(i)
|
// v.isplaying()
|
// voicePlayer.playerAt(url: v.vioceSoundUrl)
|
// break
|
// }
|
// }
|
|
|
// if viewModel.answerType.value == .success{
|
// let v = rootViewModel.answerCount.value
|
// rootViewModel.answerCount.accept(v + 1)
|
// viewModel.answerType.accept(.none)
|
//
|
// for sub in stackView.arrangedSubviews as! [StudyHandleView]{
|
// sub.btn_pay.isEnabled = false
|
// sub.btn_choose.isEnabled = false
|
// }
|
//
|
// DispatchQueue.main.asyncAfter(deadline: .now()+2) {
|
// self.resetStackView()
|
// }
|
// }
|
|
}
|
}
|