Created
May 31, 2021 07:39
-
-
Save CodeSlicing/5ebcfb4bf4f753141a9b53c13257aba7 to your computer and use it in GitHub Desktop.
Native Source code for CodeSlicing episode on Property Wrappers Part 03: Edit-Views and Bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EditingUserDemoNative.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 | |
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 EditingUserDemoNative: 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]) { user in | |
appState.users[index] = user | |
}) { | |
let user = appState.users[index] | |
Text("\(user.name): \(user.age)") | |
.font(.title) | |
} | |
} | |
.navigationTitle("Breakfast Rota") | |
} | |
} | |
} | |
private struct EditUser: View { | |
@Environment(\.presentationMode) var presentationMode | |
@State var user: User | |
let saveHandler: (User) -> () | |
init(user: User, saveHandler: @escaping (User) -> ()) { | |
_user = State(initialValue: user) | |
self.saveHandler = saveHandler | |
} | |
var body: some View { | |
ZStack { | |
VStack(spacing: 50) { | |
ZStack { | |
TextField("Name", text: $user.name) | |
.font(.title) | |
.padding(.horizontal, 8) | |
.padding() | |
.clipShape(Capsule()) | |
.overlay(Capsule().stroke(Color.white, lineWidth: 2)) | |
} | |
HStack(spacing: 20) { | |
CounterButton(sfSymbol: "chevron.left.2") { | |
user.age -= 10 | |
} | |
CounterButton(sfSymbol: "chevron.left") { | |
user.age -= 1 | |
} | |
Text("\(user.age)") | |
.font(.title) | |
.frame(width: 75) | |
.padding() | |
.clipShape(Capsule()) | |
.overlay(Capsule().stroke(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)) { | |
saveHandler(user) | |
dismiss() | |
} | |
} | |
} | |
} | |
.padding(50) | |
.demoPageStyling() | |
} | |
private func dismiss() { | |
presentationMode.wrappedValue.dismiss() | |
} | |
} | |
private struct CounterButton: View { | |
let sfSymbol: String | |
let handler: () -> () | |
var body: some View { | |
Button { | |
handler() | |
} label: { | |
addSFSymbol(sfSymbol) | |
} | |
} | |
} | |
private struct ActionButton: View { | |
let sfSymbol: String | |
let color: Color | |
let handler: () -> () | |
var body: some View { | |
Button { | |
handler() | |
} label: { | |
addSFSymbol(sfSymbol) | |
.frame(width: 120, height: 75) | |
.clipShape(Capsule()) | |
.background(Capsule().fill(color)) | |
.overlay(Capsule().stroke(Color.white, lineWidth: 2)) | |
} | |
} | |
} | |
private extension View { | |
func addSFSymbol(_ sfSymbol: String) -> some View { | |
Image(systemName: sfSymbol) | |
.font(.system(size: 35, weight: .light)) | |
} | |
func demoPageStyling() -> some View { | |
frame(maxWidth: .infinity, maxHeight: .infinity) | |
.foregroundColor(.white) | |
.background(Color(white: 0.1)) | |
.ignoresSafeArea() | |
} | |
} | |
struct EditingUserDemoNative_Previews: PreviewProvider { | |
struct EditingUserDemoNative_Harness: View { | |
var body: some View { | |
EditingUserDemoNative() | |
.demoPageStyling() | |
.environment(\.colorScheme, .dark) | |
} | |
} | |
static var previews: some View { | |
EditingUserDemoNative_Harness() | |
.previewDevice("iPhone 12 Pro Max") | |
.previewDisplayName("iPhone 12 Pro Max") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment