Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some warnings in BookmarksFragment #1908

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.snackbar.Snackbar;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.quran.labs.androidquran.QuranApplication;
import com.quran.labs.androidquran.R;
import com.quran.labs.androidquran.database.BookmarksDBAdapter;
import com.quran.labs.androidquran.dao.bookmark.BookmarkResult;
import com.quran.labs.androidquran.database.BookmarksDBAdapter;
import com.quran.labs.androidquran.presenter.bookmark.BookmarkPresenter;
import com.quran.labs.androidquran.presenter.bookmark.BookmarksContextualModePresenter;
import com.quran.labs.androidquran.ui.QuranActivity;
Expand All @@ -45,7 +47,7 @@ public static BookmarksFragment newInstance(){
}

@Override
public void onAttach(Context context) {
public void onAttach(@NonNull Context context) {
super.onAttach(context);
((QuranApplication) context.getApplicationContext()).getApplicationComponent().inject(this);
setHasOptionsMenu(true);
Expand All @@ -57,7 +59,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.quran_list, container, false);

final Context context = getActivity();
final Context context = requireContext();

recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
Expand All @@ -84,7 +86,7 @@ public void onStop() {
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuItem sortItem = menu.findItem(R.id.sort);
if (sortItem != null) {
Expand Down Expand Up @@ -114,32 +116,27 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.sort_date:
bookmarkPresenter.setSortOrder(BookmarksDBAdapter.SORT_DATE_ADDED);
item.setChecked(true);
return true;
case R.id.sort_location: {
bookmarkPresenter.setSortOrder(BookmarksDBAdapter.SORT_LOCATION);
item.setChecked(true);
return true;
}
case R.id.group_by_tags: {
bookmarkPresenter.toggleGroupByTags();
item.setChecked(bookmarkPresenter.isGroupedByTags());
return true;
}
case R.id.show_recents: {
bookmarkPresenter.toggleShowRecents();
item.setChecked(bookmarkPresenter.isShowingRecents());
return true;
}
case R.id.show_date: {
bookmarkPresenter.toogleShowDate();
bookmarksAdapter.setShowDate(bookmarkPresenter.isDateShowing());
item.setChecked(bookmarkPresenter.isDateShowing());
return true;
}
if (itemId == R.id.sort_date) {
Copy link
Contributor Author

@ahmedre ahmedre Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch with R like this was will be broken on AGP 8 because R will become non-final

bookmarkPresenter.setSortOrder(BookmarksDBAdapter.SORT_DATE_ADDED);
item.setChecked(true);
return true;
} else if (itemId == R.id.sort_location) {
bookmarkPresenter.setSortOrder(BookmarksDBAdapter.SORT_LOCATION);
item.setChecked(true);
return true;
} else if (itemId == R.id.group_by_tags) {
bookmarkPresenter.toggleGroupByTags();
item.setChecked(bookmarkPresenter.isGroupedByTags());
return true;
} else if (itemId == R.id.show_recents) {
bookmarkPresenter.toggleShowRecents();
item.setChecked(bookmarkPresenter.isShowingRecents());
return true;
} else if (itemId == R.id.show_date) {
bookmarkPresenter.toogleShowDate();
bookmarksAdapter.setShowDate(bookmarkPresenter.isDateShowing());
item.setChecked(bookmarkPresenter.isDateShowing());
return true;
}

return super.onOptionsItemSelected(item);
Expand All @@ -154,7 +151,7 @@ public void onNewData(BookmarkResult items) {
}

@Override
public void onClick(QuranRow row, int position) {
public void onClick(@NonNull QuranRow row, int position) {
if (bookmarksContextualModePresenter.isInActionMode()) {
boolean checked = isValidSelection(row) &&
!bookmarksAdapter.isItemChecked(position);
Expand All @@ -167,7 +164,7 @@ public void onClick(QuranRow row, int position) {
}

@Override
public boolean onLongClick(QuranRow row, int position) {
public boolean onLongClick(@NonNull QuranRow row, int position) {
if (isValidSelection(row)) {
bookmarksAdapter.setItemChecked(position, !bookmarksAdapter.isItemChecked(position));
if (bookmarksContextualModePresenter.isInActionMode() &&
Expand Down Expand Up @@ -205,33 +202,28 @@ public boolean onContextualActionClicked(int itemId) {
Activity currentActivity = getActivity();
if (currentActivity instanceof QuranActivity) {
QuranActivity activity = (QuranActivity) currentActivity;
switch (itemId) {
case R.id.cab_delete: {
final List<QuranRow> selected = bookmarksAdapter.getCheckedItems();
final int size = selected.size();
final Resources res = getResources();
bookmarkPresenter.deleteAfterSomeTime(selected);
Snackbar snackbar = Snackbar.make(recyclerView,
res.getQuantityString(R.plurals.bookmark_tag_deleted, size, size),
BookmarkPresenter.DELAY_DELETION_DURATION_IN_MS);
snackbar.setAction(R.string.undo, mOnUndoClickListener);
snackbar.getView().setBackgroundColor(ContextCompat.getColor(activity,
R.color.snackbar_background_color));
snackbar.show();
return true;
}
case R.id.cab_new_tag: {
activity.addTag();
return true;
}
case R.id.cab_edit_tag: {
handleTagEdit(activity, bookmarksAdapter.getCheckedItems());
return true;
}
case R.id.cab_tag_bookmark: {
handleTagBookmarks(activity, bookmarksAdapter.getCheckedItems());
return true;
}
if (itemId == R.id.cab_delete) {
final List<QuranRow> selected = bookmarksAdapter.getCheckedItems();
final int size = selected.size();
final Resources res = getResources();
bookmarkPresenter.deleteAfterSomeTime(selected);
Snackbar snackbar = Snackbar.make(recyclerView,
res.getQuantityString(R.plurals.bookmark_tag_deleted, size, size),
BookmarkPresenter.DELAY_DELETION_DURATION_IN_MS);
snackbar.setAction(R.string.undo, mOnUndoClickListener);
snackbar.getView().setBackgroundColor(ContextCompat.getColor(activity,
R.color.snackbar_background_color));
snackbar.show();
return true;
} else if (itemId == R.id.cab_new_tag) {
activity.addTag();
return true;
} else if (itemId == R.id.cab_edit_tag) {
handleTagEdit(activity, bookmarksAdapter.getCheckedItems());
return true;
} else if (itemId == R.id.cab_tag_bookmark) {
handleTagBookmarks(activity, bookmarksAdapter.getCheckedItems());
return true;
}
}
return false;
Expand Down