Skip to content

Instantly share code, notes, and snippets.

View Edudjr's full-sized avatar
🏠
Working from home

Eduardo Domene Junior Edudjr

🏠
Working from home
  • Mercado Livre
  • Sao Paulo
View GitHub Profile
// MusicSheetPlotter
func plot(in view: UIView) {
...
stack.addArrangedSubview(quarterNote.clone() as! UIView)
stack.addArrangedSubview(quarterNote.clone() as! UIView)
stack.addArrangedSubview(quarterNote.clone() as! UIView)
...
}
protocol Prototype {
func clone() -> Prototype
}
extension UILabel: Prototype {
convenience init(_ original: UILabel) {
self.init(frame: .zero)
text = original.text
font = original.font
}
// ClientViewController
...
private lazy var content: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
let plotter = MusicSheetPlotter(quarterNote: quarterNote)
plotter.plot(in: view)
return view
struct MusicSheetPlotter {
let quarterNote: UILabel
func plot(in view: UIView) {
let stack = UIStackView()
stack.axis = .horizontal
stack.distribution = .equalSpacing
stack.spacing = 5.0
stack.addArrangedSubview(quarterNote)
import UIKit
import PlaygroundSupport
class ClientViewController: UIViewController {
private lazy var content: UIView = {
quarterNote
}()
private let quarterNote: UILabel = {
@Edudjr
Edudjr / CreditCardFormatter.swift
Created May 31, 2021 14:49
Extensible Masked Formatter using Swift 5
//
// CreditCardFormatter.swift
//
// Created by Eduardo Domene Junior on 31.05.21.
//
import Foundation
class CreditCardFormatter: MaskedFormatter {
init() {
@Edudjr
Edudjr / Example.swift
Last active May 12, 2021 15:31
This is a SwiftUI-like small framework based on Extensions that makes dealing with Autolayout way more fun and easier!
// How connecting constraints was before:
let myView = UIView()
myView.translatesAutoresizingMaskIntoConstraints = false
addSubview(myView)
myView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
myView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
myView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
myView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
// Now you have different ways of connecting them:
protocol AnimalReportLocatorProtocol {
func animalReport(for animal: Animal) -> AnimalReport
}
extension Container: AnimalReportLocatorProtocol {
func animalReport(for animal: Animal) -> AnimalReport {
container.resolve(AnimalReport.self, argument: animal)
}
}
struct AnimalSelection {
let animals: [Animal]
func selectAnimal(_ animal: Animal) {
let animalReport = Container.resolve(AnimalReport.self, argument: animal)
animalReport.describe()
}
}
struct AnimalReportFactory {
func makeAnimalReport(for animal: Animal) -> AnimalReport {
AnimalReport(animal: animal)
}
}
struct AnimalReport {
let animal: Animal
func describe() {
print("Animal is called \(animal.name) and is \(animal.age) years old")