CNUpSlideViewController.swift 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
//
//  CNUpSlideViewController.swift
//  metaCode
//
//  Created by zx on 2023/10/13.
//  下滑页原生页-首页

import Foundation
import UIKit

//下滑tableview背景颜色 浅灰色
let kCNDownSlideBackgroundColorHex: String = "#F7F8FA"

///CNliveDownSlideModel Type
let SlideModelTypeRecommend4: String = "4"//4一排两个 推荐课程&推荐活动
let SlideModelTypeTodayRecommend74: String = "74"//74今日推荐
let SlideModelTypeAllCourse12: String = "12"//12列表全部课程
let SlideModelTypeOneCourse75: String = "75"//75单课程大图
let SlideModelTypeTwoDuanJu5: String = "5"//5竖版短剧 一排两个
let SlideModelTypeRealHotList7: String = "7"//7实时热榜

class CNUpSlideViewController: CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource{
    
    let kCNLiveDownSlideAllCourseCellIdentifier = "kCNLiveDownSlideAllCourseCellIdentifier"
    let kCNLiveDownSlideRecommendCellIdentifier = "kCNLiveDownSlideRecommendCellIdentifier"
    let kCNLiveDownSlideTodayRecommendCellIdentifier = "kCNLiveDownSlideTodayRecommendCellIdentifier"
    let kCNLiveDownSlideSingleCourseCellIdentifier = "kCNLiveDownSlideSingleCourseCellIdentifier"
    
    //我的课程cellID
    let kCNLiveDownSlideMyCurriculumCellIdentifier = "kCNLiveDownSlideMyCurriculumCellIdentifier"
    
    var totolNum = 0//一共显示多少行cell
    
    var selectedCell: CNDownSlideSingleCourseCell?
    
    var statusBarShouldBeHidden = false
    //we need to set `View controller-based status bar appearance = YES` in info.plist.
    //so we can be able to hide statusBar.
    override var prefersStatusBarHidden: Bool {
        return statusBarShouldBeHidden
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }
    //IDmodel
    var _idModel : StandListModel?
    var idModel : StandListModel? {
        get{
            _idModel
        }
        set{
            _idModel = newValue
        }
        
    }
    var _downPath:String?
    var downPath:String? {
        get{
            return _downPath
        }
        set{
            _downPath = newValue!
            
            let databaseManager = DatabaseManager(filePath: "")
            // 检索数据
            var newDataArr = databaseManager.fetchModel(withID: Int64(newValue!) ?? 0)
            if (newDataArr != nil){
                
                //查询isGetPage是否为1 true
                if newDataArr?.first?.isGetPage == 1{
                    //请求getpage后更新本地数据库
                    let operation = BlockOperation { [weak self] in
                        //网络请求
                        self!.getPageApi(isUpdateDB: true)
                    }
                    CNLiveDownslideOperationManager.shared.addDownslideOperation(operation: operation)
                }else{
                    //不更新本地数据库
                    
                }
                
                self.dataArray.removeAllObjects()
                self.dataArray.addObjects(from: newDataArr!)
                if self.idModel?.id == 6{
                    //幼教中国,请求我的课程接口
                    self.getYjzgMyCurriculumApi()
                    let firMdl = self.dataArray.firstObject as! CNLiveDownSlideModel
                    if firMdl.type == SlideModelTypeTodayRecommend74{
                        self.isFirstMyCurriculum = false
                    }else{
                        self.isFirstMyCurriculum = true
                    }
                }else{
                    self.tableView.reloadData()
                }
            }else{
                let operation = BlockOperation { [weak self] in
                    //网络请求
                    self!.getPageApi(isUpdateDB: false)
                }
                CNLiveDownslideOperationManager.shared.addDownslideOperation(operation: operation)
            }
            
//            self.getPageApi()
        }
    }
    
    /// 获取十位时间戳
    func getTimeStamp()->Int{
        let timeInterval: TimeInterval = NSDate().timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return timeStamp
    }
    
    // MARK: - 懒加载
    ///数据源
    lazy var dataArray: NSMutableArray = {
        var dataArray = NSMutableArray()
        return dataArray
    }()
    
    // 我的课程 index
    var myCurriculumIndex = -1
    //是否第一个展示'我的课程'
    var isFirstMyCurriculum = false
    ///我的课程数据源
    lazy var myCurriculumDataArray: NSMutableArray = {
        var myCurriculumDataArray = NSMutableArray()
        return myCurriculumDataArray
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.updateStatusBarAndTabbarFrame(visible: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
        view.addSubview(tableView)
        tableView.frame = CGRect(x: 0, y: KStatusBarHeight, width: KSreenWidth, height: KSreenHeight-DownSlideBottomViewHeight-KStatusBarHeight)
    }

    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.bounces = false
        tableView.register(CNLiveDownSlideRecommendCell.self, forCellReuseIdentifier: kCNLiveDownSlideRecommendCellIdentifier)
        tableView.register(CNLiveDownSlideTodayRecommendCell.self, forCellReuseIdentifier: kCNLiveDownSlideTodayRecommendCellIdentifier)
        tableView.register(CNDownSlideSingleCourseCell.self, forCellReuseIdentifier: kCNLiveDownSlideAllCourseCellIdentifier)
        tableView.register(CNDownSlideSingleCourseCell.self, forCellReuseIdentifier:kCNLiveDownSlideSingleCourseCellIdentifier)
        tableView.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
        return tableView
    }()
    
    private func updateStatusBarAndTabbarFrame(visible: Bool) {
        statusBarShouldBeHidden = !visible
        UIView.animate(withDuration: 0.25) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }

    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        self.dataArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if self.dataArray[indexPath.row] is CNLiveAlbumBaseModel{
            //我的课程cell
            let myCurriculumModel = self.dataArray[indexPath.row] as! CNLiveAlbumBaseModel
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideAllCourseCellIdentifier) as? CNDownSlideSingleCourseCell
            else {
                let cell = CNDownSlideSingleCourseCell()
                return cell
            }
            cell.myCurriculum = myCurriculumModel
            return cell
        }else{
            let slideModel = self.dataArray[indexPath.row] as? CNLiveDownSlideModel
            
            //type=4一排两个 推荐课程&推荐活动; 74今日推荐; 12列表全部课程; 75单课程大图
            if slideModel?.type == SlideModelTypeRecommend4{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideRecommendCellIdentifier) as? CNLiveDownSlideRecommendCell
                else {
                    return CNLiveDownSlideRecommendCell()
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel?.type == SlideModelTypeTodayRecommend74{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideTodayRecommendCellIdentifier) as? CNLiveDownSlideTodayRecommendCell
                else {
                    let cell = CNLiveDownSlideTodayRecommendCell()
                    cell.slideModel = slideModel
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel?.type == SlideModelTypeAllCourse12{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideAllCourseCellIdentifier) as? CNDownSlideSingleCourseCell
                else {
                    let cell = CNDownSlideSingleCourseCell()
                    cell.slideModel = slideModel
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel?.type == SlideModelTypeOneCourse75{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideSingleCourseCellIdentifier) as? CNDownSlideSingleCourseCell
                else {
                    let cell = CNDownSlideSingleCourseCell()
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else{
                return UITableViewCell()
            }
        }
    }
    
    
    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
        UIView.animate(withDuration: 0.1) {
            row.bgBackView.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        }
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
        UIView.animate(withDuration: 0.3) {
            row.bgBackView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var cellType:CNLiveHomePageCellType = .course
        if self.dataArray[indexPath.row] is CNLiveAlbumBaseModel{
            //我的课程cell
            let myCurriculumModel = self.dataArray[indexPath.row] as! CNLiveAlbumBaseModel
            cellType = .courseToMyCurriculum
            guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
            selectedCell = cell
            let detailVC = CNSlideBaseNavigationController(rootViewController: CNUpSlideDetailViewController(cell: cell,slideModel: nil, albumModel: myCurriculumModel, cellType: cellType), cellType: cellType)
            detailVC.dismissClosure = { [weak self] in
                guard let StrongSelf = self else { return }
                StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
            }
            updateStatusBarAndTabbarFrame(visible: false)
            present(detailVC, animated: true, completion: nil)
        }else{
            let slideModel = self.dataArray[indexPath.row] as! CNLiveDownSlideModel
            ///model
            ///(1) 1,2时跳转:sharelUrl后面拼接 ?contentId=contentId&videoId=pid
            ///(2) 3,4时跳转:thirdUrl
            ///(3)“album”,“album_video”,“album_ugc” -> http://wjjh5.cnlive.com/cnYmAlbumVideoList.html?aid=contentId&title=titlle
            if slideModel.contents.first?.model ?? "0" == kCNDownSlideContentsModelModel1 || slideModel.contents.first?.model ?? "0" == kCNDownSlideContentsModelModel2 || slideModel.contents.first?.model ?? "0" == kCNDownSlideContentsModelModel3 || slideModel.contents.first?.model ?? "0" == kCNDownSlideContentsModelModel4{
                cellType = .course
            }else if slideModel.contents.first?.model ?? "0" == kCNDownSlideContentsModelModelAlbum_ugc{
                cellType = .playlet
            }else{
                //新类型
            }
            //type=4一排两个 推荐课程&推荐活动; 74今日推荐; 12列表全部课程; 75单课程大图
            if slideModel.type == SlideModelTypeAllCourse12{
                guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
                selectedCell = cell
                let detailVC = CNSlideBaseNavigationController(rootViewController: CNUpSlideDetailViewController(cell: cell,slideModel: slideModel, albumModel: nil, cellType: cellType), cellType: cellType)
                detailVC.dismissClosure = { [weak self] in
                    guard let StrongSelf = self else { return }
                    StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
                }
                updateStatusBarAndTabbarFrame(visible: false)
                present(detailVC, animated: true, completion: nil)
            }else if slideModel.type == SlideModelTypeOneCourse75{
                guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
                selectedCell = cell
                
                let detailVC = CNSlideBaseNavigationController(rootViewController: CNUpSlideDetailViewController(cell: cell,slideModel: slideModel, albumModel: nil, cellType: cellType), cellType: cellType)
                
                detailVC.dismissClosure = { [weak self] in
                    guard let StrongSelf = self else { return }
                    StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
                }
                updateStatusBarAndTabbarFrame(visible: false)
                present(detailVC, animated: true, completion: nil)
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.dataArray[indexPath.row] is CNLiveAlbumBaseModel{
            let myCurriculumModel = self.dataArray[indexPath.row] as! CNLiveAlbumBaseModel
            let contentsNum = myCurriculumModel.list.count
            //总显示块数/循环数
            let totalNum = (contentsNum >= 4) ? 4 : contentsNum
            let cellH = DownSlideAllCourseTitleH + DownSlideAllCourseRowH * CGFloat(totalNum)+DownSlideRecommendLeftGap
            return cellH
        }else{
            let slideModel = self.dataArray[indexPath.row] as? CNLiveDownSlideModel
            return CGFloat(slideModel?.cellH ?? 0)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    //MARK: - 网络请求获取详情页
    func getPageApi(isUpdateDB:Bool)
    {
//        showLoading(message: "")
        CNLiveDownSlideViewModel.requestGetPage(pageId: _downPath! as NSString) { result, status in
            hiddenLoading()
            
            if status == .success{
                // 假设你有一个 CNLiveDownSlideModel 实例要保存
                let databaseManager = DatabaseManager(filePath: "")
                
                self.dataArray.removeAllObjects()
                let array = result as! [Any]
                let newDataArray = NSMutableArray()
                var index = 1
                for slideModel in array {
                    if let model = slideModel as? CNLiveDownSlideModel{
                         let layout = CNLiveDownSlideModelLayout.init(model: model)
                        let newSlideModel = layout.getCellHModel()
                        newSlideModel.id = Int(self.downPath!) ?? 0
                        
                        let timestampIdx = index
                        newSlideModel.timeStamp = timestampIdx
                        print("timeStamp:\(timestampIdx)")
                        index = index + 1
                        newDataArray.add(newSlideModel)
                    }
                }
                let newDataArr = newDataArray as! [CNLiveDownSlideModel]
                
                databaseManager.saveModel(newDataArr)
                databaseManager.updateModel(newDataArr)
                
                self.dataArray.addObjects(from: newDataArray as! [Any])
                if self.idModel?.id == 6{
                    //幼教中国,请求我的课程接口
                    self.getYjzgMyCurriculumApi()
                    let firMdl = self.dataArray.firstObject as! CNLiveDownSlideModel
                    if firMdl.type == SlideModelTypeTodayRecommend74{
                        self.isFirstMyCurriculum = false
                    }else{
                        self.isFirstMyCurriculum = true
                    }
                }else{
                    self.tableView.reloadData()
                }
                
            }else if (status == .failed){
//                print("222222222getPageFail:\(self.downPath!)")
//                self.NetApiStatus = .apiError
            }else if (status == .noNetwork){
//                print("222222222getPageFail:\(self.downPath!)")
//                self.NetApiStatus = .notNetwork
            }else{
//                print("222222222getPageFail:\(self.downPath!)")
            }
        }
//
    }
    //MARK: - 幼教中国获取 我的课程
    func getYjzgMyCurriculumApi()
    {
//        showLoading(message: "")
        CNLiveDownSlideViewModel.getYjzgMyCurriculum(page: 1) { result, status in
            hiddenLoading()
            if status == .success{
                self.myCurriculumDataArray.removeAllObjects()
                let albumBaseModel = result as! CNLiveAlbumBaseModel
                if albumBaseModel.list.count > 0{
                    self.myCurriculumDataArray.addObjects(from: [albumBaseModel])
                }
                self.myCurriculumIndex = -1
                if self.myCurriculumDataArray.count>0{
                    //有我的课程
                    if self.isFirstMyCurriculum{
                        //第一个区块显示
                        self.myCurriculumIndex = 0
                    }else{
                        //第二个区块显示
                        self.myCurriculumIndex = 1
                    }
                }else{
                    //没有我的课程
                    self.myCurriculumIndex = -1
                }
                if self.myCurriculumIndex > -1{
                    self.dataArray.insert(albumBaseModel, at: self.myCurriculumIndex)
                }
                self.tableView.reloadData()
            }else if (status == .failed){
//                self.NetApiStatus = .apiError
            }else if (status == .noNetwork){
                
//                self.NetApiStatus = .notNetwork
            }
        }
    }
}