Skip to content

Commit

Permalink
add Color Checked and unChecked
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemradwan committed Jul 1, 2020
1 parent debce2b commit f6efa53
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 54 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/java/com/kareemradwan/stepeer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity(), SteeperView.SteeperHandler {
steeper.setAdapter(adapter)
// Assign Controller

// steeper.setController(this)
steeper.setController(this)
nextStep.setOnClickListener { steeper.nextStep() }

}
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:fillViewport="true">


<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">


<com.kradwan.stepeer.view.SteeperView
android:id="@+id/steeper"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:padding="20dp"
app:checked_color="#342987"
app:unchecked_color="#909090" />



<Button
android:layout_marginTop="20dp"
android:id="@+id/nextStep"
android:text="Next"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Next" />

</LinearLayout>
</LinearLayout>
</ScrollView>
11 changes: 8 additions & 3 deletions app/src/main/res/layout/order_step.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

android:layout_marginStart="20dp"
android:paddingStart="20dp"

android:paddingBottom="10dp"
android:paddingEnd="20dp"
android:layout_height="match_parent"
android:orientation="vertical">

Expand All @@ -12,15 +15,17 @@
android:id="@+id/tvTitle"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="Title"
android:text="@string/app_name"
android:textSize="24sp" />

<View
android:layout_width="match_parent"
android:layout_height="50dp"/>

<TextView
android:id="@+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="Title"
android:textSize="14sp" />
</LinearLayout>
1 change: 0 additions & 1 deletion stepeer/src/main/java/com/kradwan/stepeer/model/IStep.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kradwan.stepeer.model


interface IStep {
fun isChecked(): Boolean
}
10 changes: 10 additions & 0 deletions stepeer/src/main/java/com/kradwan/stepeer/model/StepColor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kradwan.stepeer.model

data class StepColor(var color: Int){

companion object{

const val COLOR_CHECKED = "checked_color"
const val COLOR_UNCHECKED = "unchecked_color"
}
}
47 changes: 33 additions & 14 deletions stepeer/src/main/java/com/kradwan/stepeer/view/SingleStepView.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.kradwan.stepeer.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.CheckBox
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.kradwan.stepeer.R
import com.kradwan.stepeer.model.IStep
import java.lang.Exception
import com.kradwan.stepeer.model.StepColor

class SingleStepView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

@SuppressLint("ViewConstructor")
class SingleStepView(context: Context, private var colors: HashMap<String, StepColor>?) :
FrameLayout(context) {

private lateinit var view: View
private lateinit var rootView: LinearLayout
Expand All @@ -22,10 +26,15 @@ class SingleStepView(context: Context, attrs: AttributeSet?) : FrameLayout(conte
private lateinit var stepDividerSolid: View
private lateinit var stepContentContainer: LinearLayout


private var checkBoxSelectedColor: StepColor = StepColor(Color.CYAN)
private var checkBoxUnSelectedColor: StepColor = StepColor(Color.BLUE)

init {
initViews()
}

@SuppressLint("ResourceType")
private fun initViews() {
view = inflate(context, R.layout.item_step, this)
rootView = view.findViewById(R.id.rootSingleStep)
Expand All @@ -34,33 +43,43 @@ class SingleStepView(context: Context, attrs: AttributeSet?) : FrameLayout(conte
stepDivider = view.findViewById(R.id.stepDivider)
stepDividerSolid = view.findViewById(R.id.stepDividerSolid)
stepContentContainer = view.findViewById(R.id.stepContentContainer)
}

fun <T : IStep> setModel(model: T, view: View, last: Boolean) {

stepCheckBox.isChecked = model.isChecked()
stepContentContainer.post {

if (model.isChecked())
selectAsDone(last)
if (colors != null) {
if (colors?.contains("checked_color")!!) {
checkBoxSelectedColor = colors?.get("checked_color")!!
}

if (colors?.contains("unchecked_color")!!) {
checkBoxUnSelectedColor = colors?.get("unchecked_color")!!
stepDivider.setBackgroundColor(checkBoxUnSelectedColor.color)
stepDividerSolid.setBackgroundColor(checkBoxUnSelectedColor.color)
stepCheckBox.buttonTintList = ColorStateList.valueOf(checkBoxUnSelectedColor.color)
}
}
}

fun <T : IStep> setModel(model: T, view: View, last: Boolean) {
stepCheckBox.isChecked = model.isChecked()
stepContentContainer.post {
try {
stepContentContainer.addView(view)
} catch (ex: Exception) {
}
if (model.isChecked())
selectAsDone(last)


if (last)
stepDivider.visibility = View.GONE

stepDivider.visibility = if (last) View.GONE else View.VISIBLE
}

}

fun selectAsDone(last: Boolean) {
stepCheckBox.isChecked = true
stepCheckBox.buttonTintList = ColorStateList.valueOf(checkBoxSelectedColor.color)

if (!last) {
stepDividerSolid.setBackgroundColor(checkBoxSelectedColor.color)
stepDividerSolid.visibility = View.VISIBLE
val move = AnimationUtils.loadAnimation(context, R.anim.move)
stepDividerSolid.animation = move
Expand Down
39 changes: 23 additions & 16 deletions stepeer/src/main/java/com/kradwan/stepeer/view/SteeperView.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.kradwan.stepeer.view


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
Expand All @@ -10,6 +11,7 @@ import androidx.core.view.get
import com.kradwan.stepeer.R
import com.kradwan.stepeer.adapter.StepAdapter
import com.kradwan.stepeer.model.IStep
import com.kradwan.stepeer.model.StepColor

class SteeperView(context: Context, private val attrs: AttributeSet?) :
FrameLayout(context, attrs) {
Expand All @@ -20,34 +22,40 @@ class SteeperView(context: Context, private val attrs: AttributeSet?) :
private var callback: SteeperHandler? = null

private var currentStep: Int = 0

private var colors = HashMap<String, StepColor>()

init {
initViews()
}

fun <T : IStep> setAdapter(adapter: StepAdapter<T>) {
@SuppressLint("ResourceType")
private fun initViews() {
view = inflate(context, R.layout.steeper_layout, this)
containerSteeper = view.findViewById(R.id.rootSteeper)

if (attrs != null) {
val defaultColor = StepColor(Color.parseColor("#1C8AFF"))
val style = context.theme.obtainStyledAttributes(attrs, R.styleable.SteeperView, 0, 0)
val checkColor = style.getColor(0, -1)
val unCheckColor = style.getColor(1, -1)
colors[StepColor.COLOR_CHECKED] =
if (checkColor != -1) StepColor(checkColor) else defaultColor
colors[StepColor.COLOR_UNCHECKED] =
if (unCheckColor != -1) StepColor(unCheckColor) else defaultColor
}
}

fun <T : IStep> setAdapter(adapter: StepAdapter<T>) {
adapter.models.forEach {
val view = adapter.onCreateView(it)


if (it.isChecked()) {
currentStep++
}

val step = SingleStepView(view.context, attrs)

val step = SingleStepView(view.context, colors)
step.setModel(it, view, adapter.models.last() == it)
containerSteeper.addView(step)

}

}

private fun initViews() {
view = inflate(context, R.layout.steeper_layout, this)
containerSteeper = view.findViewById(R.id.rootSteeper)
}

fun setController(controller: SteeperHandler) {
Expand All @@ -59,17 +67,16 @@ class SteeperView(context: Context, private val attrs: AttributeSet?) :
stepView.selectAsDone(index == containerSteeper.childCount - 1)
}


fun nextStep() {
if (currentStep != containerSteeper.childCount) {
selectAsDone(currentStep++)
}

if (currentStep == containerSteeper.childCount) {
callback?.onFinish()
}
}


interface SteeperHandler {
fun onFinish()
}
Expand Down
14 changes: 5 additions & 9 deletions stepeer/src/main/res/layout/item_step.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="10dp">
android:orientation="horizontal">

<LinearLayout
android:id="@+id/stepInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:minHeight="50dp"
android:orientation="vertical">

<CheckBox
Expand All @@ -25,16 +23,12 @@
android:layout_gravity="center_vertical"
android:button="@drawable/custom_checkbox"
android:checked="true"

android:enabled="false"
android:gravity="center_vertical" />

<FrameLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">
android:layout_height="match_parent">


<View
Expand All @@ -46,6 +40,7 @@
android:minHeight="40dp" />

<View
android:minHeight="40dp"
android:id="@+id/stepDividerSolid"
android:layout_width="4dp"
android:layout_height="match_parent"
Expand All @@ -57,6 +52,7 @@


<LinearLayout

android:id="@+id/stepContentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
9 changes: 9 additions & 0 deletions stepeer/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="SteeperView" >
<attr name="checked_color" format="color|reference" />
<attr name="unchecked_color" format="color|reference" />
</declare-styleable>

</resources>

0 comments on commit f6efa53

Please sign in to comment.