WallPaperHome722/WallpaperHD_Live/Home/C/WP_Christmas.swift
2024-07-22 15:18:42 +08:00

166 lines
5.4 KiB
Swift

//
// WP_Christmas.swift
// WallpaperHD_Live
//
// Created by 16 on 2024/7/22.
//
import UIKit
import JXSegmentedView
import MJRefresh
import SDWebImage
class WP_Christmas: WP_RootVC {
@IBOutlet weak var collectionView: UICollectionView!
var christmasWallpapers: [WallpaperData] = []
var currentPage: Int = 1 //
let pageSize: Int = 10 //
//
let header = MJRefreshNormalHeader()
//
let footer = MJRefreshAutoNormalFooter()
override func viewDidLoad() {
super.viewDidLoad()
setCollectionView()
setRefresh()
}
@objc func setRefresh(){
refreshData()
header.setRefreshingTarget(self, refreshingAction: #selector(WP_NewestVC.setRefresh))
// mj_header
header.setTitle("pull-to-refresh", for: .idle)
header.setTitle("Release updates", for: .pulling)
header.setTitle("Refreshing...", for: .refreshing)
self.collectionView.mj_header = header
//
footer.setRefreshingTarget(self, refreshingAction: #selector(WP_NewestVC.loadMoreData))
footer.setTitle("Pull up loading", for: .idle)
footer.setTitle("Release Load", for: .pulling)
footer.setTitle("Loading...", for: .refreshing)
self.collectionView.mj_footer = footer
header.beginRefreshing()
// footer.beginRefreshing()
}
//
@objc func refreshData() {
currentPage = 1
settypeNetwork()
}
//
@objc func loadMoreData() {
currentPage += 1
settypeNetwork()
}
func setCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
// collection view
let layout = CustomCollectionViewFlowLayout()
collectionView.collectionViewLayout = layout
layout.scrollDirection = .vertical
collectionView.register(UINib(nibName: "WP_WallpaperCollectionCell", bundle: nil), forCellWithReuseIdentifier: "WP_WallpaperCollectionCell")
}
func settypeNetwork(){
// JSON
guard let jsonFilePath = Bundle.main.path(forResource: "my_wallpaper", ofType: "json") else {
fatalError("Unable to locate my_wallpaper.json file.")
}
// JSON
do {
// JSON
let jsonData = try Data(contentsOf: URL(fileURLWithPath: jsonFilePath))
// JSON Swift
let decoder = JSONDecoder()
let wallpaperModels = try decoder.decode([WallpaperModel].self, from: jsonData)
var newwallpaperarr:[WallpaperData] = []
// WallpaperModel
for wallpaperModel in wallpaperModels {
//
switch wallpaperModel.name {
case "Christmas Wallpapers":
newwallpaperarr = wallpaperModel.data
default:
break
}
}
if currentPage == 1 {
// 10
let firstTenWallpapers = Array(newwallpaperarr.prefix(10))
self.christmasWallpapers = firstTenWallpapers
collectionView.reloadData()
collectionView.mj_header?.endRefreshing()
} else {
//
let startIndex = currentPage * pageSize
let moreWallpapers = Array(newwallpaperarr.prefix(startIndex))
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5){
self.christmasWallpapers = moreWallpapers
self.collectionView.reloadData()
self.collectionView.mj_footer?.endRefreshing()
}
}
} catch {
print("Error reading or parsing JSON file: \(error)")
}
}
}
extension WP_Christmas:JXSegmentedListContainerViewListDelegate{
func listView() -> UIView {
return view
}
}
extension WP_Christmas:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.christmasWallpapers.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WP_WallpaperCollectionCell", for: indexPath)as!WP_WallpaperCollectionCell
let model = self.christmasWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}