Bark/notificationContentExtension/NotificationViewController.swift
UUNEO 7b3ba2b9ac 增加下拉消息显示大图
格式化代码;删除多余的配置文件。

\t 替换成空格

去掉多余的\t
2024-12-08 19:02:59 +08:00

132 lines
4.8 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.

//
// NotificationViewController.swift
// NotificationContentExtension
//
// Created by huangfeng on 2018/7/4.
// Copyright © 2018 Fin. All rights reserved.
//
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
let noticeLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(named: "notification_copy_color")
label.font = UIFont.preferredFont(ofSize: 16)
label.adjustsFontForContentSizeCategory = true
label.textAlignment = .center
return label
}()
/// view
let imageView: UIImageView = {
let view = UIImageView()
view.contentMode = .scaleAspectFit
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.noticeLabel)
self.view.addSubview(self.imageView)
self.preferredContentSize = CGSize(width: 0, height: 1)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.preferredContentSize = CGSize(width: 0, height: 1)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.preferredContentSize = CGSize(width: 0, height: 1)
}
func didReceive(_ notification: UNNotification) {
///
self.ImageHandler(notification)
guard notification.request.content.userInfo["autocopy"] as? String == "1"
|| notification.request.content.userInfo["automaticallycopy"] as? String == "1"
else {
return
}
if let copy = notification.request.content.userInfo["copy"] as? String {
UIPasteboard.general.string = copy
} else {
UIPasteboard.general.string = notification.request.content.body
}
}
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
switch response.actionIdentifier {
case "copy":
self.copyAction(response, completionHandler: completion)
case "mute":
self.muteAction(response, completionHandler: completion)
default:
completion(.dismiss)
}
}
///
func copyAction(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
let userInfo = response.notification.request.content.userInfo
if let copy = userInfo["copy"] as? String {
UIPasteboard.general.string = copy
} else {
UIPasteboard.general.string = response.notification.request.content.body
}
showTips(text: NSLocalizedString("Copy", comment: ""))
completion(.doNotDismiss)
}
///
func muteAction(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
let groupName = response.notification.request.content.threadIdentifier
//
GroupMuteSettingManager().settings[groupName] = Date() + 60 * 60
showTips(text: String(format: NSLocalizedString("groupMuted", comment: ""), groupName.isEmpty ? "default" : groupName))
completion(.doNotDismiss)
}
func showTips(text: String) {
/// imagelabel
self.preferredContentSize = CGSize(width: 0, height: self.imageView.frame.height + 40)
self.noticeLabel.text = text
/// y
self.noticeLabel.frame = CGRect(x: 0, y: self.imageView.frame.height, width: self.view.bounds.width, height: 40)
}
}
extension NotificationViewController {
///
func ImageHandler(_ notification: UNNotification) {
Task {
guard let imageUrl = notification.request.content.userInfo["image"] as? String,
let imageFileUrl = await ImageDownloader.downloadImage(imageUrl),
let image = UIImage(contentsOfFile: imageFileUrl)
else {
self.imageView.frame = .zero
return
}
///
let viewWidth = view.bounds.size.width
let aspectRatio = image.size.width / image.size.height
let viewHeight = viewWidth / aspectRatio
let size = CGSize(width: viewWidth, height: viewHeight)
DispatchQueue.main.async {
self.preferredContentSize = size
self.imageView.image = image
self.imageView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
}
}
}
}