Skip to content

Commit

Permalink
结构调整
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenceLove committed Nov 21, 2020
1 parent 7d2a1c9 commit 57a64b8
Show file tree
Hide file tree
Showing 324 changed files with 5,210 additions and 545 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ comment:
layout: header, changes, diff
coverage:
ignore:
- HXPhotoPicker-Demo
- HXPhotoPickerExample
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ before_install:

script:
- set -o pipefail
- xcodebuild clean -workspace HXPhotoPicker-Demo.xcworkspace -scheme "HXPhotoPicker" -sdk iphonesimulator build | xcpretty
- xcodebuild clean -workspace HXPhotoPickerExample.xcworkspace -scheme "HXPhotoPickerExample" -sdk iphonesimulator build | xcpretty

after_success:
- sleep 4
Expand Down
25 changes: 25 additions & 0 deletions HXPHPicker/Bundle+HXPhotoPicker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Bundle+HXPhotoPicker.swift
// HXPHPickerExample
//
// Created by 洪欣 on 2020/11/15.
// Copyright © 2020 洪欣. All rights reserved.
//

import UIKit

extension Bundle {

class func hx_localizedString(for key: String) -> String {
return hx_localizedString(for: key, value: nil)
}

class func hx_localizedString(for key: String, value: String?) -> String {
let bundle = HXPHManager.shared.languageBundle
var newValue = bundle?.localizedString(forKey: key, value: value, table: nil)
if newValue == nil {
newValue = key
}
return newValue!
}
}
71 changes: 71 additions & 0 deletions HXPHPicker/HXAlbumView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// HXAlbumView.swift
// HXPHPickerExample
//
// Created by 洪欣 on 2020/11/17.
// Copyright © 2020 洪欣. All rights reserved.
//

import UIKit

protocol HXAlbumViewDelegate: NSObjectProtocol {

}

class HXAlbumView: UIView, UITableViewDataSource, UITableViewDelegate {

weak var delegate: HXAlbumViewDelegate?

lazy var tableView : UITableView = {
let tableView = UITableView.init(frame: CGRect.init(), style: UITableView.Style.plain)
tableView.backgroundColor = config!.backgroundColor
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
tableView.register(HXAlbumViewCell.self, forCellReuseIdentifier: "cellId")
return tableView
}()
var config: HXPHAlbumListConfiguration?
var assetCollectionsArray: [HXPHAssetCollection] = []

init(config: HXPHAlbumListConfiguration) {
super.init(frame: CGRect.zero)
self.config = config
backgroundColor = config.backgroundColor
addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return assetCollectionsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! HXAlbumViewCell
let assetCollection = assetCollectionsArray[indexPath.row]
cell.assetCollection = assetCollection
cell.config = config
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return config!.cellHeight
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let assetCollection = assetCollectionsArray[indexPath.row]

}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let myCell: HXAlbumViewCell = cell as! HXAlbumViewCell
myCell.cancelRequest()
}

override func layoutSubviews() {
super.layoutSubviews()
tableView.frame = bounds
}


required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}
103 changes: 103 additions & 0 deletions HXPHPicker/HXAlbumViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// HXAlbumViewCell.swift
// 照片选择器-Swift
//
// Created by 洪欣 on 2019/6/28.
// Copyright © 2019年 洪欣. All rights reserved.
//

import UIKit
import Photos

class HXAlbumViewCell: UITableViewCell {
lazy var albumCoverView: UIImageView = {
let albumCoverView = UIImageView.init()
albumCoverView.contentMode = UIView.ContentMode.scaleAspectFill
albumCoverView.clipsToBounds = true
return albumCoverView
}()
lazy var albumNameLb: UILabel = {
let albumNameLb = UILabel.init()
return albumNameLb
}()
lazy var photoCountLb: UILabel = {
let photoCountLb = UILabel.init()
return photoCountLb
}()
lazy var bottomLineView: UIView = {
let bottomLineView = UIView.init()
bottomLineView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.15)
return bottomLineView
}()
lazy var selectedBgView : UIView = {
let selectedBgView = UIView.init()
return selectedBgView
}()
var config : HXPHAlbumListConfiguration? {
didSet {
albumNameLb.textColor = config?.albumNameColor
albumNameLb.font = config?.albumNameFont
photoCountLb.textColor = config?.photoCountColor
photoCountLb.font = config?.photoCountFont
bottomLineView.backgroundColor = config?.separatorLineColor
backgroundColor = config?.cellBackgroudColor
if config?.cellSelectedColor != nil {
selectedBgView.backgroundColor = config?.cellSelectedColor
selectedBackgroundView = selectedBgView
}
}
}
var assetCollection: HXPHAssetCollection? {
didSet {
albumNameLb.text = assetCollection?.albumName
photoCountLb.text = String(assetCollection!.count)
requestID = assetCollection?.requestCoverImage(completion: { (image, assetCollection, info) in
if assetCollection == self.assetCollection && image != nil {
self.albumCoverView.image = image
if !HXPHAssetManager.assetDownloadIsDegraded(for: info) {
self.requestID = nil
}
}
})
}
}
var requestID: PHImageRequestID?

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
initView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initView() {
contentView.addSubview(albumCoverView)
contentView.addSubview(albumNameLb)
contentView.addSubview(photoCountLb)
contentView.addSubview(bottomLineView)
}

override func layoutSubviews() {
super.layoutSubviews()
let coverMargin : CGFloat = 5
let coverWidth = hx_height - (coverMargin * 2)
albumCoverView.frame = CGRect(x: coverMargin, y: coverMargin, width: coverWidth, height: coverWidth)

albumNameLb.hx_x = albumCoverView.frame.maxX + 10
albumNameLb.hx_size = CGSize(width: hx_width - albumNameLb.hx_x - 20, height: 16)
albumNameLb.hx_centerY = hx_height / CGFloat(2) - albumNameLb.hx_height / CGFloat(2)

photoCountLb.hx_x = albumCoverView.frame.maxX + 10
photoCountLb.hx_y = albumNameLb.frame.maxY + 5
photoCountLb.hx_size = CGSize(width: hx_width - photoCountLb.hx_x - 20, height: 14)

bottomLineView.frame = CGRect(x: coverMargin, y: hx_height - 0.5, width: hx_width - coverMargin * 2, height: 0.5)
}

func cancelRequest() {
if requestID != nil {
PHImageManager.default().cancelImageRequest(requestID!)
requestID = nil
}
}
}
162 changes: 162 additions & 0 deletions HXPHPicker/HXAlbumViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// HXAlbumViewController.swift
// 照片选择器-Swift
//
// Created by 洪欣 on 2019/6/28.
// Copyright © 2019年 洪欣. All rights reserved.
//

import UIKit
import Photos

class HXAlbumViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

lazy var tableView : UITableView = {
let tableView = UITableView.init(frame: CGRect.init(), style: UITableView.Style.plain)
tableView.backgroundColor = config!.backgroundColor
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
tableView.register(HXAlbumViewCell.self, forCellReuseIdentifier: "cellId")
if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
} else {
// Fallback on earlier versions
self.automaticallyAdjustsScrollViewInsets = false
}
return tableView
}()
var config: HXPHAlbumListConfiguration?
var assetCollectionsArray: [HXPHAssetCollection] = []
var orientationDidChange : Bool = false
var beforeOrientationIndexPath: IndexPath?
var canFetchAssetCollections: Bool = false
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
extendedLayoutIncludesOpaqueBars = true;
edgesForExtendedLayout = UIRectEdge.all;
config = hx_pickerController()!.config.albumList
view.backgroundColor = config!.backgroundColor
let backItem = UIBarButtonItem.init(title: "取消".hx_localized(), style: UIBarButtonItem.Style.done, target: self, action: #selector(didCancelItemClick))
navigationItem.rightBarButtonItem = backItem
view.addSubview(tableView)
fetchCameraAssetCollection()
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationChanged(notify:)), name: UIApplication.didChangeStatusBarOrientationNotification, object: nil)
}
@objc func deviceOrientationChanged(notify: Notification) {
beforeOrientationIndexPath = tableView.indexPathsForVisibleRows?.first
orientationDidChange = true
}
func fetchCameraAssetCollection() {
if hx_pickerController()?.cameraAssetCollection != nil {
self.pushPhotoPickerContoller(assetCollection: hx_pickerController()?.cameraAssetCollection, animated: false)
self.canFetchAssetCollections = true
title = "相册".hx_localized()
}else {
hx_pickerController()?.fetchCameraAssetCollectionCompletion = { (assetCollection) in
var cameraAssetCollection = assetCollection
if cameraAssetCollection == nil {
cameraAssetCollection = HXPHAssetCollection.init(albumName: self.config?.emptyAlbumName, coverImage: UIImage.hx_named(named: self.config!.emptyCoverImageName))
}
self.pushPhotoPickerContoller(assetCollection: cameraAssetCollection, animated: false)
self.canFetchAssetCollections = true
self.title = "相册".hx_localized()
}
}
}

func fetchAssetCollections() {
HXPHProgressHUD.showLoadingHUD(addedTo: view, animated: true)
hx_pickerController()?.fetchAssetCollections()
hx_pickerController()?.fetchAssetCollectionsCompletion = { (assetCollectionsArray) in
self.reloadTableView(assetCollectionsArray: assetCollectionsArray)
HXPHProgressHUD.hideHUD(forView: self.view, animated: true)
}
}
func reloadTableView(assetCollectionsArray: [HXPHAssetCollection]) {
self.assetCollectionsArray = assetCollectionsArray
if self.assetCollectionsArray.isEmpty {
let assetCollection = HXPHAssetCollection.init(albumName: self.config?.emptyAlbumName, coverImage: UIImage.hx_named(named: self.config!.emptyCoverImageName))
self.assetCollectionsArray.append(assetCollection)
}
self.tableView.reloadData()
}
private func pushPhotoPickerContoller(assetCollection: HXPHAssetCollection?, animated: Bool) {
let photoVC = HXPHPickerViewController.init()
photoVC.assetCollection = assetCollection
photoVC.showLoading = animated
navigationController?.pushViewController(photoVC, animated: animated)
}

@objc func didCancelItemClick() {
hx_pickerController()?.cancelCallback()
dismiss(animated: true, completion: nil)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return assetCollectionsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! HXAlbumViewCell
let assetCollection = assetCollectionsArray[indexPath.row]
cell.assetCollection = assetCollection
cell.config = config
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return config!.cellHeight
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let assetCollection = assetCollectionsArray[indexPath.row]
pushPhotoPickerContoller(assetCollection: assetCollection, animated: true)
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let myCell: HXAlbumViewCell = cell as! HXAlbumViewCell
myCell.cancelRequest()
}

func changeSubviewFrame() {
let margin: CGFloat = UIDevice.hx_leftMargin()
tableView.frame = CGRect(x: margin, y: 0, width: view.hx_width - 2 * margin, height: view.hx_height)
if navigationController?.modalPresentationStyle == UIModalPresentationStyle.fullScreen {
tableView.contentInset = UIEdgeInsets.init(top: UIDevice.hx_navigationBarHeight(), left: 0, bottom: UIDevice.hx_bottomMargin(), right: 0)
}else {
tableView.contentInset = UIEdgeInsets.init(top: navigationController!.navigationBar.hx_height, left: 0, bottom: UIDevice.hx_bottomMargin(), right: 0)
}
if orientationDidChange {
if !assetCollectionsArray.isEmpty {
tableView.scrollToRow(at: beforeOrientationIndexPath ?? IndexPath.init(row: 0, section: 0), at: UITableView.ScrollPosition.top, animated: false)
}
orientationDidChange = false
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
changeSubviewFrame()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate;
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if assetCollectionsArray.isEmpty && canFetchAssetCollections {
fetchAssetCollections()
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
deinit {
NotificationCenter.default.removeObserver(self)
print("\(self) deinit")
}
}
Loading

0 comments on commit 57a64b8

Please sign in to comment.