Skip to content

Instantly share code, notes, and snippets.

@daniloc
daniloc / EntityIterator.swift
Last active May 7, 2021 20:54
EntityIterator: Wrapper around SwiftUI's ForEach to iterate through Core Data entities according to a predicate and sort descriptors you can change at runtime
//adapted via: https://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui
import SwiftUI
import CoreData
struct EntityIterator<Entity: NSManagedObject, Content: View>: View {
var fetchRequest: FetchRequest<Entity>
var results: FetchedResults<Entity> { fetchRequest.wrappedValue }
@daniloc
daniloc / SliderView.swift
Created May 3, 2020 01:21
A quick study of making weird, custom UI components with SwiftUI
import SwiftUI
struct SliderView: View {
@State var height: CGFloat = .infinity
@State var sliderOpacity: Double = 0
let action: (() -> Void)
var slide: some Gesture {
@daniloc
daniloc / SimpleColorPicker.swift
Last active April 21, 2023 09:48
A simple color picker with enum for easy storage
struct ColorPicker: View {
enum Option: Int, CaseIterable {
case none,
red,
orange,
yellow,
green,
teal,
blue,
@daniloc
daniloc / DecimalTextField.swift
Last active April 10, 2023 17:01
SwiftUI TextField for decimal input
struct DecimalTextField: View {
@Binding var decimalValue: Decimal
@State var clearValue: Decimal = 1
var clearValueString: String {
return String("\(clearValue)")
}
@State private var lastValidInput: String?
@daniloc
daniloc / MarkdownFileV1.swift
Created March 5, 2020 15:12
Basic example of using string literals to load bundle resources
struct MarkdownFile: ExpressibleByStringLiteral {
let bundleName: String
let rawMarkdown: String?
init(stringLiteral: String) {
bundleName = stringLiteral
var loadedMarkdown: String? = nil
@daniloc
daniloc / WebViewCSSExtension.swift
Created January 23, 2020 15:30
Provide your own CSS to a WKWebView instance, even if it's loaded from a nib or storyboard.
extension WKWebView {
//via https://stackoverflow.com/a/53706678
//and https://stackoverflow.com/a/53141055
func applyStylesheet() {
guard
let path = Bundle.main.path(forResource: "style", ofType: "css"), //load style.css from your app bundle
let cssString = try? String(contentsOfFile: path).components(separatedBy: .newlines).joined()
else {
@daniloc
daniloc / warp-core.ino
Last active November 24, 2019 01:02
Using a shift register to pulse LEDs for a model warp core
/************************************************************************************************************************************
Fade in LED's one by one using ShiftPWM with one shift register
************************************************************************************************************************************/
// You can choose the latch pin yourself.
const int ShiftPWM_latchPin=8;
#define SHIFTPWM_NOSPI
const int ShiftPWM_dataPin = 11;
const int ShiftPWM_clockPin = 12;
@IBAction func handlePuppySwitch(_ sender: UISwitch) {
let updateIndexPaths = [IndexPath(row: 0, section: TableSections.dynamicSection.rawValue)]
if (sender.isOn) {
puppyLabel.text = "Puppy Enabled"
dynamicContent.insert("Puppy is on!", at: 0)
tableView.insertRows(at: updateIndexPaths, with: .bottom)
} else {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == TableSections.dynamicSection.rawValue) {
return dynamicContent.count
} else {
return staticCells.count
}
}