AsyncProxy and ActivityMapper

415 views
Skip to first unread message

kretel

unread,
Jan 6, 2012, 9:29:59 AM1/6/12
to google-we...@googlegroups.com
Hi All, 

I am trying to get the code splitting working on the ActivityMapper. 
I have taken the MVP tutorial sample (http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html) and converted that to use GIN.

After reading a number of posts on the group decided to implement ActivityAsyncProxy (following example from here:  http://code.google.com/p/google-web-toolkit/issues/detail?id=5129 )
and AsyncGoodbyeActivityProvider. Below are the listings for some of the code: 

public class AppActivityMapper implements ActivityMapper {

public AppActivityMapper() {
super();
}
ActivityAsyncProxy<GoodbyeActivity> goodbyeActivityProxy = new ActivityAsyncProxy<GoodbyeActivity>(Ginjector.INSTANCE.getAsyncGoodbyeActivityProvider());
@Override
public Activity getActivity(final Place place) {
Activity activity = null;
if (place instanceof HelloPlace) {
final HelloActivity.Factory factory = Ginjector.INSTANCE.getHelloActivityFactory();
activity = factory.get((HelloPlace) place);
}
else if (place instanceof GoodbyePlace) {
activity = goodbyeActivityProxy;
}
return activity;
}

}

And Ginjector

@GinModules({GinModule.class})
public interface Ginjector extends com.google.gwt.inject.client.Ginjector {
static final Ginjector INSTANCE = GWT.create(Ginjector.class);
EventBus getEventBus();
HelloView getHelloView();
GoodbyeView getGoodbyeView();

PlaceController getPlaceController();
HelloActivity.Factory getHelloActivityFactory();
HelloPlace.Factory getHelloPlaceFactory();
GoodbyeActivity.Factory getGoodbyeActivityFactory();
GoodbyePlace.Factory getGoodbyePlaceFactory();
AsyncProvider<GoodbyeActivity> getAsyncGoodbyeActivityProvider();

}


public class GinModule extends AbstractGinModule {

@Override
protected void configure() {
bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
bind(HelloView.class).to(HelloViewImpl.class).in(Singleton.class);
bind(GoodbyeView.class).to(GoodbyeViewImpl.class).in(Singleton.class);
bind(PlaceController.class).to(AppPlaceController.class).in(Singleton.class);
bind(new TypeLiteral<AsyncProvider<GoodbyeActivity>>() {}).to(AsyncGoodbyeActivityProvider.class);
install(new GinFactoryModuleBuilder().build(HelloActivity.Factory.class));
install(new GinFactoryModuleBuilder().build(HelloPlace.Factory.class));
install(new GinFactoryModuleBuilder().build(GoodbyeActivity.Factory.class));
install(new GinFactoryModuleBuilder().build(GoodbyePlace.Factory.class));
}

}


public class AsyncGoodbyeActivityProvider implements AsyncProvider<GoodbyeActivity> {

public void get(AsyncCallback<GoodbyeActivity> asyncCallback) {
GoodbyePlace place = new GoodbyePlace("Frank");
asyncCallback.onSuccess(Ginjector.INSTANCE.getGoodbyeActivityFactory().get(place));
}

}


All that works fine with AsyncGoodbyeActivityProvider being bound in GIN. 

However, what I want to achieve is to use AsyncProxy in AppActivityMapper . I read number of samples and couldn't find the approach that I can use with GIN. 

Could anyone recommend the solution?  

How can I use the AsyncProxy instead of writing own ActivityAsyncProxy? 


Thanks,
Kris

Patrick Julien

unread,
Jan 6, 2012, 4:09:12 PM1/6/12
to google-we...@googlegroups.com
See this patch for a model you can follow.  Pay close attention to the reviews and you should be fine using RunAsyncActivity

kretel

unread,
Jan 9, 2012, 5:04:07 AM1/9/12
to google-we...@googlegroups.com
Patrick,

Thanks for the link. 
I was more after using GWT AsyncProxy together with Gin (possibly injected to the AppActivityMapper). 
How could that work than? 

Kris. 

Jens

unread,
Jan 9, 2012, 5:30:38 AM1/9/12
to google-we...@googlegroups.com
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.

kretel

unread,
Jan 9, 2012, 5:50:08 AM1/9/12
to google-we...@googlegroups.com
Jens, 

Thanks for the code. Do you also bind the Provider for you Activity Mapper? 

Kris

Jens

unread,
Jan 9, 2012, 6:14:16 AM1/9/12
to google-we...@googlegroups.com
No the code I have posted is all I have done.

Provider and AsyncProvider (used by AsyncActivityProxy) are both used for providing concrete classes (e.g. AsyncActivityProxy, Activity1, etc.). If you dont specify a GIN rule, GIN will try to create an instance with GWT.create() which just works with (non abstract) concrete classes.

-- J.

kretel

unread,
Jan 10, 2012, 4:44:22 AM1/10/12
to google-we...@googlegroups.com
Got it working. Thanks for your help Jens and Patrick. 

Now I have a problem with @Assisted injection for injecting a place to Activity constructor. Would try to solve it using factory for AsyncAcitivtyProxy.
Unless someone has other idea? 

K

Cristian Rinaldi

unread,
Jan 12, 2012, 8:30:33 AM1/12/12
to google-we...@googlegroups.com
Hello:

Maybe my question sounds silly, but what is the need to use AsyncProvider or runAsync in the Activity, if heavier is the view and we can use AsyncProvider in the start method of the activity to get the view?

Regards.
A.U.S Cristian Rinaldi

Teléfono Móvil: (0342) 155 238 083

www.logikas.com

Lisandro de la Torre 2643 Of 5 - 3000 - Santa Fe

Teléfono Fijo: (0342) 483 5138







2012/1/10 kretel <krzyszt...@gmail.com>

K

--
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/-/UaD78qjnpycJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages