Wallpaper_Home/wallpaper_project/Home/C/WA_4KVC.swift
2024-08-19 14:40:12 +08:00

523 lines
20 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// WA_4KVC.swift
// wallpaper_project
import UIKit
import JXSegmentedView
import MJExtension
import MJRefresh
import FirebaseAnalytics
import AppLovinSDK
import Alamofire
import FirebaseRemoteConfig
import MTGSDKNewInterstitial
import MTGSDKBidding
import MTGSDK
import AnyThinkInterstitial
class WA_4KVC: WA_RootVC {
@IBOutlet weak var collectionView: UICollectionView!
var wallpapers = [WA_3DModel]()
var currentPage: Int = 1 //
let pageSize: Int = 10 //
var interstitialAd: MAInterstitialAd!
var retryAttempt = 0.0
//
let header = MJRefreshNormalHeader()
//
let footer = MJRefreshAutoNormalFooter()
// var MKArr = [WallpaperModel]()
//
var newestWallpapers: [WallpaperData] = []
var popularWallpapers: [WallpaperData] = []
var FlowersWallpapers: [WallpaperData] = []
var SuperheroesWallpapers: [WallpaperData] = []
var ChristmasWallpapers: [WallpaperData] = []
var remoteConfig: RemoteConfig!
var isadshow:Bool = false
var bidToken:String?
var newInterstitialAdManager:MTGNewInterstitialBidAdManager?
@IBOutlet weak var nameLabel: UILabel!
var type:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// self.createInterstitialAd()
Analytics.logEvent("in_4k", parameters: nil)
self.view.backgroundColor = .white
setCollectionView()
setRefresh()
if StartManager.shared.superHero == true{
// return dataimag.count
// cell.typeImagV.image = UIImage(named: "\(self.dataimag[indexPath.row])")
if self.type == 1{
self.nameLabel.text = "4K"
// self.wallpapers.count
}else if type == 2{
self.nameLabel.text = "Newest"
}else if type == 3{
self.nameLabel.text = "Popular Wallpapers"
}else if type == 4{
self.nameLabel.text = "Superheroes"
}else if type == 5{
self.nameLabel.text = "Christmas Wallpapers"
}else{
self.nameLabel.text = "Flowers"
}
}else{
// return dataimaghero.count
// cell.typeImagV.image = UIImage(named: "\(self.dataimaghero[indexPath.row])")
if self.type == 1{
self.nameLabel.text = "4K"
// self.wallpapers.count
}else if type == 2{
self.nameLabel.text = "Newest"
}else if type == 3{
self.nameLabel.text = "Popular Wallpapers"
}else if type == 4{
self.nameLabel.text = "Christmas Wallpapers"
}else{
self.nameLabel.text = "Flowers"
}
}
}
@IBAction func backbtn(_ sender: Any) {
if self.isadshow == true{
if ATAdManager.shared().interstitialReady(forPlacementID: "n66bdc1da370ec"){
ATAdManager.shared().showInterstitial(withPlacementID: "n66bdc1da370ec", in: self, delegate: self)
StartManager.shared.shelfNumber = "静态页面壁纸返回show"
setPostSHOW()
}else{
self.navigationController?.popViewController(animated: true)
}
}else{
self.navigationController?.popViewController(animated: true)
}
}
@objc func setRefresh(){
refreshData()
header.setRefreshingTarget(self, refreshingAction: #selector(WA_LIVEVC.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(WA_LIVEVC.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
setJsondata()
settypeNetwork()
}
//
@objc func loadMoreData() {
currentPage += 1
setJsondata()
settypeNetwork()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
// Remote Config
remoteConfig = RemoteConfig.remoteConfig()
//
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 0
remoteConfig.configSettings = settings
// Fetch
remoteConfig.setDefaults(fromPlist: "isopen")
// fetchRemoteConfig()
loadTopAD()
}
func loadTopAD(){
let extra: [String: Any] = [
kATAdLoadingExtraMediaExtraKey: "custom_values"
]
ATAdManager.shared().loadAD(withPlacementID: "n66bdc1da370ec", extra: extra, delegate: self)
self.fetchRemoteConfig()
}
///广
func fetchRemoteConfig() {
let localVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0"
remoteConfig.fetch{ (status, error) -> Void in
if status == .success {
print("Config fetched!")
self.remoteConfig.activate { changed, error in
if error == nil{
let js = self.remoteConfig.configValue(forKey: "isopen").jsonValue as! [String:Any]
let valueopen = js["isadopen"] as! Bool
let valueversion = js["version"] as! String
DispatchQueue.main.async {
if valueversion == localVersion{
self.isadshow = false
}else{
if valueopen {
self.isadshow = true
} else {
self.isadshow = false
}
}
}
}
}
} else {
print("Config not fetched")
if let error = error {
print("Error: \(error.localizedDescription)")
}
}
}
}
func setCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
// collection view
let layout = CustomCollectionViewFlowLayout()
collectionView.collectionViewLayout = layout
layout.scrollDirection = .vertical
collectionView.register(UINib(nibName: "WA_WallpaperCollectionCell", bundle: nil), forCellWithReuseIdentifier: "WA_WallpaperCollectionCell")
}
func setJsondata(){
// JSON
if let path = Bundle.main.path(forResource: "4k", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
// 使 MJExtension JSON
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String: Any]]
if let jsonArray = jsonArray {
// 使 MJExtension JSON
let wallpapersarr = (WA_3DModel.mj_objectArray(withKeyValuesArray: jsonArray) as? [WA_3DModel])!
if currentPage == 1 {
// 10
let firstTenWallpapers = Array(wallpapersarr.prefix(10))
self.wallpapers = firstTenWallpapers
collectionView.reloadData()
collectionView.mj_header?.endRefreshing()
} else {
//
let startIndex = currentPage * pageSize
let moreWallpapers = Array(wallpapersarr.prefix(startIndex))
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5){
self.wallpapers = moreWallpapers
self.collectionView.reloadData()
self.collectionView.mj_footer?.endRefreshing()
}
}
}
} catch {
print("Error reading JSON file:", error.localizedDescription)
}
} else {
print("JSON file not found.")
}
}
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)
// WallpaperModel
for wallpaperModel in wallpaperModels {
//
switch wallpaperModel.name {
case "Newest":
newestWallpapers = wallpaperModel.data
case "Popular Wallpapers":
popularWallpapers = wallpaperModel.data
case "Superheroes":
SuperheroesWallpapers = wallpaperModel.data
case "Christmas Wallpapers":
ChristmasWallpapers = wallpaperModel.data
case "Flowers":
FlowersWallpapers = wallpaperModel.data
default:
break
}
}
} catch {
print("Error reading or parsing JSON file: \(error)")
}
}
}
extension WA_4KVC:JXSegmentedListContainerViewListDelegate{
func listView() -> UIView {
return view
}
}
extension WA_4KVC:ATInterstitialDelegate{
// 广
func interstitialDidShow(forPlacementID placementID: String, extra: [AnyHashable : Any]) {
print("----成功")
}
/// 广
func interstitialDidClick(forPlacementID placementID: String, extra: [AnyHashable : Any]) {
print("----点击")
}
// 广
func interstitialDidClose(forPlacementID placementID: String, extra: [AnyHashable : Any]) {
print("----关闭")
// WA_TabbarCommon.TabBarController()
self.navigationController?.popViewController(animated: true)
}
func didFinishLoadingAD(withPlacementID placementID: String!) {
print("ATInterstitialViewController::didFailToLoadADWithPlacementID:\(placementID)")
}
// load
func didFailToLoadAD(withPlacementID placementID: String!, error: (any Error)!) {
print("----load失败")
print("ATInterstitialViewController::didFailToLoadADWithPlacementID:\(String(describing: placementID))---error\(String(describing: error))")
}
// 广
func didFinishLoadingADSource(withPlacementID placementID: String!,extra: [AnyHashable : Any]?) {
let networkID = extra?["network_firm_id"]
let ecpm = extra?["adsource_price"]
let country = extra?["country"]
if let adsourcePriceString = extra?["adsource_price"] as? String,
let adsourcePrice = Double(adsourcePriceString) {
StartManager.shared.ecpm = Float(adsourcePrice) / 1000
print("-----ecpm ID: \(StartManager.shared.ecpm)")
} else {
print("无法获取 adsource_price 或类型不匹配")
}
StartManager.shared.countryCode = country as? String
StartManager.shared.network = network(networkID as! Int)
StartManager.shared.shelfNumber = "静态页面壁纸load"
StartManager.shared.adId = "n66bdc1da370ec"
setPostload()
print("ATInterstitialViewController::didFailToLoadADWithPlacementID:\(placementID)")
}
}
extension WA_4KVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if StartManager.shared.superHero == true{
if self.type == 1{
return self.wallpapers.count
}else if type == 2{
return self.newestWallpapers.count
}else if type == 3{
return self.popularWallpapers.count
}else if type == 4{
return self.SuperheroesWallpapers.count
}else if type == 5{
return self.ChristmasWallpapers.count
}else{
return self.FlowersWallpapers.count
}
}else{
if self.type == 1{
return self.wallpapers.count
}else if type == 2{
return self.newestWallpapers.count
}else if type == 3{
return self.popularWallpapers.count
}else if type == 4{
return self.ChristmasWallpapers.count
}else{
return self.FlowersWallpapers.count
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_WallpaperCollectionCell", for: indexPath)as!WA_WallpaperCollectionCell
if StartManager.shared.superHero == true{
if self.type == 1{
cell.model = self.wallpapers[indexPath.row]
}else if type == 2{
let model = self.newestWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else if type == 3{
let model = self.popularWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else if type == 4{
let model = self.SuperheroesWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else if type == 5{
let model = self.ChristmasWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else{
let model = self.FlowersWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}
}else{
if self.type == 1{
cell.model = self.wallpapers[indexPath.row]
}else if type == 2{
let model = self.newestWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else if type == 3{
let model = self.popularWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else if type == 4{
let model = self.ChristmasWallpapers[indexPath.row]
if let imageURL = URL(string: model.previewThumb ?? "") {
cell.wpImagV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
}
}else{
let model = self.FlowersWallpapers[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) {
let vc = WA_DetailsVC()
vc.type = self.type
if StartManager.shared.superHero == true{
if self.type == 1{
vc.model = self.wallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 2{
vc.modeltype = self.newestWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 3{
vc.modeltype = self.popularWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 4{
vc.modeltype = self.SuperheroesWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 5{
vc.modeltype = self.ChristmasWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else{
vc.modeltype = self.FlowersWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}else{
if self.type == 1{
vc.model = self.wallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 2{
vc.modeltype = self.newestWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 3{
vc.modeltype = self.popularWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else if type == 4{
vc.modeltype = self.ChristmasWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}else{
vc.modeltype = self.FlowersWallpapers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}
}
}