Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active August 3, 2021 23:14
Show Gist options
  • Save CodeSlicing/ab87b0ced127d50e555a3df1c6bbe80b to your computer and use it in GitHub Desktop.
Save CodeSlicing/ab87b0ced127d50e555a3df1c6bbe80b to your computer and use it in GitHub Desktop.
Native source code for Quick Tips episode: Power-Up Your Previews
//
// PowerUpYourPreviewsNative.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
struct IncrementControlNative: View {
var min = Int.min
var max = Int.max
@Binding var counter: Int
var body: some View {
GeometryReader { (geo: GeometryProxy) in
let buttonSize = geo.size.height
HStack {
IncrementButton(sfSymbolName: "minus") {
if counter > min {
counter -= 1
}
}
.frame(width: buttonSize, height: buttonSize)
Text("\(counter)")
.frame(maxWidth: .infinity, maxHeight: .infinity)
IncrementButton(sfSymbolName: "plus") {
if counter < max {
counter += 1
}
}
.frame(width: buttonSize, height: buttonSize)
}
.font(.system(size: geo.size.height * 0.4))
}
}
}
private let buttonGradient = LinearGradient(
gradient: Gradient(colors: [.orange, .red]),
startPoint: .leading, endPoint: .trailing)
private struct IncrementButton: View {
let sfSymbolName: String
let handler: () -> ()
var body: some View {
GeometryReader { (geo: GeometryProxy) in
let cornerRadius = geo.size.width * 0.15
let lineWidth = geo.size.width * 0.05
Button {
handler()
} label: {
Image(systemName: sfSymbolName)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(buttonGradient)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
.overlay(RoundedRectangle(cornerRadius: cornerRadius).stroke(Color.white, lineWidth: lineWidth))
}
}
}
}
struct IncrementControlNative_Previews: PreviewProvider {
struct IncrementControlNative_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 {
IncrementControlNative(counter: $counter)
IncrementControlNative(min: 0, counter: $counterWithMin)
IncrementControlNative(max: 5, counter: $counterWithMax)
IncrementControlNative(min: 0, max: 5, counter: $counterWithMinMax)
}
.frame(width: 250, height: 60)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.foregroundColor(.white)
.background(Color(white: 0.1))
.ignoresSafeArea()
}
}
static var previews: some View {
IncrementControlNative_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