Add action to star/fav an item

This commit is contained in:
Shinokuni 2020-10-26 22:02:39 +01:00
parent 4460420f8a
commit 97ae58305c
5 changed files with 65 additions and 4 deletions

View File

@ -46,6 +46,9 @@ import org.koin.java.KoinJavaComponent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import static com.readrops.app.utils.ReadropsKeys.ACTION_BAR_COLOR;
import static com.readrops.app.utils.ReadropsKeys.IMAGE_URL;
import static com.readrops.app.utils.ReadropsKeys.ITEM_ID;
@ -66,6 +69,8 @@ public class ItemActivity extends AppCompatActivity {
private String urlToDownload;
private String imageTitle;
private boolean uiBinded;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -112,14 +117,44 @@ public class ItemActivity extends AppCompatActivity {
}));
viewModel = ViewModelCompat.getViewModel(this, ItemViewModel.class);
viewModel.getItemById(itemId).observe(this, this::bindUI);
viewModel.getItemById(itemId).observe(this, itemWithFeed1 -> {
if (!uiBinded) {
bindUI(itemWithFeed1);
uiBinded = true;
}
});
binding.activityItemFab.setOnClickListener(v -> openInNavigator());
binding.itemStarFab.setOnClickListener(v -> {
Item item = itemWithFeed.getItem();
if (item.isStarred()) {
binding.itemStarFab.setImageResource(R.drawable.ic_empty_star);
} else {
binding.itemStarFab.setImageResource(R.drawable.ic_star);
}
item.setStarred(!item.isStarred());
item.setStarredChanged(!item.isStarredChanged());
viewModel.setStarState(item.getId(), item.isStarred(), item.isStarredChanged())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> Utils.showSnackbar(binding.itemRoot, throwable.getMessage()))
.subscribe();
});
}
private void bindUI(ItemWithFeed itemWithFeed) {
this.itemWithFeed = itemWithFeed;
Item item = itemWithFeed.getItem();
if (item.isStarred()) {
binding.itemStarFab.setImageResource(R.drawable.ic_star);
}
binding.activityItemDate.setText(DateUtils.formattedDateTimeByLocal(item.getPubDate()));
if (item.getImageLink() == null)
@ -165,6 +200,7 @@ public class ItemActivity extends AppCompatActivity {
getWindow().setStatusBarColor(itemWithFeed.getBgColor());
binding.activityItemFab.setBackgroundTintList(ColorStateList.valueOf(itemWithFeed.getBgColor()));
binding.itemStarFab.setBackgroundTintList(ColorStateList.valueOf(itemWithFeed.getBgColor()));
} else if (itemWithFeed.getColor() != 0) {
binding.collapsingLayout.setBackgroundColor(itemWithFeed.getColor());
binding.collapsingLayout.setContentScrimColor(itemWithFeed.getColor());
@ -172,6 +208,7 @@ public class ItemActivity extends AppCompatActivity {
getWindow().setStatusBarColor(itemWithFeed.getColor());
binding.activityItemFab.setBackgroundTintList(ColorStateList.valueOf(itemWithFeed.getColor()));
binding.itemStarFab.setBackgroundTintList(ColorStateList.valueOf(itemWithFeed.getColor()));
}
binding.itemWebview.setItem(itemWithFeed);

View File

@ -17,6 +17,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import io.reactivex.Completable;
public class ItemViewModel extends ViewModel {
private final Database database;
@ -29,6 +31,9 @@ public class ItemViewModel extends ViewModel {
return database.itemDao().getItemById(id);
}
public Completable setStarState(int itemId, boolean starred, boolean starredChanged) {
return database.itemDao().setStarState(itemId, starred, starredChanged);
}
public Uri saveImageInCache(Bitmap bitmap, Context context) throws IOException {
File imagesFolder = new File(context.getCacheDir().getAbsolutePath(), "images");

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#727272"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
</vector>

View File

@ -54,7 +54,8 @@
android:focusable="true"
android:src="@drawable/ic_open_in_browser_white"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end" />
app:layout_anchorGravity="bottom|end" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
@ -165,4 +166,14 @@
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/item_star_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_empty_star"
android:tint="@android:color/white"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -60,8 +60,8 @@ public interface ItemDao extends BaseDao<Item> {
int getUnreadCount(int feedId);
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
@Query("Select title, Item.description, content, link, pub_date, image_link, author, read, text_color, " +
"background_color, read_time, Feed.name, Feed.id as feedId, siteUrl, Folder.id as folder_id, " +
@Query("Select Item.id, title, Item.description, content, link, pub_date, image_link, author, read, text_color, " +
"background_color, read_time, starred, Feed.name, Feed.id as feedId, siteUrl, Folder.id as folder_id, " +
"Folder.name as folder_name from Item Inner Join Feed On Item.feed_id = Feed.id Left Join Folder on Folder.id = Feed.folder_id Where Item.id = :id")
LiveData<ItemWithFeed> getItemById(int id);
@ -91,4 +91,7 @@ public interface ItemDao extends BaseDao<Item> {
@Query("Update Item set read = :read Where remoteId = :remoteId")
void setReadState(String remoteId, boolean read);
@Query("Update Item set starred = :starred, starred_changed = :starredChanged Where id = :itemId")
Completable setStarState(int itemId, boolean starred, boolean starredChanged);
}