Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Created May 31, 2021 07:39
Show Gist options
  • Save CodeSlicing/223bb66b1286708ef6e06acb86972da8 to your computer and use it in GitHub Desktop.
Save CodeSlicing/223bb66b1286708ef6e06acb86972da8 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on Property Wrappers Part 03: Edit-Views and Bindings
//
// EditingUserDemo.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Created by Adam Fordyce on 31/05/2021.
// Copyright © 2020 Adam Fordyce. All rights reserved.
//
import SwiftUI
import PureSwiftUI
private let defaultUsers: [User] = [
User(name: "Judd", age: 61),
User(name: "Ally", age: 58),
User(name: "Emilio", age: 59),
User(name: "Anthony", age: 53),
User(name: "Molly", age: 53),
]
private class AppState: ObservableObject {
@Published var users: [User] = defaultUsers
}
private struct User: Equatable, Identifiable {
var id = UUID()
var name: String
var age: Int
}
struct EditingUserDemoEdit: View {
@StateObject private var appState = AppState()
var body: some View {
NavigationView {
List(0..<appState.users.count) { index in
NavigationLink(destination: EditUser(user: appState.users[index])) {
let user = appState.users[index]
TitleText("\(user.name): \(user.age)")
}
}
.navigationTitle("Breakfast Rota")
}
}
}
private struct EditUser: View {
@Environment(\.presentationMode) var presentationMode
@State var user: User
init(user: User) {
_user = State(initialValue: user)
}
var body: some View {
ZStack {
VStack(spacing: 50) {
ZStack {
TextField("Name", text: $user.name)
.font(.title)
.hPadding(8)
.padding()
.clipCapsuleWithStroke(Color.white, lineWidth: 2)
}
HStack(spacing: 20) {
CounterButton(sfSymbol: .chevron_left_2) {
user.age -= 10
}
CounterButton(sfSymbol: .chevron_left) {
user.age -= 1
}
TitleText("\(user.age)")
.width(75)
.padding()
.clipCapsuleWithStroke(Color.white, lineWidth: 2)
CounterButton(sfSymbol: .chevron_right) {
user.age += 1
}
CounterButton(sfSymbol: .chevron_right_2) {
user.age += 10
}
}
HStack {
ActionButton(sfSymbol: .xmark, color: Color.red.opacity(0.5)) {
dismiss()
}
Spacer()
ActionButton(sfSymbol: .checkmark, color: Color.green.opacity(0.5)) {
dismiss()
}
}
}
}
.padding(50)
.demoPageStyling()
}
private func dismiss() {
presentationMode.wrappedValue.dismiss()
}
}
private struct CounterButton: View {
let sfSymbol: SFSymbolName
let callback: () -> ()
var body: some View {
Button {
callback()
} label: {
addSFSymbol(sfSymbol)
}
}
}
private struct ActionButton: View {
let sfSymbol: SFSymbolName
let color: Color
let callback: () -> ()
var body: some View {
Button {
callback()
} label: {
addSFSymbol(sfSymbol)
.frame(120, 75)
.clipCapsuleWithStroke(Color.white, lineWidth: 2, fill: color)
}
}
}
private extension View {
func addSFSymbol(_ sfSymbol: SFSymbolName) -> some View {
SFSymbol(sfSymbol)
.font(.system(size: 35, weight: .light))
}
func demoPageStyling() -> some View {
greedyFrame()
.foregroundColor(.white)
.backgroundColor(.white(0.1))
.ignoresSafeArea()
}
}
struct EditingUserDemoEdit_Previews: PreviewProvider {
struct EditingUserDemoEdit_Harness: View {
var body: some View {
EditingUserDemoEdit()
.demoPageStyling()
.envDarkMode()
}
}
static var previews: some View {
EditingUserDemoEdit_Harness()
.previewDevice(.iPhone_12_Pro_Max)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment