//
|
// HomeListenFight_lesson_5_VC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/28.
|
//
|
|
import UIKit
|
|
class HomeListenFight_lesson_5_VC: BaseVC {
|
|
private var viewModel = FightAnswerViewModel()
|
private var listenNewModel:ListenNewModel!
|
private var page:Int!
|
private var answterCount:Int = 0 //回答计数,用于确定角标
|
private var playVoiceAt:Int? //播放声音的View
|
private var playVoiceRealAt:Int? //播放声音的View -被乱序后,真实Index
|
var rootViewModel:HomeListenFightViewModel!
|
private var voicePlayer = VoicePlayer.share()
|
private var isListen:Bool = false
|
|
private lazy var collectionView:UICollectionView = {
|
let flowLayout = UICollectionViewFlowLayout()
|
let w = (JQ_ScreenW - 189 * 2 - 18) / 2.0
|
flowLayout.itemSize = CGSize(width: w, height: w * 0.70)
|
flowLayout.minimumInteritemSpacing = 15
|
flowLayout.minimumLineSpacing = 15
|
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.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
|
collection.isScrollEnabled = false
|
return collection
|
}()
|
|
private lazy var stackView:UIStackView = {
|
let sta = UIStackView()
|
sta.spacing = 89
|
sta.distribution = .equalSpacing
|
sta.axis = .horizontal
|
return sta
|
}()
|
|
private lazy var label_hint:UILabel = {
|
let label = UILabel()
|
label.font = .systemFont(ofSize: 18, weight: .medium)
|
label.textColor = .black
|
label.text = "语音对应内容"
|
label.textAlignment = .center
|
return label
|
}()
|
|
required init(page:Int,listenNewModel:ListenNewModel){
|
super.init(nibName: nil, bundle: nil)
|
self.page = page
|
self.listenNewModel = listenNewModel
|
// self.listen1Model.subjectList.shuffle()
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
collectionView.reloadData()
|
}
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
voicePlayer.delegate = self
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
voicePlayer.delegate = nil
|
VoicePlayer.share().playerInterrupt()
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
view.addSubview(stackView)
|
stackView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(19)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(52)
|
}
|
|
view.addSubview(label_hint)
|
label_hint.snp.makeConstraints { make in
|
make.left.right.equalToSuperview()
|
make.top.equalTo(stackView.snp.bottom).offset(6)
|
make.height.equalTo(0)
|
}
|
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.showsVerticalScrollIndicator = false
|
collectionView.backgroundColor = .clear
|
view.addSubview(collectionView)
|
collectionView.snp.makeConstraints { make in
|
make.top.equalTo(self.label_hint.snp.bottom).offset(11)
|
make.left.equalTo(189)
|
make.width.equalTo(JQ_ScreenW - 189 * 2)
|
make.bottom.equalToSuperview()
|
}
|
|
showHintText(false)
|
|
setAnswerStackView()
|
}
|
|
override func viewDidLayoutSubviews() {
|
super.viewDidLayoutSubviews()
|
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
|
|
let w = (collectionView.size.width - flowLayout.minimumLineSpacing) / 2
|
let h = (collectionView.size.height - flowLayout.minimumInteritemSpacing) / 2.0
|
|
if flowLayout.itemSize.width != w || flowLayout.itemSize.height != h{
|
flowLayout.itemSize = CGSize(width: w, height: h)
|
collectionView.reloadData()
|
}
|
}
|
|
override func setRx() {
|
|
}
|
|
func restore(){
|
viewModel.answerType.accept(.none)
|
answterCount = 0
|
playVoiceAt = nil
|
playVoiceRealAt = nil
|
|
for subV in view.subviews{
|
if subV is VoiceHandleView{
|
subV.removeFromSuperview()
|
}
|
}
|
setUI()
|
collectionView.reloadData()
|
}
|
|
private func setAnswerStackView(){
|
|
for subView in stackView.arrangedSubviews{
|
subView.removeFromSuperview()
|
}
|
|
if !view.subviews.contains(stackView){
|
view.addSubview(stackView)
|
stackView.snp.makeConstraints { make in
|
make.right.equalToSuperview().offset(-14)
|
make.centerY.equalToSuperview()
|
}
|
}
|
|
var tempArray = [VoiceHandleView]()
|
for (i,value) in listenNewModel.subjectList[page].enumerated(){
|
let answerView = VoiceHandleView()
|
answerView.listenType = .lesson5
|
answerView.tag = 1000+i
|
answerView.playAt {[weak self] index in
|
print("--->\(index)--\(answerView.frame.origin.x / 248)")
|
self?.playVoiceRealAt = Int(answerView.frame.origin.x) / 248
|
self?.playVoiceAt = index - 1000
|
self?.showHintText(true)
|
}
|
answerView.playUrl = value.correct
|
answerView.snp.makeConstraints { make in
|
make.width.equalTo(159)
|
make.height.equalTo(52)
|
}
|
|
UIView.animate(withDuration: 0.05 + Double(i)) {
|
answerView.alpha = 1
|
}
|
tempArray.append(answerView)
|
}
|
tempArray.shuffle()
|
stackView.addArrangedSubviews(tempArray)
|
}
|
|
private func showHintText(_ state:Bool){
|
if let at = playVoiceAt{
|
label_hint.text = listenNewModel.subjectList[page][at].name
|
UIView.animate(withDuration: 0.5) {
|
if state{
|
self.label_hint.snp.remakeConstraints { make in
|
make.left.right.equalToSuperview()
|
make.top.equalTo(self.stackView.snp.bottom).offset(6)
|
make.height.equalTo(25)
|
}
|
}else{
|
self.label_hint.snp.remakeConstraints { make in
|
make.left.right.equalToSuperview()
|
make.top.equalTo(self.stackView.snp.bottom).offset(6)
|
make.height.equalTo(0)
|
}
|
}
|
self.view.layoutIfNeeded()
|
}
|
}
|
}
|
}
|
|
extension HomeListenFight_lesson_5_VC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
if isListen == false{
|
alertError(msg: "请先听题");return
|
}
|
isListen = false
|
|
viewModel.selectIndex.accept(indexPath)
|
|
let answer = listenNewModel.subjectList[page][playVoiceAt!]
|
let selectAnswer = listenNewModel.subjectList[page][indexPath.row]
|
|
var answerType:Fight_lessonType = .none
|
if answer.id == selectAnswer.id{
|
answerType = .success
|
voicePlayer.playSuccessVoice()
|
let teamId = listenNewModel.data?.id.components(separatedBy: ",")[page]
|
rootViewModel.insertCorrectAnswer(teamId: teamId, answerId: selectAnswer.id)
|
}else{
|
answerType = .fail
|
voicePlayer.playFailVoice()
|
}
|
|
let tempSubV = stackView.arrangedSubviews[self.playVoiceRealAt!] as! VoiceHandleView
|
|
switch answerType {
|
case .success:
|
answterCount += 1
|
rootViewModel.correctNum += 1
|
viewModel.answerType.accept(.success)
|
|
let copyView = tempSubV.copyView()
|
copyView.listenType = .lesson5
|
copyView.playUrl = selectAnswer.correct
|
let newRect = tempSubV.convert(tempSubV.bounds, to: self.view)
|
copyView.frame = CGRect(origin: newRect.origin, size: CGSize(width: 159, height: 52))
|
self.view.addSubview(copyView)
|
tempSubV.alpha = 0
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
|
|
//获取Cell的顶部试图
|
let flowLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
|
|
if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as? ListenFight_lesson_1_CCell{
|
var newRect1 = cell.convert(cell.bounds, to: self.collectionView)
|
newRect1.origin.x += (collectionView.frame.origin.x + 5)
|
newRect1.origin.y += 94 + 25
|
|
UIView.animateKeyframes(withDuration: 0.4, delay: 0,options: .calculationModeLinear) {
|
copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: flowLayout.itemSize.width - 10 , height: 40))
|
|
}completion: { _ in
|
copyView.playingAction()
|
self.playVoiceRealAt = nil
|
self.playVoiceAt = nil
|
self.collectionView.reloadData()
|
}
|
}
|
}
|
|
case .fail:
|
viewModel.answerType.accept(.fail)
|
rootViewModel.errorNum += 1
|
collectionView.reloadData()
|
case .none:
|
break
|
}
|
}
|
}
|
|
extension HomeListenFight_lesson_5_VC:UICollectionViewDelegateFlowLayout{
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
|
return CGSize.zero
|
}
|
}
|
|
extension HomeListenFight_lesson_5_VC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let model = listenNewModel.subjectList[page][indexPath.row]
|
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: 8, radius: 3, offset: CGSize(width: 0, height: 1), opacity: 1)
|
if viewModel.selectIndex.value == indexPath{
|
cell.setState(state: viewModel.answerType.value)
|
}else{
|
cell.setState(state: .none)
|
}
|
cell.setListen1SubModel(model)
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
|
return reusableView
|
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return listenNewModel.subjectList[page].count
|
}
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
return 1
|
}
|
|
}
|
|
extension HomeListenFight_lesson_5_VC:VoicePlayerDelegate{
|
func playComplete() {
|
view.isUserInteractionEnabled = true
|
isListen = true
|
for subV in stackView.arrangedSubviews as! [VoiceHandleView]{
|
subV.resetView()
|
}
|
|
if viewModel.answerType.value == .success{
|
let v = rootViewModel.answerCount.value + 1
|
rootViewModel.answerCount.accept(v)
|
viewModel.answerType.accept(.none)
|
}
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+0.4) {
|
if self.answterCount >= 4{
|
// self.rootViewModel.answerItems[self.page] = self.listenNewModel.subjectList[self.page]
|
self.voicePlayer.playerEnd()
|
NotificationCenter.default.post(name: NextLession_Noti, object: nil)
|
}
|
}
|
}
|
|
func playing() {
|
view.isUserInteractionEnabled = false
|
}
|
|
}
|