Last active
April 24, 2017 23:10
-
-
Save jayrambhia/43aa40a26e9fc514e09681833e4c6c7c to your computer and use it in GitHub Desktop.
Android Litho State
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GifItem { | |
private final String id; | |
private final String image; | |
private final boolean isLiked; | |
public GifItem(JsonObject json, boolean isLiked) { | |
this.id = json.get("id").getAsString(); | |
JsonObject image = json.get("images").getAsJsonObject().get("original").getAsJsonObject(); | |
this.image = image.get("url").getAsString(); | |
this.isLiked = isLiked; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getImage() { | |
return image; | |
} | |
public boolean isLiked() { | |
return isLiked; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GifItemViewSpec { | |
... code here | |
@OnEvent(ClickEvent.class) | |
static void onLikeButtonClicked(ComponentContext c, @State boolean isLiked, @Prop GifItem gif, @Prop (optional = true) GifCallback callback) { | |
if (callback != null) { | |
callback.onGifLiked(gif.getId(), !isLiked); | |
} | |
GifItemView.updateLikeButtonAsync(c, !isLiked); | |
} | |
public interface GifCallback { | |
void onGifLiked(String id, boolean isLiked); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
.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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
... code here | |
final LikeStore likeStore = new PreferenceLikeStore(this); | |
final GifItemViewSpec.GifCallback callback = new GifItemViewSpec.GifCallback() { | |
@Override | |
public void onGifLiked(String id, boolean liked) { | |
likeStore.setLiked(id, liked); | |
} | |
}; | |
final GifProvider gifProvider = new GifProvider(responseListener, likeStore); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void updateContent(ComponentContext c, RecyclerBinder binder, List<GifItem> gifs) { | |
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()) | |
.build() | |
).build() | |
); | |
} | |
binder.insertRangeAt(0, components); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PreferenceLikeStore implements LikeStore { | |
private final SharedPreferences preferences; | |
private static final String PREFERENCE_NAME = "like_store"; | |
public PreferenceLikeStore(Context context) { | |
preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); | |
} | |
@Override | |
public void setLiked(String id, boolean liked) { | |
SharedPreferences.Editor editor = preferences.edit(); | |
if (liked) { | |
editor.putBoolean(id, true); | |
} else { | |
editor.remove(id); | |
} | |
editor.apply(); | |
} | |
@Override | |
public boolean isLiked(String id) { | |
return preferences.getBoolean(id, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment