Skip to content

Commit 4194a97

Browse files
committed
refactor: change name MultipleImagePickerConfigure -> config / doing handle preview header
1 parent 2728b3d commit 4194a97

File tree

4 files changed

+78
-45
lines changed

4 files changed

+78
-45
lines changed

Preview/PreviewHeader.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,47 @@ protocol PreviewHeaderViewDelegate: AnyObject {
77

88
class PreviewHeaderView: UIView {
99
weak var viewDelegate: PreviewHeaderViewDelegate?
10-
static let ButtonSize = CGFloat(24.0)
11-
static let TopMargin = CGFloat(15.0)
1210

1311
lazy var clearButton: UIButton = {
1412
let image = UIImage.close
1513

1614
let button = UIButton(type: .custom)
1715
button.setImage(image, for: .normal)
16+
17+
// Đặt kích thước cho hình ảnh bên trong button
18+
let imageSize = CGSize(width: 24, height: 24) // Đặt kích thước mới cho hình ảnh
19+
button.imageView?.frame = CGRect(origin: CGPoint.zero, size: imageSize)
20+
21+
button.frame = CGRect(x: 50, y: 100, width: imageSize.width, height: imageSize.height)
22+
1823
button.addTarget(self, action: #selector(PreviewHeaderView.clearAction(button:)), for: .touchUpInside)
1924

2025
return button
2126
}()
2227

2328
lazy var doneButton: UIButton = {
24-
let image = UIImage.play
29+
let button = UIButton(type: .system) // Sử dụng type .system cho button với giao diện người dùng tiêu chuẩn
2530

26-
let button = UIButton(type: .custom)
27-
button.setImage(image, for: .normal)
28-
button.addTarget(self, action: #selector(PreviewHeaderView.clearAction(button:)), for: .touchUpInside)
31+
// Đặt tiêu đề (text) cho button
32+
button.setTitle(config.doneTitle, for: .normal)
33+
34+
// Đặt màu chữ và màu nền cho button (tuỳ chọn)
35+
button.setTitleColor(config.selectedColor, for: .normal)
36+
37+
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.labelFontSize)
38+
39+
button.addTarget(self, action: #selector(PreviewHeaderView.doneAction(button:)), for: .touchUpInside)
2940

3041
return button
3142
}()
3243

3344
override init(frame: CGRect) {
3445
super.init(frame: frame)
3546

47+
if #available(iOS 11.0, *) {
48+
self.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor).isActive = true
49+
}
50+
3651
let stackView = UIStackView()
3752
stackView.translatesAutoresizingMaskIntoConstraints = false
3853
stackView.axis = .horizontal
@@ -48,8 +63,8 @@ class PreviewHeaderView: UIView {
4863

4964
// Đặt constraints cho stack view để căn chỉnh theo phía trái và phải
5065
NSLayoutConstraint.activate([
51-
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
52-
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
66+
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 24),
67+
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -24),
5368
stackView.topAnchor.constraint(equalTo: topAnchor),
5469
stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
5570
])

ios/CustomPhotoPicker/Cell.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import PhotosUI
1111
import TLPhotoPicker
1212

1313
class Cell: TLPhotoCollectionViewCell {
14-
var configure = MultipleImagePickerConfigure
15-
1614
static let longPressNotification = Notification.Name("CellLongPressNotification")
1715

1816
// Khởi tạo cell và thiết lập sự kiện Long Press
@@ -70,7 +68,7 @@ class Cell: TLPhotoCollectionViewCell {
7068

7169
override public var selectedAsset: Bool {
7270
willSet(newValue) {
73-
self.orderLabel?.backgroundColor = newValue ? self.configure.selectedColor : UIColor(red: 1, green: 1, blue: 1, alpha: 0.3)
71+
self.orderLabel?.backgroundColor = newValue ? config.selectedColor : UIColor(red: 1, green: 1, blue: 1, alpha: 0.3)
7472
}
7573
}
7674

@@ -82,7 +80,7 @@ class Cell: TLPhotoCollectionViewCell {
8280
self.orderLabel?.layer.cornerRadius = 12
8381
self.orderLabel?.layer.borderWidth = 2
8482
self.orderLabel?.layer.borderColor = UIColor.white.cgColor
85-
self.videoIconImageView?.image = self.configure.videoIcon
83+
self.videoIconImageView?.image = config.videoIcon
8684
if #available(iOS 11.0, *) {
8785
self.imageView?.accessibilityIgnoresInvertColors = true
8886
self.playerView?.accessibilityIgnoresInvertColors = true

ios/CustomPhotoPicker/CustomPhotoPickerViewController.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ class CustomPhotoPickerViewController: TLPhotosPickerViewController, ViewerContr
5555

5656
self.viewerController!.dataSource = self
5757

58+
self.viewerController?.viewSafeAreaInsetsDidChange()
59+
5860
let headerView = PreviewHeaderView()
5961
headerView.viewDelegate = self
6062

63+
headerView.backgroundColor = .white
64+
6165
self.viewerController!.headerView = headerView
6266

6367
self.present(self.viewerController!, animated: true, completion: nil)
@@ -69,7 +73,7 @@ class CustomPhotoPickerViewController: TLPhotosPickerViewController, ViewerContr
6973
super.makeUI()
7074
self.collectionView.backgroundColor = .white
7175
self.customNavItem.leftBarButtonItem?.tintColor = .black
72-
self.customNavItem.rightBarButtonItem?.tintColor = MultipleImagePickerConfigure.selectedColor
76+
self.customNavItem.rightBarButtonItem?.tintColor = config.selectedColor
7377

7478
for subview in self.view.subviews {
7579
guard let navbar = subview as? UINavigationBar else {
@@ -108,7 +112,12 @@ extension CustomPhotoPickerViewController: PreviewHeaderViewDelegate {
108112
self.viewerController?.dismiss(nil)
109113
}
110114

111-
func headerView(_: PreviewHeaderView, didDoneMenuButton _: UIButton) {
115+
func headerView(_: PreviewHeaderView, didPressDoneButton _: UIButton) {
116+
DispatchQueue.main.async {
117+
self.viewerController?.dismiss {
118+
self.dismiss(animated: true)
119+
}
120+
}
112121
// let rect = CGRect(x: 0, y: 0, width: 50, height: 50)
113122
// self.optionsController = OptionsController(sourceView: button, sourceRect: rect)
114123
// self.optionsController!.delegate = self

ios/MultipleImagePicker.swift

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ import Photos
33
import TLPhotoPicker
44
import UIKit
55

6-
var MultipleImagePickerConfigure = TLPhotosPickerConfigure()
6+
extension TLPhotosPickerConfigure {
7+
var isPreview: Bool {
8+
get { return true }
9+
set {}
10+
}
11+
12+
var isCrop: Bool {
13+
get { return true }
14+
set {}
15+
}
16+
}
17+
18+
var config = TLPhotosPickerConfigure()
719

820
@objc(MultipleImagePicker)
921
class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINavigationControllerDelegate, TLPhotosPickerLogDelegate, CropViewControllerDelegate {
@@ -59,7 +71,7 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
5971
viewController.didExceedMaximumNumberOfSelection = { [weak self] picker in
6072
self?.showExceededMaximumAlert(vc: picker, isVideo: false)
6173
}
62-
viewController.configure = MultipleImagePickerConfigure
74+
viewController.configure = config
6375
viewController.selectedAssets = self.selectedAssets
6476
viewController.logDelegate = self
6577

@@ -91,33 +103,35 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
91103
self.videoRequestOptions.isNetworkAccessAllowed = true
92104

93105
// config options
94-
MultipleImagePickerConfigure.tapHereToChange = self.options["tapHereToChange"] as! String
95-
MultipleImagePickerConfigure.numberOfColumn = self.options["numberOfColumn"] as! Int
96-
MultipleImagePickerConfigure.cancelTitle = self.options["cancelTitle"] as! String
97-
MultipleImagePickerConfigure.doneTitle = self.options["doneTitle"] as! String
98-
MultipleImagePickerConfigure.emptyMessage = self.options["emptyMessage"] as! String
99-
MultipleImagePickerConfigure.selectMessage = self.options["selectMessage"] as! String
100-
MultipleImagePickerConfigure.deselectMessage = self.options["deselectMessage"] as! String
101-
MultipleImagePickerConfigure.usedCameraButton = self.options["usedCameraButton"] as! Bool
102-
MultipleImagePickerConfigure.usedPrefetch = self.options["usedPrefetch"] as! Bool
103-
MultipleImagePickerConfigure.allowedLivePhotos = self.options["allowedLivePhotos"] as! Bool
104-
MultipleImagePickerConfigure.allowedVideo = self.options["allowedVideo"] as! Bool
105-
MultipleImagePickerConfigure.allowedAlbumCloudShared = self.options["allowedAlbumCloudShared"] as! Bool
106-
MultipleImagePickerConfigure.allowedVideoRecording = self.options["allowedVideoRecording"] as! Bool
107-
MultipleImagePickerConfigure.maxVideoDuration = self.options["maxVideoDuration"] as? TimeInterval
108-
MultipleImagePickerConfigure.autoPlay = self.options["autoPlay"] as! Bool
109-
MultipleImagePickerConfigure.muteAudio = self.options["muteAudio"] as! Bool
110-
MultipleImagePickerConfigure.singleSelectedMode = (self.options["singleSelectedMode"])! as! Bool
111-
MultipleImagePickerConfigure.maxSelectedAssets = self.options["maxSelectedAssets"] as? Int
112-
MultipleImagePickerConfigure.selectedColor = UIColor(hex: self.options["selectedColor"] as! String)
106+
config.tapHereToChange = self.options["tapHereToChange"] as! String
107+
config.numberOfColumn = self.options["numberOfColumn"] as! Int
108+
config.cancelTitle = self.options["cancelTitle"] as! String
109+
config.doneTitle = self.options["doneTitle"] as! String
110+
config.emptyMessage = self.options["emptyMessage"] as! String
111+
config.selectMessage = self.options["selectMessage"] as! String
112+
config.deselectMessage = self.options["deselectMessage"] as! String
113+
config.usedCameraButton = self.options["usedCameraButton"] as! Bool
114+
config.usedPrefetch = self.options["usedPrefetch"] as! Bool
115+
config.allowedLivePhotos = self.options["allowedLivePhotos"] as! Bool
116+
config.allowedVideo = self.options["allowedVideo"] as! Bool
117+
config.allowedAlbumCloudShared = self.options["allowedAlbumCloudShared"] as! Bool
118+
config.allowedVideoRecording = self.options["allowedVideoRecording"] as! Bool
119+
config.maxVideoDuration = self.options["maxVideoDuration"] as? TimeInterval
120+
config.autoPlay = self.options["autoPlay"] as! Bool
121+
config.muteAudio = self.options["muteAudio"] as! Bool
122+
config.singleSelectedMode = (self.options["singleSelectedMode"])! as! Bool
123+
config.maxSelectedAssets = self.options["maxSelectedAssets"] as? Int
124+
config.selectedColor = UIColor(hex: self.options["selectedColor"] as! String)
125+
126+
config.isPreview = self.options["isPreview"] as? Bool ?? false
113127

114128
let mediaType = self.options["mediaType"] as! String
115129

116-
MultipleImagePickerConfigure.mediaType = mediaType == "video" ? PHAssetMediaType.video : mediaType == "image" ? PHAssetMediaType.image : nil
130+
config.mediaType = mediaType == "video" ? PHAssetMediaType.video : mediaType == "image" ? PHAssetMediaType.image : nil
117131

118-
MultipleImagePickerConfigure.nibSet = (nibName: "Cell", bundle: MultipleImagePickerBundle.bundle())
132+
config.nibSet = (nibName: "Cell", bundle: MultipleImagePickerBundle.bundle())
119133

120-
MultipleImagePickerConfigure.allowedPhotograph = self.options["allowedPhotograph"] as! Bool
134+
config.allowedPhotograph = self.options["allowedPhotograph"] as! Bool
121135
// configure.preventAutomaticLimitedAccessAlert = self.options["preventAutomaticLimitedAccessAlert"]
122136

123137
if options["selectedAssets"] != nil {
@@ -128,10 +142,7 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
128142
func handleSelectedAssets(selectedList: NSArray) {
129143
let assetsExist = selectedList.filter { ($0 as! NSObject).value(forKey: "localIdentifier") != nil }
130144
self.videoCount = selectedList.filter { ($0 as! NSObject).value(forKey: "type") as? String == "video" }.count
131-
132-
let existLastItem = (assetsExist.last as? [String: Any])?["localIdentifier"] as? String
133-
let selectedLastItem = self.selectedAssets.last?.phAsset?.localIdentifier as? String
134-
145+
135146
var assets = [TLPHAsset]()
136147
for index in 0 ..< assetsExist.count {
137148
let value = assetsExist[index]
@@ -194,10 +205,10 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
194205
func presentCropViewController(image: UIImage) {
195206
let cropViewController = CropViewController(croppingStyle: (self.options["isCropCircle"] as! Bool) ? .circular : .default, image: image)
196207
cropViewController.delegate = self
197-
cropViewController.doneButtonTitle = MultipleImagePickerConfigure.doneTitle
198-
cropViewController.doneButtonColor = MultipleImagePickerConfigure.selectedColor
208+
cropViewController.doneButtonTitle = config.doneTitle
209+
cropViewController.doneButtonColor = config.selectedColor
199210

200-
cropViewController.cancelButtonTitle = MultipleImagePickerConfigure.cancelTitle
211+
cropViewController.cancelButtonTitle = config.cancelTitle
201212

202213
self.getTopMostViewController()?.present(cropViewController, animated: true, completion: nil)
203214
}

0 commit comments

Comments
 (0)