Android: Document edit activity filling

This commit is contained in:
jendib 2014-12-04 22:32:53 +01:00
parent 323b95ad7a
commit 89d66eca4a
13 changed files with 138 additions and 4 deletions

View File

@ -2,19 +2,25 @@ 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.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Spinner; 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.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;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -23,6 +29,11 @@ import java.util.List;
* @author bgamard. * @author bgamard.
*/ */
public class DocumentEditActivity extends ActionBarActivity { public class DocumentEditActivity extends ActionBarActivity {
/**
* Document edited.
*/
JSONObject document;
@Override @Override
protected void onCreate(Bundle args) { protected void onCreate(Bundle args) {
super.onCreate(args); super.onCreate(args);
@ -33,14 +44,28 @@ public class DocumentEditActivity extends ActionBarActivity {
return; return;
} }
// Parse input document
String documentJson = getIntent().getStringExtra("document");
if (documentJson != null) {
try {
document = new JSONObject(documentJson);
} catch (JSONException e) {
finish();
return;
}
}
// Setup the activity // Setup the activity
setContentView(R.layout.document_edit_activity); setContentView(R.layout.document_edit_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
// Language spinner
Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner); Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner);
languageSpinner.setAdapter(new LanguageAdapter(this)); LanguageAdapter languageAdapter = new LanguageAdapter(this);
languageSpinner.setAdapter(languageAdapter);
// Tags auto-complete
JSONObject tags = PreferenceUtil.getCachedJson(this, PreferenceUtil.PREF_CACHED_TAGS_JSON); JSONObject tags = PreferenceUtil.getCachedJson(this, PreferenceUtil.PREF_CACHED_TAGS_JSON);
if (tags == null) { if (tags == null) {
finish(); finish();
@ -56,11 +81,45 @@ public class DocumentEditActivity extends ActionBarActivity {
TagsCompleteTextView tagsEditText = (TagsCompleteTextView) findViewById(R.id.tagsEditText); 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
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));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.document_edit_activity, menu);
return super.onCreateOptionsMenu(menu);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.save:
finish();
return true;
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;

View File

@ -206,6 +206,12 @@ public class DocumentViewActivity extends ActionBarActivity {
startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), 1); startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), 1);
return true; return true;
case R.id.edit:
intent = new Intent(this, DocumentEditActivity.class);
intent.putExtra("document", getIntent().getStringExtra("document"));
startActivityForResult(intent, 2);
return true;
case android.R.id.home: case android.R.id.home:
finish(); finish();
return true; return true;
@ -217,6 +223,7 @@ public class DocumentViewActivity extends ActionBarActivity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Reload the current document from data after document edition
} }
/** /**

View File

@ -67,6 +67,22 @@ public class LanguageAdapter extends BaseAdapter {
return view; return view;
} }
/**
* Return the position of a language.
* 0 if it doesn't exists.
*
* @param languageId Language ID
* @return Position
*/
public int getItemPosition(String languageId) {
for (Language language : languageList) {
if (language.id.equals(languageId)) {
return languageList.indexOf(language);
}
}
return 0;
}
/** /**
* A language. * A language.
*/ */

View File

@ -133,6 +133,11 @@ public class DocListFragment extends Fragment {
return view; return view;
} }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Reload the documents after document creation and open it from data
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);

View File

@ -2,14 +2,24 @@ package com.sismics.docs.ui.view;
import android.app.DatePickerDialog; import android.app.DatePickerDialog;
import android.content.Context; import android.content.Context;
import android.text.format.DateFormat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.DatePicker; import android.widget.DatePicker;
import android.widget.TextView; import android.widget.TextView;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DatePickerView extends TextView implements DatePickerDialog.OnDateSetListener{ /**
* Date picker widget.
*
* @author bgamard
*/
public class DatePickerView extends TextView implements DatePickerDialog.OnDateSetListener {
private Date date;
public DatePickerView(Context context, AttributeSet attrs, int defStyle) { public DatePickerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
@ -41,6 +51,17 @@ public class DatePickerView extends TextView implements DatePickerDialog.OnDateS
@Override @Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setText(String.format("%s/%s/%s", monthOfYear, dayOfMonth, year)); Date date = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();
setDate(date);
}
public void setDate(Date date) {
this.date = date;
String formattedDate = DateFormat.getDateFormat(getContext()).format(date);
setText(formattedDate);
}
public Date getDate() {
return date;
} }
} }

View File

@ -10,6 +10,11 @@ import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
/**
* Divider item decoration for recycler view.
*
* @author bgamard
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration { public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider; private Drawable mDivider;

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 490 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 476 B

View File

@ -2,6 +2,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/edit"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_create_white_24dp"
android:title="@string/edit_document">
</item>
<item <item
android:id="@+id/upload_file" android:id="@+id/upload_file"
app:showAsAction="ifRoom" app:showAsAction="ifRoom"

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/save"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_done_white_24dp"
android:title="@string/save">
</item>
</menu>

View File

@ -74,6 +74,8 @@
<string name="language_french">French</string> <string name="language_french">French</string>
<string name="language_english">English</string> <string name="language_english">English</string>
<string name="language_japanese">Japanese</string> <string name="language_japanese">Japanese</string>
<string name="save">Save</string>
<string name="edit_document">Edit document</string>
</resources> </resources>