mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 05:57:57 +01:00
Android: Update view after an editing
This commit is contained in:
parent
89d66eca4a
commit
a762ce4715
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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<DocListAdapter.ViewHold
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public DocListAdapter() {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -117,4 +123,24 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -86,7 +86,7 @@ public class LanguageAdapter extends BaseAdapter {
|
||||
/**
|
||||
* A language.
|
||||
*/
|
||||
private static class Language {
|
||||
public static class Language {
|
||||
private String id;
|
||||
private int name;
|
||||
private int drawable;
|
||||
@ -103,5 +103,9 @@ public class LanguageAdapter extends BaseAdapter {
|
||||
this.name = name;
|
||||
this.drawable = drawable;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@ import com.sismics.docs.R;
|
||||
import com.sismics.docs.activity.DocumentEditActivity;
|
||||
import com.sismics.docs.activity.DocumentViewActivity;
|
||||
import com.sismics.docs.adapter.DocListAdapter;
|
||||
import com.sismics.docs.event.DocumentEditEvent;
|
||||
import com.sismics.docs.event.SearchEvent;
|
||||
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
||||
import com.sismics.docs.listener.RecyclerItemClickListener;
|
||||
@ -154,6 +155,15 @@ public class DocListFragment extends Fragment {
|
||||
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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user