This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// This is so I didn't have to repeat myself for all of the for loops below. | |
extension FormatStyle where Self == IntegerFormatStyle<Int> { | |
static func compactCount(significantDigits digits: Int = 3) -> Self { | |
.number.precision(.significantDigits(digits)).notation(.compactName) | |
} | |
} | |
// OR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
subscript (safe index: Index) -> Element? { | |
return indices.contains(index) ? self[index] : nil | |
} | |
} | |
func shorten(_ number: Int, afterDigitPlacement placement: Int = 3) -> String { | |
let symbols = ["K", "M", "B"] | |
let placementCount = String(number).count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bigger iPhones = any Max, any Plus, iPhone XR, iPhone 11 | |
switch (UITraitCollection.current.horizontalSizeClass, UITraitCollection.current.verticalSizeClass) { | |
case (.compact, .compact): | |
// Smaller iPhones in landscape | |
case (.compact, .regular): | |
// Bigger iPhones in portrait | |
// iPads in portrait during any split screen, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIView { | |
/// Sets `translatesAutoresizingMaskIntoConstraints` to false. | |
/// | |
/// // Example Usage: | |
/// | |
/// private lazy var captionLabel: UILabel = { | |
/// let label = UILabel().forAutoLayout() | |
/// label.text = viewModel.captionText | |
/// label.textAlignment = .center |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PagedMediaViewController: UIPageViewController { | |
// MARK: - Properties | |
private var mediaItems: [MediaItem] | |
private var currentIndex: Int | |
// MARK: - Init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension PagedMediaViewController: UIPageViewControllerDelegate { | |
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { | |
guard | |
let mediaVCs = pageViewController.viewControllers as? [MediaViewController], | |
let currIndex = mediaItems.firstIndex(of: mediaVCs[0].mediaItem) | |
else { return } | |
currentIndex = currIndex | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension PagedMediaViewController: UIPageViewControllerDataSource { | |
pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
guard currentIndex > 0 else { return nil } | |
return mediaViewControllerAtIndex(currentIndex - 1) | |
} | |
pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
guard currentIndex < mediaItems.count - 1 else { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
dataSource = self | |
delegate = self | |
if let firstMediaVC = mediaViewControllerAtIndex(currentIndex) { | |
let viewControllers = [firstMediaVC] | |
setViewControllers(viewControllers, direction: .forward, animated: false, completion: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func mediaViewControllerAtIndex(_ index: Int) -> MediaViewController? { | |
// returns nil if the index is out of bounds, this way the app won't crash. | |
// You can also put a print statement before you return. | |
guard (0...mediaItems.count).contains(index) else { return nil } | |
let item = mediaItems[index] | |
switch item.mediaType { | |
case .video: | |
return VideoViewController(mediaItem: item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For one dimensional array | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
let item = mediaItems[indexPath.item] | |
let pagedMediaViewController = PagedMediaController(mediaItem: item, currentIndex: indexPath.item) | |
pagedMediaViewController.modalPresentationStyle = .overFullScreen | |
present(pagedMediaViewController, animated: true) | |
} | |
// In case you're dealing with a two dimensional array like I was dealing with since the collection view's items | |
// had sections |
NewerOlder