aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryActivity.java
blob: f706f0cb6209d31a5af02c9f3a2d3cf4d36e8fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.wordpress.android.ui.posts;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.CategoryNode;

import java.util.ArrayList;

public class AddCategoryActivity extends AppCompatActivity {
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.add_category);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            id = extras.getInt("id");
        }
        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) {
                EditText categoryNameET = (EditText) findViewById(R.id.category_name);
                String category_name = categoryNameET.getText().toString();
                EditText categorySlugET = (EditText) findViewById(R.id.category_slug);
                String category_slug = categorySlugET.getText().toString();
                EditText categoryDescET = (EditText) findViewById(R.id.category_desc);
                String category_desc = categoryDescET.getText().toString();
                Spinner sCategories = (Spinner) findViewById(R.id.parent_category);
                String parent_category = "";
                if (sCategories.getSelectedItem() != null)
                    parent_category = ((CategoryNode) sCategories.getSelectedItem()).getName().trim();
                int parent_id = 0;
                if (sCategories.getSelectedItemPosition() != 0) {
                    parent_id = WordPress.wpDB.getCategoryId(id, parent_category);
                }

                if (category_name.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();

                    bundle.putString("category_name", category_name);
                    bundle.putString("category_slug", category_slug);
                    bundle.putString("category_desc", category_desc);
                    bundle.putInt("parent_id", parent_id);
                    bundle.putString("continue", "TRUE");
                    Intent mIntent = new Intent();
                    mIntent.putExtras(bundle);
                    setResult(RESULT_OK, mIntent);
                    finish();
                }

            }
        });

        cancelButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Bundle bundle = new Bundle();

                bundle.putString("continue", "FALSE");
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        });
    }

    private void loadCategories() {
        CategoryNode rootCategory = CategoryNode.createCategoryTreeFromDB(id);
        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);
        }
    }
}