Music_Player3/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift
2024-07-05 17:25:49 +08:00

246 lines
9.7 KiB
Swift
Raw 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.

//
// Macro.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/3/25.
//
import UIKit
import Foundation
import AVFoundation
import AppTrackingTransparency
import AdSupport
import MessageUI
@_exported import JXSegmentedView
@_exported import JXPagingView
//JXPagingListContainerViewextensionJXSegmentedViewListContainer
extension JXPagingListContainerView: JXSegmentedViewListContainer {}
//MARK: -
///
let screen_Width = UIScreen.main.bounds.width
///
let screen_Height = UIScreen.main.bounds.height
///
let width = screen_Width / 375
///
let height = screen_Height / 667
///
#if __IPHONE_13_0
let statusBarHeight:CGFloat = UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.size.height
#else
let statusBarHeight:CGFloat = UIApplication.shared.statusBarFrame.size.height
#endif
///
let navAndstatusBarHeight = statusBarHeight + 44
///
let iphoneX = ((statusBarHeight != 20) ? true : false)
///
let LOCAL_RELEASE_VERSION = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
///
let App_Name = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
///
let Phone_Model = UIDevice.current.model
///
let System_Version = UIDevice.current.systemVersion
///
var Language_first_local:String {
let first = Locale.preferredLanguages.first!
let languageCode = Locale(identifier: first).languageCode
if let code = languageCode {
return code
} else {
return "en"
}
}
///
var app_Version:String{
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String {
return version
}else {
return "1.0.0"
}
}
///
let bottomPadding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
///
let placeholderImage:UIImage = UIImage(named: "placeholder")!
///
let privacyUrl:URL = .init(string: "https://musiclax.mystrikingly.com/privacy")!
///
let serviceUrl:URL = .init(string: "https://musiclax.mystrikingly.com/terms")!
//MARK: -
///
typealias ActionBlock = () -> Void?
///A
var MPSideA_ModalType:MPSideA_PresentModal = .Timer
///B
var MPPositive_ModalType:MPPositive_PresentModal = .PlayerList
///nextIDID
func improveDataforLycirsAndRelated(_ song:MPPositive_SongItemModel, completion:@escaping(((String?,String?)) -> Void)) {
//next
MP_NetWorkManager.shared.requestNextLyricsAndRelated(song){ result in
completion(result)
}
}
///player
func improveDataforResouceAndCover(_ song:MPPositive_SongItemModel, completion:@escaping((([String],[Int],[String])?, [String]?) -> Void)) {
//player
MP_NetWorkManager.shared.requestAndroidPlayer(song.videoId, playlistId: "") { resourceUrls, coverUrls in
completion(resourceUrls,coverUrls)
}
}
///
func setTimesToMinSeconds(_ time:TimeInterval) -> String {
//
let min = Int((time.isNaN ? 0:time) / 60)
let second = Int((time.isNaN ? 0:time).truncatingRemainder(dividingBy: 60))
return "\(min < 10 ? "0\(min)":"\(min)"):\(second < 10 ? "0\(second)":"\(second)")"
}
///
func setTimesToMins(_ time:TimeInterval) -> String {
//
let min = Int((time.isNaN ? 0:time) / 60)
return "\(min < 10 ? "0\(min)":"\(min)")"
}
///
func authorize(observe:UIViewController) -> Bool{
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
switch status {
case .authorized:
return true
case .notDetermined:
//
AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: {(status) in
DispatchQueue.main.async(execute: {() -> Void in
_ = authorize(observe: observe)
})
})
default: ()
DispatchQueue.main.async(execute: { () -> Void in
let alertController = UIAlertController(title: "Get Microphone Access",message: "“Musiclax” asks you to turn on your microphone to recognize the decibels around you and turns on white noise for you automatically. Please go to the “Settings” page to turn on the microphone permission",preferredStyle: .alert)
let cancelAction = UIAlertAction(title:"Cancel", style: .cancel, handler:nil)
let settingsAction = UIAlertAction(title:"Settings", style: .default, handler: {
(action) -> Void in
let url = URL(string: UIApplication.openSettingsURLString)
if let url = url, UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
})
} else {
UIApplication.shared.openURL(url)
}
}
})
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
observe.present(alertController, animated: true)
})
}
return false
}
///Label
func createLabel(_ text:String? = nil, font:UIFont, textColor:UIColor, textAlignment:NSTextAlignment, lines:Int = 1) -> UILabel {
let label = UILabel()
label.text = text ?? "text"
label.font = font
label.textColor = textColor
label.textAlignment = textAlignment
label.numberOfLines = lines
return label
}
///
func switchPlayTypeBtnIcon(_ btn:UIButton) {
switch MP_PlayerManager.shared.getPlayType() {
case .normal://
btn.setBackgroundImage(UIImage(named: "Player_Normal'logo"), for: .normal)
case .random://
btn.setBackgroundImage(UIImage(named: "Player_Shuffle'logo"), for: .normal)
case .single://
btn.setBackgroundImage(UIImage(named: "Player_Single'logo"), for: .normal)
}
}
///广
func requestTrackingAuthorization(completion: @escaping (String?) -> Void) {
if #available(iOS 14, *) {
var attemptsLeft = 3
func requestAuth() {
ATTrackingManager.requestTrackingAuthorization { status in
switch status {
case .authorized:
let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
print("Authorized: IDFA = \(idfa)")
completion(idfa)
case .denied, .restricted:
print("Denied or Restricted")
completion(nil)
case .notDetermined:
print("Not Determined")
attemptsLeft -= 1
if attemptsLeft > 0 {
requestAuth() //
} else {
print("Reached maximum number of attempts")
completion(nil)
}
@unknown default:
print("Unknown status")
completion(nil)
}
}
}
requestAuth() //
} else {
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
print("Tracking enabled: IDFA = \(idfa)")
completion(idfa)
} else {
print("Tracking not enabled")
completion(nil)
}
}
}
///\n\n
func truncateTextAfterTwoNewlines(from text: String) -> String {
//
let pattern = "\n{2,}.*"
let regex = try! NSRegularExpression(pattern: pattern, options: [.dotMatchesLineSeparators])
// 使
let truncatedText = regex.stringByReplacingMatches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count), withTemplate: "")
return truncatedText
}
///
func saveLoadVideoItem(_ song:MPPositive_SongItemModel, completion:(() -> Void)?) {
DispatchQueue.global(qos: .background).async {
//
let item = MPPositive_DownloadItemModel.create()
item.coverImage = song.coverUrls!.last
item.reviewImage = song.reviewUrls!.last
item.title = song.title
item.longBylineText = song.longBylineText
item.lengthText = song.lengthText
item.shortBylineText = song.shortBylineText
item.lyrics = song.lyrics
item.lyricsID = song.lyricsID
item.videoId = song.videoId
item.relatedID = song.relatedID
//
MPPositive_DownloadItemModel.save(true)
completion?()
DispatchQueue.main.async {
//
MPPositive_LoadCoreModel.shared.reloadLoadSongViewModel(nil)
//
MP_AnalyticsManager.shared.player_b_downloadsuccess_actionAction(item.videoId, videoname: item.title ?? "", artistname: item.shortBylineText ?? "")
print("完成了对\(song.title ?? "")的下载数据保存")
}
}
}