Skip to content

Commit b028707

Browse files
author
michal_szwarc
committed
Add SwitcherItem type. Update SimpleStorageInterface. Update sample app.
1 parent 7d1e2f5 commit b028707

File tree

9 files changed

+83
-9
lines changed

9 files changed

+83
-9
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public class Settings1Activity extends MaterialSettings {
4848
</style>
4949
```
5050

51+
### StorageInterface
52+
Easiest way to save/load settings
53+
54+
* PreferencesStorageInterface - in shared preferences
55+
* SimpleStorageInterface - in memory
56+
* or write own interface to save data (sqlite, content provider or to file)
57+
5158
# Screens
5259
![Default](/screens/theme_default.png)
5360
![Rec](/screens/theme_red.png)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.kenumir.materialsettings.items;
2+
3+
import com.kenumir.materialsettings.MaterialSettings;
4+
import com.kenumir.materialsettings.R;
5+
6+
/**
7+
* Created by Kenumir on 2015-03-20.
8+
*/
9+
public class SwitcherItem extends CheckboxItem {
10+
11+
public SwitcherItem(MaterialSettings ctx, String name) {
12+
super(ctx, name);
13+
}
14+
15+
@Override
16+
public int getViewResource() {
17+
return R.layout.item_switcher;
18+
}
19+
20+
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,50 @@ public boolean load(String key, boolean defaultValue) {
2727

2828
@Override
2929
public void save(String key, String value) {
30-
30+
mem.put(key, String.valueOf(value));
3131
}
3232

3333
@Override
3434
public String load(String key, String defaultValue) {
35-
return null;
35+
if (mem.containsKey(key))
36+
return (String) mem.get(key);
37+
return defaultValue;
3638
}
3739

3840
@Override
3941
public void save(String key, Integer value) {
40-
42+
mem.put(key, Integer.valueOf(value));
4143
}
4244

4345
@Override
4446
public Integer load(String key, Integer defaultValue) {
45-
return null;
47+
if (mem.containsKey(key))
48+
return (Integer) mem.get(key);
49+
return defaultValue;
4650
}
4751

4852
@Override
4953
public void save(String key, Float value) {
50-
54+
mem.put(key, Float.valueOf(value));
5155
}
5256

5357
@Override
5458
public Float load(String key, Float defaultValue) {
55-
return null;
59+
if (mem.containsKey(key))
60+
return (Float) mem.get(key);
61+
return defaultValue;
5662
}
5763

5864
@Override
5965
public void save(String key, Long value) {
60-
66+
mem.put(key, Long.valueOf(value));
6167
}
6268

6369
@Override
6470
public Long load(String key, Long defaultValue) {
65-
return null;
71+
if (mem.containsKey(key))
72+
return (Long) mem.get(key);
73+
return defaultValue;
6674
}
6775

6876
}

library/src/main/res/layout/item_checkbox.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
android:layout_height="match_parent"
2727
android:gravity="center">
2828

29-
<android.support.v7.widget.SwitchCompat
29+
<CheckBox
3030
style="@style/Settings_Item_CheckBox"/>
3131

3232
</LinearLayout>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.kenumir.materialsettings.views.CheckableLinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
style="?attr/msItem">
5+
6+
<RelativeLayout
7+
android:layout_width="0dp"
8+
android:layout_height="match_parent"
9+
android:orientation="vertical"
10+
android:layout_weight="1"
11+
android:gravity="center_vertical">
12+
13+
<TextView
14+
android:id="@+id/material_dialog_item_title"
15+
style="?attr/msTextStyle"/>
16+
17+
<TextView
18+
android:id="@+id/material_dialog_item_subtitle"
19+
style="?attr/msSubTextStyle"
20+
android:layout_below="@+id/material_dialog_item_title"/>
21+
22+
</RelativeLayout>
23+
24+
<LinearLayout
25+
android:layout_width="wrap_content"
26+
android:layout_height="match_parent"
27+
android:gravity="center">
28+
29+
<android.support.v7.widget.SwitchCompat
30+
style="@style/Settings_Item_CheckBox"/>
31+
32+
</LinearLayout>
33+
34+
</com.kenumir.materialsettings.views.CheckableLinearLayout>

sampleapp/src/main/java/com/wt/sampleapp/Settings1Activity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.kenumir.materialsettings.items.CheckboxItem;
1919
import com.kenumir.materialsettings.items.DividerItem;
2020
import com.kenumir.materialsettings.items.HeaderItem;
21+
import com.kenumir.materialsettings.items.SwitcherItem;
2122
import com.kenumir.materialsettings.items.TextItem;
2223
import com.kenumir.materialsettings.storage.PreferencesStorageInterface;
2324
import com.kenumir.materialsettings.storage.SimpleStorageInterface;
@@ -37,6 +38,10 @@ public void onCheckedChange(CheckboxItem cbi, boolean isChecked) {
3738
}
3839
}));
3940
addItem(new DividerItem(this));
41+
addItem(new SwitcherItem(this, "key1a").setTitle("Switcher item 3 - no subtitle"));
42+
addItem(new DividerItem(this));
43+
addItem(new SwitcherItem(this, "key1b").setTitle("Switcher item 3").setSubtitle("With subtitle"));
44+
addItem(new DividerItem(this));
4045
addItem(new CheckboxItem(this, "key2").setTitle("Checkbox item 2").setSubtitle("Subtitle text 2 with long text and more txt and more and more ;-)").setDefaultValue(true));
4146
addItem(new DividerItem(this));
4247
addItem(new CheckboxItem(this, "key1").setTitle("Checkbox item 3 - no subtitle"));

screens/theme_dark.png

1.74 KB
Loading

screens/theme_default.png

5.35 KB
Loading

screens/theme_red.png

625 Bytes
Loading

0 commit comments

Comments
 (0)