Skip to content

Commit

Permalink
Add data binding support for "complete_icon" and "slider_icon" attrib…
Browse files Browse the repository at this point in the history
…utes.
  • Loading branch information
nataliialapshyna-dna committed Feb 10, 2022
1 parent 987b77b commit 5e6ee3e
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 13 deletions.
3 changes: 3 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ android {
lintOptions {
abortOnError false
}
buildFeatures {
dataBinding true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.button_reversed_slider).setOnClickListener(this);
findViewById(R.id.button_animation_duration).setOnClickListener(this);
findViewById(R.id.button_bump_vibration).setOnClickListener(this);
findViewById(R.id.button_bound).setOnClickListener(this);
}

public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ncorti.slidetoact.example;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -9,8 +11,10 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.ncorti.slidetoact.SlideToActView;
import com.ncorti.slidetoact.example.databinding.ContentBoundBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -97,6 +101,15 @@ public void onClick(final View v) {
case R.id.button_bump_vibration:
setContentView(R.layout.content_bumb_vibration);
break;
case R.id.button_bound:
ContentBoundBinding binding = DataBindingUtil.setContentView(this, R.layout.content_bound);
@SuppressLint("UseCompatLoadingForDrawables")
SliderData fullData = new SliderData("Bound SlideToAct View",
getResources().getDrawable(R.drawable.ic_android),
getResources().getDrawable(R.drawable.ic_cloud));
binding.setFullData(fullData);
binding.setPlay(true);
break;
default:
finish();
break;
Expand Down Expand Up @@ -184,4 +197,16 @@ public void onSlideResetAnimationEnded(@NonNull SlideToActView view) {
private String getTime() {
return dateFormat.format(new Date());
}

public class SliderData {
public String text;
public Drawable sliderIcon;
public Drawable completeIcon;

public SliderData(String text, Drawable sliderIcon, Drawable completeIcon) {
this.text = text;
this.sliderIcon = sliderIcon;
this.completeIcon = completeIcon;
}
}
}
15 changes: 15 additions & 0 deletions example/src/main/res/drawable/ic_pause.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="480"
android:viewportHeight="480">
<path
android:fillColor="#FF000000"
android:pathData="M413.974,71.026C368.171,25.225 307.274,0 242.5,0S116.829,25.225 71.026,71.026C25.225,116.829 0,177.726 0,242.5s25.225,125.671 71.026,171.474C116.829,459.775 177.726,485 242.5,485s125.671,-25.225 171.474,-71.026C459.775,368.171 485,307.274 485,242.5S459.775,116.829 413.974,71.026zM242.5,455C125.327,455 30,359.673 30,242.5S125.327,30 242.5,30S455,125.327 455,242.5S359.673,455 242.5,455z" />
<path
android:fillColor="#FF000000"
android:pathData="M172.5,140h55v205h-55z" />
<path
android:fillColor="#FF000000"
android:pathData="M257.5,140h55v205h-55z" />
</vector>
12 changes: 12 additions & 0 deletions example/src/main/res/drawable/ic_play.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="480"
android:viewportHeight="480">
<path
android:fillColor="#FF000000"
android:pathData="M413.974,71.026C368.171,25.225 307.274,0 242.5,0S116.829,25.225 71.026,71.026C25.225,116.829 0,177.726 0,242.5s25.225,125.671 71.026,171.474C116.829,459.775 177.726,485 242.5,485s125.671,-25.225 171.474,-71.026C459.775,368.171 485,307.274 485,242.5S459.775,116.829 413.974,71.026zM242.5,455C125.327,455 30,359.673 30,242.5S125.327,30 242.5,30S455,125.327 455,242.5S359.673,455 242.5,455z" />
<path
android:fillColor="#FF000000"
android:pathData="M181.062,336.575l162.876,-94.075l-162.876,-94.075z" />
</vector>
6 changes: 6 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,11 @@
android:layout_height="wrap_content"
android:text="@string/bump_vibration" />

<Button
android:id="@+id/button_bound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bound" />

</LinearLayout>
</ScrollView>
43 changes: 43 additions & 0 deletions example/src/main/res/layout/content_bound.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="fullData"
type="com.ncorti.slidetoact.example.SampleActivity.SliderData" />

<variable
name="play"
type="boolean" />
</data>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/slide_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical">

<com.ncorti.slidetoact.SlideToActView
android:id="@+id/slide_bound"
style="@style/SlideToActView.Example"
app:complete_icon="@{fullData.completeIcon}"
app:slider_icon="@{fullData.sliderIcon}"
app:text="@{fullData.text}" />

<com.ncorti.slidetoact.SlideToActView
android:id="@+id/slide_bound1"
style="@style/SlideToActView.Example"
app:complete_icon="@{play == true ? @drawable/ic_pause : @drawable/ic_play}"
app:slider_icon="@{play == true ? @drawable/ic_play : @drawable/ic_pause}"
app:text="Slide to play" />

</LinearLayout>
</ScrollView>
</layout>
1 change: 1 addition & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="reversed_slider">Reversed Slider</string>
<string name="animation_duration">Animation duration</string>
<string name="bump_vibration">Bump Vibration</string>
<string name="bound">Bound</string>
<string name="event_log">Event Log</string>
<string name="welcome_body">Press the following buttons to checkout different SlideToActView and understand each parameter. You can reset the sliders pressing the Reset button in the Toolbar. Enjoy :)</string>
<string name="use_android_icon">Use Android icon</string>
Expand Down
4 changes: 4 additions & 0 deletions slidetoact/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
apply plugin: 'kotlin-kapt'

version = "0.9.0"

Expand All @@ -25,6 +26,9 @@ android {
lintOptions {
abortOnError false
}
buildFeatures {
dataBinding true
}
}

dependencies {
Expand Down
60 changes: 47 additions & 13 deletions slidetoact/src/main/java/com/ncorti/slidetoact/SlideToActView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import android.view.animation.AnticipateOvershootInterpolator
import android.view.animation.OvershootInterpolator
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import androidx.annotation.StyleRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.TextViewCompat
import androidx.databinding.BindingAdapter
import com.ncorti.slidetoact.SlideToActIconUtil.createIconAnimator
import com.ncorti.slidetoact.SlideToActIconUtil.loadIconCompat
import com.ncorti.slidetoact.SlideToActIconUtil.stopIconAnimation
Expand Down Expand Up @@ -149,17 +149,34 @@ class SlideToActView @JvmOverloads constructor(
}

/** Custom Slider Icon */
@DrawableRes
var sliderIcon: Int = R.drawable.slidetoact_ic_arrow
var sliderIcon: Any = R.drawable.slidetoact_ic_arrow
set(value) {
field = value
if (field != 0) {
ResourcesCompat.getDrawable(context.resources, value, context.theme)?.let {
mDrawableArrow = it
DrawableCompat.setTint(it, iconColor)
when (value) {
is Int -> {
if (field != 0) {
ResourcesCompat.getDrawable(context.resources, value, context.theme)?.let {
mDrawableArrow = it
DrawableCompat.setTint(it, iconColor)
}
}
}
is Drawable -> {
mDrawableArrow = value
DrawableCompat.setTint(value, iconColor)
}
else -> {
ResourcesCompat.getDrawable(
context.resources,
R.drawable.slidetoact_ic_arrow,
context.theme
)?.let {
mDrawableArrow = it
DrawableCompat.setTint(it, iconColor)
}
}
invalidate()
}
invalidate()
}

/** Slider cursor position (between 0 and (`mAreaWidth - mAreaHeight)) */
Expand Down Expand Up @@ -217,14 +234,21 @@ class SlideToActView @JvmOverloads constructor(
private var mDrawableTick: Drawable
private var mFlagDrawTick: Boolean = false

@DrawableRes
var completeIcon: Int = 0
var completeIcon: Any = 0
set(value) {
field = value
if (field != 0) {
mDrawableTick = loadIconCompat(context, value)
invalidate()
when (value) {
is Int -> {
if (field != 0) {
mDrawableTick = loadIconCompat(context, value)
}
}
is Drawable -> {
mDrawableTick = value
}
else -> loadIconCompat(context, R.drawable.slidetoact_ic_check)
}
invalidate()
}

/* -------------------- PAINT & DRAW -------------------- */
Expand Down Expand Up @@ -987,3 +1011,13 @@ class SlideToActView @JvmOverloads constructor(
}
}
}

@BindingAdapter("slider_icon")
fun loadSliderIcon(view: SlideToActView, sliderIcon: Drawable) {
view.sliderIcon = sliderIcon
}

@BindingAdapter("complete_icon")
fun loadCompleteIcon(view: SlideToActView, completeIcon: Drawable) {
view.completeIcon = completeIcon
}

0 comments on commit 5e6ee3e

Please sign in to comment.