Skip to content

Instantly share code, notes, and snippets.

View jamesrochabrun's full-sized avatar
🇵🇪

James Rochabrun jamesrochabrun

🇵🇪
View GitHub Profile
@jamesrochabrun
jamesrochabrun / GenericList.swift
Created July 23, 2020 00:34
GenericList using Swift UI.
// 1
struct GenericList<Element, RowContent: View>: View where Element: Identifiable {
// 2
private let items: [Element]
private let rowContent: (Element) -> RowContent
// 3
public init(_ items: [Element], @ViewBuilder rowContent: @escaping (Element) -> RowContent) {
self.items = items
@jamesrochabrun
jamesrochabrun / PHAsset+Extension.swift
Created May 6, 2018 05:25
PHAsset extension that returns an array of assets in a time period related to an asset.
extension PHAsset {
// MARK: This returns an array of alternate assets from a PHAsset
func getAlternatePhotos() -> [PHAsset] {
/// get the collection of the asset to avoid fetching all photos in the library
let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsContaining(self, with: .moment, options: nil)
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor.init(key: "creationDate", ascending: true)]
@jamesrochabrun
jamesrochabrun / TrailingTextLoader.swift
Last active September 30, 2023 05:58
An animated Trailing text
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
struct CenteredLoadingText: View {
private let mainText: String
private let dots = [".", ".", "."]
private let repeatInterval: TimeInterval
private let accessibilityLabel: String
private let loadingAnimation: Animation
private let spacing: CGFloat
@jamesrochabrun
jamesrochabrun / GroupTaskImplemnetation.swift
Last active January 3, 2022 23:52
Using Group tasks to synchronize networking calls.
// MARK:- Async/await Group task
// 1
@available(iOS 15, *)
func asyncGroups(
from categoryIdentifiers: [ItunesCategoryIdentifier]) {
// 2
Task.init {
// 3
var sections: [ItunesCategorySection] = []
// 4
@jamesrochabrun
jamesrochabrun / ItunesCategoryIdentifier.swift
Last active January 3, 2022 23:47
Itunes category identifier model.
enum ItunesCategoryIdentifier: Int, CaseIterable {
// 1
case apps
case podcasts
case tvShows
// 2
var title: String {
switch self {
@jamesrochabrun
jamesrochabrun / DispatchGroupImplementation.swift
Last active June 17, 2021 21:30
A sample function that uses DispatchGroup API
func dispatchGroups(
from categoryIdentifiers: [ItunesCategoryIdentifier]) {
// 1
let dispatchGroup = DispatchGroup()
// 2
var sections: [ItunesCategorySection] = []
// 3
for categoryIdentifier in categoryIdentifiers {
// 4
@jamesrochabrun
jamesrochabrun / ItunesRemote.swift
Last active June 17, 2021 18:16
Itunes Remote implementation.
// 1
final class ItunesRemote: ObservableObject {
// 2
struct ItunesCategorySection: IdentifiableHashable {
let sectionID: ItunesCategoryIdentifier
let cellIDs: [FeedItemViewModel]
var id: ItunesCategoryIdentifier { sectionID }
}
@jamesrochabrun
jamesrochabrun / AsyncGenericAPI.swift
Last active June 11, 2021 02:58
Generic API async-await based
enum APIError: Error {
case requestFailed(description: String)
case jsonConversionFailure(description: String)
case invalidData
case responseUnsuccessful(description: String)
case jsonParsingFailure
case noInternet
case failedSerialization
@jamesrochabrun
jamesrochabrun / GenericClosureBasedAPI.swift
Last active June 10, 2021 20:28
Generic closure based API
/// 1 Errors
enum APIError: Error {
case requestFailed(description: String)
case jsonConversionFailure(description: String)
case invalidData
case responseUnsuccessful(description: String)
case jsonParsingFailure
case noInternet
case failedSerialization
import SwiftUI
import CompositionalList
struct FeedView: View {
@ObservedObject private var remote = ItunesRemote()
@State var selectedItem: FeedItemViewModel?
var body: some View {
NavigationView {