Skip to content

Instantly share code, notes, and snippets.

View damodarnamala's full-sized avatar

Damodar damodarnamala

View GitHub Profile
@damodarnamala
damodarnamala / FormattedTextField.swift
Created September 16, 2022 03:51 — forked from darrarski/FormattedTextField.swift
SwiftUI FormattedTextField - TextField with custom display/edit formatters
import SwiftUI
public struct FormattedTextField<Formatter: TextFieldFormatter>: View {
public init(_ title: String,
value: Binding<Formatter.Value>,
formatter: Formatter) {
self.title = title
self.value = value
self.formatter = formatter
}
@damodarnamala
damodarnamala / Makefile
Created September 16, 2022 03:52 — forked from oleander/Makefile
How to use the Swift Package Manager and CocoaPods within the same XCode application
APP="MyApp"
CONSTRUCT=xcodebuild -workspace $(APP).xcworkspace -scheme $(APP) clean
install_deps:
pod install
create_config:
swift package fetch
swift package generate-xcodeproj
wipe:
rm -rf .build $(APP).xcodeproj $(APP).xcworkspace Package.pins Pods Podfile.lock
@damodarnamala
damodarnamala / Channel.swift
Created September 16, 2022 03:53 — forked from gokselkoksal/Channel.swift
Channel implementation
public class Channel<Value> {
private class Subscription {
weak var object: AnyObject?
private let notifyBlock: (Value) -> Void
private let queue: DispatchQueue
var isValid: Bool {
return object != nil
import XCTest
struct Scenario: ScenarioGivenContinuation, ScenarioWhenContinuation {
init(_ description: String) {
print("Scenario: \(description)")
}
struct Given: ScenarioWhenContinuation {
fileprivate init(description: String, setup: () throws -> Void) rethrows {
@damodarnamala
damodarnamala / NotesApp.swift
Created September 16, 2022 04:06 — forked from jnewc/NotesApp.swift
A notes app written in >100 lines of swift using SwiftUI
import SwiftUI
let dateFormatter = DateFormatter()
struct NoteItem: Codable, Hashable, Identifiable {
let id: Int
let text: String
var date = Date()
var dateText: String {
dateFormatter.dateFormat = "MMM d yyyy, h:mm a"
@damodarnamala
damodarnamala / AVPlayer+Scrubbing.swift
Created September 16, 2022 04:24 — forked from shaps80/AVPlayer+Scrubbing.swift
Enables smooth frame-by-frame scrubbing (in both directions) – similar to Apple's applications.
public enum Direction {
case forward
case backward
}
internal var player: AVPlayer?
private var isSeekInProgress = false
private var chaseTime = kCMTimeZero
private var preferredFrameRate: Float = 23.98
@damodarnamala
damodarnamala / Store.swift
Created September 16, 2022 04:24 — forked from shaps80/Store.swift
Atomic RangeReplaceableCollection Storage in Swift
import Foundation
public final class Store<Element>: RangeReplaceableCollection {
public typealias Index = Int
public typealias SubSequence = Store<Element>
public var startIndex: Index { queue.sync { storage.startIndex } }
public var endIndex: Index { queue.sync { storage.endIndex } }
public func index(after i: Index) -> Index { queue.sync { storage.index(after: i) } }
@damodarnamala
damodarnamala / Flipped.swift
Created September 16, 2022 04:25 — forked from shaps80/Flipped.swift
Provides a CGAffineTransform suitable for flipping the Y Axis.
private func flipped(size: CGSize) -> CGAffineTransform {
let mirror = CGAffineTransform(scaleX: 1, y: -1)
let translate = CGAffineTransform(translationX: 0, y: size.height)
return mirror.concatenating(translate)
}
extension Encodable {
var debugJson: String {
(try? JSONEncoder().encode(self).debugJson) ?? ""
}
}
extension Data {
var debugJson: String {
String(decoding: self, as: UTF8.self)
}
@damodarnamala
damodarnamala / CardDecorating.swift
Created September 16, 2022 04:27 — forked from shaps80/CardDecorating.swift
A simple protocol for creating a 'Card' style component in Swift.
import UIKit
/// Decorates a view as a `Card`. Requires that the view is embedded in some `contentView`
public protocol CardDecorating {
/// View that will be styled as a card. Should hold the `contentView`.
var cardView: UIView { get }
/// Holds the main content. Subviews should be added to this view.
var contentView: UIView { get }
}