Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data binding support for "complete_icon" and "slider_icon" attributes #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ plugins {
id("com.android.library") version "7.2.1" apply false
id("org.jlleitschuh.gradle.ktlint") version "10.3.0" apply false
kotlin("android") version "1.6.21" apply false
kotlin("kapt") version "1.6.21" apply false
}
3 changes: 3 additions & 0 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ android {
lint {
abortOnError = true
}
buildFeatures {
dataBinding = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.button_animation_duration).setOnClickListener(this);
findViewById(R.id.button_bump_vibration).setOnClickListener(this);
findViewById(R.id.button_completed).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,12 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
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 @@ -100,6 +106,14 @@ public void onClick(final View v) {
case R.id.button_completed:
setContentView(R.layout.content_completed);
break;
case R.id.button_bound:
ContentBoundBinding binding = DataBindingUtil.setContentView(this, R.layout.content_bound);
SliderData fullData = new SliderData("Bound SlideToAct View",
ContextCompat.getDrawable(this, R.drawable.ic_android),
ContextCompat.getDrawable(this, R.drawable.ic_cloud));
binding.setFullData(fullData);
binding.setPlay(true);
break;
default:
finish();
break;
Expand Down Expand Up @@ -187,4 +201,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 @@ -118,5 +118,11 @@
android:layout_height="wrap_content"
android:text="@string/completed" />

<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 @@ -15,6 +15,7 @@
<string name="animation_duration">Animation duration</string>
<string name="bump_vibration">Bump Vibration</string>
<string name="completed">Completed</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.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("signing")
id("org.jlleitschuh.gradle.ktlint")
kotlin("android")
kotlin("kapt")
}

version = "0.10.0".plus(if (hasProperty("USE_SNAPSHOT")) "-SNAPSHOT" else "")
Expand All @@ -21,6 +22,9 @@ android {
lint {
abortOnError = true
}
buildFeatures {
dataBinding = true
}

publishing {
singleVariant("release") {
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 @@ -28,14 +28,14 @@ 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.ContextCompat.getSystemService
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.startIconAnimation
Expand Down Expand Up @@ -156,17 +156,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
Copy link
Owner

@cortinico cortinico Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sadly suboptimal :/

We're now exposing a public property which is a Any.
I think we should instead create other two private fields called:

private var sliderIconDrawable
private var completeIconDrawable

And let the BindingAdapter operate on those.

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 @@ -228,14 +245,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 @@ -1099,3 +1123,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
}