diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md new file mode 100644 index 0000000..9a22d6b --- /dev/null +++ b/Documentation/Changelog.md @@ -0,0 +1,12 @@ +## CHANGELOG + +### v0.3.0 +- The optimized image loading process; +- `clearCache` now global method, use: `RESegmentedControl.clearCache()`; + +### v0.2.1 +- fixed Auto-Layout warnings; + +### v0.2.0 + +First public release \ No newline at end of file diff --git a/README.md b/README.md index 095253a..f915b06 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,10 @@ Please make sure to update tests as appropriate. Sherzod Khashimov +## Changelog + +See [**changelog here**](/Documentation/Changelog.md) + ## License RESegmentedControl is available under the MIT license. See the LICENSE file for more info. diff --git a/RESegmentedControl.podspec b/RESegmentedControl.podspec index 06bde84..19d9589 100644 --- a/RESegmentedControl.podspec +++ b/RESegmentedControl.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'RESegmentedControl' - s.version = '0.2.1' + s.version = '0.3.0' s.summary = 'A Customizable Segmented Control.' # This description is used to generate tags and improve search results. @@ -22,7 +22,8 @@ Pod::Spec.new do |s| DESC s.homepage = 'https://github.com/sh-khashimov/RESegmentedControl' - # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' + s.documentation_url = 'https://sh-khashimov.github.io/RESegmentedControl' + s.screenshots = 'https://raw.githubusercontent.com/sh-khashimov/RESegmentedControl/master/Images/1.png', 'https://raw.githubusercontent.com/sh-khashimov/RESegmentedControl/master/Images/2.png', 'https://raw.githubusercontent.com/sh-khashimov/RESegmentedControl/master/Images/3.png' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'Sherzod Khashimov' => 'sh.khashimov@gmail.com' } s.source = { :git => 'https://github.com/sh-khashimov/RESegmentedControl.git', :tag => s.version.to_s } diff --git a/Sources/RESegmentedControl/Classes/Cells/SegmentCollectionViewCell.swift b/Sources/RESegmentedControl/Classes/Cells/SegmentCollectionViewCell.swift index 95233ff..7f9f5db 100755 --- a/Sources/RESegmentedControl/Classes/Cells/SegmentCollectionViewCell.swift +++ b/Sources/RESegmentedControl/Classes/Cells/SegmentCollectionViewCell.swift @@ -23,25 +23,30 @@ internal class SegmentCollectionViewCell: UICollectionViewCell { textLabel.text = item?.title ?? "" textLabel.isHidden = item?.title == nil - if let imageUrl = item?.imageUrl { - imageDownload.downloadImage(url: imageUrl) { [weak self] (image, url, error) in - guard let image = image else { return } - guard imageUrl == url else { return } - self?.imageView.image = image.withRenderingMode(self?.style?.imageRenderMode ?? .automatic) - } - } else { - if let imageName = item?.imageName { - let bundle = item?.bundle ?? Bundle.main - if let image = UIImage(named: imageName, in: bundle, compatibleWith: nil) { - imageView.image = image.withRenderingMode(style?.imageRenderMode ?? .automatic) - } - } - } - imageView.isHidden = !(item?.isImageAvailable ?? false) } } + func loadImageIfNeeded() { + if let imageUrl = item?.imageUrl { + imageDownload.downloadImage(url: imageUrl) { [weak self] (image, url, error) in + guard let image = image else { return } + guard imageUrl == url else { return } + self?.imageView.image = image.withRenderingMode(self?.style?.imageRenderMode ?? .automatic) + } + } else if let imageName = item?.imageName { + let bundle = item?.bundle ?? Bundle.main + if let image = UIImage(named: imageName, in: bundle, compatibleWith: nil) { + imageView.image = image.withRenderingMode(style?.imageRenderMode ?? .automatic) + } + } + } + + func cancelImageDownloadIfNeeded() { + guard item?.imageUrl != nil else { return } + imageDownload.cancel() + } + private var style: SegmentItemStylable? { didSet { configUI() @@ -54,14 +59,12 @@ internal class SegmentCollectionViewCell: UICollectionViewCell { self.backgroundColor = .clear item = nil imageView.image = nil - imageDownload.cancel() } override func prepareForReuse() { super.prepareForReuse() imageView.image = nil item = nil - imageDownload.cancel() } override var isSelected: Bool { diff --git a/Sources/RESegmentedControl/Classes/Models/SegmentModel.swift b/Sources/RESegmentedControl/Classes/Models/SegmentModel.swift index 1d7d36e..4cba86d 100755 --- a/Sources/RESegmentedControl/Classes/Models/SegmentModel.swift +++ b/Sources/RESegmentedControl/Classes/Models/SegmentModel.swift @@ -58,7 +58,7 @@ public struct SegmentModel { return true } - if let imageName = imageName, imageName != "" { + if imageName != "" { return true } diff --git a/Sources/RESegmentedControl/Classes/RESegmentedControl.swift b/Sources/RESegmentedControl/Classes/RESegmentedControl.swift index a19730e..934bae0 100755 --- a/Sources/RESegmentedControl/Classes/RESegmentedControl.swift +++ b/Sources/RESegmentedControl/Classes/RESegmentedControl.swift @@ -337,6 +337,18 @@ extension RESegmentedControl: UICollectionViewDataSource { return cell ?? UICollectionViewCell() } + + public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + guard collectionView == self.collectionView else { return } + guard let _cell = cell as? SegmentCollectionViewCell else { return } + _cell.loadImageIfNeeded() + } + + public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + guard collectionView == self.collectionView else { return } + guard let _cell = cell as? SegmentCollectionViewCell else { return } + _cell.cancelImageDownloadIfNeeded() + } } // - MARK: UICollectionViewDelegate diff --git a/Sources/RESegmentedControl/Classes/SegmentedControl+API.swift b/Sources/RESegmentedControl/Classes/SegmentedControl+API.swift index 0fb7301..fe77ac5 100644 --- a/Sources/RESegmentedControl/Classes/SegmentedControl+API.swift +++ b/Sources/RESegmentedControl/Classes/SegmentedControl+API.swift @@ -50,7 +50,7 @@ extension RESegmentedControl { } /// Clears images cache, that was downloaded from a remote server - public func clearCache() { + public static func clearCache() { let fileManager = FileManager() fileManager.removeImagesDirectory() }