Android: Update view after an editing

This commit is contained in:
jendib 2014-12-05 00:12:42 +01:00
parent 89d66eca4a
commit a762ce4715
6 changed files with 152 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package com.sismics.docs.activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
@ -11,6 +12,7 @@ import android.widget.Spinner;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.LanguageAdapter; import com.sismics.docs.adapter.LanguageAdapter;
import com.sismics.docs.adapter.TagAutoCompleteAdapter; 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.DatePickerView;
import com.sismics.docs.ui.view.TagsCompleteTextView; import com.sismics.docs.ui.view.TagsCompleteTextView;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
@ -23,6 +25,8 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import de.greenrobot.event.EventBus;
/** /**
* Document edition activity. * Document edition activity.
* *
@ -34,6 +38,13 @@ public class DocumentEditActivity extends ActionBarActivity {
*/ */
JSONObject document; JSONObject document;
// View cache
private EditText titleEditText;
private EditText descriptionEditText;
private TagsCompleteTextView tagsEditText;
private Spinner languageSpinner;
private DatePickerView datePickerView;
@Override @Override
protected void onCreate(Bundle args) { protected void onCreate(Bundle args) {
super.onCreate(args); super.onCreate(args);
@ -59,9 +70,13 @@ public class DocumentEditActivity extends ActionBarActivity {
setContentView(R.layout.document_edit_activity); setContentView(R.layout.document_edit_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(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 // Language spinner
Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner);
LanguageAdapter languageAdapter = new LanguageAdapter(this); LanguageAdapter languageAdapter = new LanguageAdapter(this);
languageSpinner.setAdapter(languageAdapter); languageSpinner.setAdapter(languageAdapter);
@ -78,27 +93,18 @@ public class DocumentEditActivity extends ActionBarActivity {
tagList.add(tagArray.optJSONObject(i)); tagList.add(tagArray.optJSONObject(i));
} }
TagsCompleteTextView tagsEditText = (TagsCompleteTextView) findViewById(R.id.tagsEditText);
tagsEditText.allowDuplicates(false); tagsEditText.allowDuplicates(false);
tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList)); tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList));
// Fill the activity // Fill the activity
DatePickerView datePickerView = (DatePickerView) findViewById(R.id.dateEditText);
if (document == null) { if (document == null) {
datePickerView.setDate(new Date()); datePickerView.setDate(new Date());
} else { } else {
setTitle(R.string.edit_document); setTitle(R.string.edit_document);
EditText titleEditText = (EditText) findViewById(R.id.titleEditText);
titleEditText.setText(document.optString("title")); titleEditText.setText(document.optString("title"));
EditText descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);
descriptionEditText.setText(document.optString("description")); descriptionEditText.setText(document.optString("description"));
datePickerView.setDate(new Date(document.optLong("create_date"))); datePickerView.setDate(new Date(document.optLong("create_date")));
languageSpinner.setSelection(languageAdapter.getItemPosition(document.optString("language"))); languageSpinner.setSelection(languageAdapter.getItemPosition(document.optString("language")));
JSONArray documentTags = document.optJSONArray("tags"); JSONArray documentTags = document.optJSONArray("tags");
for (int i = 0; i < documentTags.length(); i++) { for (int i = 0; i < documentTags.length(); i++) {
tagsEditText.addObject(documentTags.optJSONObject(i)); tagsEditText.addObject(documentTags.optJSONObject(i));
@ -117,6 +123,34 @@ public class DocumentEditActivity extends ActionBarActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.save: 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(); finish();
return true; return true;

View File

@ -21,6 +21,7 @@ import android.widget.TextView;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.FilePagerAdapter; import com.sismics.docs.adapter.FilePagerAdapter;
import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.event.DocumentFullscreenEvent; import com.sismics.docs.event.DocumentFullscreenEvent;
import com.sismics.docs.fragment.DocShareFragment; import com.sismics.docs.fragment.DocShareFragment;
import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.listener.JsonHttpResponseHandler;
@ -44,6 +45,16 @@ import de.greenrobot.event.EventBus;
* @author bgamard * @author bgamard
*/ */
public class DocumentViewActivity extends ActionBarActivity { 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. * File view pager.
*/ */
@ -95,7 +106,7 @@ public class DocumentViewActivity extends ActionBarActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
// Grab the document // Fill the view
refreshDocument(document); refreshDocument(document);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
@ -134,6 +145,7 @@ public class DocumentViewActivity extends ActionBarActivity {
if (description == null || description.isEmpty()) { if (description == null || description.isEmpty()) {
descriptionTextView.setVisibility(View.GONE); descriptionTextView.setVisibility(View.GONE);
} else { } else {
descriptionTextView.setVisibility(View.VISIBLE);
descriptionTextView.setText(description); descriptionTextView.setText(description);
} }
@ -141,6 +153,7 @@ public class DocumentViewActivity extends ActionBarActivity {
if (tags.length() == 0) { if (tags.length() == 0) {
tagTextView.setVisibility(View.GONE); tagTextView.setVisibility(View.GONE);
} else { } else {
tagTextView.setVisibility(View.VISIBLE);
tagTextView.setText(TagUtil.buildSpannable(tags)); tagTextView.setText(TagUtil.buildSpannable(tags));
} }
@ -203,13 +216,13 @@ public class DocumentViewActivity extends ActionBarActivity {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT) Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
.setType("*/*") .setType("*/*")
.putExtra("android.intent.extra.ALLOW_MULTIPLE", true); .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; return true;
case R.id.edit: case R.id.edit:
intent = new Intent(this, DocumentEditActivity.class); intent = new Intent(this, DocumentEditActivity.class);
intent.putExtra("document", getIntent().getStringExtra("document")); intent.putExtra("document", document.toString());
startActivityForResult(intent, 2); startActivityForResult(intent, REQUEST_CODE_EDIT_DOCUMENT);
return true; return true;
case android.R.id.home: 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. * Download the current displayed file.
*/ */
@ -277,10 +285,27 @@ public class DocumentViewActivity extends ActionBarActivity {
downloadManager.enqueue(request); downloadManager.enqueue(request);
} }
/**
* A document fullscreen event has been fired.
*
* @param event Document fullscreen event
*/
public void onEvent(DocumentFullscreenEvent event) { public void onEvent(DocumentFullscreenEvent event) {
findViewById(R.id.detailLayout).setVisibility(event.isFullscreen() ? View.GONE : View.VISIBLE); 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 @Override
protected void onDestroy() { protected void onDestroy() {
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);

View File

@ -2,6 +2,7 @@ package com.sismics.docs.adapter;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -12,6 +13,7 @@ import com.sismics.docs.R;
import com.sismics.docs.util.TagUtil; import com.sismics.docs.util.TagUtil;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Date; import java.util.Date;
@ -45,7 +47,11 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
} }
} }
/**
* Default constructor.
*/
public DocListAdapter() { public DocListAdapter() {
// Nothing
} }
@Override @Override
@ -117,4 +123,24 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
notifyDataSetChanged(); notifyDataSetChanged();
} }
/**
* Update a document.
*
* @param document Document
*/
public void updateDocument(JSONObject document) {
for (int i = 0; i < documents.length(); i++) {
JSONObject currentDoc = documents.optJSONObject(i);
if (currentDoc.optString("id").equals(document.optString("id"))) {
// This document has been modified
try {
documents.put(i, document);
notifyDataSetChanged();
} catch (JSONException e) {
Log.e(DocListAdapter.class.getSimpleName(), "Error while updating a document", e);
}
}
}
}
} }

View File

@ -86,7 +86,7 @@ public class LanguageAdapter extends BaseAdapter {
/** /**
* A language. * A language.
*/ */
private static class Language { public static class Language {
private String id; private String id;
private int name; private int name;
private int drawable; private int drawable;
@ -103,5 +103,9 @@ public class LanguageAdapter extends BaseAdapter {
this.name = name; this.name = name;
this.drawable = drawable; this.drawable = drawable;
} }
public String getId() {
return id;
}
} }
} }

View File

@ -0,0 +1,33 @@
package com.sismics.docs.event;
import org.json.JSONObject;
/**
* Document edit event.
*
* @author bgamard.
*/
public class DocumentEditEvent {
/**
* Document.
*/
private JSONObject document;
/**
* Create a document edit event.
*
* @param document Document
*/
public DocumentEditEvent(JSONObject document) {
this.document = document;
}
/**
* Getter of document.
*
* @return document
*/
public JSONObject getDocument() {
return document;
}
}

View File

@ -17,6 +17,7 @@ import com.sismics.docs.R;
import com.sismics.docs.activity.DocumentEditActivity; import com.sismics.docs.activity.DocumentEditActivity;
import com.sismics.docs.activity.DocumentViewActivity; import com.sismics.docs.activity.DocumentViewActivity;
import com.sismics.docs.adapter.DocListAdapter; import com.sismics.docs.adapter.DocListAdapter;
import com.sismics.docs.event.DocumentEditEvent;
import com.sismics.docs.event.SearchEvent; import com.sismics.docs.event.SearchEvent;
import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.listener.JsonHttpResponseHandler;
import com.sismics.docs.listener.RecyclerItemClickListener; import com.sismics.docs.listener.RecyclerItemClickListener;
@ -154,6 +155,15 @@ public class DocListFragment extends Fragment {
loadDocuments(getView(), true); loadDocuments(getView(), true);
} }
/**
* A document edit event has been fired.
*
* @param event Document edit event
*/
public void onEvent(DocumentEditEvent event) {
adapter.updateDocument(event.getDocument());
}
/** /**
* Refresh the document list. * Refresh the document list.
* *