Skip to content

Instantly share code, notes, and snippets.

View StanislavK's full-sized avatar
🚜
Working

StanislavK StanislavK

🚜
Working
View GitHub Profile
import SwiftUI
struct GenericUIViewControllerRepresentable<ViewControllerType>: UIViewControllerRepresentable
where ViewControllerType: UIViewController {
let factory: (Context) -> ViewControllerType
func makeUIViewController(context: Context) -> ViewControllerType {
factory(context)
}
// Bundle-Decodable.swift
// Usage (example): let users = Bundle.main.decode([User].self, from: "users.json")
import Foundation
extension Bundle {
func decode<T: Decodable>(_ type: T.Type, from file: String, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
@StanislavK
StanislavK / Content.swift
Created December 30, 2021 17:42
Single line list row separator (edge to edge).
import SwiftUI
struct Row: View {
let text: String
let showDivider: Bool
var body: some View {
VStack(alignment: .leading) {
Text(text)
.padding(showDivider ? [.leading, .top] : [.leading, .top, .bottom])
struct GenericUIViewRepresentable<ViewType>: UIViewRepresentable where ViewType: UIView {
let factory: (Context) -> ViewType
func makeUIView(context: Context) -> ViewType {
factory(context)
}
func updateUIView(_ uiView: ViewType, context: Context) {}
}
@StanislavK
StanislavK / .xcodesamplecode.plist
Last active June 21, 2020 08:11
Fix rendering markdown in Xcode
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>
extension Data {
func decoded<T: Decodable>(
as type: T.Type = T.self,
handledOn resultQueue: DispatchQueue = .main,
handler: @escaping (Result<T, Error>) -> Void
) {
let queue = DispatchQueue(label: "com.myapp.decoding")
let decoder = JSONDecoder()
queue.async {
extension Encodable {
func encode(with encoder: JSONEncoder = JSONEncoder()) throws -> Data {
return try encoder.encode(self)
}
}
extension Decodable {
static func decode(with decoder: JSONDecoder = JSONDecoder(), from data: Data) throws -> Self {
return try decoder.decode(Self.self, from: data)
}
#Use this when you need to have working playground and project: see more at https://www.twilio.com/blog/intro-to-markov-models-with-swift
extension UIFont {
class func printAllFonts() {
UIFont.familyNames().forEach { family in
UIFont.fontNamesForFamilyName(family).forEach { font in
print(font)
}
}
}
// Simple check if connected to internet
func isConnected() -> Bool {
// we need SCNetworkReachability object
guard let reachability = SCNetworkReachabilityCreateWithName(nil, "https://inloop-contacts.appspot.com/_ah/api/contactendpoint/v1/contact") else { return false }
// and flags
var flags = SCNetworkReachabilityFlags()
// read the "flags" into flags (via SCNetworkReachabilityGetFlags)
SCNetworkReachabilityGetFlags(reachability, &flags)