//
|
// HomeListenStory_1_VC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/30.
|
//
|
|
import UIKit
|
|
class HomeListenStory_1_VC: BaseVC {
|
|
private var viewModel = FightAnswerViewModel()
|
|
private var listen1Model:Listen1Model!
|
private var page:Int!
|
var rootViewModel:HomeListenFightViewModel!
|
|
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 - 235 * 2)
|
flowLayout.itemSize = CGSize(width: w, height: w * 0.6666)
|
flowLayout.minimumInteritemSpacing = 0
|
flowLayout.scrollDirection = .vertical
|
let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
collection.contentInset = UIEdgeInsets(top: 130, left: 0, bottom: 0, right: 0)
|
collection.register(UINib(nibName: "SimpleImageCCell", bundle: nil), forCellWithReuseIdentifier: "_SimpleImageCCell")
|
return collection
|
}()
|
|
private var voicePlayer = VoicePlayer.share()
|
|
private var isPlayedSet = Set<Int>()
|
|
required init(page:Int,listen1Model:Listen1Model){
|
super.init(nibName: nil, bundle: nil)
|
self.page = page
|
self.listen1Model = listen1Model
|
}
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
voicePlayer.delegate = self
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
voicePlayer.delegate = nil
|
voicePlayer.playerInterrupt()
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
viewModel.selectIndex.accept(IndexPath(row: 0, section: 0))
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.isScrollEnabled = false
|
collectionView.showsVerticalScrollIndicator = false
|
collectionView.backgroundColor = .clear
|
collectionView.jq_addShadows(shadowColor: UIColor.black.withAlphaComponent(0.1), corner: 8, radius: 10, offset: CGSize(width: 0, height: 2), opacity: 1)
|
view.addSubview(collectionView)
|
collectionView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(0)
|
make.left.equalTo(164)
|
make.width.equalTo(JQ_ScreenW - 164 * 2)
|
make.bottom.equalToSuperview()
|
}
|
|
view.addSubview(stackView)
|
stackView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(24)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(52)
|
make.width.greaterThanOrEqualTo(100)
|
}
|
|
stackView.removeArrangedSubviews()
|
|
var tempHandleViews = [StudyHandleView]()
|
for index in 0...2{
|
let handleView = StudyHandleView.jq_loadNibView()
|
handleView.listenType = .lesson2
|
|
if index == 0{
|
handleView.vioceSoundUrl = listen1Model.storyList[page].correct
|
}else if index == 1{
|
handleView.vioceSoundUrl = listen1Model.storyList[page].error.components(separatedBy: ",").first
|
}else{
|
handleView.vioceSoundUrl = listen1Model.storyList[page].error.components(separatedBy: ",").last
|
}
|
handleView.btn_choose.tag = 10 + index
|
handleView.tag = 20 + index
|
handleView.chooseClouse {[weak self] btn in
|
guard let weakSelf = self else { return }
|
if weakSelf.isPlayedSet.count != 3{
|
handleView.btn_choose.isSelected = false
|
alertError(msg: "请听完");return
|
}
|
|
var lessionType:Fight_lessonType = .none
|
if weakSelf.listen1Model.storyList[weakSelf.page].correct == handleView.vioceSoundUrl{
|
lessionType = .success
|
weakSelf.voicePlayer.playSuccessVoice()
|
}else{
|
lessionType = .fail
|
weakSelf.voicePlayer.playFailVoice()
|
}
|
|
switch lessionType {
|
case .success:
|
weakSelf.viewModel.answerType.accept(.success)
|
weakSelf.rootViewModel.correctNum += 1
|
handleView.btn_choose.isSelected = true
|
handleView.btn_state.setImage(UIImage(named: "icon_success_small"), for: .normal)
|
UIView.animate(withDuration: 0.5) {
|
handleView.btn_state.alpha = 1
|
}
|
|
UIView.animate(withDuration: 0.5, delay: 3) {
|
handleView.btn_state.alpha = 0
|
}
|
|
weakSelf.voicePlayer.playerAt(url: handleView.vioceSoundUrl)
|
let copyView = handleView.copyView()
|
copyView.btn_choose.alpha = 0
|
copyView.listenType = .lesson2
|
copyView.resetView()
|
copyView.isplaying()
|
let newRect = handleView.convert(handleView.bounds, to: weakSelf.view)
|
weakSelf.view.addSubview(copyView)
|
copyView.snp.makeConstraints { make in
|
make.top.equalTo(newRect.origin.y)
|
make.left.equalTo(newRect.origin.x)
|
make.width.equalTo(221)
|
make.height.equalTo(52)
|
}
|
weakSelf.view.layoutIfNeeded()
|
|
let rect = weakSelf.collectionView.convert(weakSelf.collectionView.bounds, to: weakSelf.view)
|
|
UIView.animate(withDuration: 0.5) {
|
copyView.snp.remakeConstraints { make in
|
make.top.equalTo(rect.origin.y + 135)
|
make.centerX.equalToSuperview().offset(30)
|
make.width.equalTo((JQ_ScreenW - 210 * 2))
|
make.height.equalTo(52)
|
}
|
weakSelf.view.layoutIfNeeded()
|
}
|
|
case .fail:
|
weakSelf.isPlayedSet.removeAll()
|
for subV in weakSelf.stackView.arrangedSubviews as! [StudyHandleView]{
|
subV.view_choose.alpha = 0
|
subV.btn_choose.isSelected = false
|
}
|
|
weakSelf.viewModel.answerType.accept(.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
|
}
|
DispatchQueue.main.asyncAfter(deadline:.now()+2) {
|
UIView.animate(withDuration: 0.5) {
|
handleView.btn_state.alpha = 0
|
handleView.btn_choose.isSelected = false
|
}
|
}
|
|
|
default:
|
handleView.btn_state.setImage(nil, for: .normal)
|
}
|
}
|
|
handleView.playAt {[weak self] index in
|
self?.isPlayedSet.insert(index)
|
print("播放过的\(index)")
|
}
|
|
handleView.snp.makeConstraints { make in
|
make.height.equalTo(52)
|
make.width.greaterThanOrEqualTo(221)
|
}
|
tempHandleViews.append(handleView)
|
}
|
|
tempHandleViews.shuffle()
|
stackView.addArrangedSubviews(tempHandleViews)
|
}
|
|
func restore(){
|
viewModel.answerType.accept(.none)
|
isPlayedSet.removeAll()
|
for v in stackView.arrangedSubviews as! [StudyHandleView]{
|
v.resetView()
|
v.view_choose.alpha = 0
|
}
|
setUI()
|
collectionView.reloadData()
|
}
|
}
|
|
extension HomeListenStory_1_VC:UICollectionViewDelegate{
|
|
}
|
|
extension HomeListenStory_1_VC:UICollectionViewDelegateFlowLayout{
|
|
}
|
|
extension HomeListenStory_1_VC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let m = listen1Model.storyList[page]
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_SimpleImageCCell", for: indexPath) as! SimpleImageCCell
|
cell.img_cover.sd_setImage(with: URL(string: m.img))
|
cell.jq_cornerRadius = 10
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return 1
|
}
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
return 1
|
}
|
|
}
|
|
extension HomeListenStory_1_VC:VoicePlayerDelegate{
|
func playComplete() {
|
view.isUserInteractionEnabled = true
|
if viewModel.answerType.value == .success{
|
DispatchQueue.main.asyncAfter(delay: 3.0) {
|
var dict = Dictionary<String,Any>()
|
dict["storyIntegral"] = self.listen1Model.data?.integral ?? 0
|
dict["storyId"] = self.listen1Model.data?.id ?? 0
|
NotificationCenter.default.post(name: NextLession_Noti, object: dict)
|
}
|
return
|
}
|
|
for v in stackView.arrangedSubviews as! [StudyHandleView]{
|
v.resetView()
|
v.view_choose.alpha = isPlayedSet.contains(v.tag) ? 1:0
|
}
|
}
|
|
func playing() {
|
view.isUserInteractionEnabled = false
|
}
|
}
|