Refactoring

pull/147/head
M66B 6 years ago
parent 1b5156dfc5
commit 683e95b75b

@ -34,7 +34,8 @@ import androidx.lifecycle.OnLifecycleEvent;
public class DialogBuilderLifecycle extends AlertDialog.Builder implements LifecycleObserver { public class DialogBuilderLifecycle extends AlertDialog.Builder implements LifecycleObserver {
private LifecycleOwner owner; private LifecycleOwner owner;
private AlertDialog dialog; private AlertDialog dialog;
private boolean hasMessage = false; private CharSequence title = null;
private CharSequence message = null;
public DialogBuilderLifecycle(Context context, LifecycleOwner owner) { public DialogBuilderLifecycle(Context context, LifecycleOwner owner) {
super(context); super(context);
@ -48,16 +49,13 @@ public class DialogBuilderLifecycle extends AlertDialog.Builder implements Lifec
@Override @Override
public AlertDialog.Builder setTitle(int titleId) { public AlertDialog.Builder setTitle(int titleId) {
if (hasMessage) return setTitle(getContext().getString(titleId));
throw new IllegalArgumentException("Custom message set");
return super.setTitle(titleId);
} }
@Override @Override
public AlertDialog.Builder setTitle(@Nullable CharSequence title) { public AlertDialog.Builder setTitle(@Nullable CharSequence title) {
if (hasMessage) this.title = title;
throw new IllegalArgumentException("Custom message set"); return this;
return super.setTitle(title);
} }
@Override @Override
@ -67,16 +65,23 @@ public class DialogBuilderLifecycle extends AlertDialog.Builder implements Lifec
@Override @Override
public AlertDialog.Builder setMessage(@Nullable CharSequence message) { public AlertDialog.Builder setMessage(@Nullable CharSequence message) {
hasMessage = true; this.message = message;
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null);
TextView tvMessage = dview.findViewById(R.id.tvMessage);
tvMessage.setText(message);
setView(dview);
return this; return this;
} }
@Override @Override
public AlertDialog create() { public AlertDialog create() {
if (title == null && message != null) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null);
TextView tvMessage = dview.findViewById(R.id.tvMessage);
tvMessage.setText(message);
setView(dview);
} else {
if (title != null)
super.setTitle(title);
if (message != null)
super.setMessage(message);
}
dialog = super.create(); dialog = super.create();
owner.getLifecycle().addObserver(this); owner.getLifecycle().addObserver(this);
return dialog; return dialog;
@ -87,5 +92,7 @@ public class DialogBuilderLifecycle extends AlertDialog.Builder implements Lifec
dialog.dismiss(); dialog.dismiss();
owner = null; owner = null;
dialog = null; dialog = null;
title = null;
message = null;
} }
} }

Loading…
Cancel
Save