Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active April 28, 2017 11:28
Show Gist options
  • Save jayrambhia/f44b18b528764bf7b9d5990cf6c15438 to your computer and use it in GitHub Desktop.
Save jayrambhia/f44b18b528764bf7b9d5990cf6c15438 to your computer and use it in GitHub Desktop.
Events with Litho
final EventDispatcher likeEventDispatcher = new EventDispatcher() {
@Override
public Object dispatchOnEvent(EventHandler eventHandler, Object eventState) {
// Do stuff here
return null;
}
};
HasEventDispatcher hasEventDispatcher = new HasEventDispatcher() {
@Override
public EventDispatcher getEventDispatcher() {
return likeEventDispatcher;
}
};
EventHandler<LikeChangeEvent> likeChangeEventEventHandler = new EventHandler<>(hasEventDispatcher, 1, null);
@LayoutSpec(events = { LikeChangeEvent.class, GifSelectEvent.class })
public class GifItemViewSpec {
@OnCreateInitialState
static void createInitialState(ComponentContext c, StateValue<Boolean> isLiked, @Prop boolean initLiked) {
isLiked.set(initLiked);
}
@OnCreateLayout
static ComponentLayout onCreateLayout(ComponentContext context, @Prop GifItem gif,
@Prop RequestManager glide, @State boolean isLiked) {
return Column.create(context)
.child(GifImageView.create(context)
.gif(gif)
.glide(glide)
.withLayout()
.alignSelf(YogaAlign.CENTER)
.build())
.child(Image.create(context)
.drawableRes(isLiked ? R.drawable.ic_favorite_accent_24dp :R.drawable.ic_favorite_border_accent_24dp)
.withLayout()
.clickHandler(GifItemView.onLikeButtonClicked(context))
.positionType(YogaPositionType.ABSOLUTE)
.widthDip(40)
.heightDip(40)
.paddingDip(YogaEdge.ALL, 8)
.alignSelf(YogaAlign.FLEX_END)
.build())
.clickHandler(GifItemView.onViewClicked(context))
.build();
}
@OnUpdateState
static void updateLikeButton(StateValue<Boolean> isLiked, @Param boolean updatedValue) {
isLiked.set(updatedValue);
}
@OnEvent(ClickEvent.class)
static void onLikeButtonClicked(ComponentContext c, @State boolean isLiked, @Prop GifItem gif) {
GifItemView.updateLikeButtonAsync(c, !isLiked);
GifItemView.dispatchLikeChangeEvent(GifItemView.getLikeChangeEventHandler(c), gif.getId(), !isLiked);
}
@OnEvent(ClickEvent.class)
static void onViewClicked(ComponentContext c, @Prop GifItem gif) {
GifItemView.dispatchGifSelectEvent(GifItemView.getGifSelectEventHandler(c), gif);
}
}
@Event
public class GifSelectEvent {
public GifItem gif;
}
@Event
public class LikeChangeEvent {
public String gifId;
public boolean isLiked;
}
public class MainActivity extends AppCompatActivity {
private Component homeComponent;
private LithoView root;
private boolean isFullScreen;
private static final int LIKE_CHANGE_EVENT_ID = 11;
private static final int GIF_SELECT_EVENT_ID = 12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... some code here
final LikeStore likeStore = new PreferenceLikeStore(this);
final EventHandler likeChangeHandler = new EventHandler(new HasEventDispatcher() {
@Override
public EventDispatcher getEventDispatcher() {
return new EventDispatcher() {
@Override
public Object dispatchOnEvent(EventHandler eventHandler, Object eventState) {
LikeChangeEvent event = (LikeChangeEvent) eventState;
likeStore.setLiked(event.gifId, event.isLiked);
return null;
}
};
}
}, LIKE_CHANGE_EVENT_ID, null);
final EventHandler gifSelectHandler = new EventHandler(new HasEventDispatcher() {
@Override
public EventDispatcher getEventDispatcher() {
return new EventDispatcher() {
@Override
public Object dispatchOnEvent(EventHandler eventHandler, Object eventState) {
GifSelectEvent event = (GifSelectEvent) eventState;
showFullScreen(c, glide, event.gif, likeStore, likeChangeHandler);
return null;
}
};
}
}, GIF_SELECT_EVENT_ID, null);
final GifProvider.ResponseListener responseListener = new GifProvider.ResponseListener() {
@Override
public void onSuccess(List<GifItem> gifs) {
updateContent(c, binder, glide, gifs, likeChangeHandler, gifSelectHandler);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
};
... some code here
}
private void showFullScreen(ComponentContext context, RequestManager glide, GifItem gif,
final LikeStore likeStore, EventHandler likeChangeHandler) {
Component fullScreenComponent = FullScreenComponent.create(context)
.initLiked(likeStore.isLiked(gif.getId()))
.gif(gif)
.key(gif.getId())
.glide(glide)
.likeChangeEventHandler(likeChangeHandler)
.build();
root.setComponent(fullScreenComponent);
isFullScreen = true;
}
private void updateContent(ComponentContext c, RecyclerBinder binder, List<GifItem> gifs, EventHandler likeChangeHandler,
EventHandler gifSelectHandler) {
binder.removeRangeAt(0, binder.getItemCount());
List<ComponentInfo> components = new ArrayList<>();
for (GifItem gif: gifs) {
components.add(ComponentInfo.create().component(
GifItemView.create(c)
.initLiked(gif.isLiked())
.gif(gif)
.key(gif.getId())
.likeChangeEventHandler(likeChangeHandler)
.gifSelectEventHandler(gifSelectHandler)
.build()
).build()
);
}
binder.insertRangeAt(0, components);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment