Skip to content

Instantly share code, notes, and snippets.

View eecs441staff's full-sized avatar

Sugih Jamin eecs441staff

View GitHub Profile
// UM EECS 441
import SwiftUI
struct Froggy: ViewModifier {
func body(content: Content) -> some View {
content
.font(.custom("Times New Roman", size: 32))
.foregroundColor(.green)
.bold()
@eecs441staff
eecs441staff / SwiftUI-ViewBuilder.swift
Last active March 18, 2024 20:34
ViweBuilder as result builder
// UM EECS 441
import SwiftUI
func Haiku() -> String {
"Furu ike ya kawazu tobikomu mizu no oto"
/*
"Furu ike ya"
"kawazu tobikomu"
"mizu no oto"
@eecs441staff
eecs441staff / SwiftUI-SideEffect.swift
Created March 11, 2024 22:25
SwiftUI side effect with and without onAppear
// UM EECS 441
import SwiftUI
struct ContentView: View {
@State var count = 0
var body: some View {
let _ = print("hello")
@eecs441staff
eecs441staff / SwiftUI-PlayerView.swift
Last active May 1, 2024 00:00
Button re-renders but not the two Text's
// UM EECS 441
import SwiftUI
struct Music {
var title = "Ninth Symphony"
var composer = "Beethoven"
var playlist = "Classical"
}
@eecs441staff
eecs441staff / SwiftUI-SimpleState.swift
Last active April 22, 2024 14:58
SwiftUI @State emulation with SwiftUI memory
// UM EECS 441
import SwiftUI
@propertyWrapper
struct SimpleState<T>: DynamicProperty {
private var _state: State<T> // references SwiftUI memory outside of struct
var wrappedValue: T {
get { _state.wrappedValue } // accesses reference to SwiftUI memory
nonmutating set { _state.wrappedValue = newValue } // no change to reference
@eecs441staff
eecs441staff / SwiftUI-HeapState.swift
Last active April 22, 2024 14:30
SwiftUI @State emulation using heap memory
// UM EECS 441
import SwiftUI
import Observation
@Observable // for subscription
final class HeapStore<T> {
var value: T
init(initValue: T) {
value = initValue
// UM EECS 441
import SwiftUI
@main
struct StateValueApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environment(A(1))
// UM EECS 441
import SwiftUI
import Observation
@Observable
final class RefCounter {
var count = 0
var name = "counter"
}
// UM EECS 441
import SwiftUI
import Observation
@Observable
final class RefCounter {
var count = 0
var name = "counter"
}
// UM EECS 441
import SwiftUI
import Observation
@Observable
final class RefCounter {
var count = 0
@ObservationIgnored var name = "counter"
func inc() { count += 1 }