Closes #45: Android: Delete comments

This commit is contained in:
jendib 2015-11-22 23:59:21 +01:00
parent 60ee000b6c
commit 978fbf2cf9
8 changed files with 197 additions and 8 deletions

View File

@ -20,7 +20,6 @@ public class MainApplication extends Application {
JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON); JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON);
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json); ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
// TODO google docs app: right drawer with all actions, with acls, with deep metadatas
// TODO Provide documents to intent action get content // TODO Provide documents to intent action get content
super.onCreate(); super.onCreate();
@ -28,6 +27,7 @@ public class MainApplication extends Application {
@Override @Override
public void onLowMemory() { public void onLowMemory() {
super.onLowMemory();
BitmapAjaxCallback.clearCache(); BitmapAjaxCallback.clearCache();
} }
} }

View File

@ -19,10 +19,12 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.view.ContextMenu;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
@ -35,6 +37,8 @@ import com.sismics.docs.R;
import com.sismics.docs.adapter.AclListAdapter; import com.sismics.docs.adapter.AclListAdapter;
import com.sismics.docs.adapter.CommentListAdapter; import com.sismics.docs.adapter.CommentListAdapter;
import com.sismics.docs.adapter.FilePagerAdapter; import com.sismics.docs.adapter.FilePagerAdapter;
import com.sismics.docs.event.CommentAddEvent;
import com.sismics.docs.event.CommentDeleteEvent;
import com.sismics.docs.event.DocumentDeleteEvent; import com.sismics.docs.event.DocumentDeleteEvent;
import com.sismics.docs.event.DocumentEditEvent; import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.event.DocumentFullscreenEvent; import com.sismics.docs.event.DocumentFullscreenEvent;
@ -87,6 +91,11 @@ public class DocumentViewActivity extends AppCompatActivity {
*/ */
private FilePagerAdapter filePagerAdapter; private FilePagerAdapter filePagerAdapter;
/**
* Comment list adapter.
*/
private CommentListAdapter commentListAdapter;
/** /**
* Document displayed. * Document displayed.
*/ */
@ -245,8 +254,7 @@ public class DocumentViewActivity extends AppCompatActivity {
} }
}); });
// TODO Delete comment button // Button add a comment
ImageButton imageButton = (ImageButton) findViewById(R.id.addCommentBtn); ImageButton imageButton = (ImageButton) findViewById(R.id.addCommentBtn);
imageButton.setOnClickListener(new View.OnClickListener() { imageButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -264,10 +272,8 @@ public class DocumentViewActivity extends AppCompatActivity {
commentEditText.getText().toString(), commentEditText.getText().toString(),
new JsonHttpResponseHandler() { new JsonHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, JSONObject response) { public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// TODO Send a new comment event and update the adapter properly EventBus.getDefault().post(new CommentAddEvent(response));
// if there is no adapter yet (comments not loaded), do nothing
commentEditText.setText(""); commentEditText.setText("");
updateComments();
} }
@Override @Override
@ -556,6 +562,36 @@ public class DocumentViewActivity extends AppCompatActivity {
} }
} }
/**
* A comment add event has been fired.
*
* @param event Comment add event
*/
public void onEventMainThread(CommentAddEvent event) {
if (commentListAdapter == null) return;
TextView emptyView = (TextView) findViewById(R.id.commentEmptyView);
ListView listView = (ListView) findViewById(R.id.commentListView);
emptyView.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
commentListAdapter.add(event.getComment());
}
/**
* A comment delete event has been fired.
*
* @param event Comment add event
*/
public void onEventMainThread(CommentDeleteEvent event) {
if (commentListAdapter == null) return;
TextView emptyView = (TextView) findViewById(R.id.commentEmptyView);
ListView listView = (ListView) findViewById(R.id.commentListView);
commentListAdapter.remove(event.getCommentId());
if (commentListAdapter.getCount() == 0) {
emptyView.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
}
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (document == null) return; if (document == null) return;
@ -621,6 +657,51 @@ public class DocumentViewActivity extends AppCompatActivity {
}); });
} }
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
switch (view.getId()) {
case R.id.commentListView:
if (commentListAdapter == null || document == null) return;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
JSONObject comment = commentListAdapter.getItem(info.position);
boolean writable = document.optBoolean("writable");
String creator = comment.optString("creator");
String username = ApplicationContext.getInstance().getUserInfo().optString("username");
if (writable || creator.equals(username)) {
menu.add(Menu.NONE, 0, 0, getString(R.string.comment_delete));
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// Use real ids if more than one item someday
if (item.getItemId() == 0) {
// Delete a comment
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if (commentListAdapter == null) return false;
JSONObject comment = commentListAdapter.getItem(info.position);
final String commentId = comment.optString("id");
Toast.makeText(DocumentViewActivity.this, R.string.deleting_comment, Toast.LENGTH_LONG).show();
CommentResource.remove(DocumentViewActivity.this, commentId, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
EventBus.getDefault().post(new CommentDeleteEvent(commentId));
}
@Override
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
Toast.makeText(DocumentViewActivity.this, R.string.error_deleting_comment, Toast.LENGTH_LONG).show();
}
});
return true;
}
return false;
}
/** /**
* Refresh comments list. * Refresh comments list.
*/ */
@ -633,12 +714,14 @@ public class DocumentViewActivity extends AppCompatActivity {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE); emptyView.setVisibility(View.GONE);
listView.setVisibility(View.GONE); listView.setVisibility(View.GONE);
registerForContextMenu(listView);
CommentResource.list(this, document.optString("id"), new JsonHttpResponseHandler() { CommentResource.list(this, document.optString("id"), new JsonHttpResponseHandler() {
@Override @Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) { public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
JSONArray comments = response.optJSONArray("comments"); JSONArray comments = response.optJSONArray("comments");
listView.setAdapter(new CommentListAdapter(comments)); commentListAdapter = new CommentListAdapter(comments);
listView.setAdapter(commentListAdapter);
listView.setVisibility(View.VISIBLE); listView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
if (comments.length() == 0) { if (comments.length() == 0) {

View File

@ -99,4 +99,29 @@ public class CommentListAdapter extends BaseAdapter {
return view; return view;
} }
/**
* Add a new comment.
*
* @param comment Comment
*/
public void add(JSONObject comment) {
commentList.add(comment);
notifyDataSetChanged();
}
/**
* Remove a comment.
*
* @param commentId Comment ID
*/
public void remove(String commentId) {
for (JSONObject comment : commentList) {
if (comment.optString("id").equals(commentId)) {
commentList.remove(comment);
notifyDataSetChanged();
return;
}
}
}
} }

View File

@ -0,0 +1,33 @@
package com.sismics.docs.event;
import org.json.JSONObject;
/**
* Comment add event.
*
* @author bgamard.
*/
public class CommentAddEvent {
/**
* Comment.
*/
private JSONObject comment;
/**
* Create a comment add event.
*
* @param comment Comment
*/
public CommentAddEvent(JSONObject comment) {
this.comment = comment;
}
/**
* Getter of comment.
*
* @return comment
*/
public JSONObject getComment() {
return comment;
}
}

View File

@ -0,0 +1,31 @@
package com.sismics.docs.event;
/**
* Comment delete event.
*
* @author bgamard.
*/
public class CommentDeleteEvent {
/**
* Comment ID.
*/
private String commentId;
/**
* Create a comment add event.
*
* @param commentId Comment ID
*/
public CommentDeleteEvent(String commentId) {
this.commentId = commentId;
}
/**
* Getter of commentId.
*
* @return commentId
*/
public String getCommentId() {
return commentId;
}
}

View File

@ -42,6 +42,19 @@ public class CommentResource extends BaseResource {
client.put(getApiUrl(context) + "/comment", params, responseHandler); client.put(getApiUrl(context) + "/comment", params, responseHandler);
} }
/**
* DELETE /comment/id.
*
* @param context Context
* @param commentId Comment ID
* @param responseHandler Callback
*/
public static void remove(Context context, String commentId, JsonHttpResponseHandler responseHandler) {
init(context);
client.delete(getApiUrl(context) + "/comment/" + commentId, responseHandler);
}
/** /**
* Cancel pending requests. * Cancel pending requests.
* *

View File

@ -75,6 +75,7 @@
android:layout_height="0dp" android:layout_height="0dp"
android:choiceMode="singleChoice" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:divider="@android:color/transparent"
android:transcriptMode="normal"
android:dividerHeight="0dp"/> android:dividerHeight="0dp"/>
<RelativeLayout <RelativeLayout

View File

@ -113,7 +113,10 @@
<string name="send">Send</string> <string name="send">Send</string>
<string name="add_comment">Add a comment</string> <string name="add_comment">Add a comment</string>
<string name="comment_add_failure">Error adding a comment</string> <string name="comment_add_failure">Error adding a comment</string>
<string name="adding_comment">Adding a comment...</string> <string name="adding_comment">Adding a comment</string>
<string name="comment_delete">Delete comment</string>
<string name="deleting_comment">Deleting comment</string>
<string name="error_deleting_comment">Error deleting comment</string>
</resources> </resources>