Code Monkey home page Code Monkey logo

Comments (20)

johnno1962 avatar johnno1962 commented on June 5, 2024

Subscribe to "INJECTION_BUNDLE_NOtiFICATION" and refresh your view controller. The new code needs to be executed to take effect.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

Subscribe to "INJECTION_BUNDLE_NOtiFICATION" and refresh your view. The new code needs to be executed to take effect.

I use extensions like this:
import UIKit

extension UIViewController {

#if DEBUG
@objc func injected() {
    
    for subview in self.view.subviews {
        subview.removeFromSuperview()
    }
    if let sublayers = self.view.layer.sublayers {
        for sublayer in sublayers {
            sublayer.removeFromSuperlayer()
        }
    }
    
    viewDidLoad()
}
#endif

}
#if DEBUG
extension UIView {
@objc func injected() {

    guard let vc = self.viewController() else { return }
    
    
    for subview in vc.view.subviews {
        subview.removeFromSuperview()
    }
    
    if let sublayers = vc.view.layer.sublayers {
        for sublayer in sublayers {
            sublayer.removeFromSuperlayer()
        }
    }
    vc.viewDidLoad()
}

}
#endif

this works fine with code changes in viewcontroller, but not in custom views like a custom tableView cell class.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

By the way, Sir, I want to buy your book Swift Secrets, but seems it's not available for sale in China in any form. Is there a way for me to buy it?

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

As the view controller class is not changing it doe not get the injected() call. The simplest thing you can do is to subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller displaying the custom cells to redraw unless you can add @injected method to the cells themselves to call setNeedsDisplay().

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

I don't know why it would not be available in China. Have you tried amazon? There is a kindle edition.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

I don't know why it would not be available in China. Have you tried amazon? There is a kindle edition.

Lol, I finally made a purchase with an US account, haha

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Enjoy πŸ‘

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

As the view controller class is not changing it doe not get the injected() call. The simplest thing you can do is to subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller displaying the custom cells to redraw unless you can add @injected method to the cells themselves to call setNeedsDisplay().

So, I update the code to:
` @objc func reset() {
// setupUI()
setNeedsDisplay()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    
    NotificationCenter.default.addObserver(self, selector: #selector(reset), name: NSNotification.Name(rawValue: "INJECTION_BUNDLE_NOTIFICATION"), object: nil)
    
    setupUI()
}`

The reset method gets called when command + s, but the cell layout still not changed

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Is there a way to force the cell to re-layout? Subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller reload the table is probably best. Injecting constraints on table cells doesn't sound straightforward.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

Is there a way to force the cell to re-layout? Subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller reload the table is probably best. Injecting constraints on table cells doesn't sound straightforward.

Tried tableView.reloadData in viewController's notification observing method, still no luck

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Any chance you could post a small example project showing constraints not being injected on table cells then I could look at it.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

InjectionTest.zip
Sure, you can search FIXME to see where my modification not being injected ( I do think it's injected, only I can't find a way to refresh the UI)

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Thanks for the example. There were just a couple misunderstandings. I added an @objc injected() to OrderCellView calling setupUI() and added a setNeedsLayout() to the end of that function so the constraints get recalculated. The other problem was where you were expecting statusLbl to get updated on injection. This would only happen if the OrderCellView instance was recreated as it is a property that retains the label in an instance variable. Change 'private var statusLbl:UILabel = {...' to 'private var statusLbl:UILabel {...' and you be getting closer though you would need to store the label as follows so it gets recalculated.

        let status = statusLbl
        addSubview(status)
        status.snp.makeConstraints { (make) in

Note that getters are not normally injected but as setupUI is in the same file, when it gets injected, the getters get injected. Reloading the table probably didn't work because the cells where re-used rather than recreated. Injection can only change the code. Without using the injected method in the right way it can't guarantee the code is called again.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

This is getting weird, did you change the constraints a couple of times? To me, when I only implement @objc injected() in orderCellView, and call setupUI() and setNeedLayout, nothing happened. But when I implement @objc injected() both in orderCellView and ViewController, it works only once. The rest of the tries, the console only give out layout constraints issues.

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Injection takes a bit of getting used to. The problem is probably the constraints are accumulating and need to be cleared out in OrderCellView.setupUI() somehow or recreate the OrderCellView in EAOrderCell on injection rather than trying to reuse it.

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Things work much better if you add this to OrderCellView.setupUI()

    @objc func setupUI() {
        for view in subviews {
            view.removeFromSuperview()
        }

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

Things work much better if you add this to OrderCellView.setupUI()

    @objc func setupUI() {
        for view in subviews {
            view.removeFromSuperview()
        }

Sorry for the late reply and thank you for the solution offered, I tested it and it works very well in terms of constraints modification. This is a big time saver already regarding layout. Only if we can find a way to replace all the subviews with their new instance respectively, so that the background color, text and other properies of the subview can also take effect after the modification.

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

I figure completely recreating the OrderCellView each time the EAOrderCell is @objc injected() rather than storing it is probably the your best bet but I'm not really familiar with the code.

from hotreloading.

JoyaWang avatar JoyaWang commented on June 5, 2024

I figure completely recreating the OrderCellView each time the EAOrderCell is @objc injected() rather than storing it is probably the your best bet but I'm not really familiar with the code.

I guess you can close this now, cause I observe the notification in VC, which will call setupUI, and in setupUI I assign a new tableview instance to the variable, everything works perfectly once I press command + s, even the labels and buttons text of the cell subview. So, thanks a lot for this wonderful tool!

from hotreloading.

johnno1962 avatar johnno1962 commented on June 5, 2024

Excellent πŸ‘ πŸ‘ πŸ‘

from hotreloading.

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.