Skip to content

Commit

Permalink
Merge branch 'master' into issue/15-disable-incremental-counts
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba authored Oct 28, 2019
2 parents cb2ddf9 + a7ddffc commit 57c14a1
Show file tree
Hide file tree
Showing 16 changed files with 720 additions and 15 deletions.
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,51 @@ For pie charts display. You can use the following code snippet.
PieChartSlice indicator2_2 = getPieChartSlice(LATEST_COUNT, ChartUtil.pieChartNoIndicatorKey, getResources().getString(R.string.no_button_label), getResources().getColor(R.color.colorPieChartRed), indicatorTallies);
mainLayout.addView(new PieChartIndicatorView(getContext(), getPieChartDisplayModel(addPieChartSlices(indicator2_1, indicator2_2), R.string.num_of_lieterate_children_0_60_label, R.string.sample_note)).createView());
```
### Progress indicator
This indicator widget basically has a progressbar, main title(Label) and a sub title.

Checkout the sample app for more.
The following are the configurable properties

| **Property** | **Type** | **Usage** |
| ------------- | ------------- |-------------
| progressBarForegroundColor | int | This is a color Resource ID that sets the progress bar foreground color **(API 23 and above)**|
| progressBarBackgroundColor | int |This is a color Resource ID that sets the progress bar background color **(API 23 and above)**|
| title | String | This is a label for the indicator (*appears at top*)|
| subTitle | String |This is a sub title (*label appears at bottom*)|
| progress | int | This the progress of the indicator out of 100 (*Percentage %*)|
| isTitleHidden | int | This hides or shows the title (*default false*)|
| isSubTitleHidden | int | This hides or shows the sub title (*default false*)|
|progressDrawable | int | This is the Resource ID of your custom progressbar drawable [_Template Here_](https://github.com/OpenSRP/opensrp-client-reporting/blob/master/opensrp-reporting/src/main/res/layout/numeric_indicator_view.xml) |

**NB:** For more flexibility and configurations, it is also possible to style the widget by overriding the widgets drawable. You can do this by cloning the file [**here**](https://github.com/OpenSRP/opensrp-client-reporting/blob/master/opensrp-reporting/src/main/res/layout/numeric_indicator_view.xml) and making the necessary changes. Then place the files in your app's drawables folder. This is especially needed to configure the progress bar colors and styling for **API < 23**

using the property **progressDrawable** listed above, you can set the progressDrawable for individual instances of the ProgressIndicatorViews both programmatically or via xml styling.

**Programmatically:**
```
ProgressIndicatorView progressWidget = getActivity().findViewById(R.id.progressIndicatorView);
progressWidget.setProgress(42);
progressWidget.setTitle("Users registered - 42%");
progressWidget.setProgressDrawable(R.drawable.progress_indicator_bg);
progressWidget.setProgressBarForegroundColor(ContextCompat.getColor(getContext(), R.color.pnc_circle_red));
progressWidget.setProgressBarBackgroundColor(ContextCompat.getColor(getContext(), R.color.pnc_circle_yellow));
```

**Via XML:**

```
<org.smartregister.reporting.view.ProgressIndicatorView
android:id="@+id/progressIndicatorView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:progressBarBackgroundColor="@color/text_black"
app:progressBarForegroundColor="@color/colorPastelGreen"
app:subTitle="Coverage"
app:progressDrawable="@drawable/custom_progress_indicator_bg"
```




*Checkout the sample app for more examples..*
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,21 @@ private boolean hasInitializedIndicators(SQLiteDatabase sqLiteDatabase) {
if (!appOnDebugMode && (!isAppUpdated() || isIndicatorsInitialized())) {
return true;
}
truncateIndicatorDefinitionTables(sqLiteDatabase);
return false;
}

public void truncateIndicatorDefinitionTables(SQLiteDatabase sqLiteDatabase) {
if (sqLiteDatabase != null) {
indicatorRepository.truncateTable(sqLiteDatabase);
indicatorQueryRepository.truncateTable(sqLiteDatabase);
} else {
indicatorRepository.truncateTable();
indicatorQueryRepository.truncateTable();
}
return false;
}

private void readConfigFile(String configFilePath, SQLiteDatabase sqLiteDatabase) {
public void readConfigFile(String configFilePath, SQLiteDatabase sqLiteDatabase) {
initYamlIndicatorConfig();
Iterable<Object> indicatorsFromFile = null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
package org.smartregister.reporting.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.smartregister.reporting.R;

import timber.log.Timber;

/**
* Created by ndegwamartin on 2019-09-16.
*/
public class ProgressIndicatorView extends LinearLayout {

private int progressBarBackgroundColor;
private int progressBarForegroundColor;
private int progressDrawable;
private String title;
private String subTitle;
private int progress;
private boolean isTitleHidden;
private boolean isSubTitleHidden;

private static final String PROGRESSBAR_FOREGROUND_COLOR = "progressbar_foreground_color";
private static final String PROGRESSBAR_BACKGROUND_COLOR = "progressbar_background_color";
private static final String PROGRESSBAR_TITLE = "progressbar_title";
private static final String PROGRESSBAR_SUB_TITLE = "progressbar_sub_title";
private static final String PROGRESSBAR_PROGRESS = "progressbar_progress";
private static final String PROGRESSBAR_INSTANCE_STATE = "progressbar_instance_state";
private static final String PROGRESSBAR_DRAWABLE = "progresbar_drawable";

private AttributeSet attrs;

public ProgressIndicatorView(Context context) {
super(context);
initView();

}

public ProgressIndicatorView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
setupAttributes(attrs);
}

public ProgressIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
setupAttributes(attrs);
}

@TargetApi(android.os.Build.VERSION_CODES.LOLLIPOP)
public ProgressIndicatorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
setupAttributes(attrs);
}

/**
* Set init configs for parent layout view
*/
private void initView() {
inflate(getContext(), R.layout.progress_indicator_view, this);
setGravity(Gravity.LEFT);
setOrientation(VERTICAL);
}

/**
* @param attrs an attribute set styled attributes from theme
*/

protected void setupAttributes(AttributeSet attrs) {
this.attrs = attrs;

TypedArray typedArray = getStyledAttributes();

try {

resetLayoutParams(typedArray);

} finally {

typedArray.recycle();// We must recycle TypedArray objects as they are shared
}
}

protected TypedArray getStyledAttributes() {
return getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ProgressIndicatorView, 0, 0);
}

private void resetLayoutParams(TypedArray typedArray) {

title = TextUtils.isEmpty(title) ? typedArray.getString(R.styleable.ProgressIndicatorView_title) : title;
title = TextUtils.isEmpty(title) ? progress + "%" : title;

subTitle = TextUtils.isEmpty(subTitle) ? typedArray.getString(R.styleable.ProgressIndicatorView_subTitle) : subTitle;

TextView titleTextView = findViewById(R.id.title_textview);
titleTextView.setText(title);
titleTextView.setVisibility(isTitleHidden ? View.GONE : View.VISIBLE);

TextView subTitleTextView = findViewById(R.id.sub_title_textview);
subTitleTextView.setText(subTitle);
subTitleTextView.setVisibility(isSubTitleHidden ? View.GONE : View.VISIBLE);

progress = progress > 0 ? progress : typedArray.getInteger(R.styleable.ProgressIndicatorView_progress, 0);

setResourceValues(typedArray);

processProgressBarLayoutReset(progress, progressBarForegroundColor, progressBarBackgroundColor, progressDrawable);

}

private void setResourceValues(TypedArray typedArray) {

progressBarForegroundColor = progressBarForegroundColor != 0 ? progressBarForegroundColor : typedArray.getColor(R.styleable.ProgressIndicatorView_progressBarForegroundColor, 0);
progressBarBackgroundColor = progressBarBackgroundColor != 0 ? progressBarBackgroundColor : typedArray.getColor(R.styleable.ProgressIndicatorView_progressBarBackgroundColor, 0);
progressDrawable = progressDrawable != 0 ? progressDrawable : typedArray.getResourceId(R.styleable.ProgressIndicatorView_progressDrawable, 0);

}

private void processProgressBarLayoutReset(int progress, int progressBarForegroundColor, int progressBarBackgroundColor, int progressDrawable) {

ProgressBar progressBarView = findViewById(R.id.progressbar_view);
progressBarView.setProgressDrawable(progressDrawable > 0 ? getContext().getResources().getDrawable(progressDrawable) : progressBarView.getProgressDrawable());
progressBarView.setProgress(progress);

programmaticallyResetProgressBarBackgroundDrawable(progressBarView, progressBarForegroundColor, progressBarBackgroundColor);

}

@TargetApi(Build.VERSION_CODES.M)
protected void programmaticallyResetProgressBarBackgroundDrawable(ProgressBar progressBarView, int progressBarForegroundColor, int progressBarBackgroundColor) {
try {
LayerDrawable progressBarDrawable = (LayerDrawable) progressBarView.getProgressDrawable().mutate();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0).mutate();
ClipDrawable clipDrawable = (ClipDrawable) progressBarDrawable.getDrawable(1).mutate();

if (progressBarBackgroundColor != 0)
backgroundDrawable.setColorFilter(progressBarBackgroundColor, PorterDuff.Mode.SRC_IN);

if (progressBarForegroundColor != 0) {

GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColor(progressBarForegroundColor);
gdDefault.setCornerRadius(10);
gdDefault.setStroke(2, progressBarBackgroundColor);

clipDrawable.setDrawable(gdDefault);

}
} catch (Exception | NoSuchMethodError e) {
Timber.d(e);
}
}

@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(PROGRESSBAR_INSTANCE_STATE, super.onSaveInstanceState());// Store base view state

bundle.putInt(PROGRESSBAR_FOREGROUND_COLOR, this.progressBarForegroundColor);
bundle.putInt(PROGRESSBAR_BACKGROUND_COLOR, this.progressBarBackgroundColor);
bundle.putString(PROGRESSBAR_TITLE, this.title);
bundle.putString(PROGRESSBAR_SUB_TITLE, this.subTitle);
bundle.putInt(PROGRESSBAR_PROGRESS, this.progress);
bundle.putInt(PROGRESSBAR_DRAWABLE, this.progressDrawable);


return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
Parcelable updatedState = null;

if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
// Load back our custom view state

this.progressBarForegroundColor = bundle.getInt(PROGRESSBAR_FOREGROUND_COLOR);
this.progressBarBackgroundColor = bundle.getInt(PROGRESSBAR_BACKGROUND_COLOR);
this.title = bundle.getString(PROGRESSBAR_TITLE);
this.subTitle = bundle.getString(PROGRESSBAR_SUB_TITLE);
this.progress = bundle.getInt(PROGRESSBAR_PROGRESS);
this.progressDrawable = bundle.getInt(PROGRESSBAR_DRAWABLE);

updatedState = bundle.getParcelable(PROGRESSBAR_INSTANCE_STATE);// Load base view state back
}

super.onRestoreInstanceState(updatedState != null ? updatedState : state);// Pass base view state to super
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

}

private void refreshLayout() {

TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ProgressIndicatorView, 0, 0);

try {
resetLayoutParams(typedArray);
invalidate();
requestLayout();
} finally {
typedArray.recycle();
}
}

public int getProgressBarBackgroundColor() {
return progressBarBackgroundColor;
}

/**
* Only works for API 23 and onwards other use a custom drawable xml to customize style
*/
@TargetApi(Build.VERSION_CODES.M)
public void setProgressBarBackgroundColor(int progressBarBackgroundColor) {
this.progressBarBackgroundColor = progressBarBackgroundColor;
refreshLayout();
}

public int getProgressBarForegroundColor() {
return progressBarForegroundColor;
}

/**
* Only works for API 23 and onwards other use a custom drawable xml to customize style
*/
@TargetApi(Build.VERSION_CODES.M)
public void setProgressBarForegroundColor(int progressBarForegroundColor) {
this.progressBarForegroundColor = progressBarForegroundColor;
refreshLayout();
}

public String getTitle() {
return title;
}

/**
* Sets title of indicator (top text)
*/
public void setTitle(String title) {
this.title = title;
refreshLayout();
}

public String getSubTitle() {
return subTitle;
}

/**
* Sets subtitle of indicator (top text)
*/
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
refreshLayout();
}

public int getProgress() {
return progress;
}

/**
* Sets progress of indicator bar
*/
public void setProgress(int progress) {
this.progress = progress;
refreshLayout();
}

/**
* Sets hides title of indicator
*/
public void hideTitle(boolean isHidden) {
this.isTitleHidden = isHidden;
refreshLayout();
}

public void hideSubTitle(boolean isHidden) {
this.isSubTitleHidden = isHidden;
refreshLayout();
}

public void setProgressDrawable(int progressDrawable) {
this.progressDrawable = progressDrawable;
refreshLayout();
}

public int getProgressDrawable() {
return progressDrawable;
}
}
Loading

0 comments on commit 57c14a1

Please sign in to comment.