[android] AliasFragment: Wait for activity component to be created

before injecting the ViewModel
parent 5a99cb93
Pipeline #2923 passed with stage
in 10 minutes and 32 seconds
......@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.BriarActivity;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static java.util.Objects.requireNonNull;
......@@ -28,6 +29,7 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
private ConversationViewModel viewModel;
private EditText aliasEditText;
private Button setButton;
public static AliasDialogFragment newInstance() {
return new AliasDialogFragment();
......@@ -38,35 +40,40 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.BriarDialogTheme);
if (getActivity() == null) return;
((BriarActivity) getActivity()).getActivityComponent().inject(this);
viewModel = ViewModelProviders.of(getActivity(), viewModelFactory)
.get(ConversationViewModel.class);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_alias_dialog, container,
false);
aliasEditText = v.findViewById(R.id.aliasEditText);
setButton = v.findViewById(R.id.setButton);
Button cancelButton = v.findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(v1 -> getDialog().cancel());
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
BriarActivity a = (BriarActivity) requireNonNull(getActivity());
a.getActivityComponent().inject(this);
viewModel = ViewModelProviders.of(getActivity(), viewModelFactory)
.get(ConversationViewModel.class);
Contact contact = requireNonNull(viewModel.getContact().getValue());
String alias = contact.getAlias();
aliasEditText.setText(alias);
if (alias != null) aliasEditText.setSelection(alias.length());
Button setButton = v.findViewById(R.id.setButton);
setButton.setOnClickListener(v1 -> {
viewModel.setContactAlias(aliasEditText.getText().toString());
getDialog().dismiss();
});
Button cancelButton = v.findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(v1 -> getDialog().cancel());
return v;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment