Add read/unread item state

This commit is contained in:
Shinokuni 2019-04-04 21:44:49 +02:00
parent 37f3a33600
commit ed224cced3
6 changed files with 66 additions and 16 deletions

View File

@ -5,9 +5,7 @@ import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
@ -20,14 +18,14 @@ import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.readrops.app.viewmodels.ItemViewModel;
import com.readrops.app.R;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.utils.DateUtils;
import com.readrops.app.utils.GlideApp;
import com.readrops.app.utils.ReadropsWebView;
import com.readrops.app.utils.Utils;
import com.readrops.app.viewmodels.ItemViewModel;
public class ItemActivity extends AppCompatActivity {
@ -111,11 +109,9 @@ public class ItemActivity extends AppCompatActivity {
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ItemViewModel.class);
viewModel.getItemById(itemId).observe(this, this::bindUI);
actionButton.setOnClickListener(v -> openLink());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void bindUI(ItemWithFeed itemWithFeed) {
this.itemWithFeed = itemWithFeed;
Item item = itemWithFeed.getItem();

View File

@ -57,6 +57,7 @@ import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableCompletableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
@ -65,6 +66,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
public static final String TAG = MainActivity.class.getSimpleName();
public static final int ADD_FEED_REQUEST = 1;
public static final int MANAGE_FEEDS_REQUEST = 2;
public static final int ITEM_REQUEST = 3;
private RecyclerView recyclerView;
private MainItemListAdapter adapter;
@ -85,6 +87,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private int feedCount;
private int feedNb;
private int itemToUpdatePos;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -188,12 +191,32 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
ViewPreloadSizeProvider preloadSizeProvider = new ViewPreloadSizeProvider();
adapter = new MainItemListAdapter(GlideApp.with(this), preloadSizeProvider);
adapter.setOnItemClickListener(itemWithFeed -> {
adapter.setOnItemClickListener((itemWithFeed, position) -> {
Intent intent = new Intent(this, ItemActivity.class);
intent.putExtra(ItemActivity.ITEM_ID, itemWithFeed.getItem().getId());
intent.putExtra(ItemActivity.IMAGE_URL, itemWithFeed.getItem().getImageLink());
startActivityForResult(intent, ITEM_REQUEST);
viewModel.setItemRead(itemWithFeed.getItem().getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableCompletableObserver() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
});
itemWithFeed.getItem().setRead(true);
adapter.notifyItemChanged(position, itemWithFeed);
startActivity(intent);
});
RecyclerViewPreloader<String> preloader = new RecyclerViewPreloader<String>(Glide.with(this), adapter, preloadSizeProvider, 10);

View File

@ -20,7 +20,7 @@ public interface ItemDao {
@Query("Select * from Item Order By pub_date DESC")
LiveData<List<Item>> getAll();
@Query("Select Item.id, title, clean_description, image_link, pub_date, Feed.name, text_color, background_color, icon_url, read_time, Feed.id as feedId, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Folder.id = Feed.folder_id Order By Item.id DESC")
@Query("Select Item.id, title, clean_description, image_link, pub_date, read, Feed.name, text_color, background_color, icon_url, read_time, Feed.id as feedId, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Folder.id = Feed.folder_id Order By Item.id DESC")
LiveData<List<ItemWithFeed>> getAllItemWithFeeds();
@Query("Select case When :guid In (Select guid from Item) Then 'true' else 'false' end")
@ -32,6 +32,9 @@ public interface ItemDao {
@Insert
void insertAll(List<Item> items);
@Query("Select title, Item.description, content, link, pub_date, image_link, author, text_color, background_color, read_time, Feed.name, Feed.id as feedId, siteUrl, Folder.id as folder_id, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Item.id = :id And Folder.id = Feed.folder_id")
@Query("Update Item set read = 1 Where id = :itemId")
void setRead(int itemId);
@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, Folder.name as folder_name from Item Inner Join Feed, Folder on Item.feed_id = Feed.id And Item.id = :id And Folder.id = Feed.folder_id")
LiveData<ItemWithFeed> getItemById(int id);
}

View File

@ -58,6 +58,8 @@ public class Item implements Comparable<Item> {
@ColumnInfo(name = "read_time")
private double readTime;
private boolean read;
public int getId() {
return id;
}
@ -165,6 +167,14 @@ public class Item implements Comparable<Item> {
return description;
}
public boolean isRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
}
public static List<Item> itemsFromRSS(List<RSSItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();

View File

@ -15,6 +15,7 @@ import com.readrops.app.utils.ParsingResult;
import java.util.HashMap;
import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;
@ -40,10 +41,6 @@ public class MainViewModel extends AndroidViewModel {
return repository.sync(feeds);
}
public void addFeed(ParsingResult parsingResult) {
repository.addFeed(parsingResult);
}
public Single<Integer> getFeedCount() {
return repository.getFeedCount();
}
@ -61,4 +58,11 @@ public class MainViewModel extends AndroidViewModel {
emitter.onSuccess(foldersWithFeeds);
});
}
public Completable setItemRead(int itemId) {
return Completable.create(emitter -> {
db.itemDao().setRead(itemId);
emitter.onComplete();
});
}
}

View File

@ -82,6 +82,17 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull List<Object> payloads) {
if (payloads.size() > 0) {
ItemWithFeed itemWithFeed = (ItemWithFeed) payloads.get(0);
float alpha = itemWithFeed.getItem().isRead() ? 0.5f : 1.0f;
holder.itemView.setAlpha(alpha);
} else
onBindViewHolder(holder, position);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder viewHolder, int i) {
ItemWithFeed itemWithFeed = getItem(i);
@ -133,6 +144,9 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
viewHolder.itemFolderName.setText(itemWithFeed.getFolder().getName());
else
viewHolder.itemFolderName.setText(resources.getString(R.string.no_folder));
float alpha = itemWithFeed.getItem().isRead() ? 0.5f : 1.0f;
viewHolder.itemView.setAlpha(alpha);
}
@Override
@ -163,7 +177,7 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
}
public interface OnItemClickListener {
void onItemClick(ItemWithFeed itemWithFeed);
void onItemClick(ItemWithFeed itemWithFeed, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
@ -189,7 +203,7 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION)
listener.onItemClick(getItem(position));
listener.onItemClick(getItem(position), position);
}));
itemTitle = itemView.findViewById(R.id.item_title);