Created
July 9, 2021 16:45
-
-
Save CodeSlicing/fbd9968a4bf6e9b284495a88d20a21b7 to your computer and use it in GitHub Desktop.
Source code for Quick Tips episode: Power-Up Your Previews
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
// | |
// PowerUpYourPreviews.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 08/07/2021. | |
// Copyright © 2021 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
struct IncrementControl: View { | |
@Binding var counter: Int | |
var min = Int.min | |
var max = Int.max | |
var body: some View { | |
GeometryReader { (geo: GeometryProxy) in | |
HStack { | |
IncrementButton(sfSymbolName: .minus) { | |
if counter > min { | |
counter -= 1 | |
} | |
} | |
.frame(geo.height) | |
Text("\(counter)") | |
.greedyWidth() | |
IncrementButton(sfSymbolName: .plus) { | |
if counter < max { | |
counter += 1 | |
} | |
} | |
.frame(geo.height) | |
} | |
.font(.system(size: geo.heightScaled(0.4))) | |
} | |
} | |
} | |
private let buttonGradient = LinearGradient([.orange, .red], to: .trailing) | |
private struct IncrementButton: View { | |
let sfSymbolName: SFSymbolName | |
let handler: () -> () | |
var body: some View { | |
GeometryReader { (geo: GeometryProxy) in | |
Button { | |
handler() | |
} label: { | |
SFSymbol(sfSymbolName) | |
.greedyFrame() | |
.clipRoundedRectangleWithStroke(geo.widthScaled(0.15), Color.white, | |
lineWidth: geo.widthScaled(0.05), fill: buttonGradient) | |
} | |
} | |
} | |
} | |
struct IncrementControl_Previews: PreviewProvider { | |
struct IncrementControl_Harness: View { | |
@State private var counter = 2 | |
@State private var counterWithMin = 2 | |
@State private var counterWithMax = 2 | |
@State private var counterWithMinMax = 2 | |
var body: some View { | |
VStack(spacing: 20) { | |
Group { | |
IncrementControl(counter: $counter) | |
IncrementControl(counter: $counterWithMin, min: 0) | |
IncrementControl(counter: $counterWithMax, max: 4) | |
IncrementControl(counter: $counterWithMinMax, min: 0, max: 4) | |
} | |
.frame(250, 60) | |
} | |
.greedyFrame() | |
.foregroundColor(.white) | |
.backgroundColor(.white(0.1)) | |
.ignoresSafeArea() | |
} | |
} | |
static var previews: some View { | |
IncrementControl_Harness() | |
.previewDevice(.iPhone_12_Pro_Max) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment