563 lines
22 KiB
Swift
563 lines
22 KiB
Swift
//
|
||
// WA_DetailsVC.swift
|
||
// wallpaper_project
|
||
|
||
|
||
import UIKit
|
||
import SDWebImage
|
||
import Photos
|
||
import SVProgressHUD
|
||
import AppLovinSDK
|
||
import FirebaseAnalytics
|
||
import Alamofire
|
||
import FirebaseRemoteConfig
|
||
import AnyThinkInterstitial
|
||
|
||
class WA_DetailsVC: WA_RootVC {
|
||
|
||
|
||
@IBOutlet weak var bgView: UIView!
|
||
|
||
@IBOutlet weak var backBtn: UIButton!
|
||
|
||
|
||
@IBOutlet weak var setBtn: UIButton!
|
||
|
||
@IBOutlet weak var preimageV: UIImageView!
|
||
|
||
var model = WA_3DModel()
|
||
|
||
var modeltype = WallpaperData()
|
||
|
||
var preV:UIView?
|
||
|
||
let activityView = UIActivityIndicatorView()
|
||
|
||
var type:Int = 0
|
||
|
||
var interstitialAd: MAInterstitialAd!
|
||
var retryAttempt = 0.0
|
||
|
||
var isback:Bool = false
|
||
|
||
var remoteConfig: RemoteConfig!
|
||
|
||
var isadshow:Bool = false
|
||
var bidToken:String?
|
||
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
// self.createInterstitialAd()
|
||
// interstitialAd.load()
|
||
|
||
if type == 1{
|
||
let imageURL = URL(string: model.preview ?? "")
|
||
preimageV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
|
||
}else{
|
||
let imageURL = URL(string: modeltype.source ?? "")
|
||
preimageV.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "Rectangle"))
|
||
}
|
||
Analytics.logEvent("in_Details", parameters: nil)
|
||
|
||
|
||
setBtn.layer.cornerRadius = 10
|
||
|
||
// 添加手势识别器到 UIView 上
|
||
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
|
||
preimageV.isUserInteractionEnabled = true
|
||
preimageV.addGestureRecognizer(tapGesture)
|
||
|
||
activityView.center = CGPoint(x: self.view.center.x, y: self.view.center.y - kSafeArea_Top - 44)
|
||
// 停止后,隐藏菊花
|
||
activityView.hidesWhenStopped = true
|
||
activityView.color = .black;
|
||
activityView.style = UIActivityIndicatorView.Style.whiteLarge
|
||
|
||
self.view.addSubview(activityView)
|
||
backBtn.layer.cornerRadius = 20
|
||
|
||
if type == 1{
|
||
let model1 = WA_3DModel()
|
||
model1.preview = self.model.preview
|
||
model1.title = self.model.title
|
||
model1.thumbnail = self.model.thumbnail
|
||
model1.is_free = self.model.is_free
|
||
model1.category = self.model.category
|
||
model1.id = self.model.id
|
||
model1.cnt_like = self.model.cnt_like
|
||
HistoryManager.shared.addHistoryItem(item: model1)
|
||
}else{
|
||
let model1 = WallpaperData()
|
||
model1.original = self.modeltype.original
|
||
model1.previewThumb = self.modeltype.previewThumb
|
||
model1.source = self.modeltype.source
|
||
model1.banner = self.modeltype.banner
|
||
HistoryjpgManager.shared.addHistoryItem(item: model1)
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
@IBAction func setting(_ sender: Any) {
|
||
|
||
Analytics.logEvent("touch_savephoto", parameters: nil)
|
||
self.activityView.startAnimating()
|
||
saveImageToPhotoLibrary()
|
||
|
||
if self.isadshow == true{
|
||
if ATAdManager.shared().interstitialReady(forPlacementID: "n66bdc1aea4d27"){
|
||
ATAdManager.shared().showInterstitial(withPlacementID: "n66bdc1aea4d27", in: self, delegate: self)
|
||
StartManager.shared.shelfNumber = "详情页面SHOW"
|
||
setPostSHOW()
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
|
||
self.activityView.stopAnimating()
|
||
// self.view.makeToast("Save Success", duration: 1.5, position: .center)
|
||
// 创建一个 UIAlertController
|
||
let alertController = UIAlertController(title: "Successfully saved", message: "Do you need to set up a tutorial", preferredStyle: .alert)
|
||
|
||
// 添加一个取消按钮
|
||
alertController.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
|
||
|
||
// 添加一个确定按钮
|
||
alertController.addAction(UIAlertAction(title: "Need", style: .default, handler: { action in
|
||
// 点击确定按钮后执行的操作
|
||
// print("点击了确定按钮")
|
||
let vc = WA_TipVC()
|
||
self.present(vc, animated: true)
|
||
}))
|
||
|
||
// 在当前视图控制器中 present 显示提示框
|
||
self.present(alertController, animated: true, completion: nil)
|
||
}
|
||
}
|
||
}else{
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
|
||
self.activityView.stopAnimating()
|
||
// self.view.makeToast("Save Success", duration: 1.5, position: .center)
|
||
// 创建一个 UIAlertController
|
||
let alertController = UIAlertController(title: "Successfully saved", message: "Do you need to set up a tutorial", preferredStyle: .alert)
|
||
|
||
// 添加一个取消按钮
|
||
alertController.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
|
||
|
||
// 添加一个确定按钮
|
||
alertController.addAction(UIAlertAction(title: "Need", style: .default, handler: { action in
|
||
// 点击确定按钮后执行的操作
|
||
// print("点击了确定按钮")
|
||
let vc = WA_TipVC()
|
||
self.present(vc, animated: true)
|
||
}))
|
||
|
||
// 在当前视图控制器中 present 显示提示框
|
||
self.present(alertController, animated: true, completion: nil)
|
||
}
|
||
}
|
||
}
|
||
|
||
}else{
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
|
||
self.activityView.stopAnimating()
|
||
// self.view.makeToast("Save Success", duration: 1.5, position: .center)
|
||
// 创建一个 UIAlertController
|
||
let alertController = UIAlertController(title: "Successfully saved", message: "Do you need to set up a tutorial", preferredStyle: .alert)
|
||
|
||
// 添加一个取消按钮
|
||
alertController.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
|
||
|
||
// 添加一个确定按钮
|
||
alertController.addAction(UIAlertAction(title: "Need", style: .default, handler: { action in
|
||
// 点击确定按钮后执行的操作
|
||
// print("点击了确定按钮")
|
||
let vc = WA_TipVC()
|
||
self.present(vc, animated: true)
|
||
}))
|
||
|
||
// 在当前视图控制器中 present 显示提示框
|
||
self.present(alertController, animated: true, completion: nil)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
override func viewWillAppear(_ animated: Bool) {
|
||
super.viewWillAppear(animated)
|
||
navigationController?.navigationBar.isHidden = true
|
||
// self.createInterstitialAd()
|
||
|
||
// 初始化 Remote Config
|
||
remoteConfig = RemoteConfig.remoteConfig()
|
||
|
||
|
||
|
||
// 设置最小获取间隔(开发期间可以设置较小的值)
|
||
let settings = RemoteConfigSettings()
|
||
settings.minimumFetchInterval = 0
|
||
remoteConfig.configSettings = settings
|
||
// Fetch 配置值
|
||
remoteConfig.setDefaults(fromPlist: "isopen")
|
||
// Fetch 配置值
|
||
|
||
|
||
loadTopAD()
|
||
|
||
}
|
||
func loadTopAD(){
|
||
let extra: [String: Any] = [
|
||
kATAdLoadingExtraMediaExtraKey: "custom_values"
|
||
]
|
||
ATAdManager.shared().loadAD(withPlacementID: "n66bdc1aea4d27", 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 saveImageToPhotoLibrary() {
|
||
guard let image = self.preimageV.image else {
|
||
print("找不到要保存的图片")
|
||
SVProgressHUD.showInfo(withStatus: "Can't find the picture to save")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
return
|
||
}
|
||
|
||
PHPhotoLibrary.requestAuthorization { status in
|
||
switch status {
|
||
case .authorized:
|
||
self.saveImage(image: image)
|
||
case .denied, .restricted:
|
||
print("用户拒绝了访问相册权限")
|
||
SVProgressHUD.showInfo(withStatus: "Album access denied")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
case .notDetermined:
|
||
print("用户尚未选择是否允许访问相册")
|
||
SVProgressHUD.showInfo(withStatus: "You have not chosen whether to allow access to the album")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
@unknown default:
|
||
break
|
||
}
|
||
}
|
||
}
|
||
|
||
func saveImage(image: UIImage) {
|
||
PHPhotoLibrary.shared().performChanges({
|
||
// 创建向照片库添加新图像资产的请求
|
||
PHAssetChangeRequest.creationRequestForAsset(from: image)
|
||
}) { success, error in
|
||
if success {
|
||
print("图片保存成功")
|
||
|
||
|
||
} else if let error = error {
|
||
print("图片保存失败:\(error.localizedDescription)")
|
||
SVProgressHUD.showError(withStatus: "Image saving failed:\(error.localizedDescription)")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
@IBAction func collectBtn(_ sender: Any) {
|
||
Analytics.logEvent("touch_collect", parameters: nil)
|
||
if isLoggedIn(){
|
||
if type == 1{
|
||
let model1 = WA_3DModel()
|
||
model1.preview = self.model.preview
|
||
model1.title = self.model.title
|
||
model1.thumbnail = self.model.thumbnail
|
||
model1.is_free = self.model.is_free
|
||
model1.category = self.model.category
|
||
model1.id = self.model.id
|
||
model1.cnt_like = self.model.cnt_like
|
||
sc4kManager.shared.addHistoryItem(item: model1)
|
||
}else{
|
||
let model1 = WallpaperData()
|
||
model1.original = self.modeltype.original
|
||
model1.previewThumb = self.modeltype.previewThumb
|
||
model1.source = self.modeltype.source
|
||
model1.banner = self.modeltype.banner
|
||
scjpgManager.shared.addHistoryItem(item: model1)
|
||
}
|
||
SVProgressHUD.showInfo(withStatus: "Collection successful")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
}else{
|
||
let vc = WA_LoginVC()
|
||
navigationController?.pushViewController(vc, animated: true)
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
@IBAction func previewBtn(_ sender: Any) {
|
||
Analytics.logEvent("touch_preview", parameters: nil)
|
||
UIView.animate(withDuration: 0.5, animations: {
|
||
self.bgView.frame.origin.y += 0
|
||
self.bgView.alpha = 0
|
||
self.backBtn.isHidden = true
|
||
})
|
||
|
||
showView()
|
||
}
|
||
|
||
// 手势识别器的处理方法
|
||
@objc func viewTapped() {
|
||
// 根据当前 UIView 的位置和透明度进行不同的动画
|
||
if bgView.frame.origin.y == UIScreen.main.bounds.height - bgView.frame.height {
|
||
// 当 UIView 在底部时,执行向下移动并渐变消失的动画
|
||
UIView.animate(withDuration: 0.5, animations: {
|
||
self.bgView.frame.origin.y += self.bgView.frame.height
|
||
self.bgView.alpha = 0
|
||
self.backBtn.isHidden = true
|
||
})
|
||
} else {
|
||
|
||
self.backBtn.isHidden = false
|
||
// 当 UIView不在底部时,执行向上移动并渐变显示的动画
|
||
UIView.animate(withDuration: 0.5, animations: {
|
||
self.bgView.frame.origin.y -= self.bgView.frame.height
|
||
self.bgView.alpha = 1
|
||
})
|
||
}
|
||
}
|
||
|
||
func showView() {
|
||
// 加载预览视图并添加到当前视图控制器的视图上
|
||
let previewView = Bundle.main.loadNibNamed("WA_PreView", owner: nil, options: nil)?.first as! WA_PreView
|
||
previewView.flashView.layer.cornerRadius = 25
|
||
previewView.cameraV.layer.cornerRadius = 25
|
||
|
||
previewView.touchBlock = {
|
||
|
||
self.backBtn.isHidden = false
|
||
// 当 UIView 不在底部时,执行向上移动并渐变显示的动画
|
||
UIView.animate(withDuration: 0.5, animations: {
|
||
self.bgView.frame.origin.y -= 0
|
||
self.bgView.alpha = 1
|
||
})
|
||
}
|
||
view.addSubview(previewView)
|
||
|
||
|
||
// 设置预览视图初始位置在屏幕左侧外面
|
||
previewView.frame.origin.x = -previewView.frame.width
|
||
|
||
// 执行动画
|
||
UIView.animate(withDuration: 1.0) {
|
||
// 将预览视图移回主视图中心
|
||
previewView.frame = self.view.frame
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
@IBAction func shareBtn(_ sender: Any) {
|
||
Analytics.logEvent("touch_share", parameters: nil)
|
||
|
||
self.activityView.startAnimating()
|
||
|
||
// 创建要分享的图片
|
||
guard let image = self.preimageV.image else {
|
||
SVProgressHUD.showInfo(withStatus: "Can't find the image to share")
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
||
SVProgressHUD.dismiss()
|
||
}
|
||
print("找不到要分享的图片")
|
||
return
|
||
}
|
||
|
||
// 创建要分享的对象数组
|
||
let items: [Any] = [image]
|
||
|
||
// 创建 UIActivityViewController 实例
|
||
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
|
||
|
||
// 设置 UIActivityViewController 的主题
|
||
activityViewController.modalPresentationStyle = .popover
|
||
|
||
// 在 iPad 上设置 UIActivityViewController 的位置
|
||
if let popoverController = activityViewController.popoverPresentationController {
|
||
popoverController.sourceView = self.view
|
||
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
|
||
popoverController.permittedArrowDirections = []
|
||
}
|
||
|
||
// 弹出分享视图
|
||
self.present(activityViewController, animated: true, completion: nil)
|
||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
|
||
self.activityView.stopAnimating()
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
@IBAction func back(_ sender: Any) {
|
||
isback = true
|
||
|
||
if self.isadshow == true{
|
||
if ATAdManager.shared().interstitialReady(forPlacementID: "n66bdc1aea4d27"){
|
||
ATAdManager.shared().showInterstitial(withPlacementID: "n66bdc1aea4d27", in: self, delegate: self)
|
||
StartManager.shared.shelfNumber = "详情页面SHOW"
|
||
setPostSHOW()
|
||
self.navigationController?.popViewController(animated: true)
|
||
}else{
|
||
self.navigationController?.popViewController(animated: true)
|
||
}
|
||
|
||
}else{
|
||
self.navigationController?.popViewController(animated: true)
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
extension WA_DetailsVC: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()
|
||
if isback == true{
|
||
self.navigationController?.popViewController(animated: true)
|
||
}else{
|
||
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
|
||
self.activityView.stopAnimating()
|
||
// self.view.makeToast("Save Success", duration: 1.5, position: .center)
|
||
// 创建一个 UIAlertController
|
||
let alertController = UIAlertController(title: "Successfully saved", message: "Do you need to set up a tutorial", preferredStyle: .alert)
|
||
|
||
// 添加一个取消按钮
|
||
alertController.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
|
||
|
||
// 添加一个确定按钮
|
||
alertController.addAction(UIAlertAction(title: "Need", style: .default, handler: { action in
|
||
// 点击确定按钮后执行的操作
|
||
// print("点击了确定按钮")
|
||
let vc = WA_TipVC()
|
||
self.present(vc, animated: true)
|
||
}))
|
||
|
||
// 在当前视图控制器中 present 显示提示框
|
||
self.present(alertController, animated: true, completion: nil)
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
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 = "n66bdc1aea4d27"
|
||
setPostload()
|
||
|
||
print("ATInterstitialViewController::didFailToLoadADWithPlacementID:\(placementID)")
|
||
|
||
}
|
||
}
|