Skip to content

Commit 634ead3

Browse files
committed
Add max number of states #1
1 parent 9bc7e31 commit 634ead3

File tree

6 files changed

+68
-22
lines changed

6 files changed

+68
-22
lines changed

CHANGELOG.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# Changelog
22

3-
## `1.3.1` (14/11/10)
3+
## `1.4.0` (17/12/18)
4+
5+
- Fix NPE when replacing state before view being measured #3
6+
- Add max number of states #1
7+
8+
## `1.3.1` (14/12/18)
49

510
- Fix NPE when trying to create state bitmap #2
611

7-
## `1.3.0` (11/11/10)
12+
## `1.3.0` (11/12/18)
813

914
- Add extra methods to add states from strings.
1015
- Fix wrong type for disabled_text_color attribute.
1116

12-
## `1.2.1` (10/11/10)
17+
## `1.2.1` (10/12/18)
1318

1419
- Prevent NPE when selecting state if view not initialised.
1520

16-
## `1.2.0` (10/11/10)
21+
## `1.2.0` (10/12/18)
1722

1823
- Allow replacing state directly from a string.
1924

20-
## `1.1.0` (10/11/10)
25+
## `1.1.0` (10/12/18)
2126

2227
- Allow replacing state without styles.
2328
- Allow adding state directly from a string.
2429
- Allow not to notify listeners when selecting state.
2530

26-
## `1.0.0` (09/11/10)
31+
## `1.0.0` (09/12/18)
2732

2833
- Initial release.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add the dependency:
2525

2626
```gradle
2727
dependencies {
28-
compile 'com.github.davidmigloz:multi-state-switch:1.3.1'
28+
compile 'com.github.davidmigloz:multi-state-switch:1.4.0'
2929
}
3030
```
3131

lib/src/main/java/com/davidmiguel/multistateswitch/MultiStateSwitch.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class MultiStateSwitch extends View {
6565
private int disabledTextColor;
6666
@Dimension
6767
private int disabledTextSize;
68+
private int maxNumberStates;
6869

6970
private final Rect drawingArea = new Rect();
7071

@@ -135,6 +136,7 @@ private void processAttributes(@NonNull Context context, @Nullable AttributeSet
135136
disabledBackgroundColor = a.getColor(R.styleable.MultiStateSwitch_multistateswitch_disabled_background_color, 0);
136137
disabledTextColor = a.getColor(R.styleable.MultiStateSwitch_multistateswitch_disabled_text_color, 0);
137138
disabledTextSize = a.getDimensionPixelSize(R.styleable.MultiStateSwitch_multistateswitch_disabled_text_size, 0);
139+
maxNumberStates = a.getInt(R.styleable.MultiStateSwitch_multistateswitch_max_number_states, -1);
138140
} finally {
139141
a.recycle();
140142
}
@@ -158,9 +160,10 @@ private void configEditMode() {
158160
if (!isInEditMode()) {
159161
return;
160162
}
161-
addState(new State("ONE"));
162-
addState(new State("TWO"));
163-
addState(new State("THREE"));
163+
int numMockStates = maxNumberStates > 0 ? maxNumberStates : 3;
164+
for (int i = 1; i <= numMockStates; i++) {
165+
addStateFromString(Integer.toString(i));
166+
}
164167
}
165168

166169
/**
@@ -171,6 +174,9 @@ public void addState(@NonNull State state, @Nullable StateStyle stateStyle) {
171174
if (states == null) {
172175
createDataStructures(DEFAULT_NUM_STATES);
173176
}
177+
if (hasMaxNumberStates() && getNumberStates() >= getMaxNumberStates()) {
178+
return;
179+
}
174180
states.add(state);
175181
if (stateStyle != null) {
176182
statesStyles.put(states.size() - 1, stateStyle);
@@ -197,6 +203,9 @@ public void addStates(@NonNull List<State> states, @Nullable List<StateStyle> st
197203
createDataStructures(states.size());
198204
}
199205
for (int i = 0; i < states.size(); i++) {
206+
if (hasMaxNumberStates() && getNumberStates() >= getMaxNumberStates()) {
207+
return;
208+
}
200209
this.states.add(states.get(i));
201210
if (stateStyles != null) {
202211
statesStyles.put(states.size() - 1, stateStyles.get(i));
@@ -622,6 +631,35 @@ public int getNumberStates() {
622631
return states != null ? states.size() : 0;
623632
}
624633

634+
/**
635+
* Sets the max number of states. If you try to add a new state but the number of states is
636+
* already maxNumberStates the state will be ignored. By default is -1 which means that there
637+
* is no restriction.
638+
* This parameter is also used to determine how many states to show in the editor preview. If
639+
* it is set to no limit, 3 will be rendered by default, if not the number of states drawn will
640+
* match maxNumberStates.
641+
*/
642+
public void setMaxNumberStates(int maxNumberStates) {
643+
if (maxNumberStates == 0) {
644+
throw new IllegalArgumentException("Max number of states cannot be zero!");
645+
}
646+
this.maxNumberStates = maxNumberStates;
647+
}
648+
649+
/**
650+
* Returns max number of states. By default is -1 which means that there is no restriction.
651+
*/
652+
public int getMaxNumberStates() {
653+
return maxNumberStates;
654+
}
655+
656+
/**
657+
* Checks whether there is a limit in the number of states or not.
658+
*/
659+
public boolean hasMaxNumberStates() {
660+
return getMaxNumberStates() > 0;
661+
}
662+
625663
/**
626664
* Sets typeface.
627665
*/

lib/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<attr name="multistateswitch_disabled_background_color" format="color|reference" />
1717
<attr name="multistateswitch_disabled_text_color" format="color|reference" />
1818
<attr name="multistateswitch_disabled_text_size" format="dimension|reference" />
19+
<attr name="multistateswitch_max_number_states" format="integer|reference" />
1920

2021
</declare-styleable>
2122
</resources>

lib/src/main/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
<item name="multistateswitch_disabled_background_color">@color/multistateswitch_white</item>
1414
<item name="multistateswitch_disabled_text_color">@color/multistateswitch_water_blue</item>
1515
<item name="multistateswitch_disabled_text_size">@dimen/multistateswitch_disabled_text_size</item>
16+
<item name="multistateswitch_max_number_states">-1</item>
1617
</style>
1718
</resources>

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@
4444
android:layout_width="wrap_content"
4545
android:layout_height="55dp"
4646
android:layout_marginTop="16dp"
47-
app:multistateswitch_disabled_state_enabled="true"
4847
app:layout_constraintLeft_toLeftOf="parent"
4948
app:layout_constraintRight_toRightOf="parent"
50-
app:layout_constraintTop_toBottomOf="@id/disabledLabel" />
49+
app:layout_constraintTop_toBottomOf="@id/disabledLabel"
50+
app:multistateswitch_disabled_state_enabled="true"
51+
app:multistateswitch_max_number_states="5" />
5152

5253
<TextView
5354
android:id="@+id/customizedLabel"
@@ -66,12 +67,12 @@
6667
android:layout_marginTop="16dp"
6768
android:paddingStart="100dp"
6869
android:paddingEnd="100dp"
69-
app:multistateswitch_disabled_state_enabled="true"
70-
app:multistateswitch_disabled_state_index="1"
71-
app:multistateswitch_selected_state_index="1"
7270
app:layout_constraintLeft_toLeftOf="parent"
7371
app:layout_constraintRight_toRightOf="parent"
74-
app:layout_constraintTop_toBottomOf="@id/customizedLabel" />
72+
app:layout_constraintTop_toBottomOf="@id/customizedLabel"
73+
app:multistateswitch_disabled_state_enabled="true"
74+
app:multistateswitch_disabled_state_index="1"
75+
app:multistateswitch_selected_state_index="1" />
7576

7677
<TextView
7778
android:id="@+id/selectLabel"
@@ -88,27 +89,27 @@
8889
android:layout_width="wrap_content"
8990
android:layout_height="wrap_content"
9091
android:text="@string/cold"
91-
app:layout_constraintTop_toBottomOf="@id/selectLabel"
92+
app:layout_constraintEnd_toStartOf="@id/select2Btn"
9293
app:layout_constraintStart_toStartOf="parent"
93-
app:layout_constraintEnd_toStartOf="@id/select2Btn"/>
94+
app:layout_constraintTop_toBottomOf="@id/selectLabel" />
9495

9596
<Button
9697
android:id="@+id/select2Btn"
9798
android:layout_width="wrap_content"
9899
android:layout_height="wrap_content"
99100
android:text="@string/off"
100-
app:layout_constraintTop_toBottomOf="@id/selectLabel"
101+
app:layout_constraintEnd_toStartOf="@id/select3Btn"
101102
app:layout_constraintStart_toEndOf="@id/select1Btn"
102-
app:layout_constraintEnd_toStartOf="@id/select3Btn"/>
103+
app:layout_constraintTop_toBottomOf="@id/selectLabel" />
103104

104105
<Button
105106
android:id="@+id/select3Btn"
106107
android:layout_width="wrap_content"
107108
android:layout_height="wrap_content"
108109
android:text="@string/hot"
109-
app:layout_constraintTop_toBottomOf="@id/selectLabel"
110+
app:layout_constraintEnd_toEndOf="parent"
110111
app:layout_constraintStart_toEndOf="@id/select2Btn"
111-
app:layout_constraintEnd_toEndOf="parent"/>
112+
app:layout_constraintTop_toBottomOf="@id/selectLabel" />
112113

113114
<TextView
114115
android:id="@+id/listenerLabel"

0 commit comments

Comments
 (0)