246 lines
8.7 KiB
Swift
246 lines
8.7 KiB
Swift
//
|
|
// Untitled.swift
|
|
// Fableon
|
|
//
|
|
// Created by 湖北秦九 on 2025/8/29.
|
|
//
|
|
|
|
import SwiftUI
|
|
import JXPlayer
|
|
|
|
//@MainActor
|
|
class FAShortDetailViewModel: JXPlayerListViewModel, ObservableObject {
|
|
|
|
private(set) var dataArr: [FAShortDetailModel] = []
|
|
|
|
var shortPlayId: String = ""
|
|
|
|
///上一次上报播放时长的节点
|
|
private var lastUploadTime: TimeInterval = 0
|
|
|
|
var previousEpisode: FAVideoInfoModel? {
|
|
guard dataArr.count > 0 else { return nil }
|
|
let detailModel = dataArr[self.currentIndexPath.section]
|
|
let row = self.currentIndexPath.row - 1
|
|
if row < 0 { return nil }
|
|
return detailModel.episodeList?[row]
|
|
}
|
|
|
|
var nextEpisode: FAVideoInfoModel? {
|
|
guard dataArr.count > 0 else { return nil }
|
|
let detailModel = dataArr[self.currentIndexPath.section]
|
|
let row = self.currentIndexPath.row + 1
|
|
if row >= (detailModel.episodeList?.count ?? 0) { return nil }
|
|
|
|
return detailModel.episodeList?[row]
|
|
}
|
|
|
|
weak var popView: UIView?
|
|
|
|
private var payDataRequest: FAPayDataRequest?
|
|
|
|
|
|
func requestDetailData(indexPath: IndexPath? = nil, completer: ((_ code: Int) -> Void)?) {
|
|
FAAPI.requestShortDetailData(shortPlayId: shortPlayId) { [weak self] model, code, msg in
|
|
guard let self = self else { return }
|
|
if let model = model {
|
|
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 {
|
|
var row: Int?
|
|
model.episodeList?.enumerated().forEach {
|
|
if $1.short_play_video_id == videoInfo.short_play_video_id {
|
|
row = $0
|
|
}
|
|
}
|
|
if let row = row {
|
|
targetIndexPath = .init(row: row, section: 0)
|
|
}
|
|
}
|
|
|
|
self.playerListVC?.scrollToItem(indexPath: targetIndexPath, animated: false)
|
|
}
|
|
}
|
|
completer?(code ?? -1)
|
|
}
|
|
}
|
|
|
|
|
|
override func playProgressDidChange(player: any JXPlayerCell, time: TimeInterval) {
|
|
if time > 1 {
|
|
(player.model as? FAVideoInfoModel)?.play_seconds = Int(time) * 1000
|
|
}
|
|
|
|
if (time >= lastUploadTime + 5 || time < lastUploadTime) && time >= 5 {
|
|
lastUploadTime = time
|
|
uploadPlayTime()
|
|
}
|
|
}
|
|
|
|
private func uploadPlayTime() {
|
|
let videoInfo = self.currentCell?.model as? FAVideoInfoModel
|
|
let currentTime = self.currentCell?.currentTime ?? 0
|
|
let duration = self.currentCell?.durationTime ?? 0
|
|
|
|
var time = currentTime
|
|
if currentTime >= duration {
|
|
time = 0
|
|
}
|
|
|
|
guard let shortPlayId = videoInfo?.short_play_id, let videoId = videoInfo?.short_play_video_id else { return }
|
|
FAAPI.requestUploadPlayTime(shortPlayId: shortPlayId, videoId: videoId, seconds: Int(time) * 1000)
|
|
}
|
|
|
|
}
|
|
|
|
extension FAShortDetailViewModel {
|
|
|
|
func onEpSelectorView() {
|
|
let view = FAEpSelectorView()
|
|
view.selectedIndex = self.currentIndexPath.row
|
|
view.model = self.dataArr[currentIndexPath.section]
|
|
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 handleUnlockVideo() {
|
|
unlockVideo { [weak self] finish in
|
|
if finish {
|
|
self?.playerListVC?.reloadData {
|
|
self?.playerListVC?.play()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///打开充值页面
|
|
func openRechargeView() {
|
|
guard let videoInfo = self.currentCell?.model as? FAVideoInfoModel else { return }
|
|
guard self.popView == nil else { return }
|
|
|
|
self.payDataRequest = FAPayDataRequest()
|
|
if let model = FAIapManager.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? FAOldVideoRechargeView {
|
|
view.model = model
|
|
} else if let view = self.popView as? FANewVideoRechargeView {
|
|
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: FAPayDateModel, _ videoInfo: FAVideoInfoModel) {
|
|
guard self.popView == nil else { return }
|
|
FAStatAPI.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: FAPayDateModel, _ videoInfo: FAVideoInfoModel) {
|
|
let view = FAOldVideoRechargeView()
|
|
view.model = model
|
|
view.videoInfo = videoInfo
|
|
view.buyFinishHandle = { [weak self] in
|
|
guard let self = self else { return }
|
|
self.requestDetailData(indexPath: self.currentIndexPath, completer: nil)
|
|
}
|
|
view.didDismissHandle = { [weak self] in
|
|
guard let self = self else { return }
|
|
self._showVipRetainAlert(videoInfo)
|
|
}
|
|
view.present(in: nil)
|
|
self.popView = view
|
|
}
|
|
|
|
private func _openNewRechargeView(_ model: FAPayDateModel, _ videoInfo: FAVideoInfoModel) {
|
|
let view = FANewVideoRechargeView()
|
|
view.model = model
|
|
view.videoInfo = videoInfo
|
|
view.present(in: nil)
|
|
self.popView = view
|
|
}
|
|
|
|
private func _showVipRetainAlert(_ videoInfo: FAVideoInfoModel) {
|
|
|
|
|
|
payDataRequest = FAPayDataRequest()
|
|
|
|
payDataRequest?.requestVipRetainPayInfo { [weak self] model in
|
|
guard let self = self else { return }
|
|
guard let model = model else { return }
|
|
let view = FAVipRetainAlert()
|
|
view.model = model
|
|
view.videoInfo = videoInfo
|
|
view.buyFinishHandle = { [weak self] in
|
|
guard let self = self else { return }
|
|
self.requestDetailData(indexPath: self.currentIndexPath, completer: nil)
|
|
}
|
|
view.show(in: FATool.keyWindow)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension FAShortDetailViewModel {
|
|
|
|
private func unlockVideo(completer: ((_ finish: Bool) -> Void)?) {
|
|
guard let videoInfo = self.currentCell?.model as? FAVideoInfoModel else { return }
|
|
guard let shortPlayId = videoInfo.short_play_id else { return }
|
|
guard let videoId = videoInfo.short_play_video_id else { return }
|
|
|
|
FAAPI.requestCoinUnlockVideo(shortPlayId: shortPlayId, videoId: videoId) { [weak self] model in
|
|
guard let self = self else { return }
|
|
guard let model = model else {
|
|
completer?(false)
|
|
return
|
|
}
|
|
|
|
switch model.status {
|
|
case .jump:
|
|
FAToast.show(text: "buy_fail_toast_02".localized)
|
|
case .noPlay:
|
|
FAToast.show(text: "buy_fail_toast_01".localized)
|
|
case .notEnough:
|
|
self.openRechargeView()
|
|
default: break
|
|
}
|
|
|
|
if model.status == .success {
|
|
FALogin.manager.requestUserInfo {
|
|
videoInfo.is_lock = false
|
|
completer?(true)
|
|
}
|
|
} else {
|
|
completer?(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|