Skip to content

Commit

Permalink
Merge pull request #9 from muratcanbur/master
Browse files Browse the repository at this point in the history
This PR introduces show methods with stateinfo.
  • Loading branch information
erkutaras authored Nov 26, 2018
2 parents ec4e0b6 + 058399a commit 8283805
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 27
versionCode 10
versionName "1.0.9"
versionCode 11
versionName "1.1.0"

vectorDrawables.useSupportLibrary = true
}
Expand Down
65 changes: 49 additions & 16 deletions library/src/main/java/com/erkutaras/statelayout/StateLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StateLayout @JvmOverloads constructor(context: Context,
private var infoLayout: View? = null
private var loadingWithContentLayout: View? = null

private var state: State = CONTENT
private var state: State = NONE

override fun onFinishInflate() {
super.onFinishInflate()
Expand Down Expand Up @@ -61,14 +61,35 @@ class StateLayout @JvmOverloads constructor(context: Context,
}

private fun updateWithState() {
when(state) {
when (state) {
LOADING -> loading()
CONTENT -> content()
INFO, ERROR, EMPTY -> info()
LOADING_WITH_CONTENT -> loadingWithContent()
NONE -> hideAll()
}
}

private fun checkChildCount() {
if (childCount > 4 || childCount == 0) {
throwChildCountException()
}
}

private fun hideAll() {
loadingLayout?.visibility = View.GONE
contentLayout?.visibility = View.GONE
infoLayout?.visibility = View.GONE
loadingWithContentLayout?.visibility = GONE
}

private fun throwChildCountException(): Nothing =
throw IllegalStateException("StateLayout can host only one direct child")

private fun inflate(@LayoutRes layoutId: Int): View? {
return LayoutInflater.from(context).inflate(layoutId, null)
}

fun initialState(state: State) {
this.state = state
}
Expand All @@ -82,6 +103,22 @@ class StateLayout @JvmOverloads constructor(context: Context,
return this
}

fun showLoading(stateInfo: StateInfo?) {
showState(stateInfo)
}

fun showContent(stateInfo: StateInfo?) {
showState(stateInfo)
}

fun showInfo(stateInfo: StateInfo?) {
showState(stateInfo)
}

fun showEmpty(stateInfo: StateInfo?) {
showState(stateInfo)
}

fun content(): StateLayout {
state = CONTENT
loadingLayout?.visibility = View.GONE
Expand Down Expand Up @@ -122,7 +159,7 @@ class StateLayout @JvmOverloads constructor(context: Context,
return info()
}

fun infoButtonListener(block:() -> Unit) {
fun infoButtonListener(block: () -> Unit) {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.setOnClickListener {
block.invoke()
}
Expand Down Expand Up @@ -166,6 +203,7 @@ class StateLayout @JvmOverloads constructor(context: Context,
stateInfo.infoMessage?.let { infoMessage(it) }
stateInfo.infoButtonText?.let { infoButtonText(it) }
}
null, NONE -> hideAll()
}
}

Expand All @@ -178,25 +216,20 @@ class StateLayout @JvmOverloads constructor(context: Context,
return this
}

private fun checkChildCount() {
if (childCount > 4 || childCount == 0) {
throwChildCountException()
}
}

private fun throwChildCountException(): Nothing =
throw IllegalStateException("StateLayout can host only one direct child")

private fun inflate(@LayoutRes layoutId: Int): View? {
return LayoutInflater.from(context).inflate(layoutId, null)
}
fun provideLoadingStateInfo() = StateInfo(state = LOADING)
fun provideContentStateInfo() = StateInfo(state = CONTENT)
fun provideErrorStateInfo() = StateInfo(state = ERROR)
fun provideLoadingWithContentStateInfo() = StateInfo(state = LOADING_WITH_CONTENT)
fun provideInfoStateInfo() = StateInfo(state = INFO)
fun provideEmptyStateInfo() = StateInfo(state = EMPTY)
fun provideNoneStateInfo() = StateInfo(state = NONE)

interface OnStateLayoutListener {
fun onStateLayoutInfoButtonClick()
}

enum class State {
LOADING, CONTENT, INFO, LOADING_WITH_CONTENT, ERROR, EMPTY
LOADING, CONTENT, INFO, LOADING_WITH_CONTENT, ERROR, EMPTY, NONE
}

data class StateInfo(
Expand Down

0 comments on commit 8283805

Please sign in to comment.