Skip to content

Instantly share code, notes, and snippets.

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

Gary Tokman gtokman

🏴‍☠️
Focusing
View GitHub Profile
@gtokman
gtokman / ContentView.swift
Last active March 28, 2021 15:16
Video - example
import AVKit
struct ContentView: View {
@State var videos: [Video] = []
var body: some View {
VideoView()
.onReceive(
NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime),
@gtokman
gtokman / ViewController.swift
Created March 28, 2021 14:35
AnyCancellable -combine
// many publishers
var cancellables = Set<AnyCancellable>()
$textSubject
.compactMap { $0 }
.assign(to: \.text, on: label)
.store(in: &cancellables)
// or single publisher
@gtokman
gtokman / ViewController.swift
Created March 28, 2021 14:27
assign - combine
// Create a new search subject with a default value
class ViewController: UIViewController {
@Published var buttonTapSubject: Int = 0
@Published var textSubject: String = ""
var cancellables = Set<AnyCancellable>()
let button = UIButton()
let label = UILabel()
override func viewDidLoad() {
@gtokman
gtokman / ViewController.swift
Created March 28, 2021 14:12
sink - combine
Just("hello world!")
.sink { str in
print("the string is:", str)
}
@gtokman
gtokman / ViewController.swift
Created March 28, 2021 13:45
Published - combine
// Create a new search subject with a default value
final class ViewController: UIViewController {
// Published subject initialized to zero
@Published var buttonTapSubject: Int = 0
var cancellables = Set<AnyCancellable>()
let button = UIButton()
let label = UILabel()
@gtokman
gtokman / ViewController.swift
Last active March 28, 2021 18:50
CurrentValueSubject - combine
// Create a new search subject with a default value
let searchSubject = CurrentValueSubject<String, Never>("")
// Subscribe to updates
searchSubject
.debounce(for: 0.3, scheduler: DispatchQueue.main)
.map(searchBook(for:))
.flatMap {$0.map(\.author)}
.subscribe(on: DispatchQueue.main)
.sink { author in
@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)
@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 / 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 / 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%;