Skip to content

Commit 7374167

Browse files
committed
Remove generics
1 parent 03676ad commit 7374167

9 files changed

+68
-57
lines changed

Example/INSPhotoGallery/CustomModelViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ extension CustomModelViewController: UICollectionViewDataSource, UICollectionVie
4444
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
4545
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ExampleCollectionViewCell
4646
let currentPhoto = photos[indexPath.row]
47-
let galleryPreview = INSPhotosViewController<CustomPhotoModel>(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
47+
let galleryPreview = INSPhotosViewController(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
4848

4949
galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in
50-
if let index = self?.photos.indexOf(photo) {
50+
if let index = self?.photos.indexOf({$0 === photo}) {
5151
let indexPath = NSIndexPath(forItem: index, inSection: 0)
5252
let cell = collectionView.cellForItemAtIndexPath(indexPath) as? ExampleCollectionViewCell
5353
return cell

Example/INSPhotoGallery/CustomOverlayView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import UIKit
1010
import INSNibLoading
1111

1212
class CustomOverlayView: INSNibLoadedView {
13-
weak var photosViewController: UIViewController?
13+
weak var photosViewController: INSPhotosViewController?
1414

1515
// Pass the touches down to other views
1616
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
@@ -27,7 +27,7 @@ class CustomOverlayView: INSNibLoadedView {
2727

2828

2929
extension CustomOverlayView: INSPhotosOverlayViewable {
30-
func populateWithPhoto<T: INSPhotoViewable>(photo: T) {
30+
func populateWithPhoto(photo: INSPhotoViewable) {
3131

3232
}
3333
func setHidden(hidden: Bool, animated: Bool) {

Example/INSPhotoGallery/ViewController.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ViewController: UIViewController {
1313
@IBOutlet weak var collectionView: UICollectionView!
1414
var useCustomOverlay = false
1515

16-
lazy var photos: [INSPhoto] = {
16+
lazy var photos: [INSPhotoViewable] = {
1717
return [
1818
INSPhoto(imageURL: NSURL(string: "http://inspace.io/assets/portfolio/thumb/13-3f15416ddd11d38619289335fafd498d.jpg"), thumbnailImage: UIImage(named: "thumbnailImage")!),
1919
INSPhoto(imageURL: NSURL(string: "http://inspace.io/assets/portfolio/thumb/13-3f15416ddd11d38619289335fafd498d.jpg"), thumbnailImage: UIImage(named: "thumbnailImage")!),
@@ -31,7 +31,9 @@ class ViewController: UIViewController {
3131
collectionView.dataSource = self
3232

3333
for photo in photos {
34-
photo.attributedTitle = NSAttributedString(string: "Example caption text\ncaption text", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
34+
if let photo = photo as? INSPhoto {
35+
photo.attributedTitle = NSAttributedString(string: "Example caption text\ncaption text", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
36+
}
3537
}
3638
}
3739
}
@@ -50,13 +52,13 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl
5052
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
5153
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ExampleCollectionViewCell
5254
let currentPhoto = photos[indexPath.row]
53-
let galleryPreview = INSPhotosViewController<INSPhoto>(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
55+
let galleryPreview = INSPhotosViewController(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
5456
if useCustomOverlay {
5557
galleryPreview.overlayView = CustomOverlayView(frame: CGRect.zero)
5658
}
5759

5860
galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in
59-
if let index = self?.photos.indexOf(photo) {
61+
if let index = self?.photos.indexOf({$0 === photo}) {
6062
let indexPath = NSIndexPath(forItem: index, inSection: 0)
6163
return collectionView.cellForItemAtIndexPath(indexPath) as? ExampleCollectionViewCell
6264
}

INSPhotoGallery/INSPhoto.swift

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
import Foundation
2121
import UIKit
22-
23-
public protocol INSPhotoViewable: class {
22+
/*
23+
* This is marked as @objc because of Swift bug http://stackoverflow.com/questions/30100787/fatal-error-array-cannot-be-bridged-from-objective-c-why-are-you-even-trying when passing for example [INSPhoto] array
24+
* to INSPhotosViewController
25+
*/
26+
@objc public protocol INSPhotoViewable: class {
2427
var image: UIImage? { get }
2528
var thumbnailImage: UIImage? { get }
2629

@@ -31,13 +34,13 @@ public protocol INSPhotoViewable: class {
3134
}
3235

3336
public class INSPhoto: INSPhotoViewable, Equatable {
34-
public var image: UIImage?
35-
public var thumbnailImage: UIImage?
37+
@objc public var image: UIImage?
38+
@objc public var thumbnailImage: UIImage?
3639

3740
var imageURL: NSURL?
3841
var thumbnailImageURL: NSURL?
3942

40-
public var attributedTitle: NSAttributedString?
43+
@objc public var attributedTitle: NSAttributedString?
4144

4245
init(image: UIImage?, thumbnailImage: UIImage?) {
4346
self.image = image
@@ -54,14 +57,14 @@ public class INSPhoto: INSPhotoViewable, Equatable {
5457
self.thumbnailImage = thumbnailImage
5558
}
5659

57-
public func loadImageWithCompletionHandler(completion: (image: UIImage?, error: NSError?) -> ()) {
60+
@objc public func loadImageWithCompletionHandler(completion: (image: UIImage?, error: NSError?) -> ()) {
5861
if let image = image {
5962
completion(image: image, error: nil)
6063
return
6164
}
6265
loadImageWithURL(imageURL, completion: completion)
6366
}
64-
public func loadThumbnailImageWithCompletionHandler(completion: (image: UIImage?, error: NSError?) -> ()) {
67+
@objc public func loadThumbnailImageWithCompletionHandler(completion: (image: UIImage?, error: NSError?) -> ()) {
6568
if let thumbnailImage = thumbnailImage {
6669
completion(image: thumbnailImage, error: nil)
6770
return

INSPhotoGallery/INSPhotoViewController.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import UIKit
2121

22-
public class INSPhotoViewController<T: INSPhotoViewable>: UIViewController, UIScrollViewDelegate {
23-
var photo: T
22+
public class INSPhotoViewController: UIViewController, UIScrollViewDelegate {
23+
var photo: INSPhotoViewable
2424

2525
var longPressGestureHandler: ((UILongPressGestureRecognizer) -> ())?
2626

@@ -45,11 +45,15 @@ public class INSPhotoViewController<T: INSPhotoViewable>: UIViewController, UISc
4545
return activityIndicator
4646
}()
4747

48-
public init(photo: T) {
48+
public init(photo: INSPhotoViewable) {
4949
self.photo = photo
5050
super.init(nibName: nil, bundle: nil)
5151
}
5252

53+
required public init?(coder aDecoder: NSCoder) {
54+
fatalError("init(coder:) has not been implemented")
55+
}
56+
5357
deinit {
5458
scalingImageView.delegate = nil
5559
}

INSPhotoGallery/INSPhotosDataSource.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919

2020
import Foundation
2121

22-
struct INSPhotosDataSource<T: INSPhotoViewable> {
23-
var photos: NSArray = []
22+
struct INSPhotosDataSource{
23+
var photos: [INSPhotoViewable] = []
2424

2525
var numberOfPhotos: Int {
2626
return photos.count
2727
}
2828

29-
func photoAtIndex(index: Int) -> T? {
29+
func photoAtIndex(index: Int) -> INSPhotoViewable? {
3030
if (index < photos.count && index >= 0) {
31-
return photos[index] as? T;
31+
return photos[index];
3232
}
3333
return nil
3434
}
3535

36-
func indexOfPhoto(photo: T) -> Int? {
37-
return photos.indexOfObject(photo)
36+
func indexOfPhoto(photo: INSPhotoViewable) -> Int? {
37+
return photos.indexOf({ $0 === photo})
3838
}
3939

40-
func containsPhoto(photo: T) -> Bool {
40+
func containsPhoto(photo: INSPhotoViewable) -> Bool {
4141
return indexOfPhoto(photo) != nil
4242
}
4343

44-
subscript(index: Int) -> T? {
44+
subscript(index: Int) -> INSPhotoViewable? {
4545
get {
4646
return photoAtIndex(index)
4747
}

INSPhotoGallery/INSPhotosOverlayView.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
import UIKit
2121

22-
public protocol INSPhotosOverlayViewable: class {
23-
weak var photosViewController: UIViewController? { get set }
22+
public protocol INSPhotosOverlayViewable:class {
23+
weak var photosViewController: INSPhotosViewController? { get set }
2424

25-
func populateWithPhoto<T: INSPhotoViewable>(photo: T)
25+
func populateWithPhoto(photo: INSPhotoViewable)
2626
func setHidden(hidden: Bool, animated: Bool)
2727
func view() -> UIView
2828
}
@@ -38,7 +38,7 @@ public class INSPhotosOverlayView: UIView , INSPhotosOverlayViewable {
3838
public private(set) var captionLabel: UILabel!
3939

4040
public private(set) var navigationItem: UINavigationItem!
41-
public weak var photosViewController: UIViewController?
41+
public weak var photosViewController: INSPhotosViewController?
4242
private var currentPhoto: INSPhotoViewable?
4343

4444
var leftBarButtonItem: UIBarButtonItem? {
@@ -105,10 +105,10 @@ public class INSPhotosOverlayView: UIView , INSPhotosOverlayViewable {
105105
}
106106
}
107107

108-
public func populateWithPhoto<T: INSPhotoViewable>(photo: T) {
108+
public func populateWithPhoto(photo: INSPhotoViewable) {
109109
self.currentPhoto = photo
110110

111-
if let photosViewController = photosViewController as? INSPhotosViewController<T> {
111+
if let photosViewController = photosViewController {
112112
if let index = photosViewController.dataSource.indexOfPhoto(photo) {
113113
navigationItem.title = "\(index+1) of \(photosViewController.dataSource.numberOfPhotos)"
114114
}

INSPhotoGallery/INSPhotosViewController.swift

+24-22
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import UIKit
2121
import MessageUI
2222

23-
public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIViewControllerTransitioningDelegate {
24-
public typealias INSPhotosViewControllerReferenceViewHandler = (photo: T) -> (UIView?)
25-
public typealias INSPhotosViewControllerNavigateToPhotoHandler = (photo: T) -> ()
26-
public typealias INSPhotosViewControllerDismissHandler = (viewController: INSPhotosViewController) -> ()
27-
public typealias INSPhotosViewControllerLongPressHandler = (photo: T, gestureRecognizer: UILongPressGestureRecognizer) -> (Bool)
28-
23+
public typealias INSPhotosViewControllerReferenceViewHandler = (photo: INSPhotoViewable) -> (UIView?)
24+
public typealias INSPhotosViewControllerNavigateToPhotoHandler = (photo: INSPhotoViewable) -> ()
25+
public typealias INSPhotosViewControllerDismissHandler = (viewController: INSPhotosViewController) -> ()
26+
public typealias INSPhotosViewControllerLongPressHandler = (photo: INSPhotoViewable, gestureRecognizer: UILongPressGestureRecognizer) -> (Bool)
27+
28+
29+
public class INSPhotosViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIViewControllerTransitioningDelegate {
30+
2931
public var referenceViewForPhotoWhenDismissingHandler: INSPhotosViewControllerReferenceViewHandler?
3032
public var navigateToPhotoHandler: INSPhotosViewControllerNavigateToPhotoHandler?
3133
public var willDismissHandler: INSPhotosViewControllerDismissHandler?
@@ -44,17 +46,17 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
4446
}
4547
}
4648

47-
public var currentPhotoViewController: INSPhotoViewController<T>? {
48-
return pageViewController.viewControllers?.first as? INSPhotoViewController<T>
49+
public var currentPhotoViewController: INSPhotoViewController? {
50+
return pageViewController.viewControllers?.first as? INSPhotoViewController
4951
}
5052

51-
public var currentPhoto: T? {
53+
public var currentPhoto: INSPhotoViewable? {
5254
return currentPhotoViewController?.photo
5355
}
5456

5557
// MARK: - Private
5658
private(set) var pageViewController: UIPageViewController!
57-
private(set) var dataSource: INSPhotosDataSource<T>
59+
private(set) var dataSource: INSPhotosDataSource
5860

5961
let interactiveAnimator: INSPhotosInteractionAnimator = INSPhotosInteractionAnimator()
6062
let transitionAnimator: INSPhotosTransitionAnimator = INSPhotosTransitionAnimator()
@@ -78,26 +80,26 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
7880
}
7981

8082
required public init?(coder aDecoder: NSCoder) {
81-
dataSource = INSPhotosDataSource<T>(photos: [])
83+
dataSource = INSPhotosDataSource(photos: [])
8284
super.init(nibName: nil, bundle: nil)
8385
initialSetupWithInitialPhoto(nil)
8486
}
8587

8688
public override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
87-
dataSource = INSPhotosDataSource<T>(photos: [])
89+
dataSource = INSPhotosDataSource(photos: [])
8890
super.init(nibName: nil, bundle: nil)
8991
initialSetupWithInitialPhoto(nil)
9092
}
9193

92-
public init(photos: [T], initialPhoto: T? = nil, referenceView: UIView? = nil) {
93-
dataSource = INSPhotosDataSource<T>(photos: photos)
94+
public init(photos: [INSPhotoViewable], initialPhoto: INSPhotoViewable? = nil, referenceView: UIView? = nil) {
95+
dataSource = INSPhotosDataSource(photos: photos)
9496
super.init(nibName: nil, bundle: nil)
9597
initialSetupWithInitialPhoto(initialPhoto)
9698
transitionAnimator.startingView = referenceView
9799
transitionAnimator.endingView = currentPhotoViewController?.scalingImageView.imageView
98100
}
99101

100-
private func initialSetupWithInitialPhoto(initialPhoto: T? = nil) {
102+
private func initialSetupWithInitialPhoto(initialPhoto: INSPhotoViewable? = nil) {
101103
overlayView.photosViewController = self
102104
setupPageViewControllerWithInitialPhoto(initialPhoto)
103105

@@ -155,15 +157,15 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
155157
overlayView.setHidden(true, animated: false)
156158
}
157159

158-
private func setupPageViewControllerWithInitialPhoto(initialPhoto: T? = nil) {
160+
private func setupPageViewControllerWithInitialPhoto(initialPhoto: INSPhotoViewable? = nil) {
159161
pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: [UIPageViewControllerOptionInterPageSpacingKey: 16.0])
160162
pageViewController.view.backgroundColor = UIColor.clearColor()
161163
pageViewController.delegate = self
162164
pageViewController.dataSource = self
163165

164166
if let photo = initialPhoto where dataSource.containsPhoto(photo) {
165167
changeToPhoto(photo, animated: false)
166-
} else if let photo = dataSource.photos.firstObject as? T {
168+
} else if let photo = dataSource.photos.first {
167169
changeToPhoto(photo, animated: false)
168170
}
169171
}
@@ -176,7 +178,7 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
176178

177179
// MARK: - Public
178180

179-
public func changeToPhoto(photo: T, animated: Bool) {
181+
public func changeToPhoto(photo: INSPhotoViewable, animated: Bool) {
180182
if !dataSource.containsPhoto(photo) {
181183
return
182184
}
@@ -239,8 +241,8 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
239241

240242
// MARK: - UIPageViewControllerDataSource / UIPageViewControllerDelegate
241243

242-
private func initializePhotoViewControllerForPhoto(photo: T) -> INSPhotoViewController<T> {
243-
let photoViewController = INSPhotoViewController<T>(photo: photo)
244+
private func initializePhotoViewControllerForPhoto(photo: INSPhotoViewable) -> INSPhotoViewController {
245+
let photoViewController = INSPhotoViewController(photo: photo)
244246
singleTapGestureRecognizer.requireGestureRecognizerToFail(photoViewController.doubleTapGestureRecognizer)
245247
photoViewController.longPressGestureHandler = { [weak self] gesture in
246248
guard let weakSelf = self else {
@@ -268,7 +270,7 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
268270
}
269271

270272
@objc public func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
271-
guard let photoViewController = viewController as? INSPhotoViewController<T>,
273+
guard let photoViewController = viewController as? INSPhotoViewController,
272274
let photoIndex = dataSource.indexOfPhoto(photoViewController.photo),
273275
let newPhoto = dataSource[photoIndex-1] else {
274276
return nil
@@ -277,7 +279,7 @@ public class INSPhotosViewController<T: INSPhotoViewable>: UIViewController, UIP
277279
}
278280

279281
@objc public func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
280-
guard let photoViewController = viewController as? INSPhotoViewController<T>,
282+
guard let photoViewController = viewController as? INSPhotoViewController,
281283
let photoIndex = dataSource.indexOfPhoto(photoViewController.photo),
282284
let newPhoto = dataSource[photoIndex+1] else {
283285
return nil

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Simple Usage
1515

1616
```swift
17-
lazy var photos: [INSPhoto] = {
17+
lazy var photos: [INSPhotoViewable] = {
1818
return [
1919
INSPhoto(imageURL: NSURL(string: "http://inspace.io/assets/portfolio/thumb/13-3f15416ddd11d38619289335fafd498d.jpg"), thumbnailImage: UIImage(named: "thumbnailImage")!),
2020
INSPhoto(imageURL: NSURL(string: "http://inspace.io/assets/portfolio/thumb/13-3f15416ddd11d38619289335fafd498d.jpg"), thumbnailImage: UIImage(named: "thumbnailImage")!),
@@ -30,7 +30,7 @@ func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath i
3030
let galleryPreview = INSPhotosViewController<INSPhoto>(photos: photos, initialPhoto: currentPhoto, referenceView: cell)
3131

3232
galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in
33-
if let index = self?.photos.indexOf(photo) {
33+
if let index = self?.photos.indexOf({$0 === photo}) {
3434
let indexPath = NSIndexPath(forItem: index, inSection: 0)
3535
return collectionView.cellForItemAtIndexPath(indexPath) as? ExampleCollectionViewCell
3636
}
@@ -45,7 +45,7 @@ func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath i
4545
You are able to create your custom photo model which can be use instead default `INSPhoto`. Default implementation don't cache images. If you would like to use some caching mechanism or use some library for downloading images for example `HanekeSwift` use must implement `INSPhotoViewable` protocol.
4646

4747
```swift
48-
public protocol INSPhotoViewable: NSObjectProtocol {
48+
@objc public protocol INSPhotoViewable: class {
4949
var image: UIImage? { get }
5050
var thumbnailImage: UIImage? { get }
5151

0 commit comments

Comments
 (0)