Skip to content

Commit 95d9097

Browse files
author
xiaoqi
committed
update for custom view
1 parent adccf44 commit 95d9097

8 files changed

Lines changed: 153 additions & 14 deletions

File tree

sample/build.gradle

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66

77
defaultConfig {
88
minSdkVersion 14
9-
targetSdkVersion 25
9+
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"
1212

@@ -22,17 +22,17 @@ android {
2222
}
2323

2424
dependencies {
25-
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
2626
compile fileTree(include: ['*.jar'], dir: 'libs')
2727
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2828
exclude group: 'com.android.support', module: 'support-annotations'
2929
})
30-
implementation 'com.android.support:appcompat-v7:26.1.0'
30+
implementation 'com.android.support:appcompat-v7:26.0.1'
3131
testImplementation 'junit:junit:4.12'
32-
implementation 'com.noober:savehelper:2.1.0'
33-
annotationProcessor 'com.noober:processor:2.1.0'
34-
implementation 'com.noober:savehelper-api:2.1.0'
35-
// implementation project(':savehelper')
36-
// implementation project(':savehelper-api')
37-
// annotationProcessor project(':savehelper-processor')
32+
// implementation 'com.noober:savehelper:2.1.0'
33+
// annotationProcessor 'com.noober:processor:2.1.0'
34+
// implementation 'com.noober:savehelper-api:2.1.0'
35+
implementation project(':savehelper')
36+
implementation project(':savehelper-api')
37+
annotationProcessor project(':savehelper-processor')
3838
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.recover.autosavesample;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.os.Parcelable;
6+
import android.support.annotation.Nullable;
7+
import android.util.AttributeSet;
8+
import android.util.Log;
9+
import android.util.SparseArray;
10+
import android.view.View;
11+
12+
import com.noober.api.NeedSave;
13+
import com.noober.savehelper.SaveHelper;
14+
15+
/**
16+
* Created by xiaoqi on 18/6/21
17+
*/
18+
public class CustomView extends View {
19+
20+
@NeedSave
21+
int a;
22+
23+
public CustomView(Context context) {
24+
super(context);
25+
}
26+
27+
public CustomView(Context context, @Nullable AttributeSet attrs) {
28+
super(context, attrs);
29+
}
30+
31+
32+
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
33+
super(context, attrs, defStyleAttr);
34+
}
35+
36+
37+
@Nullable
38+
@Override
39+
protected Parcelable onSaveInstanceState() {
40+
Log.e("CustomView", "onSaveInstanceState");
41+
return super.onSaveInstanceState();
42+
}
43+
44+
@Override
45+
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
46+
47+
SaveHelper.save(this, container);
48+
a = 2;
49+
50+
Bundle bundle = new Bundle();
51+
bundle.putInt("A", a);
52+
container.put(1, bundle);
53+
super.dispatchSaveInstanceState(container);
54+
Log.e("CustomView", "dispatchSaveInstanceState");
55+
}
56+
57+
@Override
58+
protected void onRestoreInstanceState(Parcelable state) {
59+
super.onRestoreInstanceState(state);
60+
Log.e("CustomView", "onRestoreInstanceState");
61+
}
62+
63+
@Override
64+
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
65+
super.dispatchRestoreInstanceState(container);
66+
SaveHelper.recover(this, container);
67+
Bundle bundle = (Bundle) container.get(1);
68+
a = bundle.getInt("A");
69+
Log.e("CustomView", "dispatchRestoreInstanceState");
70+
}
71+
}

sample/src/main/java/com/recover/autosavesample/SampleActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class SampleActivity extends AppCompatActivity {
6464
// long[] longArray;
6565
// @NeedSave
6666
// Size size;
67-
@NeedSave
67+
@NeedSave
6868
SizeF sizeFS;
6969

7070
@NeedSave

sample/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
android:paddingRight="@dimen/activity_horizontal_margin"
1111
android:paddingTop="@dimen/activity_vertical_margin"
1212
tools:context="com.recover.autosavesample.SampleActivity">
13-
13+
<com.recover.autosavesample.CustomView
14+
android:id="@+id/view"
15+
android:layout_width="match_parent"
16+
android:layout_height="match_parent" />
1417
</RelativeLayout>

savehelper-processor/src/main/java/com/noober/processor/AutoSaveProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
7777
private HelperClass getHelperClass(Element element) {
7878
TypeElement encloseElement = (TypeElement) element.getEnclosingElement();
7979
String fullClassName = encloseElement.getQualifiedName().toString();
80-
HelperClass annotatedClass = mHelperClassMap.get(fullClassName);
80+
HelperClass annotatedClass = mHelperClassMap.get(fullClassName);
8181
if (annotatedClass == null) {
8282
annotatedClass = new HelperClass(encloseElement, elementUtils, messager);
8383
mHelperClassMap.put(fullClassName, annotatedClass);

savehelper/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77

88
defaultConfig {
99
minSdkVersion 9
10-
targetSdkVersion 26
10+
targetSdkVersion 27
1111
versionCode 1
1212
versionName "1.0"
1313

@@ -31,7 +31,7 @@ dependencies {
3131
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
3232
exclude group: 'com.android.support', module: 'support-annotations'
3333
})
34-
// provided 'com.android.support:appcompat-v7:+'
34+
// provided 'com.android.support:appcompat-v7:+'
3535
testImplementation 'junit:junit:4.12'
3636
}
3737

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.noober.savehelper;
2+
3+
4+
import android.os.Parcelable;
5+
import android.util.SparseArray;
6+
7+
public interface ISaveViewStateHelper<T> {
8+
9+
void save(T clazz, SparseArray<Parcelable> container);
10+
void recover(T clazz, SparseArray<Parcelable> container);
11+
}

savehelper/src/main/java/com/noober/savehelper/SaveHelper.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.noober.savehelper;
22

33
import android.os.Bundle;
4+
import android.os.Parcelable;
45
import android.os.PersistableBundle;
6+
import android.util.SparseArray;
57

68
import java.util.HashMap;
79
import java.util.Map;
810

911
public class SaveHelper {
1012
private static Map<String,ISaveInstanceStateHelper> helperCache = new HashMap<>();
1113

14+
private static Map<String,ISaveViewStateHelper> viewHelperCache = new HashMap<>();
15+
1216
private final static String HELPER_END = "_SaveStateHelper";
1317

1418
/**
@@ -71,6 +75,37 @@ public static <T> void save(T save, Bundle outState, PersistableBundle persisten
7175
}
7276
}
7377

78+
79+
/**
80+
* added while need to save data, used in custom view
81+
*
82+
* @param save current custom view
83+
* @param container SparseArray<Parcelable>
84+
*/
85+
public static <T> void save(T save, SparseArray<Parcelable> container){
86+
if(container != null){
87+
ISaveViewStateHelper<T> viewSaveHelper = findViewSaveHelper(save);
88+
if(viewSaveHelper != null){
89+
viewSaveHelper.save(save, container);
90+
}
91+
}
92+
}
93+
94+
/**
95+
* recover for custom view
96+
*
97+
* @param save current custom view
98+
* @param container SparseArray<Parcelable>
99+
*/
100+
public static <T> void recover(T save, SparseArray<Parcelable> container){
101+
if(container != null){
102+
ISaveViewStateHelper<T> viewSaveHelper = findViewSaveHelper(save);
103+
if(viewSaveHelper != null){
104+
viewSaveHelper.recover(save, container);
105+
}
106+
}
107+
}
108+
74109
private static <T> ISaveInstanceStateHelper<T> findSaveHelper(T cl) {
75110
String clazz = cl.getClass().getName();
76111
ISaveInstanceStateHelper<T> saveInstanceStateHelper = helperCache.get(clazz);
@@ -91,4 +126,23 @@ private static <T> ISaveInstanceStateHelper<T> findSaveHelper(T cl) {
91126
return saveInstanceStateHelper;
92127
}
93128

129+
private static <T> ISaveViewStateHelper<T> findViewSaveHelper(T cl) {
130+
String clazz = cl.getClass().getName();
131+
ISaveViewStateHelper<T> saveViewStateHelper = viewHelperCache.get(clazz);
132+
if(saveViewStateHelper == null){
133+
try {
134+
Class<?> findClass = Class.forName(clazz + HELPER_END);
135+
saveViewStateHelper = (ISaveViewStateHelper<T>)findClass.newInstance();
136+
viewHelperCache.put(clazz,saveViewStateHelper);
137+
} catch (ClassNotFoundException e) {
138+
// ignore
139+
} catch (InstantiationException e) {
140+
e.printStackTrace();
141+
} catch (IllegalAccessException e) {
142+
e.printStackTrace();
143+
}
144+
}
145+
return saveViewStateHelper;
146+
}
147+
94148
}

0 commit comments

Comments
 (0)