// // 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 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) } } }