Skip to content

Commit

Permalink
ValueChangedListener & new functions added
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkin Çakar committed Dec 19, 2016
1 parent 612cd0e commit 6ecc0f9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 16 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Download

Download [the latest JAR][2] or grab via Gradle:
```groovy
compile 'com.travijuu.numberpicker:numberpicker:1.0.4'
compile 'com.travijuu.numberpicker:numberpicker:1.0.5'
```
or Maven:
```xml
<dependency>
<groupId>com.travijuu.numberpicker</groupId>
<artifactId>numberpicker</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```

Expand Down Expand Up @@ -139,27 +139,47 @@ Getters/Setters
- getMax()
- setUnit(int value)
- getUnit()
- setValue(int value)
- getValue()

Useful functions
--------
- setValue(int value)
- increment()
- increment(int unit)
- decrement()
- decrement(int unit)

Listeners
---------
- setLimitExceededListener(LimitExceededListener limitExceededListener)

this informs you when you try to set lower or higer than the given min/max limits
**LimitExceededListener** is triggered when you try to set lower or higher than the given min/max limits

```java
public class DefaultLimitExceededListener implements LimitExceededListener {

public void limitExceeded(int limit, int exceededValue) {

String message = String.format("Value cannot be setted to %d because the limit is %d.", limit, exceededValue);
String message = String.format("NumberPicker cannot set to %d because the limit is %d.", exceededValue, limit);
Log.v(this.getClass().getSimpleName(), message);
}
}
```

- setValueChangedListener(ValueChangedListener valueChangedListener)

**ValueChangedListener** is triggered when the NumberPicker is incremented or decremented.

*Note:* `setValue`method will not trigger this listener.

```java
public class DefaultValueChangedListener implements ValueChangedListener {

public void valueChanged(int value, ActionEnum action) {

String actionText = action == ActionEnum.INCREMENT ? "incremented" : "decremented";
String message = String.format("NumberPicker is %s to %d", actionText, value);
Log.v(this.getClass().getSimpleName(), message);
}
}
```
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# org.gradle.parallel=true

#VERSION_NAME=1.0.3-SNAPSHOT
VERSION_NAME=1.0.4
VERSION_CODE=3
VERSION_NAME=1.0.5
VERSION_CODE=4
GROUP=com.github.travijuu

POM_DESCRIPTION=A simple customizable NumberPicker plugin for Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
public interface LimitExceededListener {

public void limitExceeded(int limit, int exceededValue);
void limitExceeded(int limit, int exceededValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.travijuu.numberpicker.library.Interface;

import com.travijuu.numberpicker.library.Enums.ActionEnum;

/**
* Created by travijuu on 19/12/16.
*/

public interface ValueChangedListener {

void valueChanged(int value, ActionEnum action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public ActionListener(NumberPicker layout, ActionEnum action) {

@Override
public void onClick(View v) {
if (this.action == ActionEnum.INCREMENT)
this.layout.increment();
else
this.layout.decrement();
switch (this.action) {
case INCREMENT:
this.layout.increment();
break;
case DECREMENT:
this.layout.decrement();
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DefaultLimitExceededListener implements LimitExceededListener {

public void limitExceeded(int limit, int exceededValue) {

String message = String.format("Value cannot be setted to %d because the limit is %d.", limit, exceededValue);
String message = String.format("NumberPicker cannot set to %d because the limit is %d.", exceededValue, limit);
Log.v(this.getClass().getSimpleName(), message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.travijuu.numberpicker.library.Listener;

import android.util.Log;

import com.travijuu.numberpicker.library.Enums.ActionEnum;
import com.travijuu.numberpicker.library.Interface.ValueChangedListener;

/**
* Created by travijuu on 19/12/16.
*/

public class DefaultValueChangedListener implements ValueChangedListener {

public void valueChanged(int value, ActionEnum action) {

String actionText = action == ActionEnum.INCREMENT ? "incremented" : "decremented";
String message = String.format("NumberPicker is %s to %d", actionText, value);
Log.v(this.getClass().getSimpleName(), message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

import com.travijuu.numberpicker.library.Enums.ActionEnum;
import com.travijuu.numberpicker.library.Interface.LimitExceededListener;
import com.travijuu.numberpicker.library.Interface.ValueChangedListener;
import com.travijuu.numberpicker.library.Listener.ActionListener;
import com.travijuu.numberpicker.library.Listener.DefaultLimitExceededListener;
import com.travijuu.numberpicker.library.Listener.DefaultValueChangedListener;

/**
* Created by travijuu on 26/05/16.
Expand Down Expand Up @@ -40,6 +42,7 @@ public class NumberPicker extends LinearLayout {

// listeners
private LimitExceededListener limitExceededListener;
private ValueChangedListener valueChangedListener;

public NumberPicker(Context context) {
super(context, null);
Expand Down Expand Up @@ -80,11 +83,14 @@ private void initialize(Context context, AttributeSet attrs) {
this.incrementButton = (Button) findViewById(R.id.increment);
this.displayTextView = (TextView) findViewById(R.id.display);

// register button click listeners
this.incrementButton.setOnClickListener(new ActionListener(this, ActionEnum.INCREMENT));
this.decrementButton.setOnClickListener(new ActionListener(this, ActionEnum.DECREMENT));

// init listener for exceeding upper and lower limits
this.setLimitExceededListener(new DefaultLimitExceededListener());
// init listener for increment&decrement
this.setValueChangedListener(new DefaultValueChangedListener());

// update ui view
this.updateView();
Expand Down Expand Up @@ -122,9 +128,13 @@ public void setLimitExceededListener(LimitExceededListener limitExceededListener
this.limitExceededListener = limitExceededListener;
}

public void setValueChangedListener(ValueChangedListener valueChangedListener) {
this.valueChangedListener = valueChangedListener;
}

public void setValue(int value) {
if (value < this.minValue || value > this.maxValue) {
limitExceededListener.limitExceeded(value < this.minValue ? this.minValue : this.maxValue, value);
this.limitExceededListener.limitExceeded(value < this.minValue ? this.minValue : this.maxValue, value);
return;
}

Expand All @@ -137,10 +147,28 @@ public int getValue() {
}

public void increment() {
this.setValue(this.currentValue + this.unit);
this.changeValueBy(this.unit);
}

public void increment(int unit) {
this.changeValueBy(unit);
}

public void decrement() {
this.setValue(this.currentValue - this.unit);
this.changeValueBy(-this.unit);
}

public void decrement(int unit) {
this.changeValueBy(-unit);
}

private void changeValueBy(int unit) {
int oldValue = this.getValue();

this.setValue(this.currentValue + unit);

if (oldValue != this.getValue()) {
this.valueChangedListener.valueChanged(this.getValue(), unit > 0 ? ActionEnum.INCREMENT : ActionEnum.DECREMENT);
}
}
}

0 comments on commit 6ecc0f9

Please sign in to comment.