Skip to content

Commit

Permalink
Merge pull request #14 from techery/feature/setters
Browse files Browse the repository at this point in the history
Feature/setters
  • Loading branch information
Den-Rimus committed Dec 17, 2015
2 parents ba6de13 + aafca8d commit 4f54e0a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.idea
/library/build
*.iml
**/*.iml
101 changes: 0 additions & 101 deletions app/app.iml

This file was deleted.

6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "io.techery.properratingbar.sample"
Expand All @@ -22,8 +22,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile project(':library')
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public class MainActivity extends AppCompatActivity {

@Bind(R.id.rootView)
ViewGroup rootView;
@Bind(R.id.ratingBar)
ProperRatingBar ratingBar;
@Bind(R.id.upperRatingBar)
ProperRatingBar upperRatingBar;
@Bind(R.id.lowerRatingBar)
ProperRatingBar lowerRatingBar;

private String[] ticksArray = new String[] {"$", "★"};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -27,17 +31,24 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//
ratingBar.setListener(ratingListener);
lowerRatingBar.setListener(ratingListener);
}

@OnClick(R.id.toggleTick) void toggleTick() { // here goes shit-code. No time to do properly
String tick;
if (upperRatingBar.getSymbolicTick().equals(ticksArray[0])) tick = ticksArray[1];
else tick = ticksArray[0];
upperRatingBar.setSymbolicTick(tick);
}

@OnClick(R.id.getRatingButton) void getRatingClicked() {
Snackbar.make(rootView,
String.format(getString(R.string.get_rating_snack_caption_format), ratingBar.getRating()),
String.format(getString(R.string.get_rating_snack_caption_format), lowerRatingBar.getRating()),
Snackbar.LENGTH_SHORT).show();
}

@OnClick(R.id.setRatingButton) void setRatingClicked() {
ratingBar.setRating(2);
lowerRatingBar.setRating(2);
Snackbar.make(rootView, R.string.set_rating_snack_caption, Snackbar.LENGTH_SHORT).show();
}

Expand Down
12 changes: 11 additions & 1 deletion app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/>

<io.techery.properratingbar.ProperRatingBar
android:id="@+id/upperRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
Expand All @@ -36,6 +37,15 @@
app:prb_totalTicks="5"
/>

<Button
android:id="@+id/toggleTick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/switch_tick_button_caption"
android:layout_marginBottom="15dp"
/>

<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -44,7 +54,7 @@
/>

<io.techery.properratingbar.ProperRatingBar
android:id="@+id/ratingBar"
android:id="@+id/lowerRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<string name="symbolic_tick_prb_caption">ProperRatingBar with symbolic tick:</string>
<string name="drawable_tick_prb_caption">ProperRatingBar with drawable tick, with attached listener, clickable:</string>
<string name="switch_tick_button_caption">Switch tick</string>
<string name="get_rating_button_caption">Get rating</string>
<string name="set_rating_button_caption">Set rating to \'2\'</string>
<string name="set_rating_snack_caption">Rating was set to \'2\'</string>
Expand Down
4 changes: 2 additions & 2 deletions library/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.techery.properratingbar"
android:versionCode="2"
android:versionName="0.0.2"
android:versionCode="3"
android:versionName="0.0.3"
/>
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ group = "io.techery"
android {

compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 14
Expand All @@ -25,5 +25,5 @@ repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.android.support:support-annotations:23.1.1'
}
33 changes: 28 additions & 5 deletions library/src/io/techery/properratingbar/ProperRatingBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,19 @@ public class ProperRatingBar extends LinearLayout {
public ProperRatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
addChildren(context);
}

private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProperRatingBar);
//
totalTicks = a.getInt(R.styleable.ProperRatingBar_prb_totalTicks, DF_TOTAL_TICKS);
rating = a.getInt(R.styleable.ProperRatingBar_prb_defaultRating, DF_DEFAULT_TICKS);
if (rating > totalTicks) rating = totalTicks;
lastSelectedTickIndex = rating - 1;
//
isClickable = a.getBoolean(R.styleable.ProperRatingBar_prb_clickable, DF_CLICKABLE);
//
symbolicTick = a.getString(R.styleable.ProperRatingBar_prb_symbolicTick);
if (symbolicTick == null) symbolicTick = context.getString(DF_SYMBOLIC_TICK_RES);
//
customTextSize = a.getDimensionPixelSize(R.styleable.ProperRatingBar_android_textSize,
context.getResources().getDimensionPixelOffset(DF_SYMBOLIC_TEXT_SIZE_RES));
customTextStyle = a.getInt(R.styleable.ProperRatingBar_android_textStyle, DF_SYMBOLIC_TEXT_STYLE);
Expand All @@ -101,14 +100,24 @@ private void init(Context context, AttributeSet attrs) {
tickSpacing = a.getDimensionPixelOffset(R.styleable.ProperRatingBar_prb_tickSpacing,
context.getResources().getDimensionPixelOffset(DF_TICK_SPACING_RES));
//
afterInit();
//
a.recycle();
}

private void afterInit() {
if (rating > totalTicks) rating = totalTicks;
lastSelectedTickIndex = rating - 1;
//
if (tickNormalDrawable == null || tickSelectedDrawable == null) {
useSymbolicTick = true;
}
//
a.recycle();
addChildren(this.getContext());
}

private void addChildren(Context context) {
this.removeAllViews();
for (int i = 0; i < totalTicks; i++) {
addChild(context, i);
}
Expand Down Expand Up @@ -239,7 +248,7 @@ public SavedState[] newArray(int size) {
}

///////////////////////////////////////////////////////////////////////////
// them getters and setters methods
// Them getter and setter methods
///////////////////////////////////////////////////////////////////////////

/**
Expand All @@ -254,9 +263,14 @@ public RatingListener getListener() {
/**
* Set the {@link RatingListener} to be called when user taps rating bar's ticks
* @param listener listener to set
*
* @throws IllegalArgumentException if listener is <b>null</b>
*/
public void setListener(RatingListener listener) {
if (listener == null) throw new IllegalArgumentException("listener cannot be null!");

this.listener = listener;
this.isClickable = true;
}

/**
Expand Down Expand Up @@ -284,4 +298,13 @@ public void setRating(int rating) {
lastSelectedTickIndex = rating - 1;
redrawChildren();
}

public void setSymbolicTick(String tick) {
this.symbolicTick = tick;
afterInit();
}

public String getSymbolicTick() {
return this.symbolicTick;
}
}

0 comments on commit 4f54e0a

Please sign in to comment.