Skip to content

Instantly share code, notes, and snippets.

View christopherkarani's full-sized avatar

Christopher Karani christopherkarani

View GitHub Profile
@christopherkarani
christopherkarani / ConcurrencyFetcher.swift
Created November 8, 2021 22:36 — forked from stinger/ConcurrencyFetcher.swift
Swift Concurrency fetcher
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String), parserError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
case .apiError(let reason), .parserError(let reason):
return reason
}
extension NSMutableAttributedString {
var fontSize:CGFloat { return 14 }
var boldFont:UIFont { return UIFont(name: "AvenirNext-Bold", size: fontSize) ?? UIFont.boldSystemFont(ofSize: fontSize) }
var normalFont:UIFont { return UIFont(name: "AvenirNext-Regular", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)}
func bold(_ value:String) -> NSMutableAttributedString {
let attributes:[NSAttributedString.Key : Any] = [
.font : boldFont
]
var millisecondsSince1970: Int {
return Int(self.timeIntervalSince1970.rounded())
}
init(milliseconds: Int) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
}
extension UIView {
func height(constant: CGFloat) {
setConstraint(value: constant, attribute: .height)
}
func width(constant: CGFloat) {
setConstraint(value: constant, attribute: .width)
}
private func removeConstraint(attribute: NSLayoutConstraint.Attribute) {
/// A little pieve of genius by me that culculates cell size base on the image given while maintaining aspect ratio
open class MediaMessageSizeCalculator {
open func messageContainerSize(for image: UIImage) -> CGSize {
let maxWidth = UIScreen.main.bounds.width - 16
let sizeForMediaItem = { (maxWidth: CGFloat, item: UIImage) -> CGSize in
if maxWidth < item.size.width {
// Maintain the ratio if width is too great
let height = maxWidth * item.size.height / item.size.width
return CGSize(width: maxWidth, height: height)
}
protocol Dimension {
func constraint(equalToConstant c: CGFloat) -> NSLayoutConstraint
}
protocol Anchor {
func constraint(equalTo anchor: Self, constant: CGFloat) -> NSLayoutConstraint
func constraint(greaterThanOrEqualTo anchor: Self, constant: CGFloat) -> NSLayoutConstraint
func constraint(lessThanOrEqualTo anchor: Self, constant: CGFloat) -> NSLayoutConstraint
}
if !UIAccessibilityIsReduceTransparencyEnabled() {
view.backgroundColor = .clear
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
class ArrayImpl<T> {
var space: Int
var count: Int
var ptr: UnsafeMutablePointer<T>
init(count: Int = 0, ptr: UnsafeMutablePointer<T> = nil) {
self.count = count
self.space = count
@christopherkarani
christopherkarani / countries.swift
Created February 1, 2018 09:11 — forked from maxgiraldo/countries.swift
All country codes including full name
let countries = [
"AF - Afghanistan",
"AX - Aland Islands",
"AL - Albania",
"DZ - Algeria",
"AS - American Samoa",
"AD - Andorra",
"AO - Angola",
"AI - Anguilla",
"AQ - Antarctica",
@christopherkarani
christopherkarani / us-states-array
Created January 31, 2018 11:27 — forked from iamjason/us-states-array
Swift US States Array
let state = [ "AK - Alaska",
"AL - Alabama",
"AR - Arkansas",
"AS - American Samoa",
"AZ - Arizona",
"CA - California",
"CO - Colorado",
"CT - Connecticut",
"DC - District of Columbia",
"DE - Delaware",