mirror of https://github.com/tasks/tasks
Extract tag activity classes
parent
6254d61fd8
commit
a49e6e7ffc
@ -0,0 +1,60 @@
|
||||
package com.todoroo.astrid.tags;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.todoroo.andlib.sql.Criterion;
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
import com.todoroo.andlib.utility.DialogUtilities;
|
||||
import com.todoroo.astrid.actfm.TagViewFragment;
|
||||
import com.todoroo.astrid.api.AstridApiConstants;
|
||||
import com.todoroo.astrid.dao.MetadataDao;
|
||||
import com.todoroo.astrid.dao.TagDataDao;
|
||||
import com.todoroo.astrid.data.Metadata;
|
||||
import com.todoroo.astrid.data.TagData;
|
||||
import com.todoroo.astrid.service.TagDataService;
|
||||
|
||||
import org.tasks.R;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class DeleteTagActivity extends TagActivity {
|
||||
|
||||
@Inject TagDataDao tagDataDao;
|
||||
@Inject TagDataService tagDataService;
|
||||
@Inject MetadataDao metadataDao;
|
||||
|
||||
@Override
|
||||
protected void showDialog() {
|
||||
DialogUtilities.okCancelDialog(this, getString(R.string.DLG_delete_this_tag_question, tag), getOkListener(), getCancelListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent ok() {
|
||||
return deleteOrLeaveTag(this, tag, uuid);
|
||||
}
|
||||
|
||||
private Intent deleteOrLeaveTag(Context context, String tag, String uuid) {
|
||||
int deleted = deleteTagMetadata(uuid);
|
||||
TagData tagData = tagDataDao.fetch(uuid, TagData.ID, TagData.UUID, TagData.DELETION_DATE, TagData.MEMBER_COUNT, TagData.USER_ID);
|
||||
Intent tagDeleted = new Intent(AstridApiConstants.BROADCAST_EVENT_TAG_DELETED);
|
||||
if(tagData != null) {
|
||||
tagData.setDeletionDate(DateUtilities.now());
|
||||
tagDataService.save(tagData);
|
||||
tagDeleted.putExtra(TagViewFragment.EXTRA_TAG_UUID, tagData.getUuid());
|
||||
}
|
||||
Toast.makeText(context, context.getString(R.string.TEA_tags_deleted, tag, deleted), Toast.LENGTH_SHORT).show();
|
||||
|
||||
context.sendBroadcast(tagDeleted);
|
||||
return tagDeleted;
|
||||
}
|
||||
|
||||
private int deleteTagMetadata(String uuid) {
|
||||
Metadata deleted = new Metadata();
|
||||
deleted.setDeletionDate(DateUtilities.now());
|
||||
|
||||
return metadataDao.update(Criterion.and(MetadataDao.MetadataCriteria.withKey(TaskToTagMetadata.KEY), TaskToTagMetadata.TAG_UUID.eq(uuid)), deleted);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
package com.todoroo.astrid.tags;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.todoroo.andlib.service.ContextManager;
|
||||
import com.todoroo.andlib.utility.DialogUtilities;
|
||||
import com.todoroo.astrid.actfm.TagViewFragment;
|
||||
import com.todoroo.astrid.api.AstridApiConstants;
|
||||
|
||||
import org.tasks.R;
|
||||
|
||||
public class RenameTagActivity extends TagActivity {
|
||||
|
||||
private EditText editor;
|
||||
|
||||
@Override
|
||||
protected void showDialog() {
|
||||
editor = new EditText(this);
|
||||
DialogUtilities.viewDialog(this, getString(R.string.DLG_rename_this_tag_header, tag), editor, getOkListener(), getCancelListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent ok() {
|
||||
if(editor == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String text = editor.getText().toString();
|
||||
if (text == null || text.length() == 0) {
|
||||
return null;
|
||||
} else {
|
||||
int renamed = tagService.rename(uuid, text);
|
||||
Toast.makeText(this, getString(R.string.TEA_tags_renamed, tag, text, renamed),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
||||
if (renamed > 0) {
|
||||
Intent intent = new Intent(AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED);
|
||||
intent.putExtra(TagViewFragment.EXTRA_TAG_UUID, uuid);
|
||||
ContextManager.getContext().sendBroadcast(intent);
|
||||
return intent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.todoroo.astrid.tags;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.todoroo.astrid.actfm.TagViewFragment;
|
||||
import com.todoroo.astrid.data.RemoteModel;
|
||||
|
||||
import org.tasks.R;
|
||||
import org.tasks.injection.InjectingActivity;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public abstract class TagActivity extends InjectingActivity {
|
||||
|
||||
String tag;
|
||||
String uuid;
|
||||
|
||||
@Inject TagService tagService;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
tag = getIntent().getStringExtra(TagFilterExposer.TAG);
|
||||
uuid = getIntent().getStringExtra(TagViewFragment.EXTRA_TAG_UUID);
|
||||
|
||||
if(tag == null || RemoteModel.isUuidEmpty(uuid)) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
showDialog();
|
||||
}
|
||||
|
||||
protected DialogInterface.OnClickListener getOkListener() {
|
||||
return new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
try {
|
||||
Intent result = ok();
|
||||
if (result != null) {
|
||||
setResult(RESULT_OK, result);
|
||||
} else {
|
||||
toastNoChanges();
|
||||
setResult(RESULT_CANCELED);
|
||||
}
|
||||
} finally {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected DialogInterface.OnClickListener getCancelListener() {
|
||||
return new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
try {
|
||||
toastNoChanges();
|
||||
} finally {
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private void toastNoChanges() {
|
||||
Toast.makeText(this, R.string.TEA_no_tags_modified,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
protected abstract void showDialog();
|
||||
|
||||
protected abstract Intent ok();
|
||||
}
|
||||
|
Loading…
Reference in New Issue