Skip to content

Commit

Permalink
Merge branch 'release/0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
sangcomz committed Apr 27, 2018
2 parents 12e9491 + 28d70bd commit 3690f32
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 30 deletions.
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

# Chameleoen
# Chameleon


<img src="/pic/logo.png" width = 20%>

[![](https://jitpack.io/v/sangcomz/chameleon.svg)](https://jitpack.io/#sangcomz/chameleon)

Chameleoen deals with the Status of RecyclerView.
Chameleon deals with the Status of RecyclerView.

## What's New in 0.0.3? :tada:
- Add ability to access current state :clap: [@jordond](https://github.com/jordond)

## How to Use

Expand All @@ -17,7 +20,7 @@ Chameleoen deals with the Status of RecyclerView.
}
dependencies {
compile 'com.github.sangcomz:Chameleon:v0.0.1'
compile 'com.github.sangcomz:Chameleon:v0.0.3'
}
```
### Usage
Expand Down Expand Up @@ -46,6 +49,7 @@ Chameleoen deals with the Status of RecyclerView.
app:progressDrawable="@drawable/drawable_progress"
app:useEmptyButton="true"
app:useErrorButton="true"
app:defaultState="LOADING"
tools:context="xyz.sangcomz.chameleonsample.MainActivity">

<android.support.v7.widget.RecyclerView
Expand All @@ -57,22 +61,41 @@ Chameleoen deals with the Status of RecyclerView.
```

#### STATUS
| Status | Description |
|:--------:|------------------|
| CONTENT | show content |
| LOADING | show progress  |
| EMPTY | show empty view |
| ERROR | show error view |
| Status | Description |
|:--------:|-------------------|
| CONTENT | show content |
| LOADING | show progress   |
| EMPTY | show empty view |
| ERROR | show error view |
| NONE | show RecyclerView |

##### usage
```kotlin
// Set state using showState
chameleon.showState(Chameleon.STATE.CONTENT)
chameleon.showState(Chameleon.STATE.LOADING)
chameleon.showState(Chameleon.STATE.EMPTY)
chameleon.showState(Chameleon.STATE.ERROR)
chameleon.showState(Chameleon.STATE.NONE)

// Or use the extension functions
chameleon.setContent()
chameleon.setLoading()
chameleon.setEmpty()
chameleon.setError()
chameleon.setNone()

// As well as toggling between states with a boolean
viewModel.isLoading.observe(this, Observer {
chameleon.loadingOrContent(it) // true = LOADING, false = CONTENT
})

chameleon.contentOrEmpty(true) // CONTENT
chameleon.contentOrEmpty(false) // EMPTY

chameleon.setEmptyButtonClickListener { Toast.makeText(this, "Empty Button!", Toast.LENGTH_LONG).show() }
chameleon.setErrorButtonClickListener { Toast.makeText(this, "Error Button!", Toast.LENGTH_LONG).show() }
chameleon.setStateChangeListener { newState, oldState -> Log.d("Main", "Was $oldState is now $newState") }
```

#### attribute
Expand Down Expand Up @@ -105,6 +128,9 @@ chameleon.setErrorButtonClickListener { Toast.makeText(this, "Error Button!", To
| useErrorButton | Change whether to use error view Button | false |
| progressDrawable | progress drawable setting | - |
| isLargeProgress | Whether to use large progress | false |
| defaultState | Sets the initial state for Chameleon | NONE |

Note - A state of `NONE` means Chameleon won't do anything, and will just show the RecyclerView.


## Result Screen
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'xyz.sangcomz:dalcomz:0.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':chameleon')
}
10 changes: 7 additions & 3 deletions app/src/main/java/xyz/sangcomz/chameleonsample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ class MainActivity : AppCompatActivity() {
}

private fun setChameleonList() {
root.showState(Chameleon.STATE.LOADING)
rv_main_list.adapter = ChameleonAdapter()
rv_main_list.layoutManager = LinearLayoutManager(this)
rv_main_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
getChameleons()
.delay(3000, TimeUnit.MILLISECONDS)
.delay(5000, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Expand All @@ -38,7 +37,9 @@ class MainActivity : AppCompatActivity() {
{
root.showState(Chameleon.STATE.ERROR)
})

root.setStateChangeListener { newState, oldState ->
Toast.makeText(this, "state was $oldState and now is $newState", Toast.LENGTH_LONG).show()
}

}

Expand All @@ -49,6 +50,9 @@ class MainActivity : AppCompatActivity() {

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_displayState -> {
Toast.makeText(this, "State is ${root.getState()}", Toast.LENGTH_LONG).show()
}
R.id.menu_content -> {
root.showState(Chameleon.STATE.CONTENT)
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M11,17h2v-6h-2v6zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM11,9h2L13,7h-2v2z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
app:progressDrawable="@drawable/drawable_progress"
app:useEmptyButton="true"
app:useErrorButton="true"
app:defaultState="LOADING"
tools:context="xyz.sangcomz.chameleonsample.MainActivity">

<android.support.v7.widget.RecyclerView
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/menu_group">

<item
android:id="@+id/menu_displayState"
android:title="@string/display"
android:icon="@drawable/ic_info"
app:showAsAction="always" />

<item
android:id="@+id/menu_content"
android:title="@string/content" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<string name="empty">Empty</string>
<string name="sub_empty">Where is chameleon?</string>
<string name="sub_error">The chameleon is dead.</string>
<string name="display">Display State</string>
</resources>
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

buildscript {
ext.kotlin_version = '1.2.30'
ext.support_version = '27.1.0'
ext.constraint_version = '1.1.0-beta6'
ext.support_version = '27.1.1'
ext.constraint_version = '1.1.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

Expand Down
4 changes: 2 additions & 2 deletions chameleon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ dependencies {
compile "com.android.support:recyclerview-v7:$support_version"
compile "com.android.support.constraint:constraint-layout:$constraint_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
49 changes: 44 additions & 5 deletions chameleon/src/main/java/xyz/sangcomz/chameleon/Chameleon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.Drawable
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintSet
import android.support.constraint.ConstraintSet.*
import android.support.constraint.ConstraintSet.BOTTOM
import android.support.constraint.ConstraintSet.END
import android.support.constraint.ConstraintSet.PARENT_ID
import android.support.constraint.ConstraintSet.START
import android.support.constraint.ConstraintSet.TOP
import android.support.v4.content.ContextCompat
import android.support.v7.widget.AppCompatButton
import android.support.v7.widget.AppCompatImageView
Expand All @@ -30,6 +34,7 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
LOADING,
ERROR,
EMPTY,
NONE,
CONTENT
}

Expand All @@ -41,8 +46,10 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
private var stateButton: AppCompatButton? = null
private var errorButtonListener: ((View) -> Unit)? = null
private var emptyButtonListener: ((View) -> Unit)? = null
private var stateChangeListener: ((newState: STATE, oldState: STATE) -> Unit)? = null

private var chameleonAttr: ChameleonAttr? = null
private var currentState: STATE = STATE.EMPTY

init {
attrs?.let {
Expand Down Expand Up @@ -81,13 +88,32 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
it.getColor(R.styleable.Chameleon_errorButtonBackgroundColor, ContextCompat.getColor(context, R.color.colorTitleText)),
it.getBoolean(R.styleable.Chameleon_useErrorButton, false),
it.getDrawable(R.styleable.Chameleon_progressDrawable),
it.getBoolean(R.styleable.Chameleon_isLargeProgress, false)
it.getBoolean(R.styleable.Chameleon_isLargeProgress, false),
stateFromInt(it.getInt(R.styleable.Chameleon_defaultState, -1))
)
}

}
}

/**
* Get the matching state from the view attribute value
* If the `defaultState` is not set by the developer,
* then by default `STATE.CONTENT` will be set
*
* @param int - Value from the attribute
* @return State to set
*/
private fun stateFromInt(int: Int): STATE {
return when (int) {
1 -> STATE.LOADING
2 -> STATE.ERROR
3 -> STATE.EMPTY
4 -> STATE.NONE
else -> STATE.CONTENT
}
}

override fun addView(child: View?) {
checkValid(child)
super.addView(child)
Expand Down Expand Up @@ -115,6 +141,8 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
initStateSubTextView()
initStateButton(it)
initStateProgressBar(it)

showState(it.defaultState)
}
val constraintSet = ConstraintSet()
constraintSet.clone(this)
Expand Down Expand Up @@ -287,8 +315,6 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
setViewVisibility(progressViewVisible = View.VISIBLE)
}
STATE.EMPTY -> {


chameleonAttr?.let {
setStateImageView(it.emptyDrawable ?: R.drawable.ic_empty.getDrawable(context))
setStateTitleTextView(it.emptyText, it.emptyTextSize, it.emptyTextColor)
Expand All @@ -305,11 +331,20 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
subViewVisible = View.VISIBLE,
retryViewVisible = if (it.useEmptyButton) View.VISIBLE else View.GONE)
}

}
STATE.NONE -> setViewVisibility()
}

if (state != currentState) {
stateChangeListener?.invoke(state, currentState)
}
currentState = state
}

fun getState(): STATE = currentState

fun hasNoContent(): Boolean = stateContentView?.adapter?.itemCount == 0

private fun setViewVisibility(contentViewVisible: Int = View.GONE,
imageViewVisible: Int = View.GONE,
titleViewVisible: Int = View.GONE,
Expand Down Expand Up @@ -365,4 +400,8 @@ open class Chameleon(context: Context?, attrs: AttributeSet?) : ConstraintLayout
fun setEmptyButtonClickListener(clickListener: (View) -> Unit) {
emptyButtonListener = clickListener
}

fun setStateChangeListener(listener: ((newState: STATE, oldState: STATE) -> Unit)) {
stateChangeListener = listener
}
}
35 changes: 35 additions & 0 deletions chameleon/src/main/java/xyz/sangcomz/chameleon/ext/ChameleonExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xyz.sangcomz.chameleon.ext

import xyz.sangcomz.chameleon.Chameleon

fun Chameleon.setError() {
this.showState(Chameleon.STATE.ERROR)
}

fun Chameleon.setLoading() {
this.showState(Chameleon.STATE.LOADING)
}

fun Chameleon.setEmpty() {
this.showState(Chameleon.STATE.EMPTY)
}

fun Chameleon.setContent() {
this.showState(Chameleon.STATE.CONTENT)
}

fun Chameleon?.setErrorIfEmpty() {
if (this != null) {
if (this.hasNoContent()) {
this.setError()
}
}
}

fun Chameleon?.contentOrEmpty(hasContent: Boolean) {
if (hasContent) this?.setContent() else this?.setEmpty()
}

fun Chameleon?.loadingOrContent(isLoading: Boolean) {
if (isLoading) this?.setLoading() else this?.setContent()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.sangcomz.chameleon.model

import android.graphics.drawable.Drawable
import xyz.sangcomz.chameleon.Chameleon

/**
* Created by seokwon.jeong on 17/11/2017.
Expand Down Expand Up @@ -30,7 +31,6 @@ data class ChameleonAttr(var emptyText: String,
var errorButtonBackgroundColor: Int,
var useErrorButton:Boolean,
var progressDrawable: Drawable?,
var isLargeProgress: Boolean


var isLargeProgress: Boolean,
var defaultState: Chameleon.STATE
)
7 changes: 7 additions & 0 deletions chameleon/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
<attr name="progressDrawable" format="reference" />
<attr name="isLargeProgress" format="boolean" />

<attr name="defaultState" format="enum">
<enum name="LOADING" value="1" />
<enum name="ERROR" value="2" />
<enum name="EMPTY" value="3" />
<enum name="NONE" value="4" />
<enum name="CONTENT" value="5" />
</attr>

<!--<attr name="sectionBackgroundColor" format="color" />-->
<!--<attr name="timeLineWidth" format="dimension" />-->
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ':app', ':chameleon'

gradle.ext.set('versionCode', 2)
gradle.ext.set('versionName', '0.0.2')
gradle.ext.set('versionCode', 3)
gradle.ext.set('versionName', '0.0.3')

gradle.ext.set('minSdk', 15)
gradle.ext.set('targetSdk', 27)
Expand Down

0 comments on commit 3690f32

Please sign in to comment.