//
|
// HomeListenGame_1_VC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/28.
|
//
|
|
import UIKit
|
|
let Games_1_Refresh_Noti = Notification.Name.init("Games_1_Refresh_Noti")
|
|
class HomeListenGame_1_VC: BaseVC {
|
|
private var viewModel = FightAnswerViewModel()
|
private var listen1Model:Listen1Model!
|
|
var rootViewModel:HomeListenFightViewModel!
|
|
private var totalCount:Int = 1 //游戏的总数量
|
|
private lazy var label_class:UILabel = {
|
let label = UILabel()
|
label.textColor = .white
|
label.text = "1"
|
label.textAlignment = .center
|
label.font = UIFont.init(name: "Impact", size: 21)
|
return label
|
|
}()
|
|
private lazy var label_hint:UILabel = {
|
let label = UILabel()
|
label.textColor = UIColor(hexStr: "#EE1111")
|
label.text = "请在10s内选择答案!"
|
label.textAlignment = .center
|
label.isHidden = true
|
label.font = .systemFont(ofSize: 14, weight: .medium)
|
return label
|
}()
|
|
private lazy var view_studyHandleView:VoiceHandleView = {
|
let studyHandleView = VoiceHandleView()
|
return studyHandleView
|
}()
|
|
private lazy var collectionView:UICollectionView = {
|
let flowLayout = UICollectionViewFlowLayout()
|
let w = (JQ_ScreenW - 35 * 2 - 12) / 4.0
|
flowLayout.itemSize = CGSize(width: w, height: w * 0.73)
|
flowLayout.minimumInteritemSpacing = 3
|
flowLayout.minimumLineSpacing = 13
|
flowLayout.scrollDirection = .vertical
|
let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
collection.contentInset = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 35)
|
collection.register(UINib(nibName: "ListenFight_Game_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_Game_CCell")
|
return collection
|
}()
|
|
private var timer:Timer?
|
private var times:Int = 10
|
private var voicePlayer = VoicePlayer.share()
|
|
private var answerSet = Set<Listen1SubModel>()
|
private var currentAnswer:Listen1SubModel?{
|
didSet{
|
if let v = currentAnswer{
|
view_studyHandleView.playUrl = v.correct
|
voicePlayer.playerAt(url: v.correct)
|
viewModel.answerType.accept(.none)
|
}
|
}
|
}
|
|
required init(listen1Model:Listen1Model){
|
super.init(nibName: nil, bundle: nil)
|
self.listen1Model = listen1Model
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
voicePlayer.delegate = self
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
timer?.invalidate()
|
voicePlayer.delegate = nil
|
voicePlayer.playerEnd()
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
#if DEBUG
|
listen1Model?.data?.time = 60
|
#endif
|
|
if listen1Model != nil{
|
times = listen1Model?.data?.time ?? 10
|
collectionView.reloadData()
|
label_hint.isHidden = false
|
label_hint.text = "准备听题"
|
|
for v in listen1Model?.subjectList ?? []{
|
answerSet.insert(v)
|
}
|
|
print("--->开始答题:剩余:\(answerSet.count)")
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+3) {
|
self.currentAnswer = self.answerSet.randomElement() //随机
|
if self.timer == nil{self.startTimer()}
|
}
|
}
|
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
let view_class_title = UIView()
|
view_class_title.jq_cornerRadius = 16
|
view_class_title.backgroundColor = UIColor(hexStr: "#FBCF0F")
|
view.addSubview(view_class_title)
|
view_class_title.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(40)
|
make.left.equalToSuperview().offset(40)
|
make.height.equalTo(32)
|
make.width.greaterThanOrEqualTo(32)
|
}
|
|
view_class_title.addSubview(label_class)
|
label_class.text = "\(totalCount)"
|
label_class.snp.makeConstraints { make in
|
make.left.equalTo(11)
|
make.right.equalTo(-12)
|
make.centerY.equalToSuperview()
|
}
|
|
|
view.addSubview(view_studyHandleView)
|
view_studyHandleView.snp.makeConstraints { make in
|
make.left.equalTo(view_class_title.snp.right).offset(12)
|
make.centerY.equalTo(view_class_title)
|
make.width.equalTo(159)
|
make.height.equalTo(52)
|
}
|
|
view.addSubview(label_hint)
|
label_hint.snp.makeConstraints { make in
|
make.left.equalTo(view_studyHandleView.snp.right).offset(23)
|
make.centerY.equalTo(view_class_title)
|
make.height.equalTo(20)
|
}
|
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.showsVerticalScrollIndicator = false
|
collectionView.jq_addShadows(shadowColor: UIColor.black.withAlphaComponent(0.1), corner: 8, radius: 10, offset: CGSize(width: 0, height: 2), opacity: 1)
|
collectionView.backgroundColor = .clear
|
view.addSubview(collectionView)
|
collectionView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(99)
|
make.left.right.equalToSuperview()
|
make.bottom.equalToSuperview()
|
}
|
|
view.layoutIfNeeded()
|
}
|
|
|
func startTimer(){
|
if timer == nil{
|
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(runloopTime), userInfo: nil, repeats: true)
|
}
|
timer?.fire()
|
RunLoop.current.add(timer!, forMode: .default)
|
}
|
|
@objc private func runloopTime(){
|
print("进入。。。")
|
times -= 1
|
label_hint.text = "请在\(times)s内选择答案!"
|
|
if times == 0{
|
timer?.fireDate = .distantFuture
|
if let c = currentAnswer{
|
answerSet.remove(c)
|
}
|
currentAnswer = answerSet.randomElement() //随机
|
times = listen1Model?.data?.time ?? 0
|
timer?.fireDate = .distantPast
|
totalCount += 1
|
rootViewModel.errorNum += 1
|
label_class.text = "\(totalCount)"
|
|
//答题完成
|
if self.answerSet.count == 0{completeQuestion()}
|
}
|
}
|
|
private func answerQuestion(){
|
view.layoutIfNeeded()
|
|
guard let row = viewModel.selectIndex.value?.row else { alertError(msg: "请选择");return }
|
|
var answerType:Fight_lessonType = .none
|
|
if currentAnswer?.id == listen1Model?.subjectList[row].id{
|
answerType = .success
|
}else{
|
answerType = .fail
|
}
|
switch answerType {
|
case .success:
|
viewModel.answerType.accept(.success)
|
collectionView.reloadData()
|
rootViewModel.correctNum += 1
|
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: viewModel.selectIndex.value!) as? ListenFight_Game_CCell{
|
let newRect = cell.contentView.convert(cell.bounds, from: self.collectionView)
|
let x = abs(newRect.origin.x) + self.collectionView.contentInset.left + 5
|
let y = abs(newRect.origin.y) + 99 + 5
|
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
|
let copyView = view_studyHandleView.copyView()
|
view.addSubview(copyView)
|
|
UIView.animate(withDuration: 0.5) {
|
copyView.frame = CGRect(x: x, y: y, width: layout.itemSize.width - 10, height: 40)
|
} completion: { _ in
|
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
|
// self.collectionView.reloadData()
|
self.voicePlayer.playerAt(url: self.currentAnswer?.correct)
|
self.timer?.fireDate = .distantFuture
|
//答题完成
|
if self.answerSet.count == 0{
|
self.completeQuestion()
|
}
|
}
|
}
|
}
|
|
case .fail:
|
rootViewModel.errorNum += 1
|
viewModel.answerType.accept(.fail)
|
collectionView.reloadData()
|
DispatchQueue.main.asyncAfter(deadline: .now()+3) {
|
self.currentAnswer = self.answerSet.randomElement()
|
}
|
case .none:
|
break
|
}
|
}
|
|
private func completeQuestion(){
|
print("答题完成")
|
self.label_hint.text = "答题完成"
|
self.timer?.invalidate()
|
self.rootViewModel.answerItems[0] = self.listen1Model
|
NotificationCenter.default.post(name: NextLession_Noti, object: ["gameId":listen1Model.data!.id,"gameIntegral":listen1Model.data!.integral])
|
}
|
}
|
|
extension HomeListenGame_1_VC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
let model = listen1Model!.subjectList[indexPath.row]
|
if answerSet.contains(model){
|
viewModel.selectIndex.accept(indexPath)
|
answerQuestion()
|
}
|
}
|
}
|
|
extension HomeListenGame_1_VC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let model = listen1Model!.subjectList[indexPath.row]
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: indexPath) as! ListenFight_Game_CCell
|
if viewModel.selectIndex.value == indexPath{
|
cell.setState(state: viewModel.answerType.value)
|
}else{
|
cell.setState(state: .none)
|
}
|
|
cell.setModel(model)
|
return cell
|
}
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return listen1Model?.subjectList.count ?? 0
|
}
|
}
|
|
extension HomeListenGame_1_VC:VoicePlayerDelegate{
|
func playComplete() {
|
view_studyHandleView.resetView()
|
|
if viewModel.answerType.value == .success{
|
self.label_hint.text = "准备请听下一题"
|
DispatchQueue.main.asyncAfter(deadline: .now()+3) {
|
self.times = self.listen1Model?.data?.time ?? 10
|
#if DEBUG
|
self.times = 60
|
#endif
|
self.totalCount += 1
|
self.rootViewModel.correctNum += 1
|
self.label_class.text = "\(self.totalCount)"
|
self.answerSet.remove(self.currentAnswer!)
|
self.currentAnswer = self.answerSet.randomElement()
|
self.viewModel.answerType.accept(.none)
|
print("--->下一题:\(self.currentAnswer?.id ?? 0) 剩余\(self.answerSet.count) 计数:\(self.totalCount)")
|
self.timer?.fireDate = .distantPast
|
}
|
}
|
}
|
|
func playing() {
|
view_studyHandleView.playing()
|
}
|
|
}
|