0
votes

I'm attempting to put together a multi-project application, wherein one of the sub-projects has multiple views for a single presenter. I am using Gin to inject views into my presenters.

The sub-project contains the presenter and the 2 different views. I have 2 separate gin modules, each binding one of the views to the view interface.

As per Thomas Broyer's suggestion on the answer to this post, my Ginjectors are wrapped in a "holder" class that calls the GWT.create on the particular ginjector. The appropriate holder is configured in the gwt.xml file using a replace-with statement.

When I run my project in Dev Mode, I see the alternate view appear as I expect it to. However, when I compile the project, I still only get the default view. Also, only 6 permutations (I would expect more on account of the replace-with logic), and I do not get the view I expect in the different scenarios.

Here is some code to illustrate.

Subproject.gwt.xml contains this:

<replace-with class="com.example.GinjectorDesktopHolder">
    <when-type-is class="com.example.GinjectorHolder" />        
</replace-with>

<replace-with class="com.example.GinjectorTabletHolder">
    <when-type-is class="com.example.GinjectorHolder" />
    <when-property-is name="formfactor" value="tablet" />
</replace-with>

The "formfactor" variable is defined in a gwt.xml copied verbatim from GWT's mobilewebapp sample project.

The Holder classes look like this:

public abstract class GinjectorHolder {
    public abstract Ginjector getGinjector();
}


public class GinjectorTabletHolder extends GinjectorHolder {
    @Override
    public Ginjector getGinjector() {
        return GWT.create(GinjectorTablet.class);
    }   
}


public class GinjectorDesktopHolder extends GinjectorHolder {
    @Override
    public Ginjector getGinjector() {
        return GWT.create(GinjectorDesktop.class);
    }
}

My Ginjectors look like this:

public interface MyGinjector {
    MyView getView();
    EventBus getEventBus();
}


@GinModules({ModuleDesktop.class})
public interface GinjectorDesktop extends Ginjector, MyGinjector {}


@GinModules({ModuleTablet.class})
public interface GinjectorTablet extends Ginjector, MyGinjector {}

My modules look like this:

public class ModuleDesktop extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(MyPresenter.View.class).to(DesktopView.class);
    }
}


public class ModuleTablet extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(MyPresenter.View.class).to(TabletView.class);
    }
}

And finally, in my presenter proxy, basically the entry point into this particular sub-project, I have this line:

GinjectorHolder holder = GWT.create(GinjectorHolder.class);
MyGinjector ginjector = holder.getGinjector();      

As mentioned earlier, when I run in Dev Mode and put in breakpoints, I can see the appropriate GinjectorHolder is created. The FormFactor.gwt.xml (linked above) provides a switch for using a URL param to switch to the context you'd like to see. So I can do formfactor=tablet in the URL and the Tablet Ginjector Holder is created.

2
You are right in assuming there should be more permutations generated. I used the EXACT same configuration with GIN and the formfactor.js file and I usually get 16 perms. Do you have a user.agent property configured in your .gwt.xml file? - Chris Hinshaw
Hi Chris - I have debugged to the level that I know that I am getting the right form factor, because I was able to locate and debug the GWT JS in my browser. I also debugged that code in Safari, debugging my ipad. So I am certain that part is working. And like I mentioned, I do get the different views when running in dev mode. The only thing I can think of is that perhaps my gin modules aren't properly configured across the 2 projects. Did you use multiple projects when you did this? - Lavie Tobey
No I use a single project, how have you configured your gwt-maven-plugin to compile the two separate projects? Everything else looks correct, it may be that the gwt compiler is not seeing the second project when running - Chris Hinshaw
The sub-project only outputs a JAR file. Only the main project does the gwt-compile. Right now, I have 2 sub-projects that actually contain views, and a common project that has some shared code. Those 3 projects output just a JAR w/source code. The 4th project contains my sole EntryPoint class. It has been working quite well, my code splitting is properly recognized by the GWT compiler. Until this change, I have had no problems with this. - Lavie Tobey
When you remove the <collapse-property name="formfactor" values="*"/> line (from the mobilewebapp sample), does it compile more permutations then? - Chris Lercher

2 Answers

1
votes

As mentioned in the comments, removing the line

<collapse-property name="formfactor" values="*"/>

leads to the expected increase in the number of permutations.

Still, it's mysterious, why this is necessary, because usually it should be possible to collapse any properties you like - it just means, that each browser has to download more code, but should still get everything it needs. Could be a bug.

0
votes

First of all instead of mapping view to viewimpl you can bind it to viewprovider, and then based on user-agent values you can return the appropriate instance to bind to.