Giter Site home page Giter Site logo

Comments (5)

zserge avatar zserge commented on September 1, 2024

Hi, @corbt! Glad to hear your interest to Anvil is back :)

As you might remember the old Anvil's config() was needed only to get access to the real view object.
In the new Anvil you always have access to the real view object inside your renderable lambdas. Just call Anvil.currentView() and you will get the view instance which is currently being modified, e.g.:

linearLayout {
  val layout = (LinearLayout) Anvil.currentView();
  // Here you may use real LinearLayout instance
  button {
     if (!myButtonIsInitialized) {
        // Get real Button instance
        val b = (Button) Anvil.currentView();
        initializeMyButton(b);
        myButtonIsInitialized = true;
     }
     text("Click me")
     onClick { v -> ... }
     ....
  }
}

The only catch here is that the first rendering cycle may happen to early and if you want, say, to get view width in pixels - it will return zero. So you may force rendering to be done in a loop again and again until view width becomes a valid number:

int mWidth = -1;
...
frameLayout(() -> {
  if (mWidth <= 0) {
    if ((mWidth = Anvil.currentView().getWidth()) == 0) {
      post(Anvil::render);
    }
  }
});

from anvil.

corbt avatar corbt commented on September 1, 2024

Ok, sounds good. A related question: often I want to call a function on the view only on the first render (which was a property of the config attribute). I suppose I could use a sentinel boolean to mark when my code has been run once and skip it after that, but is there a way to get this behavior built into the framework? If not I guess I'll just create my own config that implements that behavior. :)

from anvil.

zserge avatar zserge commented on September 1, 2024

Sorry, at the moment sentinel boolean is the only solution. I'm still not sure if a function like config is really needed in Anvil core. I expect most of the UI to be controlled in a declarative manner via Anvil DSL, so config is rather an exception.

I would implement config as config(cb: () -> Boolean) that would execute cb and re-schedule Anvil.render until the cb returns true. In simple cases it would run it once, in more complex it will stop once view is initialized. But such implementation would require to keep a flag variable bound to the view, which implies using a WeakHashMap etc. This generic solution can quickly become complicated for a small library like Anvil.

So I suggest writing config-like wrappers manually for now, unless we see that every Anvil user does it and their implementations look very similar. Then it would be wise to have a generic implementation added to the Anvil core.

from anvil.

corbt avatar corbt commented on September 1, 2024

Ok. I suspect that this will be a useful/commonly-requested function, but if you want to leave it out until more people request it that's fine with me. I've included my implementation below so if anyone else needs this functionality they can copy it into their project.

public interface ConfigListener {
    abstract public void config(View v);
}

public static void config(final ConfigListener listener) {
    attr(configFunc.instance, listener);
}

private final static class configFunc implements Anvil.AttrFunc<ConfigListener> {
    private final static configFunc instance = new configFunc();
    @Override
    public void apply(View view, ConfigListener listener, ConfigListener old) {
        if(old == null)
            listener.config(view);
    }
}

from anvil.

zserge avatar zserge commented on September 1, 2024

Ok, Anvil 0.2.0 is here, it includes the 'init' function that runs the attached lambda only once, like:

textView {
  init {
    val tv: TextView = Anvil.currentView()
    tv.setText("hello");
  }
}

It follows the code you've submitted, just renamed it to 'init' since it seems to be more precise (initialization happens once for sure, while configuration may happen many times).

from anvil.

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.