SynthReel/SynthReel/Class/Player/VM/SRShortPlayerViewModel.swift
2025-12-08 16:04:26 +08:00

219 lines
7.1 KiB
Swift

//
// SRShortPlayerViewModel.swift
// SynthReel
//
// Created by on 2025/11/18.
// Copyright © 2025 SR. All rights reserved.
//
import UIKit
import JXPlayer
import YYText
import HWPanModal
@MainActor
class SRShortPlayerViewModel: JXPlayerListViewModel {
var shortId: String = "0"
var dataArr: [SRShortDetailModel] = []
var recommandList: [SRShortModel] = []
private var payDataRequest: SRPayDataRequest?
///
private(set) var isShowRecommand = false
private var recommandTimer: Timer?
weak var popView: UIView?
nonisolated required init() {
super.init()
}
@MainActor
func requestShortDetail(indexPath: IndexPath? = nil) async -> Int? {
let (model, code, _) = await SRShortApi.requestShortDetail(shortId)
guard let model = model else { return code }
self.dataArr.removeAll()
self.dataArr.append(model)
self.playerListVC?.reloadData { [weak self] in
guard let self = self else { return }
var targetIndexPath = IndexPath(row: 0, section: 0)
if let indexPath = indexPath,
indexPath.row < (model.episodeList?.count ?? 0) {
targetIndexPath = indexPath
} else if let videoInfo = model.video_info {
if let row = model.episodeList?.firstIndex(where: {
$0.short_play_video_id == videoInfo.short_play_video_id
}) {
targetIndexPath = IndexPath(row: row, section: 0)
}
}
isShowRecommand = false
recommandTimer?.invalidate()
recommandTimer = nil
recommandTimer = Timer.scheduledTimer(
timeInterval: 6,
target: YYTextWeakProxy(target: self),
selector: #selector(handleRecommandTimer),
userInfo: nil,
repeats: false
)
self.playerListVC?.scrollToItem(indexPath: targetIndexPath, animated: false)
}
return code
}
@MainActor
private func unlockVideo(completer: ((_ finish: Bool) -> Void)?) async {
guard let videoInfo = self.currentCell?.model as? SRVideoInfoModel else { return }
guard let shortPlayId = videoInfo.short_play_id else { return }
guard let videoId = videoInfo.short_play_video_id else { return }
let (model) = await SRShortApi.requestCoinUnlockVideo(shortId: shortPlayId,videoId: videoId)
guard let model = model else {
completer?(false)
return
}
switch model.status {
case .jump:
SRToast.show(text: "buy_fail_toast_02".localized)
case .noPlay:
SRToast.show(text: "buy_fail_toast_01".localized)
case .notEnough:
//广
self.openRechargeView()
break
default: break
}
if model.status == .success {
Task {
await SRAccountManager.manager.updateUserInfo()
videoInfo.is_lock = false
completer?(true)
}
} else {
completer?(false)
}
}
}
extension SRShortPlayerViewModel {
func onEpSelectorView() {
let view = SREpSelectorView()
view.model = self.dataArr[currentIndexPath.section]
view.selectedIndex = self.currentIndexPath.row
view.didSelected = { [weak self] index in
guard let self = self else { return }
self.playerListVC?.scrollToItem(indexPath: IndexPath(row: index, section: currentIndexPath.section), animated: false)
}
view.present(in: nil)
self.popView = view
}
func requsetRecommandData() async {
let (model, _, _) = await SRShortApi.requestRecommand()
guard let model = model else { return }
self.recommandList = model
}
@MainActor
func handleUnlockVideo() async {
await unlockVideo { [weak self] finish in
guard let self = self else { return }
if finish {
SRToast.show(text: "synthreel_success".localized)
self.playerListVC?.reloadData {
self.playerListVC?.play()
}
}
}
}
@MainActor
func handleAdUnlockVideo() async {
}
func openRechargeView() {
guard let videoInfo = self.currentCell?.model as? SRVideoInfoModel else { return }
guard self.popView == nil else { return }
self.payDataRequest = SRPayDataRequest()
if let model = SRIapManager.manager.payDateModel {
self._openRechargeView(model, videoInfo)
self.payDataRequest?.requestProducts(isLoding: false) { [weak self] model in
guard let self = self else { return }
guard let model = model else { return }
if let view = self.popView as? SRVideoRechargeView {
view.model = model
}
}
} else {
self.payDataRequest?.requestProducts(isLoding: true) { [weak self] model in
guard let self = self else { return }
guard let model = model else { return }
self._openRechargeView(model, videoInfo)
}
}
}
private func _openRechargeView(_ model: SRPayDateModel, _ videoInfo: SRVideoInfoModel) {
guard self.popView == nil else { return }
Task {
await SRStatAPI.requestEventStat(orderCode: nil, shortPlayId: videoInfo.short_play_id, videoId: videoInfo.short_play_video_id, eventKey: .payTemplateDialog, errorMsg: nil, otherParamenters: [
"event_name" : "pay open"
])
}
// if model.pay_mode == 1 {
// _openNewRechargeView(model, videoInfo)
// } else {
_openOldRechargeView(model, videoInfo)
// }
}
private func _openOldRechargeView(_ model: SRPayDateModel, _ videoInfo: SRVideoInfoModel) {
Task {
await SRStatAPI.requestEventStat(orderCode: nil, shortPlayId: videoInfo.short_play_id, videoId: videoInfo.short_play_video_id, eventKey: .payTemplateDialog, errorMsg: nil, otherParamenters: [
"event_name" : "pay open"
])
}
let view = SRVideoRechargeView()
view.model = model
view.videoInfo = videoInfo
view.buyFinishHandle = { [weak self] in
guard let self = self else { return }
Task{
await self.requestShortDetail(indexPath: self.currentIndexPath)
}
}
view.didDismissHandle = { [weak self] in
guard let self = self else { return }
// self._showVipRetainAlert(videoInfo)
}
view.present(in: nil)
self.popView = view
}
@objc private func handleRecommandTimer() {
self.isShowRecommand = true
}
}