Skip to content

Instantly share code, notes, and snippets.

View marlonjames71's full-sized avatar
💻
Learning Swift & SwiftUI

Marlon Raskin marlonjames71

💻
Learning Swift & SwiftUI
View GitHub Profile
@marlonjames71
marlonjames71 / Twitter Likes Count Format with Swift's FormatStyle.swift
Last active February 28, 2023 05:53
Swift's formatting abilities are insanely good. I have created my own implementation of this formatting style not knowing that this API existed. I just learned about this yesterday (2023-02-26). Here's a fantastic resource all about Swift's formatting abilities: [Fucking Format Style](https://fuckingformatstyle.com/).
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
@marlonjames71
marlonjames71 / twitterMetricsDisplayFormatter.swift
Last active February 9, 2023 07:51
This methods is for formatting numbers so they're shortened and shown with K for thousands, M for millions, and B for billions.
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
// 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,
@marlonjames71
marlonjames71 / forautolayout.swift
Last active July 9, 2021 21:54
A little helper method when doing programmatic auto layout. No more typing out `translatesAutoresizingMaskIntoConstraints`.
extension UIView {
/// Sets `translatesAutoresizingMaskIntoConstraints` to false.
///
/// // Example Usage:
///
/// private lazy var captionLabel: UILabel = {
/// let label = UILabel().forAutoLayout()
/// label.text = viewModel.captionText
/// label.textAlignment = .center
class PagedMediaViewController: UIPageViewController {
// MARK: - Properties
private var mediaItems: [MediaItem]
private var currentIndex: Int
// MARK: - Init
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
}
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 }
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)
}
}
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)
// 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