Skip to content

Commit

Permalink
- The optimized image loading process;
Browse files Browse the repository at this point in the history
- clearCache now global method;
- added changelog;
  • Loading branch information
sh-khashimov committed Dec 30, 2019
1 parent 995c50c commit 035a5e2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
12 changes: 12 additions & 0 deletions Documentation/Changelog.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 3 additions & 2 deletions RESegmentedControl.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public struct SegmentModel {
return true
}

if let imageName = imageName, imageName != "" {
if imageName != "" {
return true
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/RESegmentedControl/Classes/RESegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 035a5e2

Please sign in to comment.