Android: Document deleting

This commit is contained in:
jendib 2014-12-13 16:27:27 +01:00
parent dd59172a19
commit 5befef2992
8 changed files with 174 additions and 18 deletions

View File

@ -21,7 +21,6 @@ public class MainApplication extends Application {
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
// TODO Fullscreen preview
// TODO Document deleting
// TODO Files adding/deleting
super.onCreate();

View File

@ -1,7 +1,10 @@
package com.sismics.docs.activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
@ -18,15 +21,19 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.sismics.docs.R;
import com.sismics.docs.adapter.FilePagerAdapter;
import com.sismics.docs.event.DocumentDeleteEvent;
import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.event.DocumentFullscreenEvent;
import com.sismics.docs.fragment.DocShareFragment;
import com.sismics.docs.listener.JsonHttpResponseHandler;
import com.sismics.docs.model.application.ApplicationContext;
import com.sismics.docs.resource.DocumentResource;
import com.sismics.docs.resource.FileResource;
import com.sismics.docs.util.DialogUtil;
import com.sismics.docs.util.PreferenceUtil;
import com.sismics.docs.util.TagUtil;
@ -227,6 +234,10 @@ public class DocumentViewActivity extends ActionBarActivity {
startActivityForResult(intent, REQUEST_CODE_EDIT_DOCUMENT);
return true;
case R.id.delete_document:
deleteDocument();
return true;
case android.R.id.home:
finish();
return true;
@ -287,6 +298,62 @@ public class DocumentViewActivity extends ActionBarActivity {
downloadManager.enqueue(request);
}
/**
* Delete the current document.
*/
private void deleteDocument() {
if (document == null) return;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.delete_document_title)
.setMessage(R.string.delete_document_message)
.setCancelable(true)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Dismiss the confirmation dialog
dialog.dismiss();
// Show a progress dialog while deleting
final ProgressDialog progressDialog = ProgressDialog.show(DocumentViewActivity.this,
getString(R.string.document_editing_title),
getString(R.string.document_editing_message), true, true,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
DocumentResource.cancel(DocumentViewActivity.this);
}
});
// Actual delete server call
final String documentId = document.optString("id");
DocumentResource.delete(DocumentViewActivity.this, documentId, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
EventBus.getDefault().post(new DocumentDeleteEvent(documentId));
}
@Override
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
Toast.makeText(DocumentViewActivity.this, R.string.document_delete_failure, Toast.LENGTH_LONG).show();
}
@Override
public void onFinish() {
progressDialog.dismiss();
}
});
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
/**
* A document fullscreen event has been fired.
*
@ -302,12 +369,26 @@ public class DocumentViewActivity extends ActionBarActivity {
* @param event Document edit event
*/
public void onEvent(DocumentEditEvent event) {
if (document == null) return;
if (event.getDocument().optString("id").equals(document.optString("id"))) {
// The current document has been modified, refresh it
refreshDocument(event.getDocument());
}
}
/**
* A document delete event has been fired.
*
* @param event Document delete event
*/
public void onEvent(DocumentDeleteEvent event) {
if (document == null) return;
if (event.getDocumentId().equals(document.optString("id"))) {
// The current document has been deleted, close this activity
finish();
}
}
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);

View File

@ -2,7 +2,6 @@ package com.sismics.docs.adapter;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,10 +12,11 @@ import com.sismics.docs.R;
import com.sismics.docs.util.TagUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Adapter of documents.
@ -27,7 +27,7 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
/**
* Displayed documents.
*/
private JSONArray documents;
private List<JSONObject> documents;
/**
* ViewHolder.
@ -64,7 +64,7 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
JSONObject document = documents.optJSONObject(position);
JSONObject document = documents.get(position);
holder.titleTextView.setText(document.optString("title"));
@ -82,7 +82,7 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
if (documents == null) {
return 0;
}
return documents.length();
return documents.size();
}
/**
@ -96,14 +96,14 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
return null;
}
return documents.optJSONObject(position);
return documents.get(position);
}
/**
* Clear the documents.
*/
public void clearDocuments() {
documents = new JSONArray();
documents = new ArrayList<>();
notifyDataSetChanged();
}
@ -114,11 +114,11 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
*/
public void addDocuments(JSONArray documents) {
if (this.documents == null) {
this.documents = new JSONArray();
this.documents = new ArrayList<>();
}
for (int i = 0; i < documents.length(); i++) {
this.documents.put(documents.optJSONObject(i));
this.documents.add(documents.optJSONObject(i));
}
notifyDataSetChanged();
@ -130,17 +130,29 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
* @param document Document
*/
public void updateDocument(JSONObject document) {
for (int i = 0; i < documents.length(); i++) {
JSONObject currentDoc = documents.optJSONObject(i);
for (int i = 0; i < documents.size(); i++) {
JSONObject currentDoc = documents.get(i);
if (currentDoc.optString("id").equals(document.optString("id"))) {
// This document has been modified
try {
documents.put(i, document);
documents.set(i, document);
notifyDataSetChanged();
} catch (JSONException e) {
Log.e(DocListAdapter.class.getSimpleName(), "Error while updating a document", e);
}
}
}
/**
* Delete a document.
*
* @param documentId Document ID
*/
public void deleteDocument(String documentId) {
for (int i = 0; i < documents.size(); i++) {
JSONObject currentDoc = documents.get(i);
if (currentDoc.optString("id").equals(documentId)) {
// This document has been deleted
documents.remove(i);
notifyDataSetChanged();
}
}
}
}

View File

@ -0,0 +1,31 @@
package com.sismics.docs.event;
/**
* Document delete event.
*
* @author bgamard.
*/
public class DocumentDeleteEvent {
/**
* Document ID.
*/
private String documentId;
/**
* Create a document delete event.
*
* @param documentId Document ID
*/
public DocumentDeleteEvent(String documentId) {
this.documentId = documentId;
}
/**
* Getter of documentId.
*
* @return documentId
*/
public String getDocumentId() {
return documentId;
}
}

View File

@ -18,6 +18,7 @@ import com.sismics.docs.activity.DocumentEditActivity;
import com.sismics.docs.activity.DocumentViewActivity;
import com.sismics.docs.adapter.DocListAdapter;
import com.sismics.docs.event.DocumentAddEvent;
import com.sismics.docs.event.DocumentDeleteEvent;
import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.event.SearchEvent;
import com.sismics.docs.listener.JsonHttpResponseHandler;
@ -163,6 +164,15 @@ public class DocListFragment extends Fragment {
adapter.updateDocument(event.getDocument());
}
/**
* A document delete event has been fired.
*
* @param event Document delete event
*/
public void onEvent(DocumentDeleteEvent event) {
adapter.deleteDocument(event.getDocumentId());
}
/**
* A document add event has been fired.
*

View File

@ -46,6 +46,19 @@ public class DocumentResource extends BaseResource {
client.get(getApiUrl(context) + "/document/" + id, responseHandler);
}
/**
* DELETE /document/id.
*
* @param context Context
* @param id ID
* @param responseHandler Callback
*/
public static void delete(Context context, String id, JsonHttpResponseHandler responseHandler) {
init(context);
client.delete(getApiUrl(context) + "/document/" + id, responseHandler);
}
/**
* PUT /document.
*

View File

@ -34,4 +34,10 @@
android:title="@string/download_document">
</item>
<item
android:id="@+id/delete_document"
app:showAsAction="collapseActionView"
android:title="@string/delete_document">
</item>
</menu>

View File

@ -79,6 +79,10 @@
<string name="error_editing_document">Network error, please try again</string>
<string name="document_editing_title">Please wait</string>
<string name="document_editing_message">Sending your data</string>
<string name="delete_document">Delete document</string>
<string name="delete_document_title">Delete document</string>
<string name="delete_document_message">Really delete this document and all associated files?</string>
<string name="document_delete_failure">Network error while deleting this document</string>
</resources>