Giter Site home page Giter Site logo

Comments (10)

someoneAnyone avatar someoneAnyone commented on May 27, 2024 1

Would something like this swiftui project help?

from swiftui-notes.

heckj avatar heckj commented on May 27, 2024

I'll definitely show using a textfield getting updates within the patterns - both with SwiftUI and with UIKit. I might pass on AppKit for starters though, since I'm not yet running the beta on the Mac

from swiftui-notes.

dirk68-fu avatar dirk68-fu commented on May 27, 2024

Perhaps my question was not clear. I was asking about using the changes of a Textfield as source for a pipeline, like shown in the WWDC video.

from swiftui-notes.

heckj avatar heckj commented on May 27, 2024

I think I understand - The current proposed method based on what I can find in apple's docs is leveraging the NSNotificationCenter for all UIKit updates, and then using that as the publisher to which you can react. Casey Liss even has some sample bits out there (he's not a fan of using NSNoticationCenter in this way, and I can't say I blame him here- https://www.caseyliss.com/2019/6/18/quick-notes-on-yesterdays-post) , as does @AvdLee in his playground at https://github.com/AvdLee/CombineSwiftPlayground. There's also something of an example at https://github.com/tailec/CombineExamples if you want to dig around a bit in that code.

I need to sit down and make it work for myself in my own samples, and I intend to include something like this, as its clearly a pattern folks would like to use.

from swiftui-notes.

dirk68-fu avatar dirk68-fu commented on May 27, 2024

Very interesting links. Thank you for sharing them. Though they all use UIKit. I wanted to know the easiest way to get a publisher from a SwiftUI TextField.

This should be as easy as calling a method like textfield.publisher(...) but it isn't. Or do I miss something.

from swiftui-notes.

heckj avatar heckj commented on May 27, 2024

I haven't looked at depth into SwiftUI to really have a solid answer yet, but in perusing the documentation there's a closure that you can set up (at initialization) to be called when the contents are edited (https://developer.apple.com/documentation/swiftui/textfield/3338359-init), and there may also be a direct way to look for the property being modified, such as @published property wrapper - but I'm getting quite a bit out ahead of anything I've tried to really know as yet.

SwiftUI includes a number of publishers built into their views, but I'm not clear on what they are or their uses as yet.

from swiftui-notes.

heckj avatar heckj commented on May 27, 2024

It also appears that initializing TextFields can be bound to a model, so it may be more sensible (depending on how you're using it), to get a publisher from the model - I suspect it depends entirely on where the source of truth is for the TextField - such as @State or @ObjectBinding

from swiftui-notes.

dirk68-fu avatar dirk68-fu commented on May 27, 2024

Thanks, that seems to contain the necessary code. I still have to remove the URL specific features though to extract the essence.

from swiftui-notes.

dirk68-fu avatar dirk68-fu commented on May 27, 2024

In the meantime I found a solution myself. The key to it was that $property of a @Published property is a Publisher itself:

import PlaygroundSupport
import SwiftUI
import Combine

PlaygroundPage.current.needsIndefiniteExecution = true


class TextEngine:ObservableObject{
    @Published var textInput = ""
    @Published var textOutput = ""
    private var worker:AnyCancellable? = nil
    
    init(){
        worker = $textInput.print("raw input")
            .debounce(for: 2, scheduler: RunLoop.main)
        .removeDuplicates{$0 == $1}
        .print("debounced and duplicates removed")
        .assign(to:\.textOutput, on:self)
    }
}

let engine = TextEngine()

struct LiveView:View {
    @ObservedObject var engine:TextEngine

    var body: some View {
        VStack{
            TextField("Enter text", text: $engine.textInput)
            .textFieldStyle(RoundedBorderTextFieldStyle())
            Text(engine.textOutput)
            Spacer()
        }.padding()
    }
}

let liveView = UIHostingController(rootView: LiveView(engine: engine).frame(width:770, height:400))
liveView.preferredContentSize = CGSize(width: 770, height: 1100)
PlaygroundPage.current.liveView = liveView

from swiftui-notes.

heckj avatar heckj commented on May 27, 2024

I've got a model/example now properly in place, as of the pr #25

from swiftui-notes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.