Skip to content

Commit da1f1aa

Browse files
author
michal_szwarc
committed
Add new demo examples. Extra frames to add own content i preferences activity.
1 parent ba76dd8 commit da1f1aa

25 files changed

+376
-80
lines changed

library/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.kenumir.materialsettings">
1+
<manifest
2+
package="com.kenumir.materialsettings">
33

44
<application />
55

library/src/main/java/com/kenumir/materialsettings/MaterialSettings.java

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,130 @@
11
package com.kenumir.materialsettings;
22

33
import android.os.Bundle;
4-
import android.os.PersistableBundle;
54
import android.support.v7.app.ActionBarActivity;
65
import android.support.v7.widget.Toolbar;
76
import android.view.View;
7+
import android.widget.FrameLayout;
88
import android.widget.LinearLayout;
99

10+
import com.kenumir.materialsettings.storage.SimpleStorageInterface;
1011
import com.kenumir.materialsettings.storage.StorageInterface;
1112

1213
import java.util.HashMap;
14+
import java.util.Map;
1315

1416
/**
1517
* Created by Kenumir on 2015-03-15.
1618
*/
1719
public abstract class MaterialSettings extends ActionBarActivity {
1820

21+
public static enum ContentFrames {
22+
FRAME_TOP(0),
23+
FRAME_TOP_INSIDE(1),
24+
FRAME_BOTTOM(2),
25+
FRAME_BOTTOM_INSIDE(3);
26+
27+
private int id;
28+
29+
ContentFrames(int idx) {
30+
id = idx;
31+
}
32+
public int getValue() {
33+
return this.id;
34+
}
35+
}
36+
37+
private static String SAVE_PREFIX = "SSI_";
38+
1939
private LinearLayout material_settings_content;
2040
private Toolbar toolbar;
2141
private StorageInterface mStorageInterface;
2242
private HashMap<String, MaterialSettingsItem> items;
43+
private FrameLayout[] frames;
2344

2445
@Override
2546
public void onCreate(Bundle savedInstanceState) {
2647
super.onCreate(savedInstanceState);
2748
setContentView(R.layout.activity_settings);
2849

29-
items = new HashMap<String, MaterialSettingsItem>();
50+
items = new HashMap<>();
3051

3152
toolbar = (Toolbar) findViewById(R.id.toolbar);
3253
setSupportActionBar(toolbar);
3354

3455
material_settings_content = (LinearLayout) findViewById(R.id.material_settings_content);
56+
frames = new FrameLayout[4];
57+
frames[0] = (FrameLayout) findViewById(R.id.material_settings_top_frame);
58+
frames[1] = (FrameLayout) findViewById(R.id.material_settings_top_frame_inside);
59+
frames[2] = (FrameLayout) findViewById(R.id.material_settings_bottom_frame_inside);
60+
frames[3] = (FrameLayout) findViewById(R.id.material_settings_bottom_frame);
3561

3662
mStorageInterface = initStorageInterface();
63+
64+
if (savedInstanceState != null) {
65+
for(String key : savedInstanceState.keySet()) {
66+
if (key.startsWith(SAVE_PREFIX)) {
67+
String keyName = key.substring(SAVE_PREFIX.length());
68+
Object value = savedInstanceState.get(key);
69+
//Log.i("tests", "k: " + key + " k2: " + keyName +", t=" + value.getClass().getName() + ", v=" + value);
70+
if (value instanceof String) {
71+
mStorageInterface.save(keyName, (String) value);
72+
} else if (value instanceof Integer) {
73+
mStorageInterface.save(keyName, (Integer) value);
74+
} else if (value instanceof Float) {
75+
mStorageInterface.save(keyName, (Float) value);
76+
} else if (value instanceof Long) {
77+
mStorageInterface.save(keyName, (Long) value);
78+
} else if (value instanceof Boolean) {
79+
mStorageInterface.save(keyName, (Boolean) value);
80+
} else {
81+
mStorageInterface.save(keyName, value.toString());
82+
}
83+
}
84+
}
85+
}
86+
}
87+
88+
@Override
89+
protected void onSaveInstanceState(Bundle outState) {
90+
StorageInterface si = getStorageInterface();
91+
if (si instanceof SimpleStorageInterface) {
92+
saveAll();
93+
Map<String, ?> all = ((SimpleStorageInterface) si).getAll();
94+
if (all.size() > 0) {
95+
// save to bundle
96+
for(String key : all.keySet()) {
97+
Object value = all.get(key);
98+
if (value instanceof String) {
99+
outState.putString(SAVE_PREFIX + key, (String) value);
100+
} else if (value instanceof Integer) {
101+
outState.putInt(SAVE_PREFIX + key, (Integer) value);
102+
} else if (value instanceof Float) {
103+
outState.putFloat(SAVE_PREFIX + key, (Float) value);
104+
} else if (value instanceof Long) {
105+
outState.putString(SAVE_PREFIX + key, (String) value);
106+
} else if (value instanceof Boolean) {
107+
outState.putBoolean(SAVE_PREFIX + key, (Boolean) value);
108+
} else {
109+
outState.putString(SAVE_PREFIX + key, value.toString());
110+
}
111+
}
112+
}
113+
}
114+
super.onSaveInstanceState(outState);
115+
}
116+
117+
public FrameLayout getContentFrame(ContentFrames frame) {
118+
return frames[frame.getValue()];
119+
}
120+
121+
/**
122+
* save all settings values at StorageInterface
123+
*/
124+
public void saveAll() {
125+
for(String key : items.keySet()) {
126+
items.get(key).save();
127+
}
37128
}
38129

39130
public void addItem(MaterialSettingsItem item) {

library/src/main/java/com/kenumir/materialsettings/MaterialSettingsItem.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
*/
1313
public abstract class MaterialSettingsItem {
1414

15-
protected MaterialSettings mContext;
15+
protected Context mContext;
16+
protected MaterialSettings mMaterialSettings;
1617
protected String name;
1718

18-
public MaterialSettingsItem(MaterialSettings ctx, String name) {
19+
public MaterialSettingsItem(Context ctx, String name) {
1920
this.mContext = ctx;
21+
if (ctx instanceof MaterialSettings)
22+
this.mMaterialSettings = (MaterialSettings) ctx;
2023
this.name = name;
2124
}
2225

@@ -37,11 +40,19 @@ public View getView(ViewGroup parent) {
3740
return null;
3841
}
3942

43+
public void setMaterialSettings(MaterialSettings m) {
44+
mMaterialSettings = m;
45+
}
46+
4047
public StorageInterface getStorageInterface() {
41-
return mContext.getStorageInterface();
48+
if (mMaterialSettings != null)
49+
return mMaterialSettings.getStorageInterface();
50+
else
51+
return null;
4252
}
4353

4454
public abstract int getViewResource();
4555
public abstract void setupView(View v);
56+
public abstract void save();
4657

4758
}

library/src/main/java/com/kenumir/materialsettings/items/CheckboxItem.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.widget.CompoundButton;
66
import android.widget.TextView;
77

8-
import com.kenumir.materialsettings.MaterialSettings;
98
import com.kenumir.materialsettings.MaterialSettingsItem;
109
import com.kenumir.materialsettings.R;
1110
import com.kenumir.materialsettings.views.CheckableLinearLayout;
@@ -25,7 +24,7 @@ public static interface OnCheckedChangeListener {
2524
private CheckableLinearLayout mCheckableLinearLayout;
2625
private OnCheckedChangeListener mOnCheckedChangeListener;
2726

28-
public CheckboxItem(MaterialSettings ctx, String name) {
27+
public CheckboxItem(Context ctx, String name) {
2928
super(ctx, name);
3029
}
3130

@@ -36,7 +35,8 @@ public int getViewResource() {
3635

3736
@Override
3837
public void setupView(View v) {
39-
checked = getStorageInterface().load(name, isDefaultValue());
38+
39+
checked = getStorageInterface() != null ? getStorageInterface().load(name, isDefaultValue()) : isDefaultValue();
4040
mCheckableLinearLayout = (CheckableLinearLayout) v;
4141
titleView = (TextView) v.findViewById(R.id.material_dialog_item_title);
4242
subtitleView = (TextView) v.findViewById(R.id.material_dialog_item_subtitle);
@@ -49,13 +49,19 @@ public void setupView(View v) {
4949
mCheckableLinearLayout.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
5050
@Override
5151
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
52-
mContext.getStorageInterface().save(name, isChecked);
52+
save();
5353
if (getOnCheckedChangeListener() != null)
5454
getOnCheckedChangeListener().onCheckedChange(CheckboxItem.this, isChecked);
5555
}
5656
});
5757
}
5858

59+
@Override
60+
public void save() {
61+
if (getStorageInterface() != null)
62+
getStorageInterface().save(name, isChecked());
63+
}
64+
5965
public CheckboxItem updateTitle(String newTitle) {
6066
if (titleView != null)
6167
titleView.setText(newTitle);
@@ -89,6 +95,7 @@ public CheckboxItem setSubtitle(String subtitle) {
8995
}
9096

9197
public boolean isChecked() {
98+
//Log.d("tests", "C: " + name + "=" + mCheckableLinearLayout.isChecked());
9299
return mCheckableLinearLayout.isChecked();
93100
}
94101

library/src/main/java/com/kenumir/materialsettings/items/DividerItem.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.view.View;
55

6-
import com.kenumir.materialsettings.MaterialSettings;
76
import com.kenumir.materialsettings.MaterialSettingsItem;
87
import com.kenumir.materialsettings.R;
98

@@ -12,7 +11,7 @@
1211
*/
1312
public class DividerItem extends MaterialSettingsItem {
1413

15-
public DividerItem(MaterialSettings ctx) {
14+
public DividerItem(Context ctx) {
1615
super(ctx, null);
1716
}
1817

@@ -25,4 +24,9 @@ public int getViewResource() {
2524
public void setupView(View v) {
2625

2726
}
27+
28+
@Override
29+
public void save() {
30+
// NOP
31+
}
2832
}

library/src/main/java/com/kenumir/materialsettings/items/HeaderItem.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.view.View;
55
import android.widget.TextView;
66

7-
import com.kenumir.materialsettings.MaterialSettings;
87
import com.kenumir.materialsettings.MaterialSettingsItem;
98
import com.kenumir.materialsettings.R;
109

@@ -15,7 +14,7 @@ public class HeaderItem extends MaterialSettingsItem {
1514

1615
private String title;
1716

18-
public HeaderItem(MaterialSettings ctx) {
17+
public HeaderItem(Context ctx) {
1918
super(ctx, null);
2019
}
2120

@@ -29,6 +28,11 @@ public void setupView(View v) {
2928
((TextView) v).setText(title);
3029
}
3130

31+
@Override
32+
public void save() {
33+
// NOP
34+
}
35+
3236
public String getTitle() {
3337
return title;
3438
}

library/src/main/java/com/kenumir/materialsettings/items/SwitcherItem.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.kenumir.materialsettings.items;
22

3-
import com.kenumir.materialsettings.MaterialSettings;
3+
import android.content.Context;
4+
45
import com.kenumir.materialsettings.R;
56

67
/**
78
* Created by Kenumir on 2015-03-20.
89
*/
910
public class SwitcherItem extends CheckboxItem {
1011

11-
public SwitcherItem(MaterialSettings ctx, String name) {
12+
public SwitcherItem(Context ctx, String name) {
1213
super(ctx, name);
1314
}
1415

library/src/main/java/com/kenumir/materialsettings/items/TextItem.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package com.kenumir.materialsettings.items;
22

3+
import android.content.Context;
34
import android.view.View;
4-
import android.widget.CompoundButton;
5-
import android.widget.LinearLayout;
65
import android.widget.TextView;
76

8-
import com.kenumir.materialsettings.MaterialSettings;
97
import com.kenumir.materialsettings.MaterialSettingsItem;
108
import com.kenumir.materialsettings.R;
11-
import com.kenumir.materialsettings.views.CheckableLinearLayout;
129

1310
/**
1411
* Created by Kenumir on 2015-03-16.
@@ -24,7 +21,7 @@ public static interface OnClickListener {
2421
private OnClickListener onclick;
2522
private TextView titleView, subtitleView;
2623

27-
public TextItem(MaterialSettings ctx, String name) {
24+
public TextItem(Context ctx, String name) {
2825
super(ctx, name);
2926
}
3027

@@ -50,6 +47,11 @@ public void onClick(View v) {
5047
});
5148
}
5249

50+
@Override
51+
public void save() {
52+
// NOP
53+
}
54+
5355
public TextItem updateTitle(String newTitle) {
5456
if (titleView != null)
5557
titleView.setText(newTitle);

library/src/main/java/com/kenumir/materialsettings/storage/PreferencesStorageInterface.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55

6+
import java.util.Map;
7+
68
/**
79
* Created by Kenumir on 2015-03-18.
810
*/
@@ -15,12 +17,12 @@ public PreferencesStorageInterface(Context ctx) {
1517
}
1618

1719
@Override
18-
public void save(String key, boolean value) {
20+
public void save(String key, Boolean value) {
1921
prefs.edit().putBoolean(key, value).apply();
2022
}
2123

2224
@Override
23-
public boolean load(String key, boolean defaultValue) {
25+
public boolean load(String key, Boolean defaultValue) {
2426
return prefs.getBoolean(key, defaultValue);
2527
}
2628

@@ -63,4 +65,10 @@ public void save(String key, Long value) {
6365
public Long load(String key, Long defaultValue) {
6466
return prefs.getLong(key, defaultValue);
6567
}
68+
69+
@Override
70+
public Map<String, ?> getAll() {
71+
72+
return prefs.getAll();
73+
}
6674
}

0 commit comments

Comments
 (0)