Skip to content

Instantly share code, notes, and snippets.

View buh's full-sized avatar
📦
Swift

Alexey Bukhtin buh

📦
Swift
View GitHub Profile
@buh
buh / UIKitAppKitPreview.swift
Last active November 15, 2022 14:05
Helpers to quickly create SwiftUI previews for the UIKit/AppKit view and view controller
///
/// Helpers to quickly create SwiftUI previews for the UIKit/AppKit view and view controller.
///
/// For example (UIKit):
/// ```
/// @available(iOS 14.0, *) // This is only needed for .ignoresSafeArea()
/// struct YourViewController_Previews: PreviewProvider {
/// static var previews: some View {
/// UIViewControllerRepresentableProvider { _ in
/// YourViewController()
@buh
buh / APIClient.swift
Last active October 16, 2022 16:28
A simple networking API client
import Foundation
/// A client protocol for sending API requests.
protocol APIClientProtocol: AnyObject {
/// Sends an API request.
/// - Parameters:
/// - request: the API request.
/// - completion: a completion with the request result.
func send<T: Decodable>(_ request: APIRequest, _ completion: @escaping (Result<T, APIError>) -> Void)
}
@buh
buh / ContentView.swift
Created June 19, 2022 22:06 — forked from wtsnz/ContentView.swift
Playing around with hosting SwiftUI Views in an NSWindow from inside a View 🙃 (also works for the NSStatusBar item!)
import SwiftUI
struct ContentView: View {
@State var now = Date()
@State var showWindow = false
@State var text: String = ""
let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
@buh
buh / AuthenticationStateExample.swift
Last active December 14, 2023 09:42
Example for a Finite-State Machine
/// The authentication state.
enum AuthenticationState: StateType {
// A list of events.
enum Event {
case userSignIn(email: String, password: String)
case accessTokenReceived(AccessToken)
case userReceived(User)
case userSignedOut
}
@buh
buh / ReadFrame.swift
Last active April 7, 2024 12:05
SwiftUI view extension to read the frame and bind it to the reader.
//
// Created by Alexey Bukhtin on 26/03/2021.
//
import SwiftUI
extension View {
/// Reads the view frame and bind it to the reader.
/// - Parameters:
@interface NSData (AES)
- (instancetype)aesEncryptWithKey:(NSData *)key iv:(NSData *)iv;
- (instancetype)aesDecryptWithKey:(NSData *)key iv:(NSData *)iv;
@end
@buh
buh / NSObject+CSProperties.h
Last active August 29, 2015 13:57
Helper for JSON encode/decode custom class properties
//
// NSObject+CSProperties.h
// Cheapshot
//
// Created by Alexey Bukhtin on 17.02.14.
// Copyright (c) 2014 Cheapshot. All rights reserved.
//
@interface NSObject (CSProperties)
@buh
buh / NSManagedObject+ManagedObject.m
Created March 26, 2014 11:43
Core Data Fetch Async
@implementation NSManagedObject (ManagedObject)
+ (void)fetchAsyncToMainContextWithRequest:(void (^)(NSFetchRequest *request))block
completion:(void (^)(NSArray *objects))completion
{
if (!completion) {
return;
}
NSManagedObjectContext *mainContext = [NSManagedObjectContext mainContext];