Skip to content

Instantly share code, notes, and snippets.

View ericlewis's full-sized avatar

Eric Lewis ericlewis

View GitHub Profile
@ericlewis
ericlewis / StorageProviders.swift
Last active February 17, 2022 16:31
All the speed of State with all the benefits of App/SceneStorage!
import SwiftUI
// MARK: Demo
struct ContentView: View {
@State
private var isActive = false
@SceneStorage("OtherKey")
private var isActiveStock = false
@ericlewis
ericlewis / Toggle+Label.swift
Created February 16, 2022 15:19
An initializer overload which also pushes an environment value on toggle status to the toggle's label.
import SwiftUI
struct IsToggledOnKey: EnvironmentKey {
static let defaultValue: Bool = false
}
extension EnvironmentValues {
fileprivate var _isToggledOn: Bool {
get { self[IsToggledOnKey.self] }
set { self[IsToggledOnKey.self] = newValue }
@ericlewis
ericlewis / Components+Label.swift
Created February 16, 2022 14:49
Initializer overloads to make working with labels easier.
import SwiftUI
extension Toggle where Label == SwiftUI.Label<Text, Image> {
public init(_ titleKey: LocalizedStringKey, systemImage named: String, isOn: Binding<Bool>) {
self.init(isOn: isOn, label: { Label(titleKey, systemImage: named) })
}
public init(_ titleKey: LocalizedStringKey, image named: String, isOn: Binding<Bool>) {
self.init(isOn: isOn, label: { Label(titleKey, image: named) })
}
debug server graph://127.0.0.1:51000/?token=1706701259
== UInt32, 4 bytes ==
(layout #:length 18
(== #:size 4 #:type UInt32))
== EnvironmentValues, 16 bytes ==
(layout #:length 23
(enum #:size 8 #:type Optional<Element>
(case 0
(read 8)))
(enum #:size 8 #:type Optional<Tracker>
@ericlewis
ericlewis / Example.swift
Last active February 11, 2022 00:13
Customize the separator insets on lists in SwiftUI. Only tested on iOS 15. Does not work on SidebarListStyles or interface idioms that aren't pad / phone.
struct ContentView: View {
var body: some View {
List {
ForEach(0..<100) { index in
Text("Index: \(index)")
.listRowSeparatorInsets(
.init(
top: 0,
left: CGFloat(index) * 5,
@ericlewis
ericlewis / Example.swift
Last active February 8, 2022 16:10
PagingView by working with SwiftUI internals. Useful for building "flat" view containers, like a paging view! The paging view itself uses yet another internal type. But serves as a good example for how to build a useful view.
import SwiftUI
struct ContentView: View {
@State
private var show = false
@State
private var selection = 0
var body: some View {
@ericlewis
ericlewis / ContentView.swift
Created February 3, 2022 16:33
Using enums as Views. Just copy and paste into an App Playground.
import SwiftUI
struct Application: Identifiable {
enum Icon {
case url(URL)
case systemImage(String)
}
let id: UUID = .init()
let name: String
// Works.
extension VW {
public static func buildBlock<Content: DynamicViewContent>(_ content: Content) -> some View where Content.Data: RandomAccessCollection, Content.Data.Element: Identifiable {
ViewBuilder.buildBlock(VStack { ForEach(content.data) { _ in Text("Test") } })
}
}
// Crashes
extension VW {
public static func buildBlock<Content: DynamicViewContent>(_ content: Content) -> some View where Content.Data: RandomAccessCollection, Content.Data.Element: Identifiable {
@ericlewis
ericlewis / index.html
Created April 9, 2021 02:54
softwarejameson.dance - feel free to make this responsive and better overall
<!DOCTYPE html>
<html>
<style type="text/css">
body {
width: 100%;
height: 100%;
position: absolute;
background-color: black;
top: 0%;
left: 0%;
@ericlewis
ericlewis / PlaidView.swift
Last active December 3, 2021 18:41
LinkKit V2 for SwiftUI
import LinkKit
import SwiftUI
struct PlaidViewPresenter: UIViewControllerRepresentable {
@Binding var isPresented: Bool
var configuration: LinkPublicKeyConfiguration
var exitCallback: ((LinkExit) -> Void)?
var successCallback: ((LinkSuccess) -> Void)?