From a762ce4715f924aaaebf80c8305eb77aca910d3e Mon Sep 17 00:00:00 2001 From: jendib Date: Fri, 5 Dec 2014 00:12:42 +0100 Subject: [PATCH] Android: Update view after an editing --- .../docs/activity/DocumentEditActivity.java | 54 +++++++++++++++---- .../docs/activity/DocumentViewActivity.java | 43 +++++++++++---- .../sismics/docs/adapter/DocListAdapter.java | 26 +++++++++ .../sismics/docs/adapter/LanguageAdapter.java | 6 ++- .../sismics/docs/event/DocumentEditEvent.java | 33 ++++++++++++ .../docs/fragment/DocListFragment.java | 10 ++++ 6 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 docs-android/app/src/main/java/com/sismics/docs/event/DocumentEditEvent.java diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java index bf6e0f10..889d7010 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java @@ -2,6 +2,7 @@ package com.sismics.docs.activity; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -11,6 +12,7 @@ import android.widget.Spinner; import com.sismics.docs.R; import com.sismics.docs.adapter.LanguageAdapter; import com.sismics.docs.adapter.TagAutoCompleteAdapter; +import com.sismics.docs.event.DocumentEditEvent; import com.sismics.docs.ui.view.DatePickerView; import com.sismics.docs.ui.view.TagsCompleteTextView; import com.sismics.docs.util.PreferenceUtil; @@ -23,6 +25,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import de.greenrobot.event.EventBus; + /** * Document edition activity. * @@ -34,6 +38,13 @@ public class DocumentEditActivity extends ActionBarActivity { */ JSONObject document; + // View cache + private EditText titleEditText; + private EditText descriptionEditText; + private TagsCompleteTextView tagsEditText; + private Spinner languageSpinner; + private DatePickerView datePickerView; + @Override protected void onCreate(Bundle args) { super.onCreate(args); @@ -59,9 +70,13 @@ public class DocumentEditActivity extends ActionBarActivity { setContentView(R.layout.document_edit_activity); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); + languageSpinner = (Spinner) findViewById(R.id.languageSpinner); + tagsEditText = (TagsCompleteTextView) findViewById(R.id.tagsEditText); + datePickerView = (DatePickerView) findViewById(R.id.dateEditText); + titleEditText = (EditText) findViewById(R.id.titleEditText); + descriptionEditText = (EditText) findViewById(R.id.descriptionEditText); // Language spinner - Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner); LanguageAdapter languageAdapter = new LanguageAdapter(this); languageSpinner.setAdapter(languageAdapter); @@ -78,27 +93,18 @@ public class DocumentEditActivity extends ActionBarActivity { tagList.add(tagArray.optJSONObject(i)); } - TagsCompleteTextView tagsEditText = (TagsCompleteTextView) findViewById(R.id.tagsEditText); tagsEditText.allowDuplicates(false); tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList)); // Fill the activity - DatePickerView datePickerView = (DatePickerView) findViewById(R.id.dateEditText); if (document == null) { datePickerView.setDate(new Date()); } else { setTitle(R.string.edit_document); - - EditText titleEditText = (EditText) findViewById(R.id.titleEditText); titleEditText.setText(document.optString("title")); - - EditText descriptionEditText = (EditText) findViewById(R.id.descriptionEditText); descriptionEditText.setText(document.optString("description")); - datePickerView.setDate(new Date(document.optLong("create_date"))); - languageSpinner.setSelection(languageAdapter.getItemPosition(document.optString("language"))); - JSONArray documentTags = document.optJSONArray("tags"); for (int i = 0; i < documentTags.length(); i++) { tagsEditText.addObject(documentTags.optJSONObject(i)); @@ -117,6 +123,34 @@ public class DocumentEditActivity extends ActionBarActivity { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.save: + JSONObject outputDoc = new JSONObject(); + try { + if (document != null) { + outputDoc.putOpt("id", document.optString("id")); + outputDoc.putOpt("shared", document.optBoolean("shared")); + } + outputDoc.putOpt("title", titleEditText.getText().toString()); + outputDoc.putOpt("description", descriptionEditText.getText().toString()); + if (languageSpinner.getSelectedItem() != null) { + LanguageAdapter.Language language = (LanguageAdapter.Language) languageSpinner.getSelectedItem(); + outputDoc.putOpt("language", language.getId()); + } + if (datePickerView.getDate() != null) { + outputDoc.putOpt("create_date", datePickerView.getDate().getTime()); + } + JSONArray tags = new JSONArray(); + for (Object object : tagsEditText.getObjects()) { + if (object instanceof JSONObject) { + tags.put(object); + } + } + outputDoc.putOpt("tags", tags); + } catch (JSONException e) { + Log.e(DocumentEditActivity.class.getSimpleName(), "Error building JSON for document", e); + } + + EventBus.getDefault().post(new DocumentEditEvent(outputDoc)); + setResult(RESULT_OK); finish(); return true; 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 40df90b6..570b8d5b 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 @@ -21,6 +21,7 @@ import android.widget.TextView; import com.sismics.docs.R; import com.sismics.docs.adapter.FilePagerAdapter; +import com.sismics.docs.event.DocumentEditEvent; import com.sismics.docs.event.DocumentFullscreenEvent; import com.sismics.docs.fragment.DocShareFragment; import com.sismics.docs.listener.JsonHttpResponseHandler; @@ -44,6 +45,16 @@ import de.greenrobot.event.EventBus; * @author bgamard */ public class DocumentViewActivity extends ActionBarActivity { + /** + * Request code of adding file. + */ + public static final int REQUEST_CODE_ADD_FILE = 1; + + /** + * Request code of editing document. + */ + public static final int REQUEST_CODE_EDIT_DOCUMENT = 2; + /** * File view pager. */ @@ -95,7 +106,7 @@ public class DocumentViewActivity extends ActionBarActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); - // Grab the document + // Fill the view refreshDocument(document); EventBus.getDefault().register(this); @@ -134,6 +145,7 @@ public class DocumentViewActivity extends ActionBarActivity { if (description == null || description.isEmpty()) { descriptionTextView.setVisibility(View.GONE); } else { + descriptionTextView.setVisibility(View.VISIBLE); descriptionTextView.setText(description); } @@ -141,6 +153,7 @@ public class DocumentViewActivity extends ActionBarActivity { if (tags.length() == 0) { tagTextView.setVisibility(View.GONE); } else { + tagTextView.setVisibility(View.VISIBLE); tagTextView.setText(TagUtil.buildSpannable(tags)); } @@ -203,13 +216,13 @@ public class DocumentViewActivity extends ActionBarActivity { Intent intent = new Intent(Intent.ACTION_GET_CONTENT) .setType("*/*") .putExtra("android.intent.extra.ALLOW_MULTIPLE", true); - startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), 1); + startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), REQUEST_CODE_ADD_FILE); return true; case R.id.edit: intent = new Intent(this, DocumentEditActivity.class); - intent.putExtra("document", getIntent().getStringExtra("document")); - startActivityForResult(intent, 2); + intent.putExtra("document", document.toString()); + startActivityForResult(intent, REQUEST_CODE_EDIT_DOCUMENT); return true; case android.R.id.home: @@ -221,11 +234,6 @@ public class DocumentViewActivity extends ActionBarActivity { } } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // TODO Reload the current document from data after document edition - } - /** * Download the current displayed file. */ @@ -277,10 +285,27 @@ public class DocumentViewActivity extends ActionBarActivity { downloadManager.enqueue(request); } + /** + * A document fullscreen event has been fired. + * + * @param event Document fullscreen event + */ public void onEvent(DocumentFullscreenEvent event) { findViewById(R.id.detailLayout).setVisibility(event.isFullscreen() ? View.GONE : View.VISIBLE); } + /** + * A document edit event has been fired. + * + * @param event Document edit event + */ + public void onEvent(DocumentEditEvent event) { + if (event.getDocument().optString("id").equals(document.optString("id"))) { + // The current document has been modified, refresh it + refreshDocument(event.getDocument()); + } + } + @Override protected void onDestroy() { EventBus.getDefault().unregister(this); diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java index 13d3555f..139bfb37 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/DocListAdapter.java @@ -2,6 +2,7 @@ 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; @@ -12,6 +13,7 @@ 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.Date; @@ -45,7 +47,11 @@ public class DocListAdapter extends RecyclerView.Adapter