Monday, January 9, 2012

Re: AsyncProxy and ActivityMapper

I simply do (note that I have a custom AbstractActivity class that contains a setPlace method to tell the activity which place to use):

public class MainActivityMapper implements ActivityMapper {

  private Provider<AsyncActivityProxy<MyActivity1> activity1;

  @Inject
  public MainActivityMapper(Provider<AsyncActivityProxy<MyActivity1>> activity1Provider) {
    activity1 = activity1Provider;
  }

  public Activity getActivity(Place place) {
    if(place instanceof Place1) {
      AsyncActivityProxy<MyActivity1> activity = activity1.get();
      activity.setPlace(place);
      return activity;
    }
    return null;
  }

}

And in my GinModule I do:

bind(AcitivityMapper.class).annotatedWith(Names.named(DisplayArea.MAIN)).to(MainAcitivtyMapper.class)

where DisplayArea.MAIN is simply a String constant "main".

Now I can inject the MainActivityMapper using

public MyExampleConstructor(@Named(DisplayArea.MAIN) ActivityMapper mainActivitymapper) { ..... }

But as you generally only need the ActivityManager I added the following code to the GinModule to be able to inject a MainActivityManager which contains the MainActivityMapper:

@Provides
@Singleton
@Named(DisplayArea.MAIN)
public ActivityManager getMainActivityManager(EventBus eventBus, @Named(DisplayArea.MAIN) ActivityMapper mapper) {
  return new ActivityManager(mapper, eventBus);
}

Maybe there are better ways but this works for me so far and everything I need is injected. I hope it helps.

-- J.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/KTCMncu5QL8J.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment