use of org.wordpress.android.fluxc.model.TermModel in project WordPress-Android by wordpress-mobile.
the class CategoryNode method createCategoryTreeFromList.
public static CategoryNode createCategoryTreeFromList(List<TermModel> categories) {
CategoryNode rootCategory = new CategoryNode(-1, -1, "");
// First pass instantiate CategoryNode objects
LongSparseArray<CategoryNode> categoryMap = new LongSparseArray<>();
CategoryNode currentRootNode;
for (TermModel category : categories) {
long categoryId = category.getRemoteTermId();
long parentId = category.getParentRemoteId();
CategoryNode node = new CategoryNode(categoryId, parentId, category.getName());
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(), rootCategory);
}
currentRootNode.children.put(category.getName(), categoryMap.get(category.getCategoryId()));
}
return rootCategory;
}
use of org.wordpress.android.fluxc.model.TermModel 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();
}
});
}
use of org.wordpress.android.fluxc.model.TermModel in project WordPress-Android by wordpress-mobile.
the class EditPostSettingsFragment method updatePostSettings.
/**
* Updates given post object with current status of settings fields
*/
public void updatePostSettings(PostModel post) {
if (!isAdded() || post == null) {
return;
}
String password = EditTextUtils.getText(mPasswordEditText);
String excerpt = EditTextUtils.getText(mExcerptEditText);
boolean publishImmediately = EditTextUtils.getText(mPubDateText).equals(getText(R.string.immediately));
String publicationDateIso8601 = "";
if (mIsCustomPubDate && publishImmediately && !post.isLocalDraft()) {
publicationDateIso8601 = DateTimeUtils.iso8601FromDate(new Date());
} else if (!publishImmediately) {
if (mIsCustomPubDate) {
publicationDateIso8601 = mCustomPubDate;
} else if (StringUtils.isNotEmpty(post.getDateCreated())) {
publicationDateIso8601 = post.getDateCreated();
}
}
post.setDateCreated(publicationDateIso8601);
String tags = "", postFormat = "";
if (!post.isPage()) {
tags = EditTextUtils.getText(mTagsEditText);
// since mTagsEditText is a `textMultiLine` field, we should replace "\n" with space
tags = tags.replace("\n", " ");
// post format
if (mPostFormatKeys != null && mPostFormatSpinner != null && mPostFormatSpinner.getSelectedItemPosition() < mPostFormatKeys.size()) {
postFormat = mPostFormatKeys.get(mPostFormatSpinner.getSelectedItemPosition());
}
}
String status;
if (mStatusSpinner != null) {
status = getPostStatusForSpinnerPosition(mStatusSpinner.getSelectedItemPosition()).toString();
} else {
status = post.getStatus();
}
if (post.supportsLocation()) {
if (mPostLocation == null) {
post.clearLocation();
} else {
post.setLocation(mPostLocation);
}
}
if (mCategories != null) {
List<Long> categoryIds = new ArrayList<>();
for (TermModel category : mCategories) {
categoryIds.add(category.getRemoteTermId());
}
post.setCategoryIdList(categoryIds);
}
if (AppPrefs.isVisualEditorEnabled() || AppPrefs.isAztecEditorEnabled()) {
post.setFeaturedImageId(mFeaturedImageId);
}
post.setExcerpt(excerpt);
post.setTagNameList(Arrays.asList(TextUtils.split(tags, ",")));
post.setStatus(status);
post.setPassword(password);
post.setPostFormat(postFormat);
}
use of org.wordpress.android.fluxc.model.TermModel in project WordPress-Android by wordpress-mobile.
the class EditPostSettingsFragment method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null || ((requestCode == RequestCodes.TAKE_PHOTO || requestCode == RequestCodes.TAKE_VIDEO))) {
Bundle extras;
switch(requestCode) {
case ACTIVITY_REQUEST_CODE_SELECT_CATEGORIES:
extras = data.getExtras();
if (extras != null && extras.containsKey("selectedCategories")) {
@SuppressWarnings("unchecked") List<TermModel> categoryList = (List<TermModel>) extras.getSerializable("selectedCategories");
mCategories = categoryList;
populateSelectedCategories();
}
break;
case MediaGalleryPickerActivity.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
ArrayList<Long> ids = ListUtils.fromLongArray(data.getLongArrayExtra(MediaGalleryPickerActivity.RESULT_IDS));
if (ids == null || ids.size() == 0) {
return;
}
updateFeaturedImage(ids.get(0));
}
}
}
}
use of org.wordpress.android.fluxc.model.TermModel in project WordPress-Android by wordpress-mobile.
the class EditPostSettingsFragment method populateSelectedCategories.
private void populateSelectedCategories() {
// Remove previous category buttons if any + select category button
List<View> viewsToRemove = new ArrayList<>();
for (int i = 0; i < mSectionCategories.getChildCount(); i++) {
View v = mSectionCategories.getChildAt(i);
if (v == null)
return;
Object tag = v.getTag();
if (tag != null && tag.getClass() == String.class && (((String) tag).startsWith(CATEGORY_PREFIX_TAG) || tag.equals("select-category"))) {
viewsToRemove.add(v);
}
}
for (View viewToRemove : viewsToRemove) {
mSectionCategories.removeView(viewToRemove);
}
viewsToRemove.clear();
// New category buttons
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
if (mCategories != null) {
for (TermModel category : mCategories) {
AppCompatButton buttonCategory = (AppCompatButton) layoutInflater.inflate(R.layout.category_button, null);
if (category != null && category.getName() != null && buttonCategory != null) {
buttonCategory.setText(Html.fromHtml(category.getName()));
buttonCategory.setTag(CATEGORY_PREFIX_TAG + category.getName());
buttonCategory.setOnClickListener(this);
mSectionCategories.addView(buttonCategory);
}
}
}
// Add select category button once the category list has been initialized
Button selectCategory = (Button) layoutInflater.inflate(R.layout.category_select_button, null);
if (selectCategory != null && mCategories != null) {
selectCategory.setOnClickListener(this);
mSectionCategories.addView(selectCategory);
}
}
Aggregations