Skip to content

Replaced Butterknife with Viewbinding in MoreBottomSheetFragment.java #5379

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

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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 @@ -15,9 +15,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import fr.free.nrw.commons.AboutActivity;
import fr.free.nrw.commons.BuildConfig;
Expand All @@ -26,11 +23,11 @@
import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.actions.PageEditClient;
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.databinding.FragmentMoreBottomSheetBinding;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.feedback.FeedbackContentCreator;
import fr.free.nrw.commons.feedback.model.Feedback;
import fr.free.nrw.commons.feedback.FeedbackDialog;
import fr.free.nrw.commons.feedback.OnFeedbackSubmitCallback;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.logging.CommonsLogSender;
import fr.free.nrw.commons.profile.ProfileActivity;
Expand All @@ -49,10 +46,8 @@ public class MoreBottomSheetFragment extends BottomSheetDialogFragment {

@Inject
CommonsLogSender commonsLogSender;
@BindView(R.id.more_profile)
TextView moreProfile;

@BindView((R.id.more_peer_review)) TextView morePeerReview;
private TextView moreProfile;

@Inject @Named("default_preferences")
JsonKvStore store;
Expand All @@ -66,20 +61,31 @@ public class MoreBottomSheetFragment extends BottomSheetDialogFragment {
public View onCreateView(@NonNull final LayoutInflater inflater,
@Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View view = inflater.inflate(R.layout.fragment_more_bottom_sheet, container, false);
ButterKnife.bind(this, view);
final @NonNull FragmentMoreBottomSheetBinding binding =
FragmentMoreBottomSheetBinding.inflate(inflater, container, false);
moreProfile = binding.moreProfile;

if(store.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED)){
morePeerReview.setVisibility(View.GONE);
binding.morePeerReview.setVisibility(View.GONE);
}

binding.moreLogout.setOnClickListener(v -> onLogoutClicked());
binding.moreFeedback.setOnClickListener(v -> onFeedbackClicked());
binding.moreAbout.setOnClickListener(v -> onAboutClicked());
binding.moreTutorial.setOnClickListener(v -> onTutorialClicked());
binding.moreSettings.setOnClickListener(v -> onSettingsClicked());
binding.moreProfile.setOnClickListener(v -> onProfileClicked());
binding.morePeerReview.setOnClickListener(v -> onPeerReviewClicked());

setUserName();
return view;
return binding.getRoot();
}

@Override
public void onAttach(@NonNull final Context context) {
super.onAttach(context);
ApplicationlessInjection
.getInstance(getActivity().getApplicationContext())
.getInstance(requireActivity().getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
}
Expand All @@ -102,42 +108,35 @@ private String getUserName(){
}


@OnClick(R.id.more_logout)
public void onLogoutClicked() {
new AlertDialog.Builder(getActivity())
protected void onLogoutClicked() {
new AlertDialog.Builder(requireActivity())
.setMessage(R.string.logout_verification)
.setCancelable(false)
.setPositiveButton(R.string.yes, (dialog, which) -> {
BaseLogoutListener logoutListener = new BaseLogoutListener();
CommonsApplication app = (CommonsApplication) getContext().getApplicationContext();
app.clearApplicationData(getContext(), logoutListener);
final CommonsApplication app = (CommonsApplication)
requireContext().getApplicationContext();
app.clearApplicationData(requireContext(), new BaseLogoutListener());
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel())
.show();
}

@OnClick(R.id.more_feedback)
public void onFeedbackClicked() {
protected void onFeedbackClicked() {
showFeedbackDialog();
}

/**
* Creates and shows a dialog asking feedback from users
*/
private void showFeedbackDialog() {
new FeedbackDialog(getContext(), new OnFeedbackSubmitCallback() {
@Override
public void onFeedbackSubmit(Feedback feedback) {
uploadFeedback(feedback);
}
}).show();
new FeedbackDialog(getContext(), this::uploadFeedback).show();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Replace anonymous inner class with method reference, suggested by the IDE

}

/**
* uploads feedback data on the server
*/
void uploadFeedback(Feedback feedback) {
FeedbackContentCreator feedbackContentCreator = new FeedbackContentCreator(getContext(), feedback);
void uploadFeedback(final Feedback feedback) {
final FeedbackContentCreator feedbackContentCreator = new FeedbackContentCreator(getContext(), feedback);

Single<Boolean> single =
pageEditClient.prependEdit("Commons:Mobile_app/Feedback", feedbackContentCreator.toString(), "Summary")
Expand All @@ -162,12 +161,10 @@ void uploadFeedback(Feedback feedback) {
* This method shows the alert dialog when a user wants to send feedback about the app.
*/
private void showAlertDialog() {
new AlertDialog.Builder(getActivity())
new AlertDialog.Builder(requireActivity())
.setMessage(R.string.feedback_sharing_data_alert)
.setCancelable(false)
.setPositiveButton(R.string.ok, (dialog, which) -> {
sendFeedback();
})
.setPositiveButton(R.string.ok, (dialog, which) -> sendFeedback())
.show();
}

Expand All @@ -194,32 +191,27 @@ private void sendFeedback() {
}
}

@OnClick(R.id.more_about)
public void onAboutClicked() {
protected void onAboutClicked() {
final Intent intent = new Intent(getActivity(), AboutActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getActivity().startActivity(intent);
requireActivity().startActivity(intent);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Several different instances - the call to requireActivity() replaces getActivity() to add an implicit (Android supplied) null check.

}

@OnClick(R.id.more_tutorial)
public void onTutorialClicked() {
protected void onTutorialClicked() {
WelcomeActivity.startYourself(getActivity());
}

@OnClick(R.id.more_settings)
public void onSettingsClicked() {
protected void onSettingsClicked() {
final Intent intent = new Intent(getActivity(), SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getActivity().startActivity(intent);
requireActivity().startActivity(intent);
}

@OnClick(R.id.more_profile)
public void onProfileClicked() {
protected void onProfileClicked() {
ProfileActivity.startYourself(getActivity(), getUserName(), false);
}

@OnClick(R.id.more_peer_review)
public void onPeerReviewClicked() {
protected void onPeerReviewClicked() {
ReviewActivity.startYourself(getActivity(), getString(R.string.title_activity_review));
}

Expand All @@ -233,7 +225,7 @@ public void onLogoutComplete() {
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(nearbyIntent);
getActivity().finish();
requireActivity().finish();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class MoreBottomSheetFragmentUnitTests {
@Mock
private lateinit var store: JsonKvStore

@Mock
private lateinit var morePeerReview: TextView

@Mock
private lateinit var pageEditClient: PageEditClient

Expand All @@ -76,7 +73,6 @@ class MoreBottomSheetFragmentUnitTests {
fragmentTransaction.commitNowAllowingStateLoss()

Whitebox.setInternalState(fragment, "store", store)
Whitebox.setInternalState(fragment, "morePeerReview", morePeerReview)
Whitebox.setInternalState(fragment, "pageEditClient", pageEditClient)

`when`(store.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED)).thenReturn(
Expand Down