diff --git a/README.md b/README.md
index c48a28e..3d5f737 100644
--- a/README.md
+++ b/README.md
@@ -17,9 +17,9 @@ You can optionally use the dependency `lyra-coder-gson` if you want to include t
```gradle
dependencies {
- compile 'com.github.fondesa:lyra:1.0.0'
+ compile 'com.github.fondesa:lyra:1.0.1'
// Use this dependency if you want to include the Gson coder.
- compile 'com.github.fondesa:lyra-coder-gson:1.0.0'
+ compile 'com.github.fondesa:lyra-coder-gson:1.0.1'
}
```
@@ -29,7 +29,7 @@ dependencies {
com.github.fondesalyra
- 1.0.0
+ 1.0.1pom
```
@@ -118,6 +118,25 @@ public class MainActivity extends Activity {
}
```
+The save/restore of the state is supported also in a custom `View`. For example:
+
+```java
+public class AutoSaveEditText extends AppCompatEditText {
+ @SaveState
+ CharSequence mText;
+
+ @Override
+ public Parcelable onSaveInstanceState() {
+ return Lyra.instance().saveState(this, super.onSaveInstanceState());
+ }
+
+ @Override
+ public void onRestoreInstanceState(Parcelable state) {
+ super.onRestoreInstanceState(Lyra.instance().restoreState(this, state));
+ }
+}
+```
+
As shown above, you can create your own custom `StateCoder`. For example, this coder will save/restore a `String` in `Base64`:
```java
diff --git a/bintray-deploy.properties b/bintray-deploy.properties
index ca8650c..63ad279 100644
--- a/bintray-deploy.properties
+++ b/bintray-deploy.properties
@@ -6,5 +6,5 @@ BINTRAY_LIB_ISSUE_TRACKER_URL=https://github.com/Fondesa/Lyra/issues
BINTRAY_LIB_GIT_URL=https://github.com/Fondesa/Lyra.git
BINTRAY_LIB_GITHUB_REPO=Fondesa/Lyra
BINTRAY_LIB_TAGS=android|lyra|saveinstancestate|bundle
-BINTRAY_LIB_VERSION=1.0.0
+BINTRAY_LIB_VERSION=1.0.1
BINTRAY_LIB_VERSION_DESCRIPTION=For further information check: https://github.com/Fondesa/Lyra
\ No newline at end of file
diff --git a/lyra/src/main/java/com/fondesa/lyra/Lyra.java b/lyra/src/main/java/com/fondesa/lyra/Lyra.java
index 2f20973..92596c6 100644
--- a/lyra/src/main/java/com/fondesa/lyra/Lyra.java
+++ b/lyra/src/main/java/com/fondesa/lyra/Lyra.java
@@ -22,10 +22,12 @@
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
+import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
+import android.view.View;
import com.fondesa.lyra.annotation.SaveState;
import com.fondesa.lyra.coder.CoderRetriever;
@@ -54,6 +56,7 @@
*/
public class Lyra {
public static final String SUB_BUNDLE_KEY = "lyra:";
+ public static final String VIEW_SUPER_STATE_BUNDLE_KEY = "view:superState";
private static final String TAG = Lyra.class.getSimpleName();
private Application mApplication;
@@ -128,7 +131,7 @@ public static String getKeyFromField(@NonNull Field field) {
/**
* Save the annotated fields' state of a class into a {@link Bundle}.
- * The fields of a class will be retrieved through a {@link FieldsRetriever}.
+ * The fields of the class will be retrieved through a {@link FieldsRetriever}.
* The coder to serialize the fields into a {@link Bundle} will be retrieved through a {@link CoderRetriever}.
*
* @param stateHolder instance of the class with annotated fields
@@ -182,7 +185,7 @@ public void saveState(@NonNull Object stateHolder, @NonNull Bundle state) {
/**
* Restore the annotated fields' state of a class from a {@link Bundle}.
- * The fields of a class will be retrieved through a {@link FieldsRetriever}.
+ * The fields of the class will be retrieved through a {@link FieldsRetriever}.
* The coder to deserialize the fields from a {@link Bundle} will be retrieved through a {@link CoderRetriever}.
*
* @param stateHolder instance of the class with annotated fields
@@ -230,6 +233,47 @@ public void restoreState(@NonNull Object stateHolder, @Nullable Bundle state) {
}
}
+ /**
+ * Save the annotated fields' state of a {@link View} class into a {@link Bundle}.
+ * The {@link Bundle} will contain the parcelable state of the {@link View}
+ * with the key {@link #VIEW_SUPER_STATE_BUNDLE_KEY}.
+ * The fields of the class will be retrieved through a {@link FieldsRetriever}.
+ * The coder to serialize the fields into a {@link Bundle} will be retrieved through a {@link CoderRetriever}.
+ *
+ * @param stateHolder instance of the class with annotated fields
+ * @param state {@link Bundle} in which you want to save the annotated fields
+ * @return {@link Bundle} containing view state and Lyra sub-bundle
+ */
+ public Parcelable saveState(@NonNull View stateHolder, @Nullable Parcelable state) {
+ Bundle wrapper = new Bundle();
+ // Put the original super state.
+ wrapper.putParcelable(VIEW_SUPER_STATE_BUNDLE_KEY, state);
+ // Save the state into the Lyra Bundle.
+ saveState((Object) stateHolder, wrapper);
+ return wrapper;
+ }
+
+ /**
+ * Restore the annotated fields' state of a {@link View} class from a {@link Parcelable}.
+ * If the state is a {@link Bundle}, the {@link View}'s state and the fields' values will be restored.
+ * The fields of the class will be retrieved through a {@link FieldsRetriever}.
+ * The coder to deserialize the fields from a {@link Bundle} will be retrieved through a {@link CoderRetriever}.
+ *
+ * @param stateHolder instance of the class with annotated fields
+ * @param state {@link Bundle} from which you want to restore the value of the annotated fields
+ * @return {@link Parcelable} containing {@link View}'s saved state
+ */
+ public Parcelable restoreState(@NonNull View stateHolder, @Nullable Parcelable state) {
+ if (state instanceof Bundle) {
+ Bundle wrapper = (Bundle) state;
+ // Get the original super state.
+ state = wrapper.getParcelable(VIEW_SUPER_STATE_BUNDLE_KEY);
+ // Restore the state saved in the Lyra Bundle.
+ restoreState((Object) stateHolder, wrapper);
+ }
+ return state;
+ }
+
@NonNull
private static SaveState getSaveStateAnnotation(@NonNull Field field) {
SaveState saveState = field.getAnnotation(SaveState.class);
diff --git a/lyra/src/test/java/com/fondesa/lyra/LyraTest.java b/lyra/src/test/java/com/fondesa/lyra/LyraTest.java
index 6af8ba5..87fdae9 100644
--- a/lyra/src/test/java/com/fondesa/lyra/LyraTest.java
+++ b/lyra/src/test/java/com/fondesa/lyra/LyraTest.java
@@ -17,7 +17,9 @@
package com.fondesa.lyra;
import android.app.Application;
+import android.content.Context;
import android.os.Bundle;
+import android.os.Parcelable;
import android.support.annotation.NonNull;
import com.fondesa.lyra.annotation.SaveState;
@@ -32,6 +34,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.ApplicationTestUtil;
import java.lang.reflect.Field;
@@ -41,6 +44,7 @@
import io.github.benas.randombeans.EnhancedRandomBuilder;
import io.github.benas.randombeans.api.EnhancedRandom;
+import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
@@ -92,11 +96,11 @@ public void testKeyFromFieldNotNull() {
@Test
public void testSaveState() throws Exception {
- final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever();
- final Bundle stateBundle = new Bundle();
final TestModels.SubClassAllModifiersAnnotatedFields saveStateObject =
new TestModels.SubClassAllModifiersAnnotatedFields();
+ final Bundle stateBundle = new Bundle();
+ final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever();
Field[] fields = retriever.getFields(saveStateObject.getClass());
for (Field field : fields) {
new FieldAccessibleRunner(field) {
@@ -125,14 +129,13 @@ public void runWithField(@NonNull Field field) throws Exception {
@Test
public void testRestoreState() throws Exception {
- final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever();
- final DefaultCoderRetriever coderRetriever = new DefaultCoderRetriever();
- final Bundle stateBundle = new Bundle();
- final Bundle lyraBundle = new Bundle();
-
final TestModels.SubClassAllModifiersAnnotatedFields saveStateObject =
new TestModels.SubClassAllModifiersAnnotatedFields();
+ final Bundle stateBundle = new Bundle();
+ final Bundle lyraBundle = new Bundle();
+ final DefaultFieldsRetriever retriever = new DefaultFieldsRetriever();
+ final DefaultCoderRetriever coderRetriever = new DefaultCoderRetriever();
Field[] fields = retriever.getFields(saveStateObject.getClass());
List