Skip to content

Instantly share code, notes, and snippets.

View Czajnikowski's full-sized avatar
🚀

Maciek Czarnik Czajnikowski

🚀
View GitHub Profile
struct Example12: View {
@State private var flag = false
var body: some View {
VStack {
Spacer()
Color.clear.overlay(WaveText("The SwiftUI Lab", waveWidth: 6, pct: flag ? 1.0 : 0.0).foregroundColor(.blue)).frame(height: 40)
Color.clear.overlay(WaveText("swiftui-lab.com", waveWidth: 6, pct: flag ? 0.0 : 1.0, size: 18).foregroundColor(.green)).frame(height: 30)
Spacer()
}.onAppear {
@Czajnikowski
Czajnikowski / Example.swift
Last active November 5, 2020 07:27
My pragmatic take on infinite scrolling/lazy loading in the SwiftUI. Given example performs `fetchNextPageIfPossible` when the row that appeared is related to the one of the last 10 of models (Identifiable-s).
ForEach(viewModel.repos) { repo in
RepositoryRow(repo: repo)
.onAppear(
ofViewRelatedTo: repo,
from: viewModel.repos,
inSuffixOfLength: 10,
perform: viewModel.fetchNextPageIfPossible
)
}
final class ComposedViewController: UIViewController {
...
private let specialization: Specialization
init(specialization: Specialization) {
self.specialization = specialization
}
...
@Czajnikowski
Czajnikowski / ViewController+Builder.swift
Last active April 28, 2019 10:52
ViewController+Builder
extension ViewController {
final class Builder {
static func buildView(somethingTextField: UITextField, printButton: UIButton) -> UIView {
let view = UIView().forAutoLayout()
view.backgroundColor = .white
let stackView = buildRootStackView(somethingTextField: somethingTextField, printButton: printButton)
view.addSubview(stackView)
stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20.0).isActive = true
@Czajnikowski
Czajnikowski / LogInViewController.swift
Last active May 7, 2019 13:42
LogInViewController
public final class LogInViewController: UIViewController {
private let emailTextField: UITextField = Builder.buildEmailTextField()
private let passwordTextField: UITextField = Builder.buildPasswordTextField()
public override func loadView() {
let logInButton: UIButton = Builder.buildLogInButton(
target: self,
action: #selector(didTapLogInButton)
)
let peopleTableViewDataSource: UITableViewDataSource = HomogeneousTableViewDataSource<PersonCell, PersonCellState> {
let person = people[$0.row]
return PersonCellState(name: person.name, surname: person.surname)
}
class HomogeneousTableViewDataSource<Cell, CellState>: NSObject, UITableViewDataSource where
Cell: UITableViewCell & Identifiable & PreparableWithState,
CellState == Cell.State {
typealias ProvideCellStateAtIndexPath = (IndexPath) -> CellState
let provideCellStateAtIndexPath: ProvideCellStateAtIndexPath
init(withCellStateAtIndexPathProvider provideCellStateAtIndexPath: @escaping ProvideCellStateAtIndexPath) {
self.provideCellStateAtIndexPath = provideCellStateAtIndexPath
}
struct PersonCellState {
let name: String
let surname: String
}
extension PersonCell: PreparableWithState {
func prepare(with state: PersonCellState) {
textLabel?.text = state.name
detailTextLabel?.text = state.surname
}
protocol PreparableWithState {
associatedtype State
func prepare(with state: State)
}
protocol Identifiable {
static var identifier: String { get }
}