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

View File

@ -31,12 +31,15 @@ public interface ItemDao {
/**
* 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
*/
@Query("Update Item set read = :readState Where id = :itemId")
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")
void setReadItLater(int itemId);

View File

@ -150,6 +150,13 @@ public class MainViewModel extends AndroidViewModel {
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) {
return Completable.create(emitter -> {
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) {
return getItem(i);
}

View File

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

View File

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

View File

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