I have the following Helper class that I keep in a folder alongside my adapters. The problem is there is unnecessary duplication. I need the context of either adapter, depending on where the helper is called from, i.e. from which adapter. How can I make the adapter that is being passed into the helper's constructor generic?
Call:
In the call, I set one of either adapter to null depending on where I am.
private final QuickReplyDialogHelper quickReplyDialogHelper = new QuickReplyDialogHelper(this, null);
quickReplyDialogHelper.quickReplyDialog(object);
Helper:
class QuickReplyDialogHelper {
private final UserProfileAdapter userProfileAdapter;
private final FeedAdapter feedAdapter;
QuickReplyDialogHelper(UserProfileAdapter userProfileAdapter, FeedAdapter feedAdapter) {
this.userProfileAdapter = userProfileAdapter;
this.feedAdapter = feedAdapter;
}
void quickReplyDialog(ParseObject object) {
if (userProfileAdapter != null) {
CharSequence colors[] = new CharSequence[]{
"1",
"2",
"3",
};
AlertDialog.Builder builder = new AlertDialog.Builder(userProfileAdapter.getmContext());
builder.setTitle("Quick Reply");
builder.setIcon(R.drawable.ic_quick_reply);
AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> {
String quickReply = null;
if (which == 0) {
quickReply = "1";
Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 1) {
quickReply = "2";
Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 2) {
quickReply = "3";
Toast.makeText(userProfileAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
}
ParseObject message = new ParseObject("Object");
message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser());
message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER));
String[] likedBy = new String[0];
message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy));
message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId());
message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply);
message.saveInBackground();
});
builder1.show();
}
if (feedAdapter != null) {
CharSequence colors[] = new CharSequence[]{
"1",
"2",
"3"
};
AlertDialog.Builder builder = new AlertDialog.Builder(feedAdapter.getmContext());
builder.setTitle("Quick Reep");
builder.setIcon(R.drawable.ic_quick_reply);
AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> {
String quickReply = null;
if (which == 0) {
quickReply = "1";
Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 1) {
quickReply = "2";
Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 2) {
quickReply = "3";
Toast.makeText(feedAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
}
ParseObject message = new ParseObject("Object");
message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser());
message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER));
String[] likedBy = new String[0];
message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy));
message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId());
message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply);
message.saveInBackground();
});
builder1.show();
}
}
}
Ideal Call:
private final QuickReplyDialogHelper quickReplyDialogHelper = new QuickReplyDialogHelper(this);
quickReplyDialogHelper.quickReplyDialog(object);
Ideal Helper:
Just one builder:
CharSequence colors[] = new CharSequence[]{
"1",
"2",
"3"
};
AlertDialog.Builder builder = new AlertDialog.Builder(genericAdapter.getmContext());
builder.setTitle("Quick Reep");
builder.setIcon(R.drawable.ic_quick_reply);
AlertDialog.Builder builder1 = builder.setItems(colors, (dialog, which) -> {
String quickReply = null;
if (which == 0) {
quickReply = "1";
Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 1) {
quickReply = "2";
Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
} else if (which == 2) {
quickReply = "3";
Toast.makeText(genericAdapter.getmContext(), quickReply, Toast.LENGTH_SHORT).show();
}
ParseObject message = new ParseObject("Object");
message.put(ParseConstants.KEY_SENDER_AUTHOR_POINTER, ParseUser.getCurrentUser());
message.put("replyAuthor", object.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER));
String[] likedBy = new String[0];
message.put(ParseConstants.KEY_LIKED_BY, Arrays.asList(likedBy));
message.put(ParseConstants.KEY_SENDER_PARSE_OBJECT_ID, object.getObjectId());
message.put(ParseConstants.KEY_NOTIFICATION_TEXT, quickReply);
message.saveInBackground();
});
builder1.show();