Skip to content

Commit

Permalink
代码优化、结构调整
Browse files Browse the repository at this point in the history
自定义语言代码逻辑修改
  • Loading branch information
SilenceLove committed Apr 1, 2024
1 parent b42ac47 commit 195912f
Show file tree
Hide file tree
Showing 69 changed files with 3,429 additions and 3,016 deletions.
108 changes: 76 additions & 32 deletions HXPhotoPickerExample.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

3,333 changes: 1,677 additions & 1,656 deletions Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions Sources/HXPhotoPicker/Camera/Config/CameraConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public struct CameraConfiguration: IndicatorTypeConfig {
public var modalPresentationStyle: UIModalPresentationStyle

/// If the built-in language is not enough, you can add a custom language text
/// PhotoManager.shared.customLanguages - custom language array
/// PhotoManager.shared.fixedCustomLanguage - If there are multiple custom languages, one can be fixed to display
/// customLanguages - custom language array
/// 如果自带的语言不够,可以添加自定义的语言文字
/// PhotoManager.shared.customLanguages - 自定义语言数组
/// PhotoManager.shared.fixedCustomLanguage - 如果有多种自定义语言,可以固定显示某一种
/// customLanguages - 自定义语言数组
public var languageType: LanguageType = .system {
didSet {
#if HXPICKER_ENABLE_EDITOR
Expand All @@ -38,6 +36,12 @@ public struct CameraConfiguration: IndicatorTypeConfig {
}
}

/// 自定义语言
public var customLanguages: [CustomLanguage] {
get { PhotoManager.shared.customLanguages }
set { PhotoManager.shared.customLanguages = newValue }
}

/// hide status bar
/// 隐藏状态栏
public var prefersStatusBarHidden: Bool = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension CameraViewController: CameraResultViewControllerDelegate {
let result = CameraController.Result.image(image)
if config.isSaveSystemAlbum {
navigationController?.view.hx.show()
AssetManager.save(
AssetSaveUtil.save(
type: .image(image),
location: location
) {
Expand Down Expand Up @@ -63,7 +63,7 @@ extension CameraViewController: CameraResultViewControllerDelegate {
let result = CameraController.Result.video(videoURL)
if config.isSaveSystemAlbum {
navigationController?.view.hx.show()
AssetManager.save(
AssetSaveUtil.save(
type: .videoURL(videoURL),
location: location
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ open class CameraViewController: HXBaseViewController {
}
return
}
AssetManager.requestCameraAccess { isGranted in
AssetPermissionsUtil.requestCameraAccess { isGranted in
if isGranted {
self.setupCamera()
}else {
Expand Down Expand Up @@ -286,7 +286,7 @@ open class CameraViewController: HXBaseViewController {
)
}
if UIImagePickerController.isSourceTypeAvailable(.camera) {
if !didLayoutPreview && AssetManager.cameraAuthorizationStatus() == .authorized {
if !didLayoutPreview && AssetPermissionsUtil.cameraAuthorizationStatus == .authorized {
if config.cameraType == .metal {
previewView.frame = previewRect
}else {
Expand Down
17 changes: 10 additions & 7 deletions Sources/HXPhotoPicker/Core/Model/CustomLanguage.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
//
// CustomLanguage.swift
// HXPhotoPicker
//
// Created by Slience on 2021/1/7.
// Created by Silence on 2024/3/30.
// Copyright © 2024 Silence. All rights reserved.
//

import Foundation

public class CustomLanguage {
/// 语言

/// 会与 Locale.preferredLanguages 进行匹配,匹配成功的才会使用。请确保正确性
public let language: String
/// 语言文件路径
public let path: String
/// 语言Bundle
public let bundle: Bundle

public init(language: String,
path: String) {
public init(
language: String,
bundle: Bundle
) {
self.language = language
self.path = path
self.bundle = bundle
}
}
30 changes: 16 additions & 14 deletions Sources/HXPhotoPicker/Core/Model/LanguageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@

import Foundation

public enum LanguageType: Int {
public enum LanguageType: Equatable {
/// 跟随系统语言
case system = 0
case system
/// 中文简体
case simplifiedChinese = 1
case simplifiedChinese
/// 中文繁体
case traditionalChinese = 2
case traditionalChinese
/// 日文
case japanese = 3
case japanese
/// 韩文
case korean = 4
case korean
/// 英文
case english = 5
case english
/// 泰语
case thai = 6
case thai
/// 印尼语
case indonesia = 7
case indonesia
/// 越南语
case vietnamese = 8
case vietnamese
/// 俄语
case russian = 9
case russian
/// 德语
case german = 10
case german
/// 法语
case french = 11
case french
/// 阿拉伯
case arabic = 12
case arabic

case custom(Bundle)
}
167 changes: 15 additions & 152 deletions Sources/HXPhotoPicker/Core/Util/AssetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@

import UIKit
import Photos
public struct AssetManager {

public enum PhotoSaveType {
case image(UIImage)
case imageURL(URL)
case videoURL(URL)
case livePhoto(imageURL: URL, videoURL: URL)
}

public enum PhotoSaveError: Error {
case notDetermined
case phAssetIsNull
}
public struct AssetManager {

/// 保存资源到系统相册
/// - Parameters:
Expand All @@ -31,26 +20,17 @@ public struct AssetManager {
@available(iOS 13.0.0, *)
@discardableResult
public static func save(
type: PhotoSaveType,
type: AssetSaveUtil.SaveType,
customAlbumName: String? = nil,
creationDate: Date = .init(),
location: CLLocation? = nil
) async throws -> PHAsset {
try await withCheckedThrowingContinuation { continuation in
save(
type: type,
customAlbumName: customAlbumName,
creationDate: creationDate,
location: location
) { result in
switch result {
case .success(let phAsset):
continuation.resume(returning: phAsset)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
try await AssetSaveUtil.save(
type: type,
customAlbumName: customAlbumName,
creationDate: creationDate,
location: location
)
}

/// 保存资源到系统相册
Expand All @@ -61,135 +41,18 @@ public struct AssetManager {
/// - location: 位置信息
/// - completion: 保存之后的结果
public static func save(
type: PhotoSaveType,
type: AssetSaveUtil.SaveType,
customAlbumName: String? = nil,
creationDate: Date = .init(),
location: CLLocation? = nil,
completion: @escaping (Result<PHAsset, Error>) -> Void
) {
var albumName: String?
if let customAlbumName = customAlbumName, customAlbumName.count > 0 {
albumName = customAlbumName
}else {
albumName = displayName
}
AssetManager.requestAuthorization {
switch $0 {
case .denied, .notDetermined, .restricted:
completion(.failure(PhotoSaveError.notDetermined))
return
default:
break
}
DispatchQueue.global().async {
var placeholder: PHObjectPlaceholder?
do {
try PHPhotoLibrary.shared().performChangesAndWait {
var creationRequest: PHAssetCreationRequest?
switch type {
case .image(let image):
creationRequest = PHAssetCreationRequest.creationRequestForAsset(
from: image
)
case .imageURL(let url):
creationRequest = PHAssetCreationRequest.creationRequestForAssetFromImage(
atFileURL: url
)
case .videoURL(let url):
creationRequest = PHAssetCreationRequest.creationRequestForAssetFromVideo(
atFileURL: url
)
case .livePhoto(let imageURL, let videoURL):
creationRequest = PHAssetCreationRequest.forAsset()
creationRequest?.addResource(with: .photo, fileURL: imageURL, options: nil)
creationRequest?.addResource(with: .pairedVideo, fileURL: videoURL, options: nil)
}
creationRequest?.creationDate = creationDate
creationRequest?.location = location
placeholder = creationRequest?.placeholderForCreatedAsset
}
if let placeholder = placeholder,
let phAsset = AssetManager.fetchAsset(
with: placeholder.localIdentifier
) {
DispatchQueue.main.async {
completion(.success(phAsset))
}
if let albumName = albumName, !albumName.isEmpty {
saveCustomAlbum(for: phAsset, albumName: albumName)
}
}else {
DispatchQueue.main.async {
completion(.failure(PhotoSaveError.phAssetIsNull))
}
}
} catch {
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
}
}

public static func createAssetCollection(for collectionName: String) -> PHAssetCollection? {
let collections = PHAssetCollection.fetchAssetCollections(
with: .album,
subtype: .albumRegular,
options: nil
AssetSaveUtil.save(
type: type,
customAlbumName: customAlbumName,
creationDate: creationDate,
location: location,
completion: completion
)
var assetCollection: PHAssetCollection?
collections.enumerateObjects { (collection, _, stop) in
if collection.localizedTitle == collectionName {
assetCollection = collection
stop.pointee = true
}
}
if assetCollection == nil {
do {
var createCollectionID: String?
try PHPhotoLibrary.shared().performChangesAndWait {
createCollectionID = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(
withTitle: collectionName
).placeholderForCreatedAssetCollection.localIdentifier
}
if let createCollectionID = createCollectionID {
assetCollection = PHAssetCollection.fetchAssetCollections(
withLocalIdentifiers: [createCollectionID],
options: nil
).firstObject
}
}catch {

}
}
return assetCollection
}

private static var displayName: String {
if let displayName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String {
return displayName.count > 0 ? displayName : "PhotoPicker"
}else if let bundleName = Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String {
return bundleName.count > 0 ? bundleName : "PhotoPicker"
}else {
return "PhotoPicker"
}
}

private static func saveCustomAlbum(
for asset: PHAsset,
albumName: String
) {
guard let assetCollection = createAssetCollection(for: albumName) else {
return
}
try? PHPhotoLibrary.shared().performChangesAndWait {
PHAssetCollectionChangeRequest(
for: assetCollection
)?.insertAssets(
[asset] as NSFastEnumeration,
at: IndexSet.init(integer: 0)
)
}
}
}
Loading

0 comments on commit 195912f

Please sign in to comment.