117 lines
4.1 KiB
Swift
117 lines
4.1 KiB
Swift
import UIKit
|
|
import WidgetKit
|
|
|
|
let widgetGroupId = "group.com.zhushenwudi.lovelivemusicplayer"
|
|
|
|
@main
|
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
private var widgetReloadWorkItem: DispatchWorkItem?
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
|
|
) -> Bool {
|
|
let userDefaults = UserDefaults(suiteName: widgetGroupId)
|
|
userDefaults?.set(false, forKey: "isShutdown")
|
|
registerHostNotifications()
|
|
return true
|
|
}
|
|
|
|
func applicationWillTerminate(_ application: UIApplication) {
|
|
let userDefaults = UserDefaults(suiteName: widgetGroupId)
|
|
userDefaults?.set(false, forKey: "isPlaying")
|
|
userDefaults?.set("", forKey: "curJpLrc")
|
|
userDefaults?.set("", forKey: "nextJpLrc")
|
|
userDefaults?.set(true, forKey: "isShutdown")
|
|
}
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
configurationForConnecting connectingSceneSession: UISceneSession,
|
|
options: UIScene.ConnectionOptions
|
|
) -> UISceneConfiguration {
|
|
if connectingSceneSession.role == UISceneSession.Role.carTemplateApplication {
|
|
let config = UISceneConfiguration(
|
|
name: "CarPlay Configuration",
|
|
sessionRole: connectingSceneSession.role
|
|
)
|
|
config.delegateClass = CarPlaySceneDelegate.self
|
|
return config
|
|
}
|
|
let config = UISceneConfiguration(
|
|
name: "Default Configuration",
|
|
sessionRole: connectingSceneSession.role
|
|
)
|
|
config.delegateClass = SceneDelegate.self
|
|
return config
|
|
}
|
|
|
|
private func registerHostNotifications() {
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpShareRequest"),
|
|
object: nil,
|
|
queue: .main
|
|
) { notification in
|
|
HostBridge.shared.presentShare(from: notification.userInfo)
|
|
}
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("fromWigetToRunner"),
|
|
object: nil,
|
|
queue: .main
|
|
) { notification in
|
|
HostBridge.shared.handleWidgetAction(from: notification.userInfo)
|
|
}
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpWidgetSaveCover"),
|
|
object: nil,
|
|
queue: .main
|
|
) { notification in
|
|
guard let path = notification.userInfo?["path"] as? String else { return }
|
|
WidgetAppUtils.shared.saveImageToFile(imagePath: path) { [weak self] in
|
|
self?.scheduleWidgetReload()
|
|
}
|
|
}
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpReloadWidgets"),
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] _ in
|
|
self?.scheduleWidgetReload()
|
|
}
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpOpenWebView"),
|
|
object: nil,
|
|
queue: .main
|
|
) { notification in
|
|
HostBridge.shared.presentWebView(from: notification.userInfo)
|
|
}
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpShowToast"),
|
|
object: nil,
|
|
queue: .main
|
|
) { notification in
|
|
guard let message = notification.userInfo?["message"] as? String else { return }
|
|
HostBridge.shared.showToast(message)
|
|
}
|
|
QqShareHost.shared.prepare()
|
|
}
|
|
|
|
func application(
|
|
_ app: UIApplication,
|
|
open url: URL,
|
|
options: [UIApplication.OpenURLOptionsKey: Any] = [:]
|
|
) -> Bool {
|
|
QqShareHost.shared.handleOpen(url)
|
|
}
|
|
|
|
/// 合并切歌、播放状态、歌词行和封面完成时挤在一起的刷新请求。
|
|
private func scheduleWidgetReload() {
|
|
widgetReloadWorkItem?.cancel()
|
|
let workItem = DispatchWorkItem {
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
}
|
|
widgetReloadWorkItem = workItem
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: workItem)
|
|
}
|
|
}
|