Skip to content

Instantly share code, notes, and snippets.

View gtokman's full-sized avatar
🏴‍☠️
Focusing

Gary Tokman gtokman

🏴‍☠️
Focusing
View GitHub Profile
import SwiftUIX
struct Friend: Identifiable { // Example model
let id: UUID = .init()
let name: String
}
struct ContentView: View {
@State var friends: [Friend] = [
.init(name: "Gary"),
@gtokman
gtokman / ContentView.swift
Last active March 13, 2021 20:39
Search - part 2
// ...
List(friends
.filter {
search(
needle: searchText.lowercased(),
haystack: $0.name.lowercased()
)
}
)
// ...
@gtokman
gtokman / ContentView.swift
Last active March 13, 2021 20:39
Overload == operator
typealias Predicate<Element> = (Element) -> Bool
func ==<Element>(lhs: KeyPath<Element, String>, rhs: String) -> Predicate<Element> { // overload ==
return { element in
guard !rhs.isEmpty else { return true } // make sure not empty
return search(
needle: rhs.lowercased(), // search
haystack: element[keyPath: lhs].lowercased() // value for keypath
)
}
@gtokman
gtokman / ContentView.swift
Created March 13, 2021 22:16
Show/Dismiss keyboard
import SwiftUIX
import Introspect
var body: some View {
NavigationView {
// ...
.onTapGesture {
print(friend.name)
Keyboard.main.dismiss() // inactive
}
@gtokman
gtokman / ContentView.swift
Created March 13, 2021 23:58
BottomSheet - Part 1
struct ContentView: View {
@State var isPresented: Bool = false
var body: some View {
Button(action: {}, label: {
Text("Present Bottom Sheet")
.padding()
})
.background(Color.blue)
.foregroundColor(.white)
@gtokman
gtokman / app.js
Created March 15, 2021 15:52
Scrollable List
import logo from "./logo.svg";
import "./App.css";
import styled from "styled-components";
function App() {
return (
<Container>
<WrapperCard>
<Card></Card>
<Card></Card>
@gtokman
gtokman / App.js
Last active March 16, 2021 18:15
flexbox primer
import styled from 'styled-components';
const Container = styled.div`
background: #36393e;
display: flex;
justify-content: center; // 1
flex-flow: column wrap; // 2
width: 100%;
height: 100%;
@gtokman
gtokman / App.js
Last active March 16, 2021 18:14
Header React
const Header = styled.div` // 1
height: 100px;
width: 100%;
background: #fff;
display: flex; // 2
justify-content: center;
`;
function App() {
return (
@gtokman
gtokman / ViewController.swift
Last active March 27, 2021 13:06
URLSession + Combine
import UIKit
import Combine
import PlaygroundSupport
let label = UILabel(frame: .init(origin: .zero, size: CGSize(width: 100, height: 100)))
let url = URL(string: "https://api.mocki.io/v1/aebff128")!
var cancellables = Set<AnyCancellable>()
URLSession
.shared
@gtokman
gtokman / ViewController.swift
Last active March 27, 2021 17:03
Passthrough subject - combine
// Create passthrough subject
let todoSubject = PassthroughSubject<Todo, Never>()
// Release subscription from memory when done
var cancellable: AnyCancellable?
// Subscribe to updates
cancellable = todoSubject
.sink { todo in
print("New todo to add to table view:", todo.task)