0

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.

Community
  • 1
  • 1
Lavie Tobey
  • 484
  • 1
  • 4
  • 12
  • 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 Aug 09 '13 at 19:24
  • 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 Aug 09 '13 at 19:26
  • 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 Aug 09 '13 at 19:31
  • 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 Aug 09 '13 at 19:35
  • When you remove the `` line (from the mobilewebapp sample), does it compile more permutations then? – Chris Lercher Aug 09 '13 at 19:53
  • I just verified and I have a MobileGinjector, TabletGinjector and DesktopGinjector and when the compiler runs it creates 18 permutations. So this should be an issue with the compiler not seeing your GinjectorTablet when compiling. You should have 12 perms generated when compiling. Are you using maven to package the project or are you using the eclipse gwt plugin to package your project. I always use mvn manually to package. Do you have more than 1 module.gwt.xml file and if so this may need to be configured in the plugin configuration. Your code looks correct btw. – Chris Hinshaw Aug 09 '13 at 19:56
  • It does! And it even works as expected! It only does 12 perms (not the 16 you mentioned earlier), but it is loading the right view for my ipad vs. my desktop. Submit the answer and I'll accept! – Lavie Tobey Aug 09 '13 at 19:57
  • wow good catch chris, my formfactor.gwt.xml does not have the collaps-property – Chris Hinshaw Aug 09 '13 at 19:58
  • Hah! Didn't even realize there were 2 different Chris's helping. Thanks to both you, I'm all set. – Lavie Tobey Aug 09 '13 at 19:59

2 Answers2

1

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.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
0

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.

durron597
  • 31,968
  • 17
  • 99
  • 158
user1254554
  • 203
  • 1
  • 4
  • 14