Veloria/Veloria/Class/Player/ViewModel/VPVideoPlayViewModel.swift

116 lines
3.8 KiB
Swift

//
// VPVideoPlayViewModel.swift
// Veloria
//
// Created by Veloria on 2025/5/22.
//
import UIKit
class VPVideoPlayViewModel: NSObject {
@objc dynamic var isPlaying: Bool = true
var currentIndexPath = IndexPath(row: 0, section: 0)
var currentPlayer: VPPlayerProtocol? {
didSet {
oldValue?.isCurrent = false
oldValue?.pause()
self.currentPlayer?.isCurrent = true
self.currentPlayer?.rate = rateModel.rate.getRate()
}
}
///
@objc dynamic lazy var rateModel = VPVideoRateModel(rate: .x1) {
didSet {
self.currentPlayer?.rate = rateModel.rate.getRate()
}
}
///
func seekToTime(toTime: Int) {
self.currentPlayer?.seekToTime(toTime: toTime)
}
///
var handlePauseOrPlay: (() -> Void)?
///
var handlePlayFinish: (() -> Void)?
///
var handlePlayTimeDidChange: ((_ time: Int) -> Void)?
///
var handleEpisode: (() -> Void)?
var handleUnlock: (() -> Void)?
///
var updateDetailDataBlock: ((_ toIndexPath: IndexPath?) -> Void)?
}
extension VPVideoPlayViewModel {
///
func selectedRevolution(revolution: VPShortModel.VideoRevolution) {
guard VPVideoRevolutionManager.manager.revolution != revolution else { return }
let userInfo = VPLoginManager.manager.userInfo
if revolution.needLogin, userInfo?.is_tourist != false, userInfo?.is_vip != true {
VPLoginManager.manager.openLogin { [weak self] in
guard let _ = self else { return }
VPVideoRevolutionManager.manager.setVideoRevolution(revolution: revolution)
}
} else if revolution.needVip, userInfo?.is_vip != true {
let alert = VPAlertView(title: "veloria_vip_activate_title".localized, subtitle: "veloria_vip_activate_content".localized, icon: UIImage(named: "alert_icon_06"), normalButtonText: "veloria_later".localized, highlightButtonText: "veloria_go".localized).show()
alert.clickHighlightButton = { [weak self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
self?.showRechargeView(revolution: revolution)
}
}
} else {
VPVideoRevolutionManager.manager.setVideoRevolution(revolution: revolution)
}
}
///
func showRechargeView(revolution: VPShortModel.VideoRevolution? = nil) {
guard let videoInfo = self.currentPlayer?.videoInfo else { return }
VPWalletAPI.requestPayTemplate(isLoding: true) { [weak self] model in
guard let self = self else { return }
guard let model = model else { return }
let view = VPPlayerRechargeView()
view.model = model
if videoInfo.is_lock != true {
view.unlockCoin = nil
} else {
view.unlockCoin = videoInfo.coins
}
view.shortPlayId = videoInfo.short_play_id
view.videoId = videoInfo.short_play_video_id
view.buyFinishBlock = { [weak self] in
guard let self = self else { return }
self.updateDetailDataBlock?(self.currentIndexPath)
}
if let revolution = revolution {
view.vipBuyFinishBlock = { [weak self] in
guard let _ = self else { return }
VPVideoRevolutionManager.manager.setVideoRevolution(revolution: revolution)
}
}
view.present(in: VPAppTool.keyWindow)
}
}
}