[go: nahoru, domu]

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

SelectCategory and AddCategory activities refreshed #21

Merged
merged 13 commits into from
Jul 31, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Display category hierarchy in "parent" spinner of "AddCategory" view
- also merge Category / CategoryNode / CategoryLevel into on CategoryNode class
  • Loading branch information
maxme committed Jul 25, 2013
commit 59aedeeecb2daf05762541cfcd5af31c657c7f33
37 changes: 0 additions & 37 deletions src/org/wordpress/android/models/Category.java

This file was deleted.

108 changes: 108 additions & 0 deletions src/org/wordpress/android/models/CategoryNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.wordpress.android.models;


import android.util.SparseArray;
import org.wordpress.android.WordPress;
import org.wordpress.android.ui.posts.CategoryArrayAdapter;

import java.util.*;

public class CategoryNode {
private int categoryId;
private String name;
private int parentId;
private int level;
SortedMap<String, CategoryNode> children = new TreeMap<String, CategoryNode>(new Comparator<String>() {
@Override
public int compare(String s, String s2) {
return s.compareToIgnoreCase(s2);
}
});

public SortedMap<String, CategoryNode> getChildren() {
return children;
}

public void setChildren(SortedMap<String, CategoryNode> children) {
this.children = children;
}

public CategoryNode(int categoryId, int parentId, String name) {
this.categoryId = categoryId;
this.parentId = parentId;
this.name = name;
}

public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getParentId() {
return parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

public int getLevel() {
return level;
}

public static CategoryNode createCategoryTreeFromDB(int blogId) {
List<String> stringCategories = WordPress.wpDB.loadCategories(blogId);
// First pass instantiate Category and CategoryNode objects
SparseArray<CategoryNode> categoryMap = new SparseArray<CategoryNode>();
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
CategoryNode currentRootNode;
for (String name : stringCategories) {
int categoryId = WordPress.wpDB.getCategoryId(blogId, name);
int parentId = WordPress.wpDB.getCategoryParentId(blogId, name);
CategoryNode node = new CategoryNode(categoryId, parentId, name);
categoryMap.put(categoryId, node);
}

// Second pass associate nodes to form a tree
for(int i = 0; i < categoryMap.size(); i++){
CategoryNode category = categoryMap.valueAt(i);
if (category.getParentId() == 0) { // root node
currentRootNode = rootCategory;
} else {
currentRootNode = categoryMap.get(category.getParentId());
}
currentRootNode.children.put(category.getName(), categoryMap.get(category.getCategoryId()));
}
return rootCategory;
}

private static void preOrderTreeTraversal(CategoryNode node, int level, ArrayList<CategoryNode> returnValue) {
if (node == null) {
return ;
}
if (node.parentId != -1) {
node.level = level;
returnValue.add(node);
}
for (CategoryNode child : node.getChildren().values()) {
preOrderTreeTraversal(child, level + 1, returnValue);
}
}

public static ArrayList<CategoryNode> getSortedListOfCategoriesFromRoot(CategoryNode node) {
ArrayList<CategoryNode> sortedCategories = new ArrayList<CategoryNode>();
preOrderTreeTraversal(node, 0, sortedCategories);
return sortedCategories;
}
}
19 changes: 12 additions & 7 deletions src/org/wordpress/android/ui/posts/AddCategoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

public class AddCategoryActivity extends Activity {
private int id;
Expand Down Expand Up @@ -100,25 +101,29 @@ public void onClick(View v) {
finish();
}
});

}

private void loadCategories() {
ArrayList<CharSequence> loadTextArray = new ArrayList<CharSequence>();
loadTextArray.clear();
List<?> categoriesVector = WordPress.wpDB.loadCategories(id);
if (categoriesVector.size() > 0) {
CategoryNode rootCategory = CategoryNode.createCategoryTreeFromDB(id);
ArrayList<CategoryNode> categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory);
if (categoryLevels.size() > 0) {
loadTextArray.add(getResources().getText(R.string.none));
for(int i=0; i < categoriesVector.size(); i++) {
loadTextArray.add(categoriesVector.get(i).toString());
for (int i = 0; i < categoryLevels.size(); i++) {
CategoryNode currentCategory = categoryLevels.get(i);
String name = "";
for (int j = 1; j < currentCategory.getLevel(); j++) {
name += " ";
}
name += currentCategory.getName();
loadTextArray.add(name);
}
ArrayAdapter<CharSequence> categories = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_dropdown_item_1line, loadTextArray);
categories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sCategories = (Spinner) findViewById(R.id.parent_category);
sCategories.setAdapter(categories);
}

}

@Override
Expand Down
35 changes: 4 additions & 31 deletions src/org/wordpress/android/ui/posts/CategoryArrayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,14 @@
import android.widget.ImageView;
import android.widget.TextView;
import org.wordpress.android.R;
import org.wordpress.android.models.Category;
import org.wordpress.android.models.CategoryNode;
import org.wordpress.android.util.EscapeUtils;

import java.util.List;

public class CategoryArrayAdapter extends ArrayAdapter<CategoryArrayAdapter.CategoryLevel> {
// Intermediate structure to populate the ListView
public static class CategoryLevel {
private Category category;
private int level;
public class CategoryArrayAdapter extends ArrayAdapter<CategoryNode> {

public CategoryLevel(Category category, int level) {
this.category = category;
this.level = level;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}
}

public CategoryArrayAdapter(Context context, int resource, List<CategoryArrayAdapter.CategoryLevel> objects) {
public CategoryArrayAdapter(Context context, int resource, List<CategoryNode> objects) {
super(context, resource, objects);
}

Expand All @@ -51,7 +25,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
View rowView = inflater.inflate(R.layout.categories_row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.categoryRowText);
ImageView levelIndicatorView = (ImageView) rowView.findViewById(R.id.categoryRowLevelIndicator);
textView.setText(EscapeUtils.escapeHtml(getItem(position).getCategory().getName()));
textView.setText(EscapeUtils.escapeHtml(getItem(position).getName()));
int level = getItem(position).getLevel();
if (level == 1) { // hide ImageView
levelIndicatorView.setVisibility(View.GONE);
Expand All @@ -62,5 +36,4 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
return rowView;
}

}
81 changes: 12 additions & 69 deletions src/org/wordpress/android/ui/posts/SelectCategoriesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.widget.ListView;
import android.widget.Toast;
Expand All @@ -20,7 +19,7 @@
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import org.wordpress.android.models.Category;
import org.wordpress.android.models.CategoryNode;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

Expand All @@ -37,25 +36,10 @@ public class SelectCategoriesActivity extends SherlockListActivity {
private final Handler mHandler = new Handler();
private Blog blog;
private ListView lv;
private CategoryNode mCategories = new CategoryNode(null);
private ArrayList<CategoryArrayAdapter.CategoryLevel> mCategoryLevels;
private CategoryNode mCategories;
private ArrayList<CategoryNode> mCategoryLevels;
private Map<String, Integer> mCategoryNames = new HashMap<String, Integer>();

// Class to efficiently construct an ordered category tree
private class CategoryNode {
SortedMap<String, CategoryNode> children = new TreeMap<String, CategoryNode>(new Comparator<String>() {
@Override
public int compare(String s, String s2) {
return s.compareToIgnoreCase(s2);
}
});
Category category;

public CategoryNode(Category category) {
this.category = category;
}
}

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Expand Down Expand Up @@ -83,14 +67,13 @@ public void onCreate(Bundle icicle) {
categoriesCSV = extras.getString("categoriesCSV");
}

createCategoryTreeFromDB();
populateOrFetchCategories();
}

private void populateCategoryList() {
mCategoryLevels = new ArrayList<CategoryArrayAdapter.CategoryLevel>();
categoryTreePreorderTraversal(mCategories, mCategoryLevels);
mCategoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(mCategories);
for (int i = 0; i < mCategoryLevels.size(); i++) {
mCategoryNames.put(mCategoryLevels.get(i).getCategory().getName(), i);
mCategoryNames.put(mCategoryLevels.get(i).getName(), i);
}

CategoryArrayAdapter categoryAdapter = new CategoryArrayAdapter(this, R.layout.categories_row, mCategoryLevels);
Expand All @@ -106,51 +89,11 @@ private void populateCategoryList() {
}
}

private void categoryTreePreorderTraversal(CategoryNode node,
ArrayList<CategoryArrayAdapter.CategoryLevel> mCategoryLevels) {
categoryTreePreorderTraversal(node, 0, mCategoryLevels);
}

private void categoryTreePreorderTraversal(CategoryNode node, int level,
ArrayList<CategoryArrayAdapter.CategoryLevel> mCategoryLevels) {
if (node == null) {
return ;
}
if (node.category != null ) {
mCategoryLevels.add(new CategoryArrayAdapter.CategoryLevel(node.category, level));
}
for (CategoryNode child : node.children.values()) {
categoryTreePreorderTraversal(child, level + 1, mCategoryLevels);
}
}

private void createCategoryTreeFromDB() {
List<String> stringCategories = WordPress.wpDB.loadCategories(blog.getId());

// First pass instantiate Category and CategoryNode objects
SparseArray<CategoryNode> categoryMap = new SparseArray<CategoryNode>();
mCategories = new CategoryNode(null);
CategoryNode currentRootNode;
for (String name : stringCategories) {
int categoryId = WordPress.wpDB.getCategoryId(blog.getId(), name);
int parentId = WordPress.wpDB.getCategoryParentId(blog.getId(), name);
Category newCategory = new Category(categoryId, parentId, name);
CategoryNode node = new CategoryNode(newCategory);
categoryMap.put(newCategory.getCategoryId(), node);
}

// Second pass associate nodes to form a tree
for(int i = 0; i < categoryMap.size(); i++){
Category category = categoryMap.valueAt(i).category;
if (category.getParentId() == 0) { // root node
currentRootNode = mCategories;
} else {
currentRootNode = categoryMap.get(category.getParentId());
}
currentRootNode.children.put(category.getName(), categoryMap.get(category.getCategoryId()));
}
private void populateOrFetchCategories() {
mCategories = CategoryNode.createCategoryTreeFromDB(blog.getId());

if (stringCategories.size() > 0) {
if (mCategories.getChildren().size() > 0) {
populateCategoryList();
} else {
refreshCategories();
Expand All @@ -164,7 +107,7 @@ public void run() {
pd.dismiss();
}

createCategoryTreeFromDB();
populateOrFetchCategories();

Toast.makeText(SelectCategoriesActivity.this, getResources().getText(R.string.adding_cat_success), Toast.LENGTH_SHORT).show();
}
Expand All @@ -189,7 +132,7 @@ public void onClick(DialogInterface dialog, int whichButton) {
if (pd.isShowing()) {
pd.dismiss();
}
createCategoryTreeFromDB();
populateOrFetchCategories();
Toast.makeText(SelectCategoriesActivity.this, getResources().getText(R.string.categories_refreshed), Toast.LENGTH_SHORT).show();
} else if (finalResult.equals("FAIL")) {
if (pd.isShowing()) {
Expand Down Expand Up @@ -393,7 +336,7 @@ private void saveAndFinish() {

for (int i = 0; i < selectedItems.size(); i++) {
if (selectedItems.get(selectedItems.keyAt(i)) == true) {
String categoryName = mCategoryLevels.get(selectedItems.keyAt(i)).getCategory().getName();
String categoryName = mCategoryLevels.get(selectedItems.keyAt(i)).getName();
selectedCategories += categoryName + ",";
}
}
Expand Down