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

Migrated AddCategoryFragment to ViewBinding #20933

Merged
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 @@ -5,8 +5,6 @@
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
Expand All @@ -17,6 +15,7 @@

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.databinding.AddCategoryBinding;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.fluxc.model.TermModel;
import org.wordpress.android.fluxc.store.TaxonomyStore;
Expand All @@ -29,8 +28,7 @@

public class AddCategoryFragment extends AppCompatDialogFragment {
private SiteModel mSite;
private EditText mCategoryEditText;
private Spinner mParentSpinner;
private AddCategoryBinding mBinding;

@Inject TaxonomyStore mTaxonomyStore;

Expand All @@ -53,16 +51,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
new MaterialAlertDialogBuilder(new ContextThemeWrapper(getActivity(), R.style.PostSettingsTheme));
// Get the layout inflater
LayoutInflater inflater = requireActivity().getLayoutInflater();

// Inflate view
//noinspection InflateParams
View view = inflater.inflate(R.layout.add_category, null);
mCategoryEditText = (EditText) view.findViewById(R.id.category_name);
mParentSpinner = (Spinner) view.findViewById(R.id.parent_category);
mBinding = AddCategoryBinding.inflate(inflater, null, false);

loadCategories();

builder.setView(view)
builder.setView(mBinding.getRoot())
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null);

Expand Down Expand Up @@ -101,12 +95,12 @@ private void initSite(Bundle savedInstanceState) {
}

private boolean addCategory() {
String categoryName = mCategoryEditText.getText().toString();
CategoryNode selectedCategory = (CategoryNode) mParentSpinner.getSelectedItem();
String categoryName = mBinding.categoryName.getText().toString();
CategoryNode selectedCategory = (CategoryNode) mBinding.parentCategory.getSelectedItem();
long parentId = (selectedCategory != null) ? selectedCategory.getCategoryId() : 0;

if (categoryName.replaceAll(" ", "").equals("")) {
mCategoryEditText.setError(getText(R.string.cat_name_required));
mBinding.categoryName.setError(getText(R.string.cat_name_required));
return false;
}

Expand All @@ -127,7 +121,7 @@ private void loadCategories() {
if (categoryLevels.size() > 0) {
ParentCategorySpinnerAdapter categoryAdapter =
new ParentCategorySpinnerAdapter(getActivity(), R.layout.categories_row_parent, categoryLevels);
mParentSpinner.setAdapter(categoryAdapter);
mBinding.parentCategory.setAdapter(categoryAdapter);
}
}

Expand All @@ -136,4 +130,10 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(WordPress.SITE, mSite);
}

@Override
public void onDestroy() {
super.onDestroy();
mBinding = null;
}
}
Loading