Add select all option in the contextual menu of the items list activity

This commit is contained in:
Shinokuni 2019-04-25 19:47:04 +02:00
parent c03e6ec3f4
commit 9f8c663176
7 changed files with 56 additions and 19 deletions

View File

@ -83,6 +83,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private int feedCount; private int feedCount;
private int feedNb; private int feedNb;
private boolean scrollToTop; private boolean scrollToTop;
private boolean allItemsSelected;
private ActionMode actionMode; private ActionMode actionMode;
@ -247,29 +248,17 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
case R.id.item_mark_read: case R.id.item_mark_read:
viewModel.setItemsReadState(getIdsFromPositions(adapter.getSelection()), true) setReadState(true, allItemsSelected);
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> Toast.makeText(getApplicationContext(),
"Error when updating in db", Toast.LENGTH_LONG).show())
.subscribe();
adapter.updateSelection(true);
break; break;
case R.id.item_mark_unread: case R.id.item_mark_unread:
viewModel.setItemsReadState(getIdsFromPositions(adapter.getSelection()), false) setReadState(false, allItemsSelected);
.subscribeOn(Schedulers.io()) break;
.observeOn(AndroidSchedulers.mainThread()) case R.id.item_select_all:
.doOnError(throwable -> Toast.makeText(getApplicationContext(), adapter.selectAll();
"Error when updating in db", Toast.LENGTH_LONG).show()) allItemsSelected = true;
.subscribe();
adapter.updateSelection(false);
break; break;
} }
updateDrawerFeeds();
actionMode.finish();
return true; return true;
} }
@ -366,6 +355,28 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}); });
} }
private void setReadState(boolean read, boolean allItems) {
if (allItems) {
viewModel.setAllItemsReadState(read)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
allItemsSelected = false;
} else {
viewModel.setItemsReadState(getIdsFromPositions(adapter.getSelection()), read)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> Toast.makeText(getApplicationContext(),
"Error when updating in db", Toast.LENGTH_LONG).show())
.subscribe();
}
adapter.updateSelection(read);
updateDrawerFeeds();
actionMode.finish();
}
@Override @Override
public void onRefresh() { public void onRefresh() {
Log.d(TAG, "syncing started"); Log.d(TAG, "syncing started");

View File

@ -31,12 +31,15 @@ public interface ItemDao {
/** /**
* Set an item read or unread * Set an item read or unread
* @param itemId if of the item to update * @param itemId id of the item to update
* @param readState 1 for read, 0 for unread * @param readState 1 for read, 0 for unread
*/ */
@Query("Update Item set read = :readState Where id = :itemId") @Query("Update Item set read = :readState Where id = :itemId")
void setReadState(int itemId, int readState); void setReadState(int itemId, int readState);
@Query("Update Item set read = :readState")
void setAllItemsReadState(int readState);
@Query("Update Item set read_it_later = 1 Where id = :itemId") @Query("Update Item set read_it_later = 1 Where id = :itemId")
void setReadItLater(int itemId); void setReadItLater(int itemId);

View File

@ -150,6 +150,13 @@ public class MainViewModel extends AndroidViewModel {
return Completable.concat(completableList); return Completable.concat(completableList);
} }
public Completable setAllItemsReadState(boolean read) {
return Completable.create(emitter -> {
db.itemDao().setAllItemsReadState(read ? 1 : 0);
emitter.onComplete();
});
}
public Completable setItemReadItLater(int itemId) { public Completable setItemReadItLater(int itemId) {
return Completable.create(emitter -> { return Completable.create(emitter -> {
db.itemDao().setReadItLater(itemId); db.itemDao().setReadItLater(itemId);

View File

@ -195,6 +195,15 @@ public class MainItemListAdapter extends PagedListAdapter<ItemWithFeed, MainItem
} }
} }
public void selectAll() {
selection.clear();
for (int i = 0; i < getItemCount(); i++) {
selection.add(i);
}
notifyDataSetChanged();
}
public ItemWithFeed getItemWithFeed(int i) { public ItemWithFeed getItemWithFeed(int i) {
return getItem(i); return getItem(i);
} }

View File

@ -14,4 +14,9 @@
android:icon="@drawable/ic_read" android:icon="@drawable/ic_read"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item
android:id="@+id/item_select_all"
android:title="@string/select_all"
app:showAsAction="never" />
</menu> </menu>

View File

@ -49,5 +49,6 @@
</string-array> </string-array>
<string name="unread">Marquer comme non lu</string> <string name="unread">Marquer comme non lu</string>
<string name="read">Marquer comme lu</string> <string name="read">Marquer comme lu</string>
<string name="select_all">Tout sélectionner</string>
</resources> </resources>

View File

@ -51,4 +51,5 @@
</string-array> </string-array>
<string name="unread">Mark as non read</string> <string name="unread">Mark as non read</string>
<string name="read">Mark as read</string> <string name="read">Mark as read</string>
<string name="select_all">Select all</string>
</resources> </resources>