Skip to content

Instantly share code, notes, and snippets.

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

Majid Jabrayilov mecid

🏠
Working from home
View GitHub Profile
import UIKit
import BackgroundTasks
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BGTaskScheduler.shared.register(
forTaskWithIdentifier: "pl.snowdog.example.refresh",
using: DispatchQueue.global()
@Published private var query: String = ""
private func fetchRepos(matching query: String) {
$query.flatMap {
self.githubService
.search(matching: $0)
.replaceError(with: [])
}
.subscribe(on: DispatchQueue.global())
.receive(on: OperationQueue.main)
private func fetchRepos(matching query: String) {
githubService
.search(matching: query)
.replaceError(with: [])
.subscribe(on: DispatchQueue.global())
.receive(on: OperationQueue.main)
.assign(to: \.repos, on: self)
}
let subject = CurrentValueSubject<String, Never>("Hello")
subject.sink { value in print(value) }
subject.send("World")
private func fetchRepos(matching query: String) {
githubService
.search(matching: query)
.replaceError(with: [])
.subscribe(on: DispatchQueue.global())
.receive(on: OperationQueue.main)
.sink { repos in print("count:", repos.count) }
}
import UIKit
import Combine
class ViewController: UITableViewController {
private let githubService: GithubService
private var cancellable: AnyCancellable?
private var repos: [Repo] = [] {
didSet {
tableView.reloadData()
}
@mecid
mecid / cia1.swift
Created June 19, 2019 12:08
Combine in action post's source code
import Foundation
import Combine
struct Repo: Decodable {
var id: Int
let owner: Owner
let name: String
let description: String
struct Owner: Decodable {
@mecid
mecid / StackViewController.swift
Created February 26, 2019 11:05
StackViewController
import UIKit
class StackViewController: UIViewController {
private let scrollView = UIScrollView()
private let stackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.addSubview(stackView)
@mecid
mecid / saccessibility8.swift
Last active February 11, 2019 08:33
saccessibility8.swift
override func layoutSubviews() {
super.layoutSubviews()
guard let max = values.max() else { return }
subviews.forEach { $0.removeFromSuperview() }
var x: CGFloat = Layout.barWidth
let maxHeight: CGFloat = bounds.height * Layout.maxHeightRatio
var accessibilityElements: [UIAccessibilityElement] = []
values.forEach { value in
let height = CGFloat(value / max) * maxHeight
@mecid
mecid / saccessibility7.swift
Last active February 11, 2019 08:33
saccessibility7.swift
import UIKit
class BarChartView: UIView {
private enum Layout {
static let barWidth: CGFloat = 30
static let maxHeightRatio: CGFloat = 0.8
}
var values: [Double] = [] {
didSet {