Search in sources :

Example 1 with CategoryNode

use of org.wordpress.android.models.CategoryNode in project WordPress-Android by wordpress-mobile.

the class AddCategoryActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((WordPress) getApplication()).component().inject(this);
    setContentView(R.layout.add_category);
    if (savedInstanceState == null) {
        mSite = (SiteModel) getIntent().getSerializableExtra(WordPress.SITE);
    } else {
        mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE);
    }
    if (mSite == null) {
        ToastUtils.showToast(this, R.string.blog_not_found, ToastUtils.Duration.SHORT);
        finish();
        return;
    }
    loadCategories();
    final Button cancelButton = (Button) findViewById(R.id.cancel);
    final Button okButton = (Button) findViewById(R.id.ok);
    okButton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            String categoryName = ((EditText) findViewById(R.id.category_name)).getText().toString();
            String categoryDesc = ((EditText) findViewById(R.id.category_desc)).getText().toString();
            Spinner categorySpinner = (Spinner) findViewById(R.id.parent_category);
            CategoryNode selectedCategory = (CategoryNode) categorySpinner.getSelectedItem();
            long parentId = (selectedCategory != null) ? selectedCategory.getCategoryId() : 0;
            if (categoryName.replaceAll(" ", "").equals("")) {
                //    Name field cannot be empty
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AddCategoryActivity.this);
                dialogBuilder.setTitle(getResources().getText(R.string.required_field));
                dialogBuilder.setMessage(getResources().getText(R.string.cat_name_required));
                dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                    // Just close the window.
                    }
                });
                dialogBuilder.setCancelable(true);
                dialogBuilder.create().show();
            } else {
                Bundle bundle = new Bundle();
                TermModel newCategory = new TermModel();
                newCategory.setTaxonomy(TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY);
                newCategory.setName(categoryName);
                newCategory.setDescription(categoryDesc);
                newCategory.setParentRemoteId(parentId);
                bundle.putSerializable(KEY_CATEGORY, newCategory);
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        }
    });
    cancelButton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            Intent mIntent = new Intent();
            setResult(RESULT_CANCELED, mIntent);
            finish();
        }
    });
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Spinner(android.widget.Spinner) Bundle(android.os.Bundle) Intent(android.content.Intent) View(android.view.View) CategoryNode(org.wordpress.android.models.CategoryNode) Button(android.widget.Button) TermModel(org.wordpress.android.fluxc.model.TermModel)

Example 2 with CategoryNode

use of org.wordpress.android.models.CategoryNode in project WordPress-Android by wordpress-mobile.

the class AddCategoryActivity method loadCategories.

private void loadCategories() {
    CategoryNode rootCategory = CategoryNode.createCategoryTreeFromList(mTaxonomyStore.getCategoriesForSite(mSite));
    ArrayList<CategoryNode> categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory);
    categoryLevels.add(0, new CategoryNode(0, 0, getString(R.string.none)));
    if (categoryLevels.size() > 0) {
        ParentCategorySpinnerAdapter categoryAdapter = new ParentCategorySpinnerAdapter(this, R.layout.categories_row_parent, categoryLevels);
        Spinner sCategories = (Spinner) findViewById(R.id.parent_category);
        sCategories.setAdapter(categoryAdapter);
    }
}
Also used : CategoryNode(org.wordpress.android.models.CategoryNode) Spinner(android.widget.Spinner)

Example 3 with CategoryNode

use of org.wordpress.android.models.CategoryNode in project WordPress-Android by wordpress-mobile.

the class SelectCategoriesActivity method populateCategoryList.

private void populateCategoryList() {
    CategoryNode categoryTree = CategoryNode.createCategoryTreeFromList(mTaxonomyStore.getCategoriesForSite(mSite));
    mCategoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(categoryTree);
    for (int i = 0; i < mCategoryLevels.size(); i++) {
        mCategoryRemoteIdsToListPositions.put(mCategoryLevels.get(i).getCategoryId(), i);
    }
    CategoryArrayAdapter categoryAdapter = new CategoryArrayAdapter(this, R.layout.categories_row, mCategoryLevels);
    mListView.setAdapter(categoryAdapter);
    if (mSelectedCategories != null) {
        for (Long selectedCategory : mSelectedCategories) {
            if (mCategoryRemoteIdsToListPositions.get(selectedCategory) != null) {
                mListView.setItemChecked(mCategoryRemoteIdsToListPositions.get(selectedCategory), true);
            }
        }
    }
    mListScrollPositionManager.restoreScrollOffset();
}
Also used : CategoryNode(org.wordpress.android.models.CategoryNode)

Aggregations

CategoryNode (org.wordpress.android.models.CategoryNode)3 Spinner (android.widget.Spinner)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 View (android.view.View)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 TermModel (org.wordpress.android.fluxc.model.TermModel)1