Skip to content

Commit 249a228

Browse files
Optimize code
1 parent 250f7f2 commit 249a228

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

loadingstateview-ktx/src/main/java/com/dylanc/loadingstateview/LoadingState.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface LoadingState {
3232
fun Fragment.setToolbar(title: String? = null, navBtnType: NavBtnType = NavBtnType.ICON, block: (ToolbarConfig.() -> Unit)? = null)
3333
fun Activity.setHeaders(vararg delegates: LoadingStateView.ViewDelegate)
3434
fun Fragment.setHeaders(vararg delegates: LoadingStateView.ViewDelegate)
35+
fun Activity.setDecorView(delegate: LoadingStateView.DecorViewDelegate)
36+
fun Fragment.setDecorView(delegate: LoadingStateView.DecorViewDelegate)
3537
fun showLoadingView()
3638
fun showContentView()
3739
fun showErrorView()
@@ -83,6 +85,14 @@ class LoadingStateImpl : LoadingState {
8385
loadingStateView?.addChildHeaders(*delegates)
8486
}
8587

88+
override fun Activity.setDecorView(delegate: LoadingStateView.DecorViewDelegate) {
89+
loadingStateView?.setDecorView(delegate)
90+
}
91+
92+
override fun Fragment.setDecorView(delegate: LoadingStateView.DecorViewDelegate) {
93+
loadingStateView?.addChildDecorView(delegate)
94+
}
95+
8696
override fun showLoadingView() {
8797
loadingStateView?.showLoadingView()
8898
}

loadingstateview/src/main/java/com/dylanc/loadingstateview/LoadingStateView.kt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
@file:Suppress("unused")
18-
1917
package com.dylanc.loadingstateview
2018

2119
import android.app.Activity
@@ -45,7 +43,7 @@ class LoadingStateView @JvmOverloads constructor(
4543
private val viewCashes: HashMap<Any, View> = HashMap()
4644

4745
/**
48-
* Constructs a LoadingStateView with an activity
46+
* Constructs a LoadingStateView with an activity and listener.
4947
*/
5048
@JvmOverloads
5149
constructor(activity: Activity, listener: OnReloadListener? = null) :
@@ -63,11 +61,7 @@ class LoadingStateView @JvmOverloads constructor(
6361
*
6462
* @param delegates the view delegates of creating view
6563
*/
66-
fun setHeaders(vararg delegates: ViewDelegate) =
67-
setDecorView(LinearDecorViewDelegate(delegates.map {
68-
register(it)
69-
getView(it.viewType)
70-
}))
64+
fun setHeaders(vararg delegates: ViewDelegate) = setDecorView(LinearDecorViewDelegate(*delegates))
7165

7266
/**
7367
* Sets an view delegate for decorating content view.
@@ -98,11 +92,7 @@ class LoadingStateView @JvmOverloads constructor(
9892
*
9993
* @param delegates the view delegates of creating view
10094
*/
101-
fun addChildHeaders(vararg delegates: ViewDelegate) =
102-
addChildDecorView(LinearDecorViewDelegate(delegates.map {
103-
register(it)
104-
getView(it.viewType)
105-
}))
95+
fun addChildHeaders(vararg delegates: ViewDelegate) = addChildDecorView(LinearDecorViewDelegate(*delegates))
10696

10797
/**
10898
* Adds child decorative view between the content and the decorative view.
@@ -128,9 +118,7 @@ class LoadingStateView @JvmOverloads constructor(
128118
*
129119
* @param delegates the view delegate of creating view
130120
*/
131-
fun register(vararg delegates: ViewDelegate) {
132-
delegates.forEach { viewDelegates[it.viewType] = it }
133-
}
121+
fun register(vararg delegates: ViewDelegate) = delegates.forEach { viewDelegates[it.viewType] = it }
134122

135123
@JvmOverloads
136124
fun showLoadingView(animation: Animation? = null) = showView(ViewType.LOADING, animation)
@@ -215,9 +203,14 @@ class LoadingStateView @JvmOverloads constructor(
215203
abstract fun getContentParent(decorView: View): ViewGroup
216204
}
217205

218-
private class LinearDecorViewDelegate(private val views: List<View>) : DecorViewDelegate() {
206+
private inner class LinearDecorViewDelegate(private val views: List<View>) : DecorViewDelegate() {
219207
private lateinit var contentParent: FrameLayout
220208

209+
constructor(vararg delegates: ViewDelegate) : this(delegates.map {
210+
register(it)
211+
getView(it.viewType)
212+
})
213+
221214
override fun onCreateDecorView(inflater: LayoutInflater) =
222215
LinearLayout(inflater.context).apply {
223216
orientation = LinearLayout.VERTICAL

0 commit comments

Comments
 (0)