Music_Player3/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift

244 lines
9.4 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
@_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
///
let DocumentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
///
func getDocumentsFileURL(_ videoID: String) -> String? {
// DocumentsURL
let documentsDirectoryURL = DocumentsURL.appendingPathComponent("Downloads")
// videoIdURL
let fileURL = documentsDirectoryURL.appendingPathComponent("\(videoID).mp4")
//
if FileManager.default.fileExists(atPath: fileURL.path) == true {
//
return fileURL.absoluteString
}else {
return nil
}
}
///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: "List_NormolPlay'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 getUserNotificationCenter() {
//
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) {
(accepted, error) in
if !accepted {
print("Users are not allowed to be notified of messages.")
}
}
}