Skip to content

Commit

Permalink
feat(android): use material based ProgressBar/ActivityIndicator
Browse files Browse the repository at this point in the history
Fixes TIMOB-28351
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Mar 5, 2021
1 parent c8b6ab7 commit b1c5d0a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,77 +1,63 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-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.
*/
package ti.modules.titanium.ui.widget;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.view.ContextThemeWrapper;
import com.google.android.material.progressindicator.CircularProgressIndicator;
import com.google.android.material.textview.MaterialTextView;
import java.util.HashMap;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.R;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TiUIActivityIndicator extends TiUIView
{
private static final String TAG = "TiUIActivityIndicator";

protected int currentStyle;
protected boolean visible;
private TextView label;
private ProgressBar progress;
public static final int PLAIN = 0;
public static final int BIG = 1;
public static final int DARK = 2;
public static final int BIG_DARK = 3;

public static final int PLAIN = android.R.attr.progressBarStyleSmall;
public static final int BIG = android.R.attr.progressBarStyleLarge;
public static final int DARK = android.R.attr.progressBarStyleSmallInverse;
public static final int BIG_DARK = android.R.attr.progressBarStyleLargeInverse;
private boolean visible;
private MaterialTextView label;
private CircularProgressIndicator progress;

public TiUIActivityIndicator(TiViewProxy proxy)
{
super(proxy);
Log.d(TAG, "Creating an activity indicator", Log.DEBUG_MODE);

/*
* use getAppCurrentActivity over getActivity since technically the activity indicator
* should show up on top of the current activity when called - not just the
* activity it was created in
*/
Activity activity = TiApplication.getAppCurrentActivity();

if (activity == null) {
Log.w(TAG, "Unable to create an activity indicator. Activity is null");
return;
}

LinearLayout view = new LinearLayout(activity);
LinearLayout view = new LinearLayout(proxy.getActivity());
view.setOrientation(LinearLayout.HORIZONTAL);
view.setGravity(Gravity.CENTER);
view.setVisibility(View.INVISIBLE);
this.visible = false;

label = new TextView(activity);
label.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
label.setPadding(0, 0, 0, 0);
label.setSingleLine(false);

currentStyle = getStyle();
progress = new ProgressBar(activity, null, currentStyle);
this.progress = new CircularProgressIndicator(proxy.getActivity());
this.progress.setIndeterminate(true);
view.addView(this.progress);

view.addView(progress);
view.addView(label);
view.setVisibility(View.INVISIBLE);
visible = false;
this.label = new MaterialTextView(proxy.getActivity());
this.label.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
this.label.setPadding(0, 0, 0, 0);
this.label.setSingleLine(false);
view.addView(this.label);

setNativeView(view);
}
Expand All @@ -86,9 +72,6 @@ public void processProperties(KrollDict d)
return;
}

if (d.containsKey(TiC.PROPERTY_STYLE)) {
setStyle(TiConvert.toInt(d, TiC.PROPERTY_STYLE));
}
if (d.containsKey(TiC.PROPERTY_FONT)) {
TiUIHelper.styleText(label, d.getKrollDict(TiC.PROPERTY_FONT));
}
Expand All @@ -98,10 +81,7 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_COLOR)) {
label.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
}
if (d.containsKey(TiC.PROPERTY_INDICATOR_COLOR)) {
progress.getIndeterminateDrawable().setColorFilter(TiConvert.toColor(d, TiC.PROPERTY_INDICATOR_COLOR),
android.graphics.PorterDuff.Mode.SRC_IN);
}
updateIndicator();

view.invalidate();
}
Expand All @@ -112,7 +92,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE);

if (key.equals(TiC.PROPERTY_STYLE)) {
setStyle(TiConvert.toInt(newValue));
updateIndicator();
} else if (key.equals(TiC.PROPERTY_FONT) && newValue instanceof HashMap) {
TiUIHelper.styleText(label, (HashMap) newValue);
label.requestLayout();
Expand All @@ -122,8 +102,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_COLOR)) {
label.setTextColor(TiConvert.toColor((String) newValue));
} else if (key.equals(TiC.PROPERTY_INDICATOR_COLOR)) {
progress.getIndeterminateDrawable().setColorFilter(TiConvert.toColor((String) newValue),
android.graphics.PorterDuff.Mode.SRC_IN);
updateIndicator();
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
Expand All @@ -149,35 +128,47 @@ public void hide()
visible = false;
}

protected int getStyle()
{
if (proxy.hasProperty(TiC.PROPERTY_STYLE)) {
int style = TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_STYLE));
if (style != PLAIN && style != BIG && style != DARK && style != BIG_DARK) {
Log.w(TAG, "Invalid value \"" + style + "\" for style.");
return PLAIN;
}
return style;
}
return PLAIN;
}

protected void setStyle(int style)
private void updateIndicator()
{
if (style == currentStyle) {
// Do not continue if proxy has been released.
if (this.proxy == null) {
return;
}
if (style != PLAIN && style != BIG && style != DARK && style != BIG_DARK) {
Log.w(TAG, "Invalid value \"" + style + "\" for style.");
return;

// Fetch assigned style ID.
int styleId = TiConvert.toInt(this.proxy.getProperty(TiC.PROPERTY_STYLE), PLAIN);
if ((styleId != PLAIN) && (styleId != BIG) && (styleId != DARK) && (styleId != BIG_DARK)) {
Log.w(TAG, "Invalid value \"" + styleId + "\" for style.");
styleId = PLAIN;
}
LinearLayout view = (LinearLayout) getNativeView();

view.removeAllViews();
progress = new ProgressBar(TiApplication.getAppCurrentActivity(), null, style);
currentStyle = style;
view.addView(progress);
view.addView(label);
view.requestLayout();
// Update indicator to use a big or small style.
int[] idArray = new int[] {
R.attr.trackThickness,
R.attr.indicatorSize,
R.attr.indicatorInset
};
int themeId = R.style.Widget_MaterialComponents_CircularProgressIndicator_ExtraSmall;
if ((styleId == BIG) || (styleId == BIG_DARK)) {
themeId = R.style.Widget_MaterialComponents_CircularProgressIndicator_Medium;
}
ContextThemeWrapper context = new ContextThemeWrapper(this.progress.getContext(), themeId);
TypedArray typedArray = context.obtainStyledAttributes(null, idArray, 0, 0);
int value = typedArray.getDimensionPixelSize(0, this.progress.getTrackThickness());
this.progress.setTrackThickness(typedArray.getDimensionPixelSize(0, this.progress.getTrackThickness()));
this.progress.setIndicatorSize(typedArray.getDimensionPixelSize(1, this.progress.getIndicatorSize()));
this.progress.setIndicatorInset(typedArray.getDimensionPixelSize(2, this.progress.getIndicatorInset()));
typedArray.recycle();

// Update indicator's color.
if (this.proxy.hasPropertyAndNotNull(TiC.PROPERTY_INDICATOR_COLOR)) {
int color = TiConvert.toColor(TiConvert.toString(this.proxy.getProperty(TiC.PROPERTY_INDICATOR_COLOR)));
this.progress.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
} else if ((styleId == DARK) || (styleId == BIG_DARK)) {
int color = Color.DKGRAY;
this.progress.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
} else {
this.progress.getIndeterminateDrawable().clearColorFilter();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-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.
*/
package ti.modules.titanium.ui.widget;

import android.content.res.ColorStateList;
import android.view.Gravity;
import android.widget.LinearLayout;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.google.android.material.textview.MaterialTextView;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.titanium.TiC;
Expand All @@ -14,17 +19,10 @@
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.content.res.ColorStateList;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TiUIProgressBar extends TiUIView
{

private TextView label;
private ProgressBar progress;
private MaterialTextView label;
private LinearProgressIndicator progress;
private LinearLayout view;

public TiUIProgressBar(final TiViewProxy proxy)
Expand All @@ -40,12 +38,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}
};
view.setOrientation(LinearLayout.VERTICAL);
label = new TextView(proxy.getActivity());
label.setGravity(Gravity.TOP | Gravity.LEFT);
label = new MaterialTextView(proxy.getActivity());
label.setGravity(Gravity.TOP | Gravity.START);
label.setPadding(0, 0, 0, 0);
label.setSingleLine(false);

progress = new ProgressBar(proxy.getActivity(), null, android.R.attr.progressBarStyleHorizontal);
progress = new LinearProgressIndicator(proxy.getActivity());
progress.setIndeterminate(false);
progress.setMax(1000);

Expand Down

0 comments on commit b1c5d0a

Please sign in to comment.