Skip to content

Instantly share code, notes, and snippets.

@varundwarkani
Created June 13, 2021 08:31
Show Gist options
  • Save varundwarkani/161fe0187dcd3221db8b05a1a4f83485 to your computer and use it in GitHub Desktop.
Save varundwarkani/161fe0187dcd3221db8b05a1a4f83485 to your computer and use it in GitHub Desktop.
public class UPIDialogFragment extends DialogFragment {
private DialogUpiBinding upiBinding;
private UPIViewModel upiViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
upiViewModel = new ViewModelProvider(requireActivity()).get(UPIViewModel.class);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
upiBinding = DialogUpiBinding.inflate(inflater, container, false);
upiBinding.setViewModel(upiViewModel);
upiBinding.setLifecycleOwner(getViewLifecycleOwner());
return upiBinding.getRoot();
}
@Override
public void onResume() {
super.onResume();
if (getDialog() == null) return;
Window window = getDialog().getWindow();
if (window == null) return;
int width = (int) (getResources().getDisplayMetrics().widthPixels * 0.8);
WindowManager.LayoutParams params = window.getAttributes();
params.width = width;
window.setAttributes(params);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setObservers();
}
private void setObservers() {
upiViewModel.isSubmitAction.observer(getViewLifecycleOwner(), canSaveData -> {
upiViewModel.canSaveData.setValue(canSaveData);
dismiss();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment