Android: file delete

This commit is contained in:
jendib 2015-01-14 23:31:53 +01:00
parent c9210c39c4
commit 6fa0b8494e
7 changed files with 157 additions and 4 deletions

View File

@ -162,7 +162,7 @@ public class DocumentEditActivity extends ActionBarActivity {
// Cancellable progress dialog
final ProgressDialog progressDialog = ProgressDialog.show(this,
getString(R.string.document_editing_title),
getString(R.string.please_wait),
getString(R.string.document_editing_message), true, true,
new DialogInterface.OnCancelListener() {
@Override

View File

@ -30,6 +30,7 @@ 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.event.FileDeleteEvent;
import com.sismics.docs.fragment.DocShareFragment;
import com.sismics.docs.listener.JsonHttpResponseHandler;
import com.sismics.docs.model.application.ApplicationContext;
@ -205,7 +206,7 @@ public class DocumentViewActivity extends ActionBarActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.document_activity, menu);
inflater.inflate(R.menu.document_view_activity, menu);
return super.onCreateOptionsMenu(menu);
}
@ -239,6 +240,10 @@ public class DocumentViewActivity extends ActionBarActivity {
startActivityForResult(intent, REQUEST_CODE_EDIT_DOCUMENT);
return true;
case R.id.delete_file:
deleteCurrentFile();
return true;
case R.id.delete_document:
deleteDocument();
return true;
@ -273,6 +278,62 @@ public class DocumentViewActivity extends ActionBarActivity {
downloadFile(fileUrl, fileName, getTitle().toString(), getString(R.string.downloading_file, position + 1));
}
private void deleteCurrentFile() {
if (fileViewPager == null || filePagerAdapter == null) return;
final JSONObject file = filePagerAdapter.getObjectAt(fileViewPager.getCurrentItem());
if (file == null) return;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.delete_file_title)
.setMessage(R.string.delete_file_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.please_wait),
getString(R.string.file_deleting_message), true, true,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
FileResource.cancel(DocumentViewActivity.this);
}
});
// Actual delete server call
final String fileId = file.optString("id");
FileResource.delete(DocumentViewActivity.this, fileId, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
EventBus.getDefault().post(new FileDeleteEvent(fileId));
}
@Override
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
Toast.makeText(DocumentViewActivity.this, R.string.file_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();
}
/**
* Download the document (all files zipped).
*/
@ -321,7 +382,7 @@ public class DocumentViewActivity extends ActionBarActivity {
// Show a progress dialog while deleting
final ProgressDialog progressDialog = ProgressDialog.show(DocumentViewActivity.this,
getString(R.string.document_editing_title),
getString(R.string.please_wait),
getString(R.string.document_deleting_message), true, true,
new DialogInterface.OnCancelListener() {
@Override
@ -394,6 +455,16 @@ public class DocumentViewActivity extends ActionBarActivity {
}
}
/**
* A file delete event has been fired.
*
* @param event File delete event
*/
public void onEvent(FileDeleteEvent event) {
if (filePagerAdapter == null) return;
filePagerAdapter.remove(event.getFileId());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (document == null) return;

View File

@ -103,4 +103,22 @@ public class FilePagerAdapter extends PagerAdapter {
return files.optJSONObject(position);
}
/**
* Remove a file.
*
* @param fileId File ID
*/
public void remove(String fileId) {
if (files == null || fileId == null) return;
for (int i = 0; i < files.length(); i++) {
JSONObject file = files.optJSONObject(i);
if (fileId.equals(file.optString("id"))) {
files.remove(i);
notifyDataSetChanged();
break;
}
}
}
}

View File

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

View File

@ -30,6 +30,19 @@ public class FileResource extends BaseResource {
client.get(getApiUrl(context) + "/file/list?id=" + documentId, responseHandler);
}
/**
* DELETE /file/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) + "/file/" + id, responseHandler);
}
/**
* PUT /file.
*
@ -57,4 +70,13 @@ public class FileResource extends BaseResource {
params.put("file", is, "file", "application/octet-stream", true);
client.put(getApiUrl(context) + "/file", params, responseHandler);
}
/**
* Cancel pending requests.
*
* @param context Context
*/
public static void cancel(Context context) {
client.cancelRequests(context, true);
}
}

View File

@ -34,6 +34,12 @@
android:title="@string/download_document">
</item>
<item
android:id="@+id/delete_file"
app:showAsAction="collapseActionView"
android:title="@string/delete_file">
</item>
<item
android:id="@+id/delete_document"
app:showAsAction="collapseActionView"

View File

@ -77,17 +77,22 @@
<string name="save">Save</string>
<string name="edit_document">Edit document</string>
<string name="error_editing_document">Network error, please try again</string>
<string name="document_editing_title">Please wait</string>
<string name="please_wait">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>
<string name="document_deleting_message">Deleting document</string>
<string name="delete_file_title">Delete file</string>
<string name="delete_file_message">Really delete the current file?</string>
<string name="file_delete_failure">Network error while deleting the current file</string>
<string name="file_deleting_message">Deleting file</string>
<string name="error_reading_file">Error while reading the file</string>
<string name="upload_notification_title">Sismics Docs</string>
<string name="upload_notification_message">Uploading the new file to the document</string>
<string name="upload_notification_error">Error uploading the new file</string>
<string name="delete_file">Delete current file</string>
</resources>