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

Use target fragments instead of tag lookups #395

Merged
merged 2 commits into from
Mar 29, 2019
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 @@ -28,7 +28,6 @@ public class DurationPickerDialogFragment extends DialogFragment
private static final String KEY_DEFAULT_VAL = "DurationPickerDialogFragment.DEFAULT_VALUE";
private static final String KEY_DISABLE_BUTTON = "DurationPickerDialogFragment.DISABLE_BUTTON";
private static final String KEY_SAVED_VAL = "DurationPickerDialogFragment.SAVED_VALUE";
private static final String KEY_RESULT_FRAGMENT = "DurationPickerDialogFragment.RESULT_FRAG";

public static final int NO_VALUE = Integer.MIN_VALUE;

Expand Down Expand Up @@ -91,18 +90,25 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
}

private void onValueSelected(int value) {
Activity parent = getActivity();

String resultFragmentTag = getArguments().getString(KEY_RESULT_FRAGMENT);
Fragment resultFragment = getFragmentManager().findFragmentByTag(resultFragmentTag);

if (resultFragmentTag != null && resultFragment instanceof OnDurationPickedListener) {
((OnDurationPickedListener) resultFragment).onDurationPicked(value);
} else if (parent instanceof OnDurationPickedListener) {
((OnDurationPickedListener) parent).onDurationPicked(value);
Fragment resultFragment = getTargetFragment();

if (resultFragment != null) {
if (resultFragment instanceof OnDurationPickedListener) {
((OnDurationPickedListener) resultFragment).onDurationPicked(value);
} else {
String targetClassName = resultFragment.getClass().getSimpleName();
Timber.w("%s does not implement OnDurationPickedListener. Ignoring chosen value.",
targetClassName);
}
} else {
Timber.w("%s does not implement OnDurationPickedListener. Ignoring chosen value.",
parent.getClass().getSimpleName());
Activity hostActivity = requireActivity();
if (hostActivity instanceof OnDurationPickedListener) {
((OnDurationPickedListener) hostActivity).onDurationPicked(value);
} else {
String targetClassName = hostActivity.getClass().getSimpleName();
Timber.w("%s does not implement OnDurationPickedListener. Ignoring chosen value.",
targetClassName);
}
}
}

Expand Down Expand Up @@ -131,20 +137,20 @@ public static class Builder {
private FragmentManager mFragmentManager;

private String mTitle;
private String mResultFragment;
private Fragment mTargetFragment;
private int mMin;
private int mMax;
private int mDefault;
private String mDisableButton;

public Builder(AppCompatActivity activity) {
mFragmentManager = activity.getSupportFragmentManager();
mResultFragment = null;
mTargetFragment = null;
}

public Builder(Fragment fragment) {
mFragmentManager = fragment.getFragmentManager();
mResultFragment = fragment.getTag();
mTargetFragment = fragment;
}

public Builder setTitle(String title) {
Expand Down Expand Up @@ -175,7 +181,6 @@ public Builder setDisableButtonText(String disableButtonText) {
public void show(String tag) {
Bundle args = new Bundle();
args.putString(KEY_TITLE, mTitle);
args.putString(KEY_RESULT_FRAGMENT, mResultFragment);
args.putInt(KEY_MIN_VAL, mMin);
args.putInt(KEY_MAX_VAL, mMax);
args.putString(KEY_DISABLE_BUTTON, mDisableButton);
Expand All @@ -184,6 +189,7 @@ public void show(String tag) {
DurationPickerDialogFragment dialogFragment = new DurationPickerDialogFragment();
dialogFragment.setArguments(args);

dialogFragment.setTargetFragment(mTargetFragment, 0);
dialogFragment.show(mFragmentManager, tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class NumberPickerDialogFragment extends DialogFragment {
private static final String KEY_DEFAULT_VAL = "NumberPickerDialogFragment.DEFAULT_VALUE";
private static final String KEY_SAVED_VAL = "NumberPickerDialogFragment.SAVED_VALUE";
private static final String KEY_WRAP_SELECTOR = "NumberPickerDialogFragment.WRAP_SELECTOR";
private static final String KEY_RESULT_FRAGMENT = "NumberPickerDialogFragment.RESULT_FRAGMENT";

private NumberPicker mNumberPicker;

Expand Down Expand Up @@ -96,22 +95,25 @@ public void onSaveInstanceState(Bundle outState) {

private void onValueSelected() {
int value = mNumberPicker.getValue();
Activity parent = getActivity();

String resultFragmentTag = getArguments().getString(KEY_RESULT_FRAGMENT);
Fragment resultFragment = getFragmentManager().findFragmentByTag(resultFragmentTag);

if (resultFragmentTag != null && resultFragment instanceof OnNumberPickedListener) {
((OnNumberPickedListener) resultFragment).onNumberPicked(value);
} else if (parent instanceof OnNumberPickedListener) {
((OnNumberPickedListener) parent).onNumberPicked(value);
Fragment resultFragment = getTargetFragment();

if (resultFragment != null) {
if (resultFragment instanceof OnNumberPickedListener) {
((OnNumberPickedListener) resultFragment).onNumberPicked(value);
} else {
String targetClassName = resultFragment.getClass().getSimpleName();
Timber.w("%s does not implement OnNumberPickedListener. Ignoring chosen value.",
targetClassName);
}
} else {
String targetClassName = (resultFragmentTag == null)
? parent.getClass().getSimpleName()
: resultFragmentTag.getClass().getSimpleName();

Timber.w("%s does not implement OnNumberPickedListener. Ignoring chosen value.",
targetClassName);
Activity hostActivity = requireActivity();
if (hostActivity instanceof OnNumberPickedListener) {
((OnNumberPickedListener) hostActivity).onNumberPicked(value);
} else {
String targetClassName = hostActivity.getClass().getSimpleName();
Timber.w("%s does not implement OnNumberPickedListener. Ignoring chosen value.",
targetClassName);
}
}
}

Expand All @@ -125,20 +127,20 @@ public static class Builder {

private String mTitle;
private String mMessage;
private String mResultFragment;
private Fragment mTargetFragment;
private int mMin;
private int mMax;
private int mDefault;
private boolean mWrapSelectorWheel;

public Builder(AppCompatActivity activity) {
mFragmentManager = activity.getSupportFragmentManager();
mResultFragment = null;
mTargetFragment = null;
}

public Builder(Fragment fragment) {
mFragmentManager = fragment.getFragmentManager();
mResultFragment = fragment.getTag();
mTargetFragment = fragment;
}

public Builder setTitle(String title) {
Expand Down Expand Up @@ -175,7 +177,6 @@ public void show(String tag) {
Bundle args = new Bundle();
args.putString(KEY_TITlE, mTitle);
args.putString(KEY_MESSAGE, mMessage);
args.putString(KEY_RESULT_FRAGMENT, mResultFragment);
args.putInt(KEY_MIN_VAL, mMin);
args.putInt(KEY_MAX_VAL, mMax);
args.putInt(KEY_DEFAULT_VAL, mDefault);
Expand All @@ -184,6 +185,7 @@ public void show(String tag) {
NumberPickerDialogFragment dialogFragment = new NumberPickerDialogFragment();
dialogFragment.setArguments(args);

dialogFragment.setTargetFragment(mTargetFragment, 0);
dialogFragment.show(mFragmentManager, tag);
}
}
Expand Down