Skip to content

Commit

Permalink
fix(android): card view "touchFeedbackColor" property is ignored
Browse files Browse the repository at this point in the history
Fixes TIMOB-28517
  • Loading branch information
jquick-axway authored and ewanharris committed Aug 9, 2021
1 parent 02ad0c8 commit 03ada5d
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 26 deletions.
Expand Up @@ -18,20 +18,23 @@
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutArrangement;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import com.google.android.material.card.MaterialCardView;

public class TiUICardView extends TiUIView
{
public int paddingLeft, paddingTop, paddingRight, paddingBottom;
private ColorStateList defaultRippleColorSateList;
private int paddingLeft, paddingTop, paddingRight, paddingBottom;

private static final String TAG = "TiUICardView";

public class TiUICardViewLayout extends TiCompositeLayout
{

public TiUICardViewLayout(Context context, LayoutArrangement arrangement)
{
super(context, arrangement, proxy);
Expand All @@ -40,7 +43,6 @@ public TiUICardViewLayout(Context context, LayoutArrangement arrangement)

public class TiCardView extends MaterialCardView
{

private TiUICardViewLayout layout;

public TiCardView(Context context, LayoutArrangement arrangement)
Expand All @@ -65,6 +67,12 @@ public void addView(View child, android.view.ViewGroup.LayoutParams params)
layout.addView(child, params);
}

@Override
public void setBackgroundColor(int color)
{
setCardBackgroundColor(color);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
Expand All @@ -79,10 +87,20 @@ public TiUICardView(final TiViewProxy proxy)
{
super(proxy);

// generate native view
if (this.nativeView == null) {
processProperties(getProxy().getProperties());
LayoutArrangement arrangement = LayoutArrangement.DEFAULT;
Object layoutValue = proxy.getProperty(TiC.PROPERTY_LAYOUT);
if (TiC.LAYOUT_VERTICAL.equals(layoutValue)) {
arrangement = LayoutArrangement.VERTICAL;
} else if (TiC.LAYOUT_HORIZONTAL.equals(layoutValue)) {
arrangement = LayoutArrangement.HORIZONTAL;
}

TiCardView cardView = new TiCardView(proxy.getActivity(), arrangement);
cardView.setPadding(0, 0, 0, 0);
cardView.setFocusable(false);
this.defaultRippleColorSateList = cardView.getRippleColor();

setNativeView(cardView);
}

public TiUICardViewLayout getLayout()
Expand Down Expand Up @@ -137,24 +155,9 @@ public void processProperties(KrollDict d)
{
super.processProperties(d);

// we create the view here
View view = null;
LayoutArrangement arrangement = LayoutArrangement.DEFAULT;

if (d.containsKey(TiC.PROPERTY_LAYOUT) && d.getString(TiC.PROPERTY_LAYOUT).equals(TiC.LAYOUT_VERTICAL)) {
arrangement = LayoutArrangement.VERTICAL;
} else if (d.containsKey(TiC.PROPERTY_LAYOUT)
&& d.getString(TiC.PROPERTY_LAYOUT).equals(TiC.LAYOUT_HORIZONTAL)) {
arrangement = LayoutArrangement.HORIZONTAL;
}

view = new TiCardView(getProxy().getActivity(), arrangement);
view.setPadding(0, 0, 0, 0);
view.setFocusable(false);
TiCardView cardview = (TiCardView) view;

if (d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
cardview.setCardBackgroundColor(TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR));
TiCardView cardview = (TiCardView) getNativeView();
if (cardview == null) {
return;
}

if (d.containsKey(TiC.PROPERTY_BORDER_COLOR)) {
Expand Down Expand Up @@ -192,6 +195,15 @@ public void processProperties(KrollDict d)
cardview.setPreventCornerOverlap(TiConvert.toBoolean(d, TiC.PROPERTY_PREVENT_CORNER_OVERLAP, false));
}

if (!d.optBoolean(TiC.PROPERTY_TOUCH_FEEDBACK, true)) {
cardview.setRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));
} else if (d.containsKey(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR)) {
String colorString = TiConvert.toString(d.get(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR));
cardview.setRippleColor(ColorStateList.valueOf(TiConvert.toColor(colorString)));
} else if (this.defaultRippleColorSateList != null) {
cardview.setRippleColor(this.defaultRippleColorSateList);
}

if (d.containsKey(TiC.PROPERTY_PADDING)) {
float radiusRight = 0;
TiDimension radiusDimRight =
Expand Down Expand Up @@ -267,8 +279,6 @@ public void processProperties(KrollDict d)
}

cardview.setContentPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

setNativeView(view);
}

@Override
Expand Down Expand Up @@ -374,11 +384,28 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
paddingTop = (int) radiusTop;
cardview.setContentPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
cardview.requestLayout();
} else if (key.equals(TiC.PROPERTY_TOUCH_FEEDBACK) || key.equals(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR)) {
boolean isEnabled = TiConvert.toBoolean(this.proxy.getProperty(TiC.PROPERTY_TOUCH_FEEDBACK), true);
if (!isEnabled) {
cardview.setRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));
} else if (this.proxy.hasProperty(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR)) {
String colorString = TiConvert.toString(this.proxy.getProperty(TiC.PROPERTY_TOUCH_FEEDBACK_COLOR));
cardview.setRippleColor(ColorStateList.valueOf(TiConvert.toColor(colorString)));
} else if (this.defaultRippleColorSateList != null) {
cardview.setRippleColor(this.defaultRippleColorSateList);
}
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}

@Override
protected boolean canApplyTouchFeedback(@NonNull KrollDict props)
{
// Don't let base class apply touch feedback and use MaterialCardView.setRippleColor() instead.
return false;
}

@Override
protected boolean hasBorder(KrollDict d)
{
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/app.js
Expand Up @@ -144,6 +144,7 @@ function loadTests() {
require('./ti.ui.alertdialog.test');
if (OS_ANDROID) {
require('./ti.ui.android.test');
require('./ti.ui.android.cardview.test');
require('./ti.ui.android.drawerlayout.test');
require('./ti.ui.android.progressindicator.test');
}
Expand Down
94 changes: 94 additions & 0 deletions tests/Resources/ti.ui.android.cardview.test.js
@@ -0,0 +1,94 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2021 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');

describe.android('Titanium.UI.Android.CardView', function () {
this.timeout(5000);

let win;
afterEach(done => { // fires after every test in sub-suites too...
if (win && !win.closed) {
win.addEventListener('close', function listener () {
win.removeEventListener('close', listener);
win = null;
done();
});
win.close();
} else {
win = null;
done();
}
});

it.iosBroken('Ti.UI.Android.CardView', () => {
should(Ti.UI.Android.CardView).not.be.undefined();
});

it('.apiName', () => {
const cardView = Ti.UI.Android.createCardView();
should(cardView).have.readOnlyProperty('apiName').which.is.a.String();
should(cardView.apiName).be.eql('Ti.UI.Android.CardView');
});

it('createCardView', (finish) => {
should(Ti.UI.Android.createCardView).not.be.undefined();
should(Ti.UI.Android.createCardView).be.a.Function();

win = Ti.UI.createWindow();
win.add(Ti.UI.Android.createCardView());
win.addEventListener('postlayout', function listener() {
win.removeEventListener('postlayout', listener);
finish();
});
win.open();
});

it('.backgroundColor', (finish) => {
win = Ti.UI.createWindow();
const cardView = Ti.UI.Android.createCardView({
backgroundColor: 'orange'
});
should(cardView.backgroundColor).be.eql('orange');
win.add(cardView);
win.addEventListener('postlayout', function listener() {
win.removeEventListener('postlayout', listener);
finish();
});
win.open();
});

it('.touchFeedback', (finish) => {
win = Ti.UI.createWindow();
const cardView = Ti.UI.Android.createCardView({
touchFeedback: false
});
should(cardView.touchFeedback).be.false();
win.add(cardView);
win.addEventListener('postlayout', function listener() {
win.removeEventListener('postlayout', listener);
finish();
});
win.open();
});

it('.touchFeedbackColor', (finish) => {
win = Ti.UI.createWindow();
const cardView = Ti.UI.Android.createCardView({
touchFeedbackColor: 'yellow'
});
should(cardView.touchFeedbackColor).be.eql('yellow');
win.add(cardView);
win.addEventListener('postlayout', function listener() {
win.removeEventListener('postlayout', listener);
finish();
});
win.open();
});
});

0 comments on commit 03ada5d

Please sign in to comment.