Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexnikol
alexnikol / code1.swift
Last active December 13, 2022 07:39
From procedural code to decorator pattern
// CODE 1
class HTTPClient {
func load(url: URL, completion: @escaping (Response) -> Void) {
// call server
// complete with result
}
}
// Responsibilities:
/// - Send request and return response
@alexnikol
alexnikol / CI_iOS.yml
Last active December 11, 2022 16:52
GitHub Actions workflow build, tests, send test coverage to code climate and show tests result convenient way in report page, Appstore deployment
# Name which will be visible in Github Actions tab in repository
name: CI iOS
# Trigger which will run this workflow. (any pull request in main page)
on:
pull_request:
branches: [ main ]
jobs:
build-and-test:
@alexnikol
alexnikol / .swift
Last active January 25, 2021 10:13
Decoding JSON with endless nesting
import Foundation
let JSON = """
[{
"sub_elements": [{
"sub_elements": [{
"id": "4315FAC2-0809-478C-BED8-54F28B0A9A57",
"name": "Shape 1"
}, {
"id": "D2985D5B-B98A-4549-A4ED-3742583A2532",
@alexnikol
alexnikol / .swift
Created August 17, 2020 07:59
Generic way to solve pagination duplications
class Pagination<Element: Decodable>: Decodable {
var list: [Element]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case list, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created August 17, 2020 07:54
Pagination duplicates problem
class Pagination: Decodable {
var pets: [Pet]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case pets, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created August 17, 2020 07:50
Default Pagination Realization
class Pagination: Decodable {
var cars: [Car]
var currentPage: Int
var totalPages: Int
enum ResultCodingKeys: String, CodingKey {
case cars, currentPage, totalPages
}
@alexnikol
alexnikol / .swift
Created August 17, 2020 07:39
Generic way code after refactoring
func handleDecoding<Element: Decodable>(responseData: Data,
responseType: Element.Type,
completion: @escaping (_ apiResponse: Element?, _ error: String?) -> Void) {
do {
let apiResponse = try JSONDecoder().decode(responseType, from: responseData)
completion(apiResponse, nil)
} catch {
completion(nil, "Network Error")
}
}
@alexnikol
alexnikol / .swift
Last active August 17, 2020 07:42
Problematic code before the Generics magic
// PetNetworkManager
func handleDecoding(responseData: Data,
completion: @escaping (_ apiResponse: Pet?, _ error: String?) -> Void) {
do {
let apiResponse = try JSONDecoder().decode(Pet.self, from: responseData)
completion(apiResponse, nil)
} catch {
completion(nil, "Network Error")
}
}
@alexnikol
alexnikol / .py
Created August 12, 2020 07:13
Insert a node at the head of a linked list. Testing
# RESULT
node1 = SinglyLinkedListNode(1)
node2 = SinglyLinkedListNode(2)
node3 = SinglyLinkedListNode(3)
node1.next = node2
node2.next = node3
result = insertNodeAtHead(node1, 33)
current_node = result
@alexnikol
alexnikol / .py
Last active August 12, 2020 07:07
Insert a node at the head of a linked list. Realization
from linked_list import SinglyLinkedListNode
def insertNodeAtHead(llist, data):
node = SinglyLinkedListNode(data)
node.next = llist
return node