Skip to content

Instantly share code, notes, and snippets.

View SergeyPetrachkov's full-sized avatar
:octocat:

Sergey Petrachkov SergeyPetrachkov

:octocat:
View GitHub Profile
@SergeyPetrachkov
SergeyPetrachkov / AsyncWaiter.swift
Created May 9, 2022 09:18 — forked from krzyzanowskim/AsyncWaiter.swift
Synchronously (well) wait for async Task value update
/// Wait for async operation to return value and call callback with the value
/// This class is intended to workaround/simplify async/await + actors isolation
/// https://twitter.com/krzyzanowskim/status/1523233140914876416
private class AsyncWaiter<T> {
var didReceiveValue: Bool = false
let value: (T) -> Void
let operation: () async throws -> T
init(_ value: @escaping (T) -> Void, operation: @escaping () async throws -> T) {
self.value = value
import Foundation
class Debounce {
private let queue: DispatchQueue
private let delay: Double
private var workItem: DispatchWorkItem?
private var cancelBlock: (() -> Void)?
init(queue: DispatchQueue, delay: Double) {
self.queue = queue
import Foundation
class Throttle {
private let queue: DispatchQueue
private let delay: Double
private var delayedBlock: (() -> Void)?
private var cancelBlock: (() -> Void)?
private var timer: DispatchSourceTimer?
private var isReady = true
private var hasDelayedBlock: Bool { delayedBlock != nil }
extension JWT {
var email: String? {
return claim(name: "email").string
}
}
@SergeyPetrachkov
SergeyPetrachkov / handleAuthorizationAppleIDButtonPress.swift
Created June 19, 2020 13:53
handleAuthorizationAppleIDButtonPress
@available(iOS 13.0, *)
@objc
private func handleAuthorizationAppleIDButtonPress() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let appleIDCredential as ASAuthorizationAppleIDCredential:
if let identityToken = appleIDCredential.identityToken,
let token = String(data: identityToken, encoding: .utf8),
let decodedToken = try? decode(jwt: token) {
// do your thing here
} else {
// something went wrong, handle it
}
@SergeyPetrachkov
SergeyPetrachkov / ChangeRoot.swift
Created November 29, 2019 12:49
ChangeRoot.swift
extension UIWindow {
/// Fix for http://stackoverflow.com/a/27153956/849645
func set(rootViewController newRootViewController: UIViewController,
withTransition transition: CATransition? = nil) {
let previousViewController = rootViewController
if let transition = transition {
// Add the transition
{
"current_page": 1,
"data": [
{
"type": "draft",
"source": "subscribe",
"trainings_count": 10,
"created_at": "2019-04-23 17:10:42",
"trainings": [
{
@SergeyPetrachkov
SergeyPetrachkov / SwaggerApiResponseHandler.swift
Created April 23, 2019 08:00
Swagger helper with generic requestData
enum ServiceError: Error {
case timeout
case nilData
}
extension SwaggerClientAPI {
private enum HeadersKeys: String {
case authorization = "Authorization"
}
class func setBearer(_ token: String) {
public struct InviteFeedModel: Codable {
public var level: Int?
public var performerId: String
public var performerName: String?
public var newParticipantId: String?
public var newParticipantName: String?
public init(level: Int?, performerId: String, performerName: String?, newParticipantId: String?, newParticipantName: String?) {
self.level = level