Skip to content

Instantly share code, notes, and snippets.

Avatar
🇵🇪

James Rochabrun jamesrochabrun

🇵🇪
View GitHub Profile
View AppleMusicFeed.swift
import SwiftUI
import CompositionalList
struct FeedView: View {
@ObservedObject private var remote = ItunesRemote()
@State var selectedItem: FeedItemViewModel?
var body: some View {
NavigationView {
View MoviesProvider.swift
// 1
final class MoviesProvider: ObservableObject {
// MARK:- Subscribers
// 2
private var cancellable: AnyCancellable?
// MARK:- Publishers
// 3
@Published var movies: [MovieViewModel] = []
@jamesrochabrun
jamesrochabrun / MovieClient.swift
Last active Jul 23, 2020
MovieClient fetches movies from Movies API.
View MovieClient.swift
final class MovieClient: CombineAPI {
// 1
let session: URLSession
// 2
init(configuration: URLSessionConfiguration) {
self.session = URLSession(configuration: configuration)
}
@jamesrochabrun
jamesrochabrun / CombineAPI.Swift
Created Jul 23, 2020
Generic nertworking layer using Combine.
View CombineAPI.Swift
// 1
protocol CombineAPI {
var session: URLSession { get }
func execute<T>(_ request: URLRequest, decodingType: T.Type, queue: DispatchQueue, retries: Int) -> AnyPublisher<T, Error> where T: Decodable
}
// 2
extension CombineAPI {
func execute<T>(_ request: URLRequest,
View SwiftUIMoviesApp.swift
// 1
@main
struct SwiftUIMoviesApp: App {
// 2
@StateObject private var model = MoviesProvider()
// 3
var body: some Scene {
WindowGroup {
// 4
@jamesrochabrun
jamesrochabrun / GenericList.swift
Created Jul 23, 2020
GenericList using Swift UI.
View GenericList.swift
// 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
View Weird.swift
import UIKit
class Vc: UIViewController {
var top: CGFloat? { 8 }
}
let viewController = Vc()
let a: CGFloat = 1.0
let b: CGFloat = 2.0
let result = (viewController as? Vc)?.top ?? a < b ? 10 : 15
View FrameTransitionAnimator.swift
class FrameTransitionAnimator: NSObject {
enum OverlayType {
case dim
case blur(style: UIBlurEffect.Style)
}
enum DimTransitionMode: Int {
case present, dismiss
View DisplayModeUpdatable.swift
/// 1
protocol DisplayModeUpdatable {
func displayModeWillChangeTo(_ displayMode: UISplitViewController.DisplayMode)
func displayModeDidChangeTo(_ displayMode: UISplitViewController.DisplayMode)
}
////
@objc func togglePrefferDisplayModeExecutingCompletion(_ executing: Bool = true) {
UIView.animate(withDuration: 0.3, animations: {
self.preferredDisplayMode = self.displayMode == .allVisible ? .primaryHidden : .allVisible
View displayModeButton.swift
/// 1
lazy var displayModeCustomButton: Button = {
let button = Button(type: .system, image: SplitViewControllerViewModel.displayModeButtonImageFor(self.displayMode), target: self, selector: #selector(togglePrefferDisplayModeExecutingCompletion))
button.constrainWidth(constant: 44.0)
button.constrainHeight(constant: 44.0)
button.imageEdgeInsets = .init(top: 8.0, left: 8.0, bottom: 8.0, right: 8.0)
return button
}()