Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active June 24, 2017 13:03
Show Gist options
  • Save jayrambhia/65d11ade3bfe49f7ceaafdf746f968d6 to your computer and use it in GitHub Desktop.
Save jayrambhia/65d11ade3bfe49f7ceaafdf746f968d6 to your computer and use it in GitHub Desktop.
Event Sync
@LayoutSpec(events = { LikeChangeEvent.class })
public class FullScreenComponentSpec {
... other stuff
@OnEvent(ClickEvent.class)
static void onLikeButtonClicked(ComponentContext c, @State boolean isLiked, @Prop GifItem gif, @Prop Component gifComponent) {
FullScreenComponent.updateLikeButtonAsync(c, !isLiked);
FullScreenComponent.dispatchLikeChangeEvent(FullScreenComponent.getLikeChangeEventHandler(c), !isLiked, gif.getId());
// Create EventHandler from gifComponent scope.
EventHandler<FavChangeEvent> handler = GifItemView.onFavChanged(gifComponent.getScopedContext());
// Dispatch the event
handler.dispatchEvent(new FavChangeEvent(!isLiked, gif.getId()));
}
}
@LayoutSpec(events = { LikeChangeEvent.class })
public class FullScreenComponentSpec {
private static final String TAG = "FullScreenComponent";
@OnCreateInitialState
static void createInitialState(ComponentContext c, StateValue<Boolean> isLiked, @Prop boolean initLiked) {
isLiked.set(initLiked);
}
@OnCreateLayout
static ComponentLayout onCreateLayout(ComponentContext context, @Prop RequestManager glide, @Prop GifItem gif, @State boolean isLiked) {
... create component view here
}
@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) {
FullScreenComponent.updateLikeButtonAsync(c, !isLiked);
// Using event (instead of a simple callback) to update database
FullScreenComponent.dispatchLikeChangeEvent(FullScreenComponent.getLikeChangeEventHandler(c), !isLiked, gif.getId());
// Send an event to update `isLiked` state of GitItemView component.
// Create FavChangeEvent
EventHandler<FavChangeEvent> handler = GifItemView.onFavChanged(c);
// Dispatch the event
handler.dispatchEvent(new FavChangeEvent(!isLiked, gif.getId()));
}
}
@LayoutSpec(events = { LikeChangeEvent.class })
public class GifItemViewSpec {
... other stuff
@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.dispatchLikeChangeEvent(GifItemView.getLikeChangeEventHandler(c), !isLiked, gif.getId());
GifItemView.updateLikeButtonAsync(c, !isLiked);
}
@OnEvent(ClickEvent.class)
static void onViewClicked(ComponentContext c, @Prop GifItem gif, @Prop (optional = true) GifCallback callback) {
if (callback != null) {
// Send component to callback
callback.onGifSelected(gif, c.getComponentScope());
}
}
@OnEvent(FavChangeEvent.class)
static void onFavChanged(ComponentContext c, @FromEvent boolean isLiked, @FromEvent String gifId, @Prop GifItem gif) {
if (gif.getId().equals(gifId)) {
GifItemView.updateLikeButtonAsync(c, isLiked);
}
}
public interface GifCallback {
void onGifSelected(GifItem gif, Component gifComponent);
}
}
@LayoutSpec(events = { LikeChangeEvent.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) {
... create component view here
}
@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.dispatchLikeChangeEvent(GifItemView.getLikeChangeEventHandler(c), !isLiked, gif.getId());
GifItemView.updateLikeButtonAsync(c, !isLiked);
}
@OnEvent(ClickEvent.class)
static void onViewClicked(ComponentContext c, @Prop GifItem gif, @Prop (optional = true) GifCallback callback) {
if (callback != null) {
callback.onGifSelected(gif);
}
}
@OnEvent(FavChangeEvent.class)
static void onFavChanged(ComponentContext c, @FromEvent boolean isLiked, @FromEvent String gifId, @Prop GifItem gif) {
if (gif.getId().equals(gifId)) {
// This will invoke updateLikeButton which will update isLiked state.
GifItemView.updateLikeButtonAsync(c, isLiked);
}
}
... some more stuff
public interface GifCallback {
void onGifSelected(GifItem gif);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment