//
|
// HomeListenFightVC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/23.
|
//
|
|
import UIKit
|
import FFPage
|
import RxRelay
|
|
let NextLession_Noti = Notification.Name.init("NextLession_Noti")
|
let ResetLession_Noti = Notification.Name.init("ResetLession_Noti")
|
//let Reload_Noti = Notification.Name.init("Reload_Noti")
|
|
enum ListenType:Int{
|
case lesson1 = 1 //自主学习-听音选图
|
case lesson2 = 2 //自主学习-看图选音
|
case lesson3 = 3 //自主学习-归纳排除
|
case lesson4 = 4 //自主学习-有问有答
|
case lesson5 = 5 //自主学习-音图相配
|
case game1 = 6 //游戏类型-超级听力
|
case game2 = 7 //游戏类型-超级记忆
|
case story1 = 8 //故事类型-自主故事1-看图配音
|
case story2 = 9 //故事类型-自主故事2-框架记忆
|
|
var rawTitle:String{
|
switch self {
|
case .lesson1:return "自主学习1-听音选图"
|
case .lesson2:return "自主学习2-看图选音"
|
case .lesson3:return "自主学习3-归纳排除"
|
case .lesson4:return "自主学习4-有问有答"
|
case .lesson5:return "自主学习5-音图相配"
|
case .game1:return "游戏类型1-超级听力"
|
case .game2:return "游戏类型2-超级记忆"
|
case .story1:return "自主故事1-看图配音"
|
case .story2:return "自主故事2-框架记忆"
|
}
|
}
|
}
|
|
enum ListenFightLine{
|
case before
|
case next
|
case none
|
}
|
|
|
//中途退出所需要
|
class ExitLearnModel{
|
var topicsIds = Set<Int>()
|
}
|
|
class HomeListenFightViewModel{
|
|
/// 当前页数
|
var currentPage = BehaviorRelay<Int?>(value: nil)
|
var maxPage = BehaviorRelay<Int>(value: 5)
|
var listenType = BehaviorRelay<ListenType>(value:.lesson1)
|
var times:Int = 0
|
var quarter = BehaviorRelay<Int?>(value: 0)
|
var week = BehaviorRelay<Int?>(value: 0)
|
var day = BehaviorRelay<Int?>(value: 0)
|
|
//游戏专属,游戏等级
|
var gameLevel = BehaviorRelay<Int>(value:0)
|
|
//回答错误数量
|
var correctNum:Int = 0{
|
didSet{
|
print("回答正确:\(correctNum)")
|
}
|
}
|
|
/// 回答错误数量
|
var errorNum:Int = 0{
|
didSet{
|
print("回答错误:\(correctNum)")
|
}
|
}
|
|
//所有回答的 两游戏在用
|
var answerItems = Dictionary<Int,Any>() //{page:0,data:String,currectAt:0}
|
var answerCount = BehaviorRelay<Int>(value: 1)
|
|
var answerItems_1 = Dictionary<String,Array<Int>>()
|
|
//回答正确的题
|
func insertCorrectAnswer(teamId:String?,answerId:Int){
|
guard teamId != nil else {return}
|
if answerItems_1[teamId!] == nil{
|
answerItems_1[teamId!] = Array<Int>()
|
}
|
|
answerItems_1[teamId!]!.append(answerId)
|
}
|
}
|
|
class HomeListenFightVC: BaseVC {
|
private var viewModel = HomeListenFightViewModel()
|
var studyScheduleModel:StudyScheduleModel? //学习进度(上级传递)
|
var listenFightLine:ListenFightLine = .none
|
var data:Any?
|
|
var maxPage = 0 //最大页记录
|
var teamScheduleModel:TeamScheduleModel? //上次中途退出,答题记录
|
var pages = [[ListenSubCardModel]]() //分页显示
|
|
private var notiObject:Dictionary<String,Any>?
|
|
private lazy var label_pageNum:UILabel = {
|
let label = UILabel()
|
label.font = .systemFont(ofSize: 14, weight: .medium)
|
label.textColor = .black.withAlphaComponent(0.81)
|
label.textAlignment = .center
|
label.numberOfLines = 2
|
label.text = "已完成:0/0\n正确率:--%"
|
return label
|
}()
|
|
private lazy var btn_forward:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("上一题", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(Config.ThemeColor, for: .normal)
|
btn.jq_borderColor = Config.ThemeColor
|
btn.backgroundColor = .white
|
btn.jq_borderWidth = 1
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var collection_card:UICollectionView = {
|
let layout = UICollectionViewFlowLayout()
|
layout.minimumInteritemSpacing = 2
|
layout.minimumLineSpacing = 2
|
layout.itemSize = CGSize(width: 24, height: 24)
|
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
collectionView.backgroundColor = .clear
|
return collectionView
|
}()
|
|
private lazy var btn_beAgain:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("重新开始", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(Config.ThemeColor, for: .normal)
|
btn.jq_borderColor = Config.ThemeColor
|
btn.backgroundColor = .white
|
btn.jq_borderWidth = 1
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var btn_continue:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("继续答题", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(Config.ThemeColor, for: .normal)
|
btn.jq_borderColor = Config.ThemeColor
|
btn.backgroundColor = .white
|
btn.jq_borderWidth = 1
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var btn_forward_mini:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("上一小题", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(Config.ThemeColor, for: .normal)
|
btn.jq_borderColor = Config.ThemeColor
|
btn.backgroundColor = .white
|
btn.isHidden = true
|
btn.jq_borderWidth = 1
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var btn_next:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("下一题", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(Config.ThemeColor, for: .normal)
|
btn.jq_borderColor = Config.ThemeColor
|
btn.backgroundColor = .white
|
btn.jq_borderWidth = 1
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var btn_exit:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("退出", for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
btn.setTitleColor(.white, for: .normal)
|
btn.backgroundColor = Config.ThemeColor
|
btn.jq_cornerRadius = 4
|
return btn
|
}()
|
|
private lazy var pageVC:FFPageViewController = {
|
let vc = FFPageViewController()
|
vc.scrollview.isScrollEnabled = false
|
return vc
|
}()
|
|
private var timer:Timer!
|
|
|
init(listenType:ListenType,quarter:Int? = nil,week:Int? = nil,day:Int? = nil) {
|
super.init(nibName: nil, bundle: nil)
|
self.viewModel.listenType.accept(listenType)
|
self.viewModel.week.accept(week)
|
self.viewModel.day.accept(day)
|
self.viewModel.quarter.accept(quarter)
|
|
if listenType == .game1 || listenType == .game2{
|
self.viewModel.maxPage.accept(1)
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
sceneDelegate?.suspendTimer()
|
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
|
|
yy_popBlock = {[weak self] in
|
self?.quitAction(isPop: true)
|
}
|
|
btn_exit.addTarget(self, action: #selector(quitAction), for: .touchUpInside)
|
btn_forward.addTarget(self, action: #selector(beforeAction), for: .touchUpInside)
|
btn_forward_mini.addTarget(self, action: #selector(beforeAction_mini), for: .touchUpInside)
|
btn_next.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
|
btn_beAgain.addTarget(self, action: #selector(beAgaionAction), for: .touchUpInside)
|
|
setPages()
|
// pageVC.reloadData()
|
|
timer = Timer(fire: .distantPast, interval: 1.0, repeats: true, block: {[weak self] _ in
|
self?.viewModel.times += 1
|
})
|
|
timer.fire()
|
RunLoop.current.add(timer, forMode: .common)
|
setPages()
|
|
if let teamSchedule = teamScheduleModel{
|
viewModel.correctNum = viewModel.correctNum + teamSchedule.correctNumber
|
viewModel.errorNum = teamSchedule.answerNumber - teamSchedule.correctNumber
|
maxPage = teamSchedule.schedule
|
|
switch viewModel.listenType.value{
|
case .lesson1:
|
|
if let m = data as? ListenNewModel{
|
self.pages = Array<ListenSubCardModel>.splitArray(m.list, subArraySize: 4)
|
let c = m.subjectList.count
|
let totalW = 24 * c + 2 * (c - 1)
|
collection_card.snp.updateConstraints { make in
|
make.width.equalTo(totalW)
|
}
|
}
|
|
case .lesson4:
|
if let m = data as? ListenNewModel{
|
self.pages = Array<ListenSubCardModel>.splitArray(m.list, subArraySize: 4)
|
let totalW = 24 * m.subjectList.count + 2 * (m.subjectList.count - 1)
|
collection_card.snp.updateConstraints { make in
|
make.width.equalTo(totalW)
|
}
|
}
|
|
case .lesson2,.lesson3,.lesson5:
|
if let m = data as? ListenNewModel{
|
self.pages = Array<ListenSubCardModel>.splitArray(m.list, subArraySize: 1)
|
let totalW = 24 * m.list.count + 2 * (m.list.count - 1)
|
collection_card.snp.updateConstraints { make in
|
make.width.equalTo(totalW)
|
}
|
}
|
default:break
|
}
|
|
switch viewModel.listenType.value {
|
case .lesson1:
|
collection_card.reloadData()
|
let nextPage = floor(Double(maxPage) / 5.0)
|
pageVC.scroll(toPage: Int(nextPage), animation: false)
|
viewModel.currentPage.accept(Int(nextPage))
|
viewModel.answerCount.accept(maxPage)
|
setPages()
|
|
case .lesson2:
|
collection_card.reloadData()
|
let maxCount = (data as! ListenNewModel).subjectList.count
|
let page = min((maxPage - 1),maxCount)
|
if pageVC.currentPage != page{
|
viewModel.currentPage.accept(page)
|
pageVC.scroll(toPage: page, animation: false)
|
setPages()
|
}
|
|
case .lesson4:
|
collection_card.reloadData()
|
let maxCount = (data as! ListenNewModel).subjectList.count
|
let page = min((maxPage - 1),maxCount)
|
if pageVC.currentPage != page{
|
viewModel.currentPage.accept(page)
|
pageVC.scroll(toPage: page, animation: false)
|
setPages()
|
}
|
|
|
case .lesson3,.lesson5:
|
collection_card.reloadData()
|
let maxCount = (data as! ListenNewModel).subjectList.count
|
let page = min((maxPage - 1),maxCount)
|
if pageVC.currentPage != page{
|
viewModel.currentPage.accept(page)
|
pageVC.scroll(toPage: page, animation: false)
|
setPages()
|
}
|
|
|
default:break
|
}
|
}
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
switch viewModel.listenType.value{
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:
|
collection_card.delegate = self
|
collection_card.dataSource = self
|
collection_card.register(CardItemCCell.self, forCellWithReuseIdentifier: "_CardItemCCell")
|
default:break
|
}
|
|
craeteFootFuncView()
|
|
pageVC.delegate = self
|
view.addSubview(pageVC.view)
|
pageVC.view.snp.makeConstraints { make in
|
make.left.right.equalToSuperview()
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
|
if self.viewModel.listenType.value == .lesson3 || self.viewModel.listenType.value == .lesson4{
|
make.bottom.equalTo(self.label_pageNum.snp.top).offset(-52)
|
}else{
|
make.bottom.equalTo(self.label_pageNum.snp.top).offset(-52)
|
}
|
}
|
}
|
|
|
private func showGameLevel(canLevel:Int){
|
ChooseLevelView.show(canLevel: canLevel) {[weak self] level in
|
guard let weakSelf = self else { return }
|
weakSelf.viewModel.gameLevel.accept(level)
|
Services.gameHearing(difficulty: level, quarter: weakSelf.viewModel.quarter.value!, week: weakSelf.viewModel.week.value!).subscribe(onNext: {result in
|
GameBeginTipView.show {
|
if let data = result.data{
|
weakSelf.data = data
|
(weakSelf.data as! Listen1Model).data?.playNow = true
|
weakSelf.pageVC.reloadData()
|
}
|
}
|
},onError: {[weak self] _ in
|
self?.navigationController?.popViewController(animated: true)
|
}).disposed(by: weakSelf.disposeBag)
|
} cancelClouse: { [weak self] in
|
self?.navigationController?.popViewController(animated: true)
|
}
|
}
|
|
private func craeteFootFuncView(){
|
|
view.addSubview(collection_card)
|
|
btn_forward.snp.makeConstraints { make in
|
make.height.equalTo(40)
|
make.width.equalTo(124)
|
}
|
|
btn_beAgain.snp.makeConstraints { make in
|
make.height.equalTo(40)
|
make.width.equalTo(124)
|
}
|
|
btn_continue.snp.makeConstraints { make in
|
make.height.equalTo(40)
|
make.width.equalTo(124)
|
}
|
|
|
btn_exit.snp.makeConstraints { make in
|
make.height.equalTo(40)
|
make.width.equalTo(124)
|
}
|
|
|
let stackView = UIStackView(arrangedSubviews: [btn_beAgain,label_pageNum,btn_exit])
|
if viewModel.listenType.value == .story2{
|
btn_next.snp.makeConstraints { make in
|
make.height.equalTo(40)
|
make.width.equalTo(124)
|
}
|
stackView.insertArrangedSubview(btn_next, at: 2)
|
}
|
|
stackView.spacing = 22
|
view.addSubview(stackView)
|
stackView.snp.makeConstraints { make in
|
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-5)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(40)
|
}
|
|
switch viewModel.listenType.value{
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:
|
collection_card.snp.makeConstraints { make in
|
make.centerX.equalToSuperview()
|
make.width.equalTo(10)
|
make.bottom.equalTo(stackView.snp.top).offset(-10)
|
make.height.equalTo(24)
|
}
|
default:break
|
}
|
}
|
|
override func setRx() {
|
NotificationCenter.default.rx.notification(NextLession_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] noti in
|
guard let weakSelf = self else { return }
|
let nextPage = (weakSelf.viewModel.currentPage.value ?? 0) + 1
|
var asComplete:Bool = false
|
switch weakSelf.viewModel.listenType.value {
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:asComplete = nextPage >= (weakSelf.data as! ListenNewModel).subjectList.count
|
case .game1,.game2:asComplete = true
|
case .story1,.story2: asComplete = nextPage >= (weakSelf.data as! Listen1Model).storyList.count
|
}
|
|
if asComplete{
|
switch weakSelf.viewModel.listenType.value {
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:
|
weakSelf.studyComplete()
|
case .game1,.game2:
|
weakSelf.notiObject = noti.object as? Dictionary<String,Any>
|
weakSelf.timer.invalidate()
|
|
if let isComplete = weakSelf.notiObject?["complete"] as? Bool{
|
if isComplete{
|
weakSelf.btn_exit.setTitle("提交", for: .normal)
|
}else{
|
weakSelf.gamesComplete(gameId: weakSelf.notiObject!["gameId"] as! Int,integral: weakSelf.notiObject!["gameIntegral"] as! Int)
|
}
|
}
|
case .story1,.story2:
|
if let dict = noti.object as? Dictionary<String,Any>{
|
let type = weakSelf.viewModel.listenType.value == .story1 ? 1:2
|
let accracy = floor(Double(weakSelf.viewModel.correctNum) / Double(weakSelf.viewModel.correctNum + weakSelf.viewModel.errorNum) * 100).int
|
weakSelf.storyComplete(storyId: dict["storyId"] as! Int, accuracy: accracy, studyTime: weakSelf.viewModel.times, type: type, integral: dict["storyIntegral"] as! Int)
|
}
|
}
|
return
|
}
|
|
if weakSelf.viewModel.listenType.value == .story2{
|
weakSelf.btn_next.isHidden = (nextPage + 1) == weakSelf.viewModel.maxPage.value
|
if weakSelf.btn_next.isHidden{
|
weakSelf.btn_exit.setTitle("完成", for: .normal)
|
}
|
}
|
|
weakSelf.listenFightLine = .next
|
weakSelf.pageVC.scroll(toPage: nextPage, animation: true)
|
weakSelf.viewModel.currentPage.accept(nextPage)
|
}).disposed(by: disposeBag)
|
|
viewModel.currentPage.subscribe(onNext: {[weak self]currentPage in
|
guard let weakSelf = self else { return }
|
guard let currentPage else {return}
|
weakSelf.btn_forward.isHidden = currentPage <= 0
|
weakSelf.setPages()
|
}).disposed(by: disposeBag)
|
|
viewModel.answerCount.subscribe(onNext: {[weak self] count in
|
self?.setPages()
|
}).disposed(by: disposeBag)
|
}
|
|
private func setPages(){
|
guard let currentPage = viewModel.currentPage.value else {return}
|
switch viewModel.listenType.value{
|
case .lesson1:
|
let m = data as! ListenNewModel
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.flatMap({$0}).count / 4)"
|
let correctNum = m.list.filter({$0.status == 2}).count //正确
|
// if correctNum > 0 {
|
let ratio = Double(correctNum) / Double(m.list.count) * 100.0
|
let ratioStr = ratio.jq_formatFloat
|
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.flatMap({$0}).count / 4)\n正确率:\(ratioStr)%"
|
// }
|
|
maxPage = viewModel.answerCount.value
|
btn_forward.isHidden = viewModel.answerCount.value == 1
|
case .lesson3:
|
let m = data as! ListenNewModel
|
// label_pageNum.text = "已完成:\(viewModel.currentPage.value + 1)/\(m.subjectList.count / 6)"
|
btn_forward.isHidden = currentPage == 0
|
let page = currentPage + 1
|
maxPage = page
|
|
let correctNum = m.list.filter({$0.status == 2}).count //正确
|
let ratio = Double(correctNum) / Double(m.list.count) * 100.0
|
let ratioStr = ratio.jq_formatFloat
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.count)\n正确率:\(ratioStr)%"
|
|
case .lesson2:
|
let m = data as! ListenNewModel
|
// label_pageNum.text = "已完成:\(viewModel.currentPage.value + 1)/\(m.subjectList.count)"
|
btn_forward.isHidden = viewModel.currentPage.value == 0
|
let page = currentPage + 1
|
maxPage = page
|
|
let correctNum = m.list.filter({$0.status == 2}).count //正确
|
let ratio = Double(correctNum) / Double(m.list.count) * 100.0
|
let ratioStr = ratio.jq_formatFloat
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.count)\n正确率:\(ratioStr)%"
|
|
case .lesson5:
|
let m = data as! ListenNewModel
|
// label_pageNum.text = "已完成:\(viewModel.currentPage.value + 1)/\(m.subjectList.count)"
|
btn_forward.isHidden = viewModel.currentPage.value == 0
|
let page = currentPage + 1
|
maxPage = page
|
|
let correctNum = m.list.filter({$0.status == 2}).count //正确
|
let ratio = Double(correctNum) / Double(m.list.count) * 100.0
|
let ratioStr = ratio.jq_formatFloat
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.count)\n正确率:\(ratioStr)%"
|
|
case .lesson4:
|
let m = data as! ListenNewModel
|
//两题为一组:需要/2
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(m.subjectList.count)"
|
let page = currentPage + 1
|
// maxPage = max(page,maxPage)
|
maxPage = page
|
|
let correctNum = m.list.filter({$0.status == 2}).count //正确
|
let ratio = Double(correctNum) / Double(m.list.count) * 100.0
|
let ratioStr = ratio.jq_formatFloat
|
label_pageNum.text = "已完成:\(viewModel.answerCount.value)/\(m.subjectList.count)\n正确率:\(ratioStr)%"
|
case .game1,.game2:
|
btn_forward.isHidden = true
|
label_pageNum.isHidden = true
|
|
if viewModel.listenType.value == .game1{
|
showGameLevel(canLevel: studyScheduleModel?.gameDifficulty ?? 0)
|
}
|
case .story1:
|
if viewModel.listenType.value == .story2{
|
btn_next.isHidden = (currentPage + 1) == viewModel.maxPage.value
|
if btn_next.isHidden{
|
btn_exit.setTitle("完成", for: .normal)
|
}
|
}
|
fallthrough
|
case .story2:
|
let count = (data as! Listen1Model).storyList.count
|
viewModel.maxPage.accept(count)
|
label_pageNum.text = "已完成:\(currentPage + 1)/\(count)"
|
}
|
}
|
|
|
/// 学习类完成
|
/// - Parameter ignorePush: 是否忽略跳转(未完成答题 :true)
|
private func studyComplete(){
|
let ids:String = viewModel.answerItems_1.keys.sorted().joined(separator: ",")
|
|
//正确率
|
let accracy = floor(Double(viewModel.correctNum) / Double(viewModel.correctNum + viewModel.errorNum) * 100).int
|
|
Services.completeLearing(type: viewModel.listenType.value.rawValue, studyTime: viewModel.times, studyIds: ids, quarter: viewModel.quarter.value!, week: viewModel.week.value!, day: viewModel.day.value!, accracy: accracy).subscribe(onNext: {data in
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
NotificationCenter.default.post(name: StudyCompleteCoinUpdate_Noti, object: data.data ?? 0)
|
}).disposed(by: disposeBag)
|
|
timer.invalidate()
|
|
let vc = HomeStudyCompleteVC(viewModel: viewModel,studyScheduleModel: studyScheduleModel!)
|
vc.title = viewModel.listenType.value.rawTitle
|
vc.viewModel = viewModel
|
push(vc: vc)
|
}
|
|
//游戏类完成
|
private func gamesComplete(gameId:Int,integral:Int){
|
|
var name = ""
|
var accuracy:Int = 0
|
var totalNum:Double = 0
|
|
if viewModel.listenType.value == .game1{
|
name = "超级听力"
|
totalNum = Double(viewModel.correctNum + viewModel.errorNum)
|
if totalNum > 0{
|
accuracy = floor(Double(viewModel.correctNum) / totalNum * 100).int
|
}
|
}else{
|
name = "超级记忆"
|
let v = viewModel.answerItems.first?.value as! Listen1Model
|
//11887:完成答题页,总题目、错误题目 数量计算逻辑错误(只要是没有答对的题目就算错误题目,不管是否答,相当于错误题目就是总题目减去正确题目)
|
totalNum = Double(v.photoList.count)
|
if totalNum > 0 && viewModel.correctNum > 0 && viewModel.errorNum > 0{
|
accuracy = floor(Double(viewModel.correctNum) / Double(totalNum) * 100).int
|
}
|
viewModel.errorNum = Int(totalNum) - viewModel.correctNum
|
}
|
|
Services.completeGames(gameId: gameId, gameName: name, difficulty: viewModel.gameLevel.value, accuracy: accuracy, useTime: viewModel.times).subscribe(onNext: {data in
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
NotificationCenter.default.post(name: StudyCompleteCoinUpdate_Noti, object: data.data ?? 0)
|
}).disposed(by: disposeBag)
|
|
timer.invalidate()
|
|
let vc = HomeStudyCompleteVC(totalNum:totalNum.int,viewModel: viewModel,studyScheduleModel: studyScheduleModel!)
|
vc.title = viewModel.listenType.value.rawTitle
|
vc.viewModel = viewModel
|
push(vc: vc)
|
}
|
|
private func storyComplete(storyId:Int,accuracy:Int,studyTime:Int,type:Int,integral:Int){
|
timer.invalidate()
|
Services.completeStory(storyId: storyId, accuracy: accuracy, studyTime: studyTime, type: type).subscribe(onNext: {data in
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
NotificationCenter.default.post(name: StudyCompleteCoinUpdate_Noti, object: data.data ?? 0)
|
}).disposed(by: disposeBag)
|
|
let vc = HomeStudyCompleteVC(viewModel: viewModel,studyScheduleModel: studyScheduleModel!)
|
vc.title = viewModel.listenType.value.rawTitle
|
vc.viewModel = viewModel
|
push(vc: vc)
|
}
|
|
deinit{
|
timer.invalidate()
|
}
|
|
@objc func quitAction(isPop:Bool = false){
|
if btn_exit.titleLabel?.text == "完成"{
|
if viewModel.listenType.value == .story2{
|
|
if isPop{
|
CommonAlertView.show(content: "未完成全部答题,确认退出吗?") {[weak self] () in
|
guard let weakSelf = self else { return }
|
for vc in weakSelf.navigationController?.viewControllers ?? []{
|
if vc.isKind(of: HomeListenVC.self){
|
weakSelf.navigationController?.popToViewController(vc, animated: true)
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
break
|
}
|
}
|
}
|
return
|
}
|
|
guard (pageVC.currentController as! HomeListenStory_2_VC).isPlayEnd else {
|
alert(msg: "请听完");return
|
}
|
|
let v = data as! Listen1Model
|
let accuracy = 100
|
storyComplete(storyId: v.data!.id, accuracy: accuracy, studyTime: viewModel.times, type: viewModel.listenType.value == .story1 ? 1:2, integral: v.data!.integral)
|
}
|
}else if btn_exit.titleLabel?.text == "提交"{
|
|
if isPop{
|
CommonAlertView.show(content: "未完成全部答题,确认退出吗?") {[weak self] () in
|
guard let weakSelf = self else { return }
|
for vc in weakSelf.navigationController?.viewControllers ?? []{
|
if vc.isKind(of: HomeListenVC.self){
|
weakSelf.navigationController?.popToViewController(vc, animated: true)
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
break
|
}
|
}
|
}
|
return
|
}
|
|
if viewModel.listenType.value == .game1 || viewModel.listenType.value == .game2{
|
if let dict = notiObject{
|
gamesComplete(gameId: dict["gameId"] as! Int,integral: dict["gameIntegral"] as! Int)
|
}
|
}
|
} else{
|
CommonAlertView.show(content: "未完成全部答题,确认退出吗?") {[weak self] () in
|
guard let weakSelf = self else { return }
|
|
let temIds = [String]()
|
let topicIds = [String]()
|
|
|
switch weakSelf.viewModel.listenType.value{
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:
|
let totalNum = weakSelf.viewModel.correctNum + weakSelf.viewModel.errorNum
|
Services.exitLearning(type:weakSelf.viewModel.listenType.value.rawValue,quarter: weakSelf.viewModel.quarter.value!,week: weakSelf.viewModel.week.value!, day: weakSelf.viewModel.day.value!, teamIds: temIds, topicIds: topicIds,answerNumber: totalNum,correctNumber:weakSelf.viewModel.correctNum,studyTime:weakSelf.viewModel.times,schedule: weakSelf.maxPage).subscribe(onNext: { data in
|
|
NotificationCenter.default.post(name: MeUserInfoUpdate_Noti, object: nil)
|
|
}).disposed(by: weakSelf.disposeBag)
|
case .game1,.game2,.story1,.story2:
|
Services.exitGameOrStory(studyTime: weakSelf.viewModel.times).subscribe(onNext: { _ in
|
|
}).disposed(by: weakSelf.disposeBag)
|
}
|
for vc in weakSelf.navigationController?.viewControllers ?? []{
|
if vc.isKind(of: HomeListenVC.self){
|
weakSelf.navigationController?.popToViewController(vc, animated: true)
|
NotificationCenter.default.post(name: Refresh_ListenSchedule_Noti, object: nil)
|
break
|
}
|
}
|
}
|
}
|
}
|
|
@objc func beAgaionAction(){
|
CommonAlertView.show(content: "是否重新开始答题?确认后将清空当前答题进度") {[unowned self] in
|
let day = self.viewModel.day.value ?? 0
|
let week = self.viewModel.week.value ?? 0
|
let type = self.viewModel.listenType.value.rawValue
|
Services.restart(day: day, type: type, week: week).subscribe(onNext: {[unowned self]_ in
|
self.pageVC.scroll(toPage: 0, animation: true)
|
if let m = (self.data as? ListenNewModel){
|
for v in m.list{
|
v.status = 1
|
}
|
self.restore()
|
self.setPages()
|
self.collection_card.reloadData()
|
NotificationCenter.default.post(name: ResetLession_Noti, object: nil)
|
}
|
}).disposed(by: disposeBag)
|
}
|
}
|
|
@objc func nextAction(){
|
listenFightLine = .next
|
if viewModel.listenType.value == .story2{
|
|
guard (pageVC.currentController as! HomeListenStory_2_VC).isPlayEnd else {
|
alert(msg: "请听完");return
|
}
|
|
let v = data as! Listen1Model
|
var dict = Dictionary<String,Any>()
|
dict["storyId"] = v.data?.id ?? 0
|
dict["storyIntegral"] = v.data?.lookIntegral ?? 0
|
NotificationCenter.default.post(name: NextLession_Noti, object: data)
|
}
|
}
|
|
@objc func beforeAction_mini(){
|
if viewModel.listenType.value == .lesson1{
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_1_VC{
|
print("---->进入")
|
vc.tobefore()
|
}
|
}
|
}
|
|
@objc func beforeAction(){
|
guard let currentPage = viewModel.currentPage.value else {return}
|
|
listenFightLine = .before
|
let beforePage = max(0, currentPage - 1)
|
switch viewModel.listenType.value {
|
case .lesson1:
|
if !(pageVC.currentController as! HomeListenFight_lesson_1_VC).isListen{
|
alert(msg: "请听完");return
|
}
|
|
let temp = viewModel.answerCount.value - 1
|
viewModel.answerCount.accept(max(1,temp))
|
case .lesson2:
|
let temp = (beforePage * 4) + 1
|
viewModel.answerCount.accept(max(1,temp))
|
default:break
|
}
|
|
|
pageVC.scroll(toPage: beforePage, animation: true)
|
viewModel.currentPage.accept(beforePage)
|
|
|
if viewModel.listenType.value == .story2{
|
guard (pageVC.currentController as! HomeListenStory_2_VC).isPlayEnd else {
|
alert(msg: "请听完");return
|
}
|
}
|
|
if viewModel.listenType.value == .lesson1{
|
let currentVC = pageVC.currentController as! HomeListenFight_lesson_1_VC
|
currentVC.tobefore();return
|
}
|
|
if viewModel.listenType.value == .lesson3{
|
(pageVC.currentController as! HomeListenFight_lesson_3_VC).restore()
|
}
|
|
if viewModel.listenType.value == .story2{
|
btn_next.isHidden = false
|
}
|
|
btn_exit.setTitle("退出", for: .normal)
|
}
|
|
private func restore(){
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_1_VC{
|
vc.restore()
|
}
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_2_VC{
|
vc.restore()
|
}
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_3_VC{
|
vc.restore()
|
}
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_4_VC{
|
vc.restore()
|
}
|
if let vc = pageVC.currentController as? HomeListenFight_lesson_5_VC{
|
vc.restore()
|
}
|
|
if let vc = pageVC.currentController as? HomeListenStory_1_VC{
|
vc.restore()
|
}
|
if let vc = pageVC.currentController as? HomeListenStory_2_VC{
|
vc.restore()
|
}
|
}
|
}
|
|
extension HomeListenFightVC:FFPageViewControllerDelegate{
|
func totalPagesOfpageViewController(_ pageViewConteoller: FFPageViewController) -> UInt {
|
|
switch viewModel.listenType.value {
|
case .lesson1,.lesson2,.lesson3,.lesson4,.lesson5:return UInt((data as! ListenNewModel).subjectList.count)
|
case .story1,.story2:
|
return UInt((data as! Listen1Model).storyList.count)
|
default:break
|
}
|
|
|
//超级听力,只有一页
|
if viewModel.listenType.value == .game1 || viewModel.listenType.value == .game2{
|
return 1
|
}
|
return UInt(viewModel.maxPage.value)
|
}
|
|
func pageViewController(_ pageViewController: FFPageViewController, currentPageChanged currentPage: Int) {
|
|
viewModel.currentPage.accept(currentPage)
|
if listenFightLine == .before{
|
restore()
|
}
|
}
|
|
func pageViewController(_ pageViewConteoller: FFPageViewController, controllerForPage page: Int) -> UIViewController {
|
if viewModel.listenType.value == .lesson1{
|
let vc = HomeListenFight_lesson_1_VC(page: page,listenNewModel:data as! ListenNewModel)
|
vc.teamScheduleModel = teamScheduleModel
|
vc.rootViewModel = viewModel
|
vc.handleClouseAction {[unowned self] in
|
self.setPages()
|
self.collection_card.reloadData()
|
}
|
return vc
|
}
|
|
if viewModel.listenType.value == .lesson2{
|
let vc = HomeListenFight_lesson_2_VC(page: page,listenNewModel:data as! ListenNewModel)
|
vc.teamScheduleModel = teamScheduleModel
|
vc.rootViewModel = viewModel
|
vc.handleClouseAction {[unowned self] in
|
self.setPages()
|
self.collection_card.reloadData()
|
}
|
return vc
|
}
|
|
if viewModel.listenType.value == .lesson3{
|
let vc = HomeListenFight_lesson_3_VC(page: page, listenNewModel: data as! ListenNewModel)
|
vc.teamScheduleModel = teamScheduleModel
|
vc.rootViewModel = viewModel
|
vc.handleClouseAction {[unowned self] in
|
self.setPages()
|
self.collection_card.reloadData()
|
}
|
return vc
|
}
|
|
if viewModel.listenType.value == .lesson4{
|
let vc = HomeListenFight_lesson_4_VC(page: page, listenNewModel: data as! ListenNewModel)
|
vc.teamScheduleModel = teamScheduleModel
|
vc.rootViewModel = viewModel
|
vc.handleClouseAction {[unowned self] in
|
self.setPages()
|
self.collection_card.reloadData()
|
}
|
return vc
|
}
|
|
if viewModel.listenType.value == .lesson5{
|
let vc = HomeListenFight_lesson_5_VC(page: page, listenNewModel: data as! ListenNewModel)
|
vc.teamScheduleModel = teamScheduleModel
|
vc.rootViewModel = viewModel
|
vc.handleClouseAction {[unowned self] in
|
self.setPages()
|
self.collection_card.reloadData()
|
}
|
return vc
|
}
|
|
if viewModel.listenType.value == .game1{
|
if data == nil{return UIViewController()}
|
let vc = HomeListenGame_1_VC(listen1Model: data as! Listen1Model)
|
vc.rootViewModel = viewModel
|
return vc
|
}
|
|
if viewModel.listenType.value == .game2{
|
let vc = HomeListenGame_2_VC(listen1Model: data as! Listen1Model)
|
vc.rootViewModel = viewModel
|
return vc
|
}
|
|
if viewModel.listenType.value == .story1{
|
let vc = HomeListenStory_1_VC(page: page, listen1Model: data as! Listen1Model)
|
vc.rootViewModel = viewModel
|
return vc
|
}
|
|
if viewModel.listenType.value == .story2{
|
let vc = HomeListenStory_2_VC(page: page, listen1Model: data as! Listen1Model)
|
vc.rootViewModel = viewModel
|
return vc
|
}
|
|
let vc = UIViewController()
|
return vc
|
}
|
}
|
|
extension HomeListenFightVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
guard pageVC.currentPage != indexPath.row else {return}
|
if let m = data as? ListenNewModel{
|
if viewModel.listenType.value == .lesson1{
|
guard pages[indexPath.row].filter({$0.status == 2}).count == 4 else {return}
|
}else{
|
guard m.list[indexPath.row].status != 1 else{return}
|
}
|
|
pageVC.scroll(toPage: indexPath.row, animation: true)
|
}
|
}
|
}
|
|
extension HomeListenFightVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
if data is ListenNewModel{
|
if viewModel.listenType.value == .lesson1{
|
return (data as! ListenNewModel).subjectList.count
|
}
|
|
if viewModel.listenType.value == .lesson2{
|
return (data as! ListenNewModel).subjectList.count
|
}
|
|
return pages.count
|
}
|
return 0
|
}
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_CardItemCCell", for: indexPath) as! CardItemCCell
|
if data is ListenNewModel{
|
let model = pages[indexPath.row]
|
cell.titleL.text = "\(indexPath.row + 1)"
|
|
if viewModel.listenType.value == .lesson1{
|
if model.filter({$0.status == 2}).count == 4{
|
cell.titleL.textColor = UIColor(hexString: "#52C41A")
|
cell.titleL.jq_borderColor = UIColor(hexString: "#52C41A")
|
}else if model.filter({$0.status == 3}).count >= 1 {
|
cell.titleL.textColor = UIColor(hexString: "#FF4D4F")
|
cell.titleL.jq_borderColor = UIColor(hexString: "#52C41A")
|
}else{
|
cell.titleL.textColor = .black.withAlphaComponent(0.25)
|
cell.titleL.jq_borderColor = .black.withAlphaComponent(0.25)
|
}
|
}else{
|
//状态1灰色未答题 2绿色正确 3红色错误
|
switch model.first!.status{
|
case 1:
|
cell.titleL.textColor = .black.withAlphaComponent(0.25)
|
cell.titleL.jq_borderColor = .black.withAlphaComponent(0.25)
|
case 2:
|
cell.titleL.textColor = UIColor(hexString: "#52C41A")
|
cell.titleL.jq_borderColor = UIColor(hexString: "#52C41A")
|
case 3:
|
cell.titleL.textColor = UIColor(hexString: "#FF4D4F")
|
cell.titleL.jq_borderColor = UIColor(hexString: "#52C41A")
|
default:break
|
}
|
}
|
}
|
return cell
|
}
|
}
|
|
class CardItemCCell:UICollectionViewCell{
|
|
var titleL:UILabel!
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
|
titleL = UILabel()
|
titleL.text = "1"
|
titleL.backgroundColor = .white
|
titleL.jq_borderColor = .black.withAlphaComponent(0.15)
|
titleL.jq_borderWidth = 0.5
|
titleL.font = .systemFont(ofSize: 13,weight: .semibold)
|
titleL.textAlignment = .center
|
contentView.addSubview(titleL)
|
titleL.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
}
|