-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GifItem - isLiked is a state. This commit shows that even though props are immutable, we can use states. Even though states get updated, we do not change props and litho supports this well. I have added PreferenceLikeStore which stores whether the gif is liked or not. In GifItemView, we are passing GifCallback as a prop to update prefreences. This prop is optional so we can also pass a null value. Signed-off-by: Jay Rambhia <[email protected]>
- Loading branch information
1 parent
af78d50
commit 1ce08c2
Showing
18 changed files
with
190 additions
and
48 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/fenchtose/lithogifsearch/components/GifImageViewSpec.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.fenchtose.lithogifsearch.components; | ||
|
||
import android.graphics.Color; | ||
import android.widget.ImageView; | ||
|
||
import com.bumptech.glide.RequestManager; | ||
import com.facebook.litho.ComponentContext; | ||
import com.facebook.litho.ComponentLayout; | ||
import com.facebook.litho.Output; | ||
import com.facebook.litho.Size; | ||
import com.facebook.litho.annotations.FromPrepare; | ||
import com.facebook.litho.annotations.MountSpec; | ||
import com.facebook.litho.annotations.OnCreateMountContent; | ||
import com.facebook.litho.annotations.OnMeasure; | ||
import com.facebook.litho.annotations.OnMount; | ||
import com.facebook.litho.annotations.OnPrepare; | ||
import com.facebook.litho.annotations.Prop; | ||
import com.facebook.litho.utils.MeasureUtils; | ||
import com.fenchtose.lithogifsearch.models.GifItem; | ||
|
||
@MountSpec | ||
public class GifImageViewSpec { | ||
|
||
@OnPrepare | ||
static void onPrepare(ComponentContext context, @Prop GifItem gif, Output<Float> ratio) { | ||
ratio.set((float) gif.getWidth() / gif.getHeight()); | ||
} | ||
|
||
@OnMeasure | ||
static void onMeasure(ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size, @FromPrepare float ratio) { | ||
// Not using ratio as of now | ||
MeasureUtils.measureWithAspectRatio(widthSpec, heightSpec, 1, size); | ||
} | ||
|
||
@OnCreateMountContent | ||
static ImageView onCreateMountContent(ComponentContext c) { | ||
ImageView view = new ImageView(c.getApplicationContext()); | ||
view.setAdjustViewBounds(true); | ||
view.setBackgroundColor(Color.WHITE); | ||
view.setScaleType(ImageView.ScaleType.CENTER); | ||
return view; | ||
} | ||
|
||
@OnMount | ||
static void onMount(ComponentContext c, ImageView view, @Prop RequestManager glide, @Prop GifItem gif) { | ||
glide.load(gif.getImage()).asGif().into(view); | ||
} | ||
} |
82 changes: 52 additions & 30 deletions
82
app/src/main/java/com/fenchtose/lithogifsearch/components/GifItemViewSpec.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,71 @@ | ||
package com.fenchtose.lithogifsearch.components; | ||
|
||
import android.graphics.Color; | ||
import android.widget.ImageView; | ||
|
||
import com.bumptech.glide.RequestManager; | ||
import com.facebook.litho.ClickEvent; | ||
import com.facebook.litho.Column; | ||
import com.facebook.litho.ComponentContext; | ||
import com.facebook.litho.ComponentLayout; | ||
import com.facebook.litho.Output; | ||
import com.facebook.litho.Size; | ||
import com.facebook.litho.annotations.FromPrepare; | ||
import com.facebook.litho.annotations.MountSpec; | ||
import com.facebook.litho.annotations.OnCreateMountContent; | ||
import com.facebook.litho.annotations.OnMeasure; | ||
import com.facebook.litho.annotations.OnMount; | ||
import com.facebook.litho.annotations.OnPrepare; | ||
import com.facebook.litho.StateValue; | ||
import com.facebook.litho.annotations.LayoutSpec; | ||
import com.facebook.litho.annotations.OnCreateInitialState; | ||
import com.facebook.litho.annotations.OnCreateLayout; | ||
import com.facebook.litho.annotations.OnEvent; | ||
import com.facebook.litho.annotations.OnUpdateState; | ||
import com.facebook.litho.annotations.Prop; | ||
import com.facebook.litho.utils.MeasureUtils; | ||
import com.facebook.litho.annotations.State; | ||
import com.facebook.litho.widget.Image; | ||
import com.facebook.yoga.YogaAlign; | ||
import com.facebook.yoga.YogaEdge; | ||
import com.facebook.yoga.YogaPositionType; | ||
import com.fenchtose.lithogifsearch.R; | ||
import com.fenchtose.lithogifsearch.models.GifItem; | ||
|
||
@MountSpec | ||
@LayoutSpec | ||
public class GifItemViewSpec { | ||
|
||
public static final String TAG = GifItemViewSpec.class.getSimpleName(); | ||
@OnCreateInitialState | ||
static void createInitialState(ComponentContext c, StateValue<Boolean> isLiked, @Prop boolean initLiked) { | ||
isLiked.set(initLiked); | ||
} | ||
|
||
@OnPrepare | ||
static void onPrepare(ComponentContext context, @Prop GifItem gif, Output<Float> ratio) { | ||
ratio.set((float) gif.getWidth() / gif.getHeight()); | ||
@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(); | ||
} | ||
|
||
@OnMeasure | ||
static void onMeasure(ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size, @FromPrepare float ratio) { | ||
MeasureUtils.measureWithAspectRatio(widthSpec, heightSpec, ratio, size); | ||
@OnUpdateState | ||
static void updateLikeButton(StateValue<Boolean> isLiked) { | ||
isLiked.set(!isLiked.get()); | ||
} | ||
|
||
@OnCreateMountContent | ||
static ImageView onCreateMountContent(ComponentContext c) { | ||
ImageView view = new ImageView(c.getApplicationContext()); | ||
view.setAdjustViewBounds(true); | ||
view.setBackgroundColor(Color.WHITE); | ||
view.setScaleType(ImageView.ScaleType.CENTER); | ||
return view; | ||
@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); | ||
} | ||
|
||
@OnMount | ||
static void onMount(ComponentContext c, ImageView view, @Prop RequestManager glide, @Prop GifItem gif) { | ||
glide.load(gif.getImage()).asGif().into(view); | ||
public interface GifCallback { | ||
void onGifLiked(String id, boolean liked); | ||
} | ||
} |
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
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/fenchtose/lithogifsearch/models/db/LikeStore.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.fenchtose.lithogifsearch.models.db; | ||
|
||
|
||
public interface LikeStore { | ||
void setLiked(String id, boolean liked); | ||
boolean isLiked(String id); | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/fenchtose/lithogifsearch/models/db/PreferenceLikeStore.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.fenchtose.lithogifsearch.models.db; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
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); | ||
} | ||
|
||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.