diff --git a/relax.offline.mp3.music.xcodeproj/project.pbxproj b/relax.offline.mp3.music.xcodeproj/project.pbxproj index 2955905..19b7545 100644 --- a/relax.offline.mp3.music.xcodeproj/project.pbxproj +++ b/relax.offline.mp3.music.xcodeproj/project.pbxproj @@ -1703,7 +1703,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.1.1.1; + CURRENT_PROJECT_VERSION = 1.1.2.1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = T93S37G27F; GENERATE_INFOPLIST_FILE = YES; @@ -1723,7 +1723,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = relax.offline.mp3.music; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1746,7 +1746,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.1.1.1; + CURRENT_PROJECT_VERSION = 1.1.2.1; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = T93S37G27F; GENERATE_INFOPLIST_FILE = YES; @@ -1766,7 +1766,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = relax.offline.mp3.music; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/relax.offline.mp3.music/AppDelegate.swift b/relax.offline.mp3.music/AppDelegate.swift index 41dae6c..1388902 100644 --- a/relax.offline.mp3.music/AppDelegate.swift +++ b/relax.offline.mp3.music/AppDelegate.swift @@ -60,6 +60,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { switch_lunch() //执行用户启动事件日志 MP_AnalyticsManager.shared.user_launchAction() +// NotificationCenter.default.addObserver(self, selector: #selector(appWillTerminateAction(_ :)), name:UIApplication.willTerminateNotification, object: nil) return true } func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool { @@ -190,6 +191,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate { print("返回前台") } } + //MARK: - 当应用被用户关闭时 + func applicationWillTerminate(_ application: UIApplication) { + print("应用关闭了") + //检索应用是否播放歌曲 + guard let load = MP_PlayerManager.shared.loadPlayer, let videoId = load.currentVideoId, let songs = load.songVideos else {return} + //将songs转为数据 + guard let data = coreSongsforJson(songs) else {return} + //当前音乐情况是否合理加载了数据 + let last:[String:Any] = ["currentVideoId":videoId, + "Songs":data] + UserDefaults.standard.set(last, forKey: "Last_Play_Songs") + + } + + // MARK: - Core Data stack lazy var persistentContainer: NSPersistentContainer = { /* diff --git a/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift b/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift index 36f7b82..d5de036 100644 --- a/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift +++ b/relax.offline.mp3.music/MP/Common/Macro(宏定义与全局量)/Macro.swift @@ -198,8 +198,29 @@ func jsonforCoreAdModel(_ data:Data) -> [MPPositive_AdModelModel]? { return nil } } - - +///将Songs模型组转为Data +func coreSongsforJson(_ array:[MPPositive_SongItemModel]) -> Data? { + guard array.isEmpty != true else {return nil} + do{ + let jsonData = try JSONEncoder().encode(array) + return jsonData + }catch { + //编译失败 + print("歌曲模型组转为数据失败,失败原因:\(error.localizedDescription)") + return nil + } +} +///将Data重新转化为Songs模型组 +func jsonforCoreSongs(_ data:Data) ->[MPPositive_SongItemModel]? { + do{ + let array:[MPPositive_SongItemModel] = try JSONDecoder().decode([MPPositive_SongItemModel].self, from: data) + return array + }catch{ + //编译失败 + print("数据转化Songs失败,失败原因:\(error.localizedDescription)") + return nil + } +} ///总事件闭包 typealias ActionBlock = () -> Void? ///A面全局模态弹出类型 diff --git a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_AdMobManager.swift b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_AdMobManager.swift index 9b6782b..5f1d4b9 100644 --- a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_AdMobManager.swift +++ b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_AdMobManager.swift @@ -14,7 +14,7 @@ class MP_AdMobManager: NSObject, GADFullScreenContentDelegate, GADNativeAdLoader static let shared = MP_AdMobManager() private let sharedInstance = GADMobileAds.sharedInstance() ///广告总开关 - private var openAdStatus:Bool = true + private var openAdStatus:Bool = false ///广告过期时间(50分钟) private let expirationTime:TimeInterval = 3000 diff --git a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_NetWorkManager.swift b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_NetWorkManager.swift index 2f43160..644a1a6 100644 --- a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_NetWorkManager.swift +++ b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_NetWorkManager.swift @@ -68,11 +68,11 @@ class MP_NetWorkManager: NSObject { private var playerVersion:String! ///禁止接入IP信息组 private let banIPs:[String] = [ - "CN", - "HK", - "TW", - "JP", - "KR" +// "CN", +// "HK", +// "TW", +// "JP", +// "KR" ] ///允许访问的区域Code(对部分内容进行塞选) private let codes:[String] = [ diff --git a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_PlayerManager.swift b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_PlayerManager.swift index ceed76f..14d9f0b 100644 --- a/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_PlayerManager.swift +++ b/relax.offline.mp3.music/MP/Common/Tool(工具封装)/MP_PlayerManager.swift @@ -118,6 +118,12 @@ class MP_PlayerManager:NSObject{ } //当前音乐的字典 private var currentInfo:[String:Any]? + //是否展示最后一首状态 + private var isLast:Bool = false + //设置是否页面展示最后一首状态 + func setLastStatus(bool:Bool) { + isLast = bool + } //当前播放器状态 private var playState:MP_PlayerStateType = .Null{ didSet{ @@ -354,6 +360,11 @@ class MP_PlayerManager:NSObject{ if startActionBlock != nil { startActionBlock!() } + //当前是否上次播放展示状态,是的话,手动暂停 + if isLast { + pause() + isLast = false + } } }else { //没有足够的数据支持播放 diff --git a/relax.offline.mp3.music/MP/MPPositive/Models/Models/MPPositive_SongItemModel.swift b/relax.offline.mp3.music/MP/MPPositive/Models/Models/MPPositive_SongItemModel.swift index 8bfe661..a9cc298 100644 --- a/relax.offline.mp3.music/MP/MPPositive/Models/Models/MPPositive_SongItemModel.swift +++ b/relax.offline.mp3.music/MP/MPPositive/Models/Models/MPPositive_SongItemModel.swift @@ -7,7 +7,7 @@ import UIKit ///歌曲模型(音乐与视频)(当网络拉取内容确定为歌曲时,转为该模型) -class MPPositive_SongItemModel: NSObject { +class MPPositive_SongItemModel: NSObject, Codable { ///序列号(在当前列表中的排序) var index:Int! ///视频源路径组(等级制,默认取第一条最低质量) diff --git a/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_PlayerLoadViewModel.swift b/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_PlayerLoadViewModel.swift index 0f59122..5641af6 100644 --- a/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_PlayerLoadViewModel.swift +++ b/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_PlayerLoadViewModel.swift @@ -28,6 +28,8 @@ class MPPositive_PlayerLoadViewModel: NSObject { } } } + //当前播放音乐 + var currentVideoId:String? //监听器 private var itemObservation: NSKeyValueObservation? // private var loadQueue:DispatchQueue! @@ -45,7 +47,7 @@ class MPPositive_PlayerLoadViewModel: NSObject { //根据列表生成一份随机播放列表 self.randomVideos = self.songVideos.shuffled() self.listViewVideos = [] - + self.currentVideoId = currentVideoId } //启动对于当前音乐的监听器 private func startObservingCurrentVideoItem() { @@ -83,6 +85,8 @@ class MPPositive_PlayerLoadViewModel: NSObject { ///将选中Video的上1位,下两位项包括本身总计4项Video进行补全转为ViewModel,并播放这首音乐 func improveData(_ targetVideoId:String, isRandom:Bool = false) { + //更新当前播放音乐ID + self.currentVideoId = targetVideoId //对于选中Video的集合 var array:[MPPositive_SongItemModel] = [] if isRandom { diff --git a/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Home(首页,各项列表页,艺术家页)/MPPositive_HomeViewController.swift b/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Home(首页,各项列表页,艺术家页)/MPPositive_HomeViewController.swift index 230f6d9..cb9a4a5 100644 --- a/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Home(首页,各项列表页,艺术家页)/MPPositive_HomeViewController.swift +++ b/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Home(首页,各项列表页,艺术家页)/MPPositive_HomeViewController.swift @@ -82,6 +82,17 @@ class MPPositive_HomeViewController: MPPositive_BaseViewController, UIViewContro MPPositive_BrowseLoadViewModel.shared.reloadBrowseLists() } } + //当home展示后尝试获取最后一次播放歌曲内容 + guard let last = UserDefaults.standard.object(forKey: "Last_Play_Songs") as? [String:Any] else {return} + guard let currentVideoId = last["currentVideoId"] as? String, let data = last["Songs"] as? Data, let array = jsonforCoreSongs(data) else {return} + //数据获取完成,使用这些数据合成一个Load + let lodaViewModel = MPPositive_PlayerLoadViewModel(array, currentVideoId: currentVideoId) + lodaViewModel.improveData(currentVideoId) + //更改播放器播放类型 + MP_PlayerManager.shared.setPlayType(.normal) + MP_PlayerManager.shared.setLastStatus(bool: true) + MP_PlayerManager.shared.loadPlayer = lodaViewModel + } deinit { NotificationCenter.default.removeObserver(self) diff --git a/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Search(搜索页)/MPPositive_SearchViewController.swift b/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Search(搜索页)/MPPositive_SearchViewController.swift index 99c17a5..53d9ad8 100644 --- a/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Search(搜索页)/MPPositive_SearchViewController.swift +++ b/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Search(搜索页)/MPPositive_SearchViewController.swift @@ -68,6 +68,11 @@ class MPPositive_SearchViewController: MPPositive_BaseViewController { [weak self] in guard let self = self else {return} historyCollectionView.reloadData() + historyCollectionView.layoutIfNeeded() + guard let layout = historyCollectionView.collectionViewLayout as? MPPositive_TagFlowLayout, let height = layout.layoutAttributeds.last?.frame.maxY else {return} + historyCollectionView.snp.updateConstraints { make in + make.height.equalTo(height) + } } grideCollectionView.reloadData() } @@ -107,7 +112,7 @@ class MPPositive_SearchViewController: MPPositive_BaseViewController { historyCollectionView.snp.makeConstraints { make in make.left.right.equalToSuperview() make.top.equalTo(deleteBtn.snp.bottom).offset(6*width) - make.height.equalTo(130*width) + make.height.equalTo(30*width) } view.addSubview(grideCollectionView) grideCollectionView.snp.makeConstraints { make in @@ -159,10 +164,18 @@ class MPPositive_SearchViewController: MPPositive_BaseViewController { MPPositive_SearchTagModel.fetchAll { [weak self] items in guard let self = self else {return} items.forEach({(MPPositive_SearchTagModel.delete($0))}) + guard let layout = historyCollectionView.collectionViewLayout as? MPPositive_TagFlowLayout else {return} + layout.originxArray.removeAll() + layout.originyArray.removeAll() + layout.layoutAttributeds.removeAll() MPPositive_LoadCoreModel.shared.reloadSearchTags { [weak self] in guard let self = self else {return} historyCollectionView.reloadData() + historyCollectionView.layoutIfNeeded() + historyCollectionView.snp.updateConstraints { make in + make.height.equalTo(0) + } } } } @@ -224,6 +237,8 @@ class MPPositive_TagFlowLayout: UICollectionViewFlowLayout { var originxArray:[CGFloat]! //y值数组 var originyArray:[CGFloat]! + //记录的布局值 + var layoutAttributeds:[UICollectionViewLayoutAttributes] = [] override init() { super.init() //列间距 @@ -257,6 +272,7 @@ class MPPositive_TagFlowLayout: UICollectionViewFlowLayout { let theAttrs = layoutAttributesForItem(at: attrs.indexPath) mutArray.append(theAttrs!) }) + layoutAttributeds = mutArray return mutArray } //处理单个item的layoutAttributes diff --git a/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchResultPreviewShowView.swift b/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchResultPreviewShowView.swift index 0c2c4a1..2316f7b 100644 --- a/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchResultPreviewShowView.swift +++ b/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchResultPreviewShowView.swift @@ -125,8 +125,8 @@ class MPPositive_SearchResultPreviewShowView: UIView, JXSegmentedListContainerVi //MARK: - tableView extension MPPositive_SearchResultPreviewShowView:UITableViewDataSource, UITableViewDelegate, UIViewControllerTransitioningDelegate { func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { - if scrollBlock != nil { - scrollBlock!() + if let block = scrollBlock { + block() } } func numberOfSections(in tableView: UITableView) -> Int {