diff --git a/library/build.gradle b/library/build.gradle index a9ba29b..97863ce 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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 } diff --git a/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt b/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt index 1f1f13e..b4bfdfc 100644 --- a/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt +++ b/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt @@ -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() @@ -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 } @@ -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 @@ -122,7 +159,7 @@ class StateLayout @JvmOverloads constructor(context: Context, return info() } - fun infoButtonListener(block:() -> Unit) { + fun infoButtonListener(block: () -> Unit) { infoLayout?.findViewById