diff --git a/Bark/AppDelegate.swift b/Bark/AppDelegate.swift index 74a9378..3071618 100644 --- a/Bark/AppDelegate.swift +++ b/Bark/AppDelegate.swift @@ -8,6 +8,7 @@ import IQKeyboardManagerSwift import IQKeyboardToolbarManager +import SVProgressHUD import SwiftyStoreKit import UIKit import UserNotifications @@ -32,7 +33,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // 必须在应用一开始就配置,否则应用可能提前在配置之前试用了 Realm() ,则会创建两个独立数据库。 setupRealm() - + + SVProgressHUD.setMinimumDismissTimeInterval(1.5) + IQKeyboardManager.shared.isEnabled = true IQKeyboardToolbarManager.shared.isEnabled = true if #available(iOS 14, *), UIDevice.current.userInterfaceIdiom == .pad { diff --git a/Bark/Info.plist b/Bark/Info.plist index 998ca49..1e69079 100644 --- a/Bark/Info.plist +++ b/Bark/Info.plist @@ -2,6 +2,8 @@ + NSPhotoLibraryAddUsageDescription + Save the image to the photo library CFBundleURLTypes diff --git a/Bark/Localizable.xcstrings b/Bark/Localizable.xcstrings index 0e1ef01..f7bb7b6 100644 --- a/Bark/Localizable.xcstrings +++ b/Bark/Localizable.xcstrings @@ -2668,6 +2668,35 @@ } } }, + "noPermission" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "No permission" + } + }, + "ja" : { + "stringUnit" : { + "state" : "translated", + "value" : "権限がありません" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "İzin yok" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "没有权限" + } + } + } + }, "Notice1" : { "extractionState" : "manual", "localizations" : { @@ -3393,6 +3422,64 @@ } } }, + "save" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Save" + } + }, + "ja" : { + "stringUnit" : { + "state" : "translated", + "value" : "保存" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Kaydet" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "保存" + } + } + } + }, + "saveSuccess" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Saved to your photos!" + } + }, + "ja" : { + "stringUnit" : { + "state" : "translated", + "value" : "アルバムに保存したよ!" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Albüme kaydedildi!" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "已保存到相册!" + } + } + } + }, "SecureConnection" : { "extractionState" : "manual", "localizations" : { diff --git a/View/MessageList/MessageItemView.swift b/View/MessageList/MessageItemView.swift index 174b11b..10a9f98 100644 --- a/View/MessageList/MessageItemView.swift +++ b/View/MessageList/MessageItemView.swift @@ -8,6 +8,8 @@ import ImageViewer_swift import Kingfisher +import Photos +import SVProgressHUD import UIKit class MessageItemView: UIView { @@ -168,13 +170,25 @@ extension MessageItemView { imageView.isHidden = false // loadDiskFileSynchronously imageView.kf.setImage(with: URL(string: image), options: [.targetCache(imageCache), .keepCurrentImageWhileLoading, .loadDiskFileSynchronously]) { [weak self] result in - // 获取系统是否是夜间模式 - let isDarkMode = UIScreen.main.traitCollection.userInterfaceStyle == .dark - self?.imageView.setupImageViewer(options: [.closeIcon(UIImage(named: "back")!), .theme(isDarkMode ? .dark : .light)]) guard let self else { return } guard let image = try? result.get().image else { return } + + // 获取系统是否是夜间模式 + let isDarkMode = UIScreen.main.traitCollection.userInterfaceStyle == .dark + var options: [ImageViewerOption] = [ + .closeIcon(UIImage(named: "back")!), + .theme(isDarkMode ? .dark : .light) + ] + if #available(iOS 14.0, *) { + options.append(.rightNavItemTitle(NSLocalizedString("save"), onTap: { _ in + // 保存 image 到相册 + self.saveImageToAlbum(image) + })) + } + self.imageView.setupImageViewer(options: options) + layoutImageView(image: image) } } else { @@ -199,3 +213,28 @@ extension MessageItemView { } } } + +@available(iOS 14.0, *) +extension MessageItemView { + func saveImageToAlbum(_ image: UIImage) { + PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in + guard status == .authorized || status == .limited else { + DispatchQueue.main.async { + SVProgressHUD.showInfo(withStatus: NSLocalizedString("noPermission")) + } + return + } + PHPhotoLibrary.shared().performChanges({ + PHAssetChangeRequest.creationRequestForAsset(from: image) + }) { success, error in + DispatchQueue.main.async { + if success { + SVProgressHUD.showSuccess(withStatus: NSLocalizedString("saveSuccess")) + } else { + SVProgressHUD.showError(withStatus: error?.localizedDescription) + } + } + } + } + } +}