Skip to content

Commit f6daf91

Browse files
Add currentViewType property and updateView method
1 parent 1406707 commit f6daf91

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import android.app.Activity
2222
import android.view.LayoutInflater
2323
import android.view.View
2424
import android.view.ViewGroup
25-
import android.view.ViewGroup.LayoutParams.*
25+
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
2626
import android.widget.FrameLayout
2727
import android.widget.LinearLayout
2828
import androidx.constraintlayout.widget.ConstraintLayout
@@ -50,7 +50,7 @@ class LoadingStateView(private val contentView: View) {
5050
this(activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0))
5151

5252
init {
53-
viewDelegatePool?.let { ViewDelegatePool(this).apply(it) }
53+
viewDelegatePool?.apply { ViewDelegatePool(this@LoadingStateView).invoke() }
5454
parent = contentView.parent as ViewGroup?
5555
register(ViewType.CONTENT, ContentViewDelegate())
5656
setDecorView(LinearDecorViewDelegate(listOf()))
@@ -195,8 +195,8 @@ class LoadingStateView(private val contentView: View) {
195195
if (viewType != currentViewHolder.viewType) {
196196
getViewHolder(viewType).rootView.visibility = View.VISIBLE
197197
if (animation != null) {
198-
animation.onStartHideAnimation(currentViewHolder.rootView, currentViewHolder.viewType!!)
199-
animation.onStartShowAnimation(getViewHolder(viewType).rootView, getViewHolder(viewType).viewType!!)
198+
animation.onStartHideAnimation(currentViewHolder.rootView, currentViewHolder.viewType)
199+
animation.onStartShowAnimation(getViewHolder(viewType).rootView, getViewHolder(viewType).viewType)
200200
} else {
201201
currentViewHolder.rootView.visibility = View.GONE
202202
}
@@ -205,15 +205,35 @@ class LoadingStateView(private val contentView: View) {
205205
}
206206
}
207207

208-
fun notifyDataSetChanged(viewType: Any) {
208+
/**
209+
* Gets the current view type.
210+
*
211+
* @since v3.0.1
212+
*/
213+
val currentViewType get() = currentViewHolder!!.viewType
214+
215+
/**
216+
* Updates the view by view type. It needs to be called after showing view.
217+
*
218+
* @since v3.0.1
219+
*/
220+
@Suppress("UNCHECKED_CAST")
221+
fun <T : ViewDelegate<*>> updateView(viewType: Any, block: Callback<T>) {
209222
val viewDelegate: ViewDelegate<ViewHolder> = getViewDelegate(viewType)
223+
block.apply { (viewDelegate as T).invoke() }
210224
for (entry in viewDelegates.entries) {
211225
if (entry.value == viewDelegate) {
212226
viewDelegate.onBindViewHolder(getViewHolder(entry.key))
213227
}
214228
}
215229
}
216230

231+
@Deprecated(
232+
"Use the method of update view.",
233+
ReplaceWith("loadingStateView.updateView<>(viewType) {\n\n}")
234+
)
235+
fun notifyDataSetChanged(viewType: Any) = updateView<ViewDelegate<*>>(viewType) {}
236+
217237
private fun addView(viewType: Any) {
218238
val viewHolder = getViewHolder(viewType)
219239
val rootView = viewHolder.rootView
@@ -238,7 +258,7 @@ class LoadingStateView(private val contentView: View) {
238258
}
239259

240260
@Suppress("UNCHECKED_CAST")
241-
fun <T : ViewDelegate<out ViewHolder>> getViewDelegate(viewType: Any) = viewDelegates[viewType] as T
261+
fun <T : ViewDelegate<*>> getViewDelegate(viewType: Any) = viewDelegates[viewType] as T
242262

243263
private fun addViewHolder(viewType: Any) {
244264
val viewDelegate: ViewDelegate<ViewHolder> = getViewDelegate(viewType)
@@ -262,7 +282,7 @@ class LoadingStateView(private val contentView: View) {
262282
}
263283

264284
open class ViewHolder(val rootView: View) {
265-
internal var viewType: Any? = null
285+
internal lateinit var viewType: Any
266286
var onReloadListener: OnReloadListener? = null
267287
internal set
268288
}
@@ -296,17 +316,21 @@ class LoadingStateView(private val contentView: View) {
296316
fun onReload()
297317
}
298318

319+
fun interface Callback<in T> {
320+
fun T.invoke()
321+
}
322+
299323
interface Animation {
300324
fun onStartShowAnimation(view: View, viewType: Any)
301325

302326
fun onStartHideAnimation(view: View, viewType: Any)
303327
}
304328

305329
companion object {
306-
private var viewDelegatePool: (ViewDelegatePool.() -> Unit)? = null
330+
private var viewDelegatePool: Callback<ViewDelegatePool>? = null
307331

308332
@JvmStatic
309-
fun setViewDelegatePool(viewDelegatePool: ViewDelegatePool.() -> Unit) {
333+
fun setViewDelegatePool(viewDelegatePool: Callback<ViewDelegatePool>) {
310334
this.viewDelegatePool = viewDelegatePool
311335
}
312336
}

0 commit comments

Comments
 (0)