Skip to content

Commit

Permalink
Fix drawable mutate issues
Browse files Browse the repository at this point in the history
Fix index exceptions
Edit constructor
  • Loading branch information
guy-4444 committed Jun 6, 2018
1 parent 018d873 commit d7587a8
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 265 deletions.
91 changes: 29 additions & 62 deletions app/src/main/java/mishmarot/guy/com/testtime/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,57 @@
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import mishmarot.guy.com.timelineunit.TimeLineLayout;
import mishmarot.guy.com.timelineunit.TimelineView;
import mishmarot.guy.com.timelineunit.StepLineLayout;
import mishmarot.guy.com.timelineunit.StepLineView;

public class MainActivity extends AppCompatActivity {

int index = 0;
TimeLineLayout timeLayout_1;
TimeLineLayout timeLayout_2;
TimeLineLayout timeLayout_3;
TimeLineLayout timeLayout_4;
int index = -1;
StepLineLayout stepLayout_1;
StepLineLayout stepLayout_2;
StepLineLayout stepLayout_3;
StepLineLayout stepLayout_4;
StepLineLayout stepLayout_5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

timeLayout_1 = (TimeLineLayout) findViewById(R.id.timeLayout_1);
timeLayout_2 = (TimeLineLayout) findViewById(R.id.timeLayout_2);
timeLayout_3 = (TimeLineLayout) findViewById(R.id.timeLayout_3);
timeLayout_4 = (TimeLineLayout) findViewById(R.id.timeLayout_4);
stepLayout_1 = (StepLineLayout) findViewById(R.id.stepLayout_1);
stepLayout_2 = (StepLineLayout) findViewById(R.id.stepLayout_2);
stepLayout_3 = (StepLineLayout) findViewById(R.id.stepLayout_3);
stepLayout_4 = (StepLineLayout) findViewById(R.id.stepLayout_4);
stepLayout_5 = (StepLineLayout) findViewById(R.id.stepLayout_5);

timeLayout_1.setTimeLines(this, TimelineView.LineOrientation.HORIZONTAL, 5, R.color.colorPrimary, android.R.color.darker_gray);
timeLayout_2.setTimeLines(this, TimelineView.LineOrientation.VERTICAL, 12, Color.argb(123, 0, 0, 255), android.R.color.darker_gray, android.R.color.darker_gray, 3, 20, 10);
timeLayout_4.setTimeLines(this, TimelineView.LineOrientation.VERTICAL, 6, Color.argb(20, 0, 244, 0), Color.argb(123, 0, 255, 0), Color.argb(123, 200, 200, 0), 4, 25, 0);
timeLayout_3.setTimeLines(this, TimelineView.LineOrientation.VERTICAL, 5, R.color.colorPrimary, android.R.color.darker_gray);
stepLayout_1.setTimeLines(this, StepLineView.LineOrientation.HORIZONTAL, 7, R.color.colorPrimary, android.R.color.darker_gray, R.color.colorPrimary);
stepLayout_2.setTimeLines(this, StepLineView.LineOrientation.VERTICAL, 12, Color.argb(123, 0, 0, 255), Color.argb(123, 0, 255, 0), Color.argb(123, 0, 255, 0), 3, 20, 10);
stepLayout_3.setTimeLines(this, StepLineView.LineOrientation.VERTICAL, 5, R.color.orange, R.color.yellow, R.color.orange);
stepLayout_4.setTimeLines(this, StepLineView.LineOrientation.VERTICAL, 6, Color.argb(200, 255, 0, 0), Color.argb(100, 255, 0, 0), Color.argb(123, 200, 200, 0), 4, 25, 0);
stepLayout_5.setTimeLines(this, StepLineView.LineOrientation.VERTICAL, 40, R.color.colorPrimary, android.R.color.darker_gray, Color.argb(123, 200, 200, 0), 4, 25, 0);

timeLayout_1.setUnitCompleted(0);
timeLayout_1.setUnitActive(1);
timeLayout_1.setUnitCompleted(2);
timeLayout_1.setUnitInactive(3);

timeLayout_2.setUnitCompleted(0);
timeLayout_2.setUnitCompleted(1);
timeLayout_2.setUnitCompleted(2);
timeLayout_2.setUnitCompleted(3);
timeLayout_2.setUnitCompleted(4);
timeLayout_2.setUnitCompleted(5);
timeLayout_2.setUnitCompleted(6);
timeLayout_2.setUnitInactive(7);

timeLayout_3.setUnitCompleted(0);
timeLayout_3.setUnitCompleted(1);
timeLayout_3.setUnitCompleted(2);
timeLayout_3.setUnitInactive(3);

// timeLayout_4.setUnitCompleted(0);
// timeLayout_4.setUnitCompleted(1);
// timeLayout_4.setUnitCompleted(2);
// timeLayout_4.setUnitCompleted(3);
// timeLayout_4.setUnitCompleted(4);
// timeLayout_4.setUnitCompleted(5);
// timeLayout_4.setUnitCompleted(6);
// timeLayout_4.setUnitInactive(7);

((Button) findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timeLayout_4.setUnitCompleted(index);
timeLayout_4.setUnitInactive(++index);
timeLayout_2.setUnitCompleted(1);

}
});

((Button) findViewById(R.id.btn2)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("pttt", "-\nColor=" + timeLayout_1.timelineViews.get(0).getMainColor() + "," + timeLayout_1.timelineViews.get(4).getMainColor());
Log.d("pttt", "-\nColor=" + timeLayout_2.timelineViews.get(0).getMainColor() + "," + timeLayout_2.timelineViews.get(4).getMainColor());
Log.d("pttt", "-\nColor=" + timeLayout_4.timelineViews.get(0).getMainColor() + "," + timeLayout_4.timelineViews.get(4).getMainColor());
Log.d("pttt", "-\nColor=" + timeLayout_3.timelineViews.get(0).getMainColor() + "," + timeLayout_3.timelineViews.get(4).getMainColor());
stepLayout_1.setUnitCompleted(index);
stepLayout_2.setUnitCompleted(index);
stepLayout_3.setUnitCompleted(index);
stepLayout_4.setUnitCompleted(index);
stepLayout_5.setUnitCompleted(index);
index++;
stepLayout_1.setUnitInactive(index);
stepLayout_2.setUnitInactive(index);
stepLayout_3.setUnitInactive(index);
stepLayout_4.setUnitInactive(index);
stepLayout_5.setUnitInactive(index);

}
});
}

@Override
protected void onResume() {
super.onResume();
//timeLayout_4.invalidate();
}
}
60 changes: 31 additions & 29 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,63 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<mishmarot.guy.com.timelineunit.TimeLineLayout
android:id="@+id/timeLayout_4"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/timeLayout_1"
android:layout_margin="5dp"
android:layout_toEndOf="@+id/timeLayout_3"
android:orientation="vertical" />

<mishmarot.guy.com.timelineunit.TimeLineLayout
android:id="@+id/timeLayout_1"
<mishmarot.guy.com.timelineunit.StepLineLayout
android:id="@+id/stepLayout_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:orientation="horizontal" />

<mishmarot.guy.com.timelineunit.TimeLineLayout
android:id="@+id/timeLayout_2"
<mishmarot.guy.com.timelineunit.StepLineLayout
android:id="@+id/stepLayout_2"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_below="@+id/timeLayout_1"
android:layout_below="@+id/stepLayout_1"
android:layout_margin="5dp"
android:orientation="horizontal">

</mishmarot.guy.com.timelineunit.TimeLineLayout>
</mishmarot.guy.com.timelineunit.StepLineLayout>

<mishmarot.guy.com.timelineunit.TimeLineLayout
android:id="@+id/timeLayout_3"
<mishmarot.guy.com.timelineunit.StepLineLayout
android:id="@+id/stepLayout_3"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/timeLayout_1"
android:layout_below="@+id/stepLayout_1"
android:layout_margin="5dp"
android:layout_toEndOf="@+id/timeLayout_2"
android:layout_toEndOf="@+id/stepLayout_2"
android:orientation="vertical" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<mishmarot.guy.com.timelineunit.StepLineLayout
android:id="@+id/stepLayout_4"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Button" />
android:layout_below="@+id/stepLayout_1"
android:layout_margin="5dp"
android:layout_toEndOf="@+id/stepLayout_3"
android:orientation="vertical" />

<mishmarot.guy.com.timelineunit.StepLineLayout
android:id="@+id/stepLayout_5"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@+id/stepLayout_1"
android:layout_margin="5dp"
android:layout_toEndOf="@+id/stepLayout_4"
android:orientation="vertical" />

<Button
android:id="@+id/btn2"
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="BTN 2" />
android:text="Increase" />


</RelativeLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<color name="colorPrimary">#004cff</color>
<color name="colorPrimaryDark">#2f7cff</color>
<color name="colorAccent">#FF4081</color>

<color name="yellow">#fff940</color>
<color name="orange">#ffaa00</color>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">Test Time</string>
<string name="app_name">Step Line Library</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package mishmarot.guy.com.timelineunit;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class StepLineLayout extends LinearLayout {

public ArrayList<StepLineView> stepLineViews;

int mainColor = Color.argb(255, 0, 0, 200);
int secondColor = Color.argb(255, 0, 0, 200);
int lineColor = Color.argb(255, 0, 0, 200);

int markerRadius = 20;
int lineSize = 3;
int linePadding = 0;
StepLineView.LineOrientation lineOrientation = StepLineView.LineOrientation.VERTICAL;

public StepLineLayout(Context context) {
this(context, null, 0);
}

public StepLineLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public StepLineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(Context context) {
this.setOrientation(LinearLayout.HORIZONTAL);
}

public void setTimeLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor, int _lineSize, int _markerRadius, int _linePadding){
this.lineSize = _lineSize;
this.markerRadius = _markerRadius;
this.linePadding = _linePadding;
setTimeLines(context, _lineOrientation, _numOfItems, _mainColor, _secondColor, _lineColor);
}

public void setTimeLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor){

// If the user send resource int instead of Color int..
try {
_lineColor = ResourcesCompat.getColor(getResources(), _lineColor, null);
} catch (Exception ignored) { }

try {
_mainColor = ResourcesCompat.getColor(getResources(), _mainColor, null);
} catch (Exception ignored) { }

try {
_secondColor = ResourcesCompat.getColor(getResources(), _secondColor, null);
} catch (Exception ignored) { }


this.mainColor = _mainColor;
this.lineColor = _lineColor;
this.secondColor = _secondColor;
setTimeLines(context, _lineOrientation, _numOfItems);
}

private void setTimeLines(Context context, StepLineView.LineOrientation _lineOrientation, int numOfItems) {
stepLineViews = new ArrayList<>();

this.lineOrientation = _lineOrientation;
if (_lineOrientation == StepLineView.LineOrientation.HORIZONTAL)
this.setOrientation(LinearLayout.HORIZONTAL);
else
this.setOrientation(LinearLayout.VERTICAL);

if (numOfItems <=0) {
return;
}
else if (numOfItems == 1) {
StepLineView stepLineView4 = getTimeLineUnit(context, StepLineView.LineType.ONLY_ONE, StepLineView.OrderStatus.ACTIVE);
stepLineViews.add(stepLineView4);
}
else {
// numOfItems > 1
StepLineView stepLineView0 = getTimeLineUnit(context, StepLineView.LineType.BEGIN, StepLineView.OrderStatus.ACTIVE);
stepLineViews.add(stepLineView0);

for (int i = 1; i < numOfItems-1; i++) {
StepLineView stepLineView1 = getTimeLineUnit(context, StepLineView.LineType.NORMAL, StepLineView.OrderStatus.ACTIVE);
stepLineViews.add(stepLineView1);
}

StepLineView stepLineView2 = getTimeLineUnit(context, StepLineView.LineType.END, StepLineView.OrderStatus.ACTIVE);
stepLineViews.add(stepLineView2);
}

attachListToView();
}

private void attachListToView() {
for (int i = 0; i < stepLineViews.size(); i++) {
this.addView(stepLineViews.get(i));
}
}

public void setUnitActive(int activeIndex) {
if (activeIndex >= 0 && activeIndex < stepLineViews.size())
stepLineViews.get(activeIndex).setOrderStatus(StepLineView.OrderStatus.ACTIVE);
}

public void setUnitInactive(int inactiveIndex) {
if (inactiveIndex >= 0 && inactiveIndex < stepLineViews.size())
stepLineViews.get(inactiveIndex).setOrderStatus(StepLineView.OrderStatus.INACTIVE);
}

public void setUnitCompleted(int completedIndex) {
if (completedIndex >= 0 && completedIndex < stepLineViews.size())
stepLineViews.get(completedIndex).setOrderStatus(StepLineView.OrderStatus.COMPLETED);
}

public StepLineView getTimeLineUnit(Context context, StepLineView.LineType lineType, StepLineView.OrderStatus orderStatus) {
// markerSize - default=20
LinearLayout.LayoutParams linearParams;
if (lineOrientation == StepLineView.LineOrientation.HORIZONTAL)
linearParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
else
linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);

float density = context.getResources().getDisplayMetrics().density;

StepLineView stepLineView = new StepLineView(context, null);
stepLineView.setMarkerSize(markerRadius);
stepLineView.setMainColor(mainColor);
stepLineView.setSecondColor(secondColor);
stepLineView.setLineColor(lineColor);
stepLineView.initLine(lineType);
stepLineView.setLayoutParams(linearParams);
stepLineView.setLineOrientation(lineOrientation);
stepLineView.setLineSize((int) (lineSize * density));
stepLineView.setMarkerSize((int) (markerRadius * density));
stepLineView.setLinePadding(linePadding);
stepLineView.setDrawables();
stepLineView.setOrderStatus(orderStatus);

return stepLineView;
}
}
Loading

0 comments on commit d7587a8

Please sign in to comment.