Created
March 27, 2021 08:13
-
-
Save CodeSlicing/a7a95f67d87a1de1a6e48d483229c236 to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode on demystifying property wrappers part 1
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
// | |
// StateAndBindingDemo.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 27/03/2021. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
struct StateAndBindingDemo: View { | |
private let _counter = State(initialValue: 0) | |
private var counter: Int { | |
_counter.wrappedValue | |
} | |
private var _$counter: Binding<Int> { | |
_counter.projectedValue | |
} | |
var body: some View { | |
VStack(spacing: 50) { | |
TitleText("Current count: \(counter)") | |
CounterUpdatingButton(counter: _$counter) | |
} | |
} | |
} | |
private struct CounterUpdatingButton: View { | |
let counter: Binding<Int> | |
var body: some View { | |
VStack { | |
Button { | |
counter.wrappedValue += 1 | |
} label: { | |
TitleText("Increment") | |
.shadow(2) | |
.padding() | |
.clipCapsuleWithStroke(Color.white, lineWidth: 2, | |
fill: LinearGradient([.red, .orange], to: .bottomTrailing)) | |
} | |
} | |
} | |
} | |
struct BindingDemo_Previews: PreviewProvider { | |
struct BindingDemo_Harness: View { | |
var body: some View { | |
StateAndBindingDemo() | |
.greedyFrame() | |
.foregroundColor(.white) | |
.backgroundColor(.white(0.1)) | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
BindingDemo_Harness() | |
.previewDevice(.iPhone_8) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment