Add AlertDialog for editing/deleting tag

This commit is contained in:
jjbillings 2016-08-16 20:32:08 -05:00
parent 50e10de08f
commit 36c1990449
7 changed files with 160 additions and 41 deletions

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tagEditText" android:layout_gravity="center_horizontal"
android:text="test_tag"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"
android:textColor="@color/white"
android:id="@+id/btnDelete"
android:drawableEnd="@drawable/ic_action_delete_white_24"
android:drawableRight="@drawable/ic_action_delete_white_24"/>
</LinearLayout>

View file

@ -1,18 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTagText"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tagTextView"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textColor="@android:color/black"
android:inputType="text" />
android:paddingLeft="@dimen/material_drawer_vertical_padding"
android:paddingRight="@dimen/material_drawer_vertical_padding"/>
<Button
android:id="@+id/btnDelete"
android:id="@+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Delete" />
</LinearLayout>
android:text="Edit"/>
</LinearLayout>

View file

@ -0,0 +1,14 @@
package com.habitrpg.android.habitica.events.commands;
import com.magicmicky.habitrpgwrapper.lib.models.Tag;
/**
* Created by jbillz on 8/16/16.
*/
public class EditTagCommand {
public Tag tag;
public EditTagCommand(Tag tag) {
this.tag = tag;
}
}

View file

@ -40,6 +40,7 @@ import com.habitrpg.android.habitica.ui.fragments.BaseMainFragment;
import com.habitrpg.android.habitica.ui.fragments.GemsPurchaseFragment;
import com.habitrpg.android.habitica.ui.helpers.DataBindingUtils;
import com.habitrpg.android.habitica.ui.helpers.UiUtils;
import com.habitrpg.android.habitica.ui.menu.EditTagsDrawerItem;
import com.habitrpg.android.habitica.ui.menu.MainDrawerBuilder;
import com.habitrpg.android.habitica.userpicture.BitmapUtils;
import com.magicmicky.habitrpgwrapper.lib.api.MaintenanceApiService;
@ -1515,4 +1516,19 @@ public class MainActivity extends BaseActivity implements Action1<Throwable>, Ha
public void removeFilterDrawerItem(int position) {
this.filterDrawer.removeItemByPosition(position);
}
public void checkTagForUpdate(List<Tag> originalTagList) {
List<IDrawerItem> drawerItems = this.filterDrawer.getDrawerItems();
for(int i = 2; i < drawerItems.size(); ++i) {
Tag t = (Tag)drawerItems.get(i).getTag();
String tagName = t.getName();
Tag originalTag = originalTagList.get(i-2);
Log.i("TAGS", "ORIGINAL TAG: " + originalTag.getName() + " COMPARING TO: " + tagName);
if(!originalTag.getName().equals(tagName)) {
Log.i("TAGS", "ORIGINAL TAG: " + originalTag.getName() + ", NEW TAG: " + tagName);
}
}
}
}

View file

@ -18,6 +18,7 @@ import com.habitrpg.android.habitica.events.commands.AddNewTaskCommand;
import com.habitrpg.android.habitica.events.commands.ChecklistCheckedCommand;
import com.habitrpg.android.habitica.events.commands.CreateTagCommand;
import com.habitrpg.android.habitica.events.commands.DeleteTagCommand;
import com.habitrpg.android.habitica.events.commands.EditTagCommand;
import com.habitrpg.android.habitica.events.commands.FilterTasksByTagsCommand;
import com.habitrpg.android.habitica.events.commands.TaskCheckedCommand;
import com.habitrpg.android.habitica.helpers.TagsHelper;
@ -29,7 +30,8 @@ import com.habitrpg.android.habitica.ui.adapter.tasks.SortableTasksRecyclerViewA
import com.habitrpg.android.habitica.ui.fragments.BaseMainFragment;
import com.habitrpg.android.habitica.ui.helpers.Debounce;
import com.habitrpg.android.habitica.ui.helpers.UiUtils;
import com.habitrpg.android.habitica.ui.menu.EditTagDrawerItem;
import com.habitrpg.android.habitica.ui.helpers.ViewHelper;
import com.habitrpg.android.habitica.ui.menu.EditTagsDrawerItem;
import com.habitrpg.android.habitica.ui.menu.EditTagsSectionDrawer;
import com.habitrpg.android.habitica.ui.menu.EditTextDrawer;
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
@ -44,13 +46,16 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@ -60,7 +65,9 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
@ -88,6 +95,7 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi
private boolean displayingTaskForm;
private boolean editingTags;
private List<Tag> tags;
private List<Tag> tagsCopy;
private HashMap<String, Boolean> tagFilterMap = new HashMap<>();
private Debounce filterChangedHandler = new Debounce(1500, 1000) {
@Override
@ -346,6 +354,11 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi
}
}
@Subscribe
public void onEvent(final EditTagCommand event) {
showEditTagDialog(event.tag);
}
@Subscribe
public void onEvent(TaskTappedEvent event) {
if (this.displayingTaskForm) {
@ -457,7 +470,7 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi
items.add(new EditTagsSectionDrawer().withEditing(this.editingTags).withName("Edit Tags"));
items.add(new EditTextDrawer());
for (Tag t : tagList) {
items.add(new EditTagDrawerItem()
items.add(new EditTagsDrawerItem()
.withName(t.getName())
.withTag(t)
);
@ -479,22 +492,28 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi
}
}
//TODO: Switch/Delete based on editing.
public void addTagFilterDrawerItem(Tag tag) {
if (this.tagsHelper != null) {
IDrawerItem item = new SwitchDrawerItem()
.withName(tag.getName())
.withTag(tag)
.withChecked(this.tagsHelper.isTagChecked(tag.getId()))
.withOnCheckedChangeListener(this);
this.activity.addFilterDrawerItem(item);
if(this.editingTags) {
IDrawerItem item = new EditTagsDrawerItem()
.withName(tag.getName())
.withTag(tag);
this.activity.addFilterDrawerItem(item);
}else {
IDrawerItem item = new SwitchDrawerItem()
.withName(tag.getName())
.withTag(tag)
.withChecked(this.tagsHelper.isTagChecked(tag.getId()))
.withOnCheckedChangeListener(this);
this.activity.addFilterDrawerItem(item);
}
}
}
public void removeTagFilterDrawerItem(Tag t) {
int pos = tags.indexOf(t);
tags.remove(t);
//Have to add 2 for the Drawer components above the actual tags' ui components.
//Have to add 2 for the Drawer components that reside above the actual tags' ui components.
this.activity.removeFilterDrawerItem(pos+2);
}
@ -539,4 +558,52 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi
refreshItem.setActionView(null);
}
}
public void editTag() {
}
public void showEditTagDialog(Tag tag) {
AlertDialog alert = new AlertDialog.Builder(this.activity)
.setTitle("Edit Tag")
.setPositiveButton("Save", (dialog, which) -> {
//TODO:Update the Tag
})
.setNeutralButton("Go Back", (dialog, which) -> {
dialog.dismiss();
})
.create();
//inflate the alertdialog view
View editTagDialogView = this.activity.getLayoutInflater().inflate(R.layout.dialog_edit_tag,null);
//Init the components
if(editTagDialogView != null) {
EditText tagEditText = (EditText)editTagDialogView.findViewById(R.id.tagEditText);
tagEditText.setText(tag.getName());
Button btnDelete = (Button)editTagDialogView.findViewById(R.id.btnDelete);
ViewHelper.SetBackgroundTint(btnDelete, ContextCompat.getColor(this.activity, R.color.worse_10));
btnDelete.setOnClickListener((View v) -> {showDeleteTagDialog(alert,tag);});
}
alert.setView(editTagDialogView);
alert.show();
}
public void showDeleteTagDialog(AlertDialog d, Tag tag) {
AlertDialog alert2 = new AlertDialog.Builder(activity)
.setTitle("Are you sure?").setMessage("Do you really want to delete?")
.setPositiveButton("Yes",(dialog,which) -> {
EventBus.getDefault().post(new DeleteTagCommand(tag));
dialog.dismiss();
d.dismiss();
})
.setNegativeButton("No",(dialog, which) -> {
dialog.dismiss();
})
.create();
alert2.show();
}
}

View file

@ -1,11 +1,10 @@
package com.habitrpg.android.habitica.ui.menu;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.events.commands.DeleteTagCommand;
import com.habitrpg.android.habitica.events.commands.EditTagCommand;
import com.habitrpg.android.habitica.ui.helpers.ViewHelper;
import com.magicmicky.habitrpgwrapper.lib.models.Tag;
import com.mikepenz.fastadapter.utils.ViewHolderFactory;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.holder.ColorHolder;
import com.mikepenz.materialdrawer.holder.StringHolder;
import com.mikepenz.materialdrawer.model.BasePrimaryDrawerItem;
@ -14,32 +13,33 @@ import com.mikepenz.materialdrawer.model.BaseViewHolder;
import org.greenrobot.eventbus.EventBus;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by jjbillings on 8/12/16.
*/
public class EditTagDrawerItem extends BasePrimaryDrawerItem<EditTagDrawerItem, EditTagDrawerItem.ViewHolder> {
public class EditTagsDrawerItem extends BasePrimaryDrawerItem<EditTagsDrawerItem, EditTagsDrawerItem.ViewHolder> {
private StringHolder name;
public EditTagDrawerItem withName(StringHolder name) {
public EditTagsDrawerItem withName(StringHolder name) {
this.name = name;
return this;
}
public EditTagDrawerItem withName(String name) {
public EditTagsDrawerItem withName(String name) {
this.name = new StringHolder(name);
return this;
}
public EditTagDrawerItem withName(@StringRes int nameRes) {
public EditTagsDrawerItem withName(@StringRes int nameRes) {
this.name = new StringHolder(nameRes);
return this;
}
@ -51,30 +51,29 @@ public class EditTagDrawerItem extends BasePrimaryDrawerItem<EditTagDrawerItem,
@Override
public int getType() {
return R.id.material_drawer_item_secondary_switch;
return R.id.material_drawer_item_primary_toggle;
}
@Override
@LayoutRes
public int getLayoutRes() {
return R.layout.edit_tag_drawer_item;
return R.layout.edit_tags_drawer_item;
}
@Override
public void bindView(final ViewHolder viewHolder) {
Context ctx = viewHolder.itemView.getContext();
//define the text color
viewHolder.editTagText.setTextColor(ColorHolder.color(getTextColor(), ctx, com.mikepenz.materialdrawer.R.attr.material_drawer_secondary_text, com.mikepenz.materialdrawer.R.color.material_drawer_secondary_text));
//set the text for the name
StringHolder.applyTo(this.getName(), viewHolder.editTagText);
StringHolder.applyTo(this.getName(), viewHolder.tagTextView);
viewHolder.tagTextView.setTextColor(ColorHolder.color(getTextColor(), ctx, com.mikepenz.materialdrawer.R.attr.material_drawer_primary_text, com.mikepenz.materialdrawer.R.color.material_drawer_primary_text));
//Setup the Delete Button
ViewHelper.SetBackgroundTint(viewHolder.btnDelete, ContextCompat.getColor(ctx, R.color.worse_10));
viewHolder.btnDelete.setEnabled(true);
ViewHelper.SetBackgroundTint(viewHolder.btnEdit, ContextCompat.getColor(ctx, R.color.brand));
viewHolder.btnEdit.setEnabled(true);
viewHolder.tag = (Tag)this.getTag();
//call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
onPostBindView(this, viewHolder.itemView);
}
@ -91,24 +90,26 @@ public class EditTagDrawerItem extends BasePrimaryDrawerItem<EditTagDrawerItem,
}
public static class ViewHolder extends BaseViewHolder implements View.OnClickListener {
private EditText editTagText;
private Button btnDelete;
private TextView tagTextView;
private Button btnEdit;
private Tag tag;
private ViewHolder(View view) {
super(view);
editTagText = (EditText)view.findViewById(R.id.editTagText);
tagTextView = (TextView)view.findViewById(R.id.tagTextView);
btnDelete = (Button)view.findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(this);
btnEdit = (Button)view.findViewById(R.id.btnEdit);
btnEdit.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(this.tag != null) {
EventBus.getDefault().post(new DeleteTagCommand(this.tag));
EventBus.getDefault().post(new EditTagCommand(this.tag));
//EventBus.getDefault().post(new DeleteTagCommand(this.tag));
}
}
}

View file

@ -132,7 +132,7 @@ public class EditTagsSectionDrawer extends AbstractDrawerItem<EditTagsSectionDra
viewHolder.editing = this.editing;
if(this.editing) {
viewHolder.btnEdit.setText("Save");
viewHolder.btnEdit.setText("Done");
}else {
viewHolder.btnEdit.setText("Edit");
}