Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:44
Show Gist options
  • Save CodeSlicing/aa10b72636745e1bb1654a86094d87d5 to your computer and use it in GitHub Desktop.
Save CodeSlicing/aa10b72636745e1bb1654a86094d87d5 to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing episode on debouncing user input with Combine
//
// DebounceStateDemoNative.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.
//
// Copyright © 2022 Adam Fordyce. All rights reserved.
//
import SwiftUI
import Combine
private class DebouncedState<Value>: ObservableObject {
@Published var currentValue: Value
@Published var debouncedValue: Value
private var subscriber: AnyCancellable?
init(initialValue: Value, delay: Double = 0.3) {
_currentValue = Published(initialValue: initialValue)
_debouncedValue = Published(initialValue: initialValue)
$currentValue
.debounce(for: .seconds(delay), scheduler: RunLoop.main)
.assign(to: &$debouncedValue)
}
}
struct DebounceStateDemoNative: View {
@StateObject private var filterText = DebouncedState(initialValue: "", delay: 0.3)
@State private var counter = 0
var body: some View {
VStack(alignment: .leading) {
Text("Input text:")
TextEditor(text: $filterText.currentValue)
.textEditorStyle()
Text("Debounced text:")
.padding(.top, 15)
Text(filterText.debouncedValue)
.textOutputStyle()
ZStack {
Text("\(counter)")
.font(.system(size: 50, weight: .light))
}
.frame(width: 100, height: 100)
.background(Circle().fill(.white))
.clipShape(Circle())
.contentShape(Circle())
.overlay(Circle().stroke(.gray, lineWidth: 1))
.offset(y: 100)
.frame(maxWidth: .infinity)
}
.padding()
.onChange(of: filterText.debouncedValue) { newValue in
counter += 1
}
}
}
private extension View {
func textEditorStyle() -> some View {
frame(height: 100)
.textBoxBorder()
}
func textOutputStyle() -> some View {
padding(5)
.frame(height: 100, alignment: .top)
.frame(maxWidth: .infinity, alignment: .leading)
.background(.white)
.textBoxBorder()
}
func textBoxBorder() -> some View {
clipShape(RoundedRectangle(cornerRadius: 5))
.contentShape(RoundedRectangle(cornerRadius: 5))
.overlay(RoundedRectangle(cornerRadius: 5).stroke(.gray, lineWidth: 1))
}
}
struct DebounceStateDemoNative_Previews: PreviewProvider {
struct DebounceStateDemoNative_Harness: View {
var body: some View {
DebounceStateDemoNative()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(LinearGradient(gradient: Gradient(colors: [Color(white: 0.8), Color(white: 0.5)]), startPoint: .topLeading, endPoint: .bottomTrailing))
.ignoresSafeArea()
}
}
static var previews: some View {
DebounceStateDemoNative_Harness()
.previewDevice("iPhone 13 Pro Max")
.previewDisplayName("iPhone 13 Pro Max")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment