Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeSlicing/afeb9e0a8515618aa353b0cef39dd4cf to your computer and use it in GitHub Desktop.
Save CodeSlicing/afeb9e0a8515618aa353b0cef39dd4cf to your computer and use it in GitHub Desktop.
Native Source code for CodeSlicing episode on Property Wrappers Part 04: Age Validation with a Property Wrapper
//
// EditingUserWithAgeValidationDemoNative.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 04/06/2021.
// Copyright © 2020 Adam Fordyce. All rights reserved.
//
import SwiftUI
private let defaultUsers: [User] = [
User(name: "Anthony", age: 53),
User(name: "Emilio", age: 59),
User(name: "Ally", age: 58),
User(name: "Molly", age: 53),
User(name: "Judd", age: 61),
]
private class AppState: ObservableObject {
@Published var users: [User] = defaultUsers.sorted(by: {$0.name < $1.name})
}
private struct User: Equatable, Identifiable {
var id = UUID()
var name: String
@Clamped(from: 0, to: 100) var age = 0
}
@propertyWrapper
private struct Clamped: Equatable {
private let from: Int
private let to: Int
private var _wrappedValue: Int = 0
init(from: Int = .min, to: Int = .max) {
self.init(wrappedValue: from, from: from, to: to)
}
init(wrappedValue: Int, from: Int = .min, to: Int = .max) {
self.from = from
self.to = to
self._wrappedValue = wrappedValue.clamped(from: from, to: to)
}
var wrappedValue: Int {
get {
_wrappedValue
}
set {
_wrappedValue = newValue.clamped(from: from, to: to)
}
}
}
struct EditingUserWithAgeValidationDemoNative: View {
@StateObject private var appState = AppState()
@State private var showingEditScreen = false
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) -> ()
@State private var errorMessage = ""
init(user: User, saveHandler: @escaping (User) -> ()) {
_user = State(initialValue: user)
self.saveHandler = saveHandler
}
private var isValid: Bool {
errorMessage.isEmpty
}
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))
if !isValid {
Text(errorMessage)
.font(.subheadline)
.foregroundColor(.orange)
.offset(y: -50)
}
}
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()
}
.disabled(!isValid)
.opacity(isValid ? 1 : 0.2)
}
}
}
.padding(50)
.demoPageStyling()
.onChange(of: user) { updatedUser in
validateUser()
}
}
private func validateUser() {
errorMessage = Validators.validateName(name: user.name)
}
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 enum Validators {
static func validateName(name: String) -> String {
if name.isEmpty {
return "Cannot be empty"
} else if name.count < 2 {
return "Must be more than 1 character"
} else if name.containsNumbers {
return "Cannot contain numbers"
} else {
return ""
}
}
}
// MARK: ----- EXTENSIONS
private extension View {
func addSFSymbol(_ sfSymbol: String) -> some View {
Image(systemName: sfSymbol)
.font(.system(size: 35, weight: .light))
}
func demoPageStyling() -> some View {
frame(width: .infinity, height: .infinity)
.foregroundColor(.white)
.background(Color(white: 0.1))
.ignoresSafeArea()
}
}
private extension Int {
func clamped(from: Int, to: Int) -> Int {
Swift.max(from, Swift.min(to, self))
}
}
private extension String {
var containsNumbers: Bool {
return rangeOfCharacter(from: .decimalDigits) != nil
}
}
struct EditingUserWithAgeValidationDemoNative_Previews: PreviewProvider {
struct EditingUserWithAgeValidationDemoNative_Harness: View {
var body: some View {
EditingUserWithAgeValidationDemoNative()
.demoPageStyling()
.environment(\.colorScheme, .dark)
}
}
static var previews: some View {
EditingUserWithAgeValidationDemoNative_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