From 89d66eca4a18c4e8827b6ecf4a11f734429370a9 Mon Sep 17 00:00:00 2001 From: jendib Date: Thu, 4 Dec 2014 22:32:53 +0100 Subject: [PATCH] Android: Document edit activity filling --- .../docs/activity/DocumentEditActivity.java | 61 +++++++++++++++++- .../docs/activity/DocumentViewActivity.java | 7 ++ .../sismics/docs/adapter/LanguageAdapter.java | 16 +++++ .../docs/fragment/DocListFragment.java | 5 ++ .../sismics/docs/ui/view/DatePickerView.java | 27 +++++++- .../docs/ui/view/DividerItemDecoration.java | 5 ++ .../drawable-xhdpi/ic_create_white_24dp.png | Bin 0 -> 378 bytes .../res/drawable-xhdpi/ic_done_white_24dp.png | Bin 0 -> 363 bytes .../drawable-xxhdpi/ic_create_white_24dp.png | Bin 0 -> 490 bytes .../drawable-xxhdpi/ic_done_white_24dp.png | Bin 0 -> 476 bytes .../src/main/res/menu/document_activity.xml | 7 ++ .../main/res/menu/document_edit_activity.xml | 12 ++++ .../app/src/main/res/values/strings.xml | 2 + 13 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 docs-android/app/src/main/res/drawable-xhdpi/ic_create_white_24dp.png create mode 100644 docs-android/app/src/main/res/drawable-xhdpi/ic_done_white_24dp.png create mode 100644 docs-android/app/src/main/res/drawable-xxhdpi/ic_create_white_24dp.png create mode 100644 docs-android/app/src/main/res/drawable-xxhdpi/ic_done_white_24dp.png create mode 100644 docs-android/app/src/main/res/menu/document_edit_activity.xml 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 90b7ed53..bf6e0f10 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,19 +2,25 @@ package com.sismics.docs.activity; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; +import android.widget.EditText; 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.ui.view.DatePickerView; import com.sismics.docs.ui.view.TagsCompleteTextView; import com.sismics.docs.util.PreferenceUtil; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -23,6 +29,11 @@ import java.util.List; * @author bgamard. */ public class DocumentEditActivity extends ActionBarActivity { + /** + * Document edited. + */ + JSONObject document; + @Override protected void onCreate(Bundle args) { super.onCreate(args); @@ -33,14 +44,28 @@ public class DocumentEditActivity extends ActionBarActivity { 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 setContentView(R.layout.document_edit_activity); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); + // Language spinner 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); if (tags == null) { finish(); @@ -56,11 +81,45 @@ public class DocumentEditActivity extends ActionBarActivity { 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)); + } + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.document_edit_activity, menu); + return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.save: + finish(); + return true; + case android.R.id.home: 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 dec36ddb..40df90b6 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 @@ -206,6 +206,12 @@ public class DocumentViewActivity extends ActionBarActivity { startActivityForResult(Intent.createChooser(intent, getText(R.string.upload_from)), 1); 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: finish(); return true; @@ -217,6 +223,7 @@ 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 } /** diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/LanguageAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/LanguageAdapter.java index fe0d76e1..65891530 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/LanguageAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/LanguageAdapter.java @@ -67,6 +67,22 @@ public class LanguageAdapter extends BaseAdapter { 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. */ diff --git a/docs-android/app/src/main/java/com/sismics/docs/fragment/DocListFragment.java b/docs-android/app/src/main/java/com/sismics/docs/fragment/DocListFragment.java index 17099d06..3919677a 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/fragment/DocListFragment.java +++ b/docs-android/app/src/main/java/com/sismics/docs/fragment/DocListFragment.java @@ -133,6 +133,11 @@ public class DocListFragment extends Fragment { 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 public void onDestroyView() { EventBus.getDefault().unregister(this); diff --git a/docs-android/app/src/main/java/com/sismics/docs/ui/view/DatePickerView.java b/docs-android/app/src/main/java/com/sismics/docs/ui/view/DatePickerView.java index 974aa9e0..83475819 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/ui/view/DatePickerView.java +++ b/docs-android/app/src/main/java/com/sismics/docs/ui/view/DatePickerView.java @@ -2,15 +2,25 @@ package com.sismics.docs.ui.view; import android.app.DatePickerDialog; import android.content.Context; +import android.text.format.DateFormat; import android.util.AttributeSet; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +/** + * Date picker widget. + * + * @author bgamard + */ +public class DatePickerView extends TextView implements DatePickerDialog.OnDateSetListener { + + private Date date; -public class DatePickerView extends TextView implements DatePickerDialog.OnDateSetListener{ - public DatePickerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @@ -41,6 +51,17 @@ public class DatePickerView extends TextView implements DatePickerDialog.OnDateS @Override 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; } } \ No newline at end of file diff --git a/docs-android/app/src/main/java/com/sismics/docs/ui/view/DividerItemDecoration.java b/docs-android/app/src/main/java/com/sismics/docs/ui/view/DividerItemDecoration.java index 98b61596..b65b4ea2 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/ui/view/DividerItemDecoration.java +++ b/docs-android/app/src/main/java/com/sismics/docs/ui/view/DividerItemDecoration.java @@ -10,6 +10,11 @@ import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; +/** + * Divider item decoration for recycler view. + * + * @author bgamard + */ public class DividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; diff --git a/docs-android/app/src/main/res/drawable-xhdpi/ic_create_white_24dp.png b/docs-android/app/src/main/res/drawable-xhdpi/ic_create_white_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..7f0ea51bf608501cafe4a27f7387a8458505faa5 GIT binary patch literal 378 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA0wn)(8}a}tg=CK)Uj~LMH3o);76yi2K%s^g z3=E|}g|8AA7_4S6Fo+k-*%fF5lweBoc6VX;-`;;_Kaj^+;1OBOz`!jG!i)^F=12eq zKY6-1hD02Gd;K)$AqSDxhYh7GX2cz7y~F}I4okhJ31{w9syt6I zFaG|YeX_TQR>*0;&<3U_XZZMQ3J+&VE^urAXCk)nG&@J?Jr#=wJpV0Ns$IpKcW5<8 zwJUsl)+i-wXTjEC&HYDyt?!nI;sXW6HH(h6-Bv0$Jl-;WKG%G-7RFl{?y;ytFI zpSs^u)z9s2uzAn)X%F{_&pQuf=h^Wqe6nPi>d%~F$9e&zY5M*%Od8MF#f4XFO!+Mg P3LpkgS3j3^P6E|0lOYGlJRm!Dr)-hSfD z?Qn+?wG?C$wW<_AyxmJ6P|!{gJx$_;8aeqL~i?wC2B|NN8Igy+fL zte&4gZtY0004R> z004l5008;`004mK004C`008P>0026e000+ooVrmw00002VoOIv0RM-N%)bBt010qN zS#tmY3ljhU3ljkVnw%H_00Cl2L_t(&-tF44Q3Npz1<<{O+ie6w+F_`LA_y1)>M(F@ zO#~1?`U#kuIaph^bo=EEBsK76Y)e1zfMHub1SmiO3J|Mz*lhZa0*QNMcNNm!|5D4O2OrRct47kN2WY~KcU(7- zKKP*H%W5Otvq7g1y-rHF;DY|o9hsE!i;V+3LDM_hlo%l72 z)~p-k2OM zh|bZ0bev}kM+0)Dtspl}8_1E<0&?ZlK_d>HoEpfTBZG9Dmo+#Ns0*hI>clC5 zx^eQLj+{KGBPS2)$T<&0;G6@ZaO|L04#x_jacm$W#{!~q^dK@v3!-y$AP0^H004R> z004l5008;`004mK004C`008P>0026e000+ooVrmw00002VoOIv0RM-N%)bBt010qN zS#tmY3ljhU3ljkVnw%H_00C4DBC$cp1JSmVFgEun6hb3zUqZPtq#Y<~|gB;`_2ek$`$UzQz4$$VYK-+K# z5*AQguX zQgYa!Ud!Qv3OGzq5r+pV + + + + + + + + + \ No newline at end of file diff --git a/docs-android/app/src/main/res/values/strings.xml b/docs-android/app/src/main/res/values/strings.xml index 07724f3a..901f7512 100644 --- a/docs-android/app/src/main/res/values/strings.xml +++ b/docs-android/app/src/main/res/values/strings.xml @@ -74,6 +74,8 @@ French English Japanese + Save + Edit document