From 25136bc1468f201634d99e142acd99b7665196ef Mon Sep 17 00:00:00 2001 From: jendib Date: Sat, 9 May 2015 23:34:43 +0200 Subject: [PATCH] Refactor, TODO --- .../docs/activity/DocumentViewActivity.java | 59 ++++++++++++------- .../docs/rest/resource/DocumentResource.java | 8 ++- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java index 00abd2b0..f927e9ae 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java @@ -71,17 +71,22 @@ public class DocumentViewActivity extends AppCompatActivity { /** * File view pager. */ - ViewPager fileViewPager; + private ViewPager fileViewPager; /** * File pager adapter. */ - FilePagerAdapter filePagerAdapter; + private FilePagerAdapter filePagerAdapter; /** * Document displayed. */ - JSONObject document; + private JSONObject document; + + /** + * Menu. + */ + private Menu menu; @Override protected void onCreate(final Bundle args) { @@ -181,29 +186,16 @@ public class DocumentViewActivity extends AppCompatActivity { // Grab the attached files updateFiles(); + + // Grab the full document (used for ACLs and writable status) + updateDocument(); } @Override public boolean onCreateOptionsMenu(final Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.document_view_activity, menu); - - // Silently get the document to know if it is writable by the current user - // If this call fails or is slow and the document is read-only, - // write actions will be allowed and will fail - DocumentResource.get(this, document.optString("id"), new JsonHttpResponseHandler() { - @Override - public void onSuccess(int statusCode, Header[] headers, JSONObject response) { - boolean writable = response.optBoolean("writable"); - - menu.findItem(R.id.share).setVisible(writable); - menu.findItem(R.id.upload_file).setVisible(writable); - menu.findItem(R.id.edit).setVisible(writable); - menu.findItem(R.id.delete_file).setVisible(writable); - menu.findItem(R.id.delete_document).setVisible(writable); - } - }); - + this.menu = menu; return super.onCreateOptionsMenu(menu); } @@ -510,6 +502,33 @@ public class DocumentViewActivity extends AppCompatActivity { } } + /** + * Update the document model. + */ + private void updateDocument() { + if (document == null) return; + + // Silently get the document to know if it is writable by the current user + // If this call fails or is slow and the document is read-only, + // write actions will be allowed and will fail + DocumentResource.get(this, document.optString("id"), new JsonHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + boolean writable = response.optBoolean("writable"); + + if (menu != null) { + menu.findItem(R.id.share).setVisible(writable); + menu.findItem(R.id.upload_file).setVisible(writable); + menu.findItem(R.id.edit).setVisible(writable); + menu.findItem(R.id.delete_file).setVisible(writable); + menu.findItem(R.id.delete_document).setVisible(writable); + } + + // TODO Show the ACLs in a sliding panel from the right + } + }); + } + /** * Refresh files list. */ diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java index 3e876c99..af836c23 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java @@ -103,7 +103,7 @@ public class DocumentResource extends BaseResource { document.put("description", documentDb.getDescription()); document.put("create_date", documentDb.getCreateDate().getTime()); document.put("language", documentDb.getLanguage()); - document.put("creator", userDao.getById(documentDb.getUserId()).getUsername()); + // TODO Add "shared" and "file_count" -> rewrite the query in SQL if (principal.isAnonymous()) { // No tags in anonymous mode (sharing) @@ -123,6 +123,10 @@ public class DocumentResource extends BaseResource { document.put("tags", tags); } + // Below is specific to GET /document/id + + document.put("creator", userDao.getById(documentDb.getUserId()).getUsername()); + // Add ACL List aclDtoList = aclDao.getBySourceId(documentId); List aclList = new ArrayList<>(); @@ -190,8 +194,8 @@ public class DocumentResource extends BaseResource { document.put("title", documentDto.getTitle()); document.put("description", documentDto.getDescription()); document.put("create_date", documentDto.getCreateTimestamp()); - document.put("shared", documentDto.getShared()); document.put("language", documentDto.getLanguage()); + document.put("shared", documentDto.getShared()); document.put("file_count", documentDto.getFileCount()); // Get tags added by the current user on this document