@@ -39,7 +39,7 @@ class LoadingStateView @JvmOverloads constructor(
39
39
private val parent: ViewGroup ?
40
40
private var currentView: View ? = null
41
41
private var viewDelegates: HashMap <Any , ViewDelegate > = HashMap ()
42
- private val viewCashes : HashMap <Any , View > = HashMap ()
42
+ private val viewCaches : HashMap <Any , View > = HashMap ()
43
43
44
44
/* *
45
45
* Constructs a LoadingStateView with an activity and listener.
@@ -111,6 +111,7 @@ class LoadingStateView @JvmOverloads constructor(
111
111
onCreateDecorView(contentView.context, LayoutInflater .from(contentView.context)).also { decorView ->
112
112
contentView.layoutParams?.let {
113
113
decorView.layoutParams = if (it is ConstraintLayout .LayoutParams ) ConstraintLayout .LayoutParams (it) else it
114
+ (it as ? ViewGroup .MarginLayoutParams )?.setMargins(0 , 0 , 0 , 0 )
114
115
}
115
116
}
116
117
@@ -122,39 +123,41 @@ class LoadingStateView @JvmOverloads constructor(
122
123
fun register (vararg delegates : ViewDelegate ) = delegates.forEach { viewDelegates[it.viewType] = it }
123
124
124
125
@JvmOverloads
125
- fun showLoadingView (animation : Animation ? = null ) = showView(ViewType .LOADING , animation )
126
+ fun showLoadingView (animatable : Animatable ? = defaultAnimatable ) = showView(ViewType .LOADING , animatable )
126
127
127
128
@JvmOverloads
128
- fun showContentView (animation : Animation ? = null ) = showView(ViewType .CONTENT , animation )
129
+ fun showContentView (animatable : Animatable ? = defaultAnimatable ) = showView(ViewType .CONTENT , animatable )
129
130
130
131
@JvmOverloads
131
- fun showErrorView (animation : Animation ? = null ) = showView(ViewType .ERROR , animation )
132
+ fun showErrorView (animatable : Animatable ? = defaultAnimatable ) = showView(ViewType .ERROR , animatable )
132
133
133
134
@JvmOverloads
134
- fun showEmptyView (animation : Animation ? = null ) = showView(ViewType .EMPTY , animation )
135
+ fun showEmptyView (animatable : Animatable ? = defaultAnimatable ) = showView(ViewType .EMPTY , animatable )
135
136
136
137
/* *
137
138
* Shows the view by view type
138
139
*
139
140
* @param viewType the view type of view delegate
140
141
*/
141
142
@JvmOverloads
142
- fun showView (viewType : Any , animation : Animation ? = null ) {
143
+ fun showView (viewType : Any , animatable : Animatable ? = defaultAnimatable ) {
143
144
val currentView = currentView
144
145
if (currentView == null ) {
145
146
addView(viewType)
146
147
} else {
147
- if (viewCashes [viewType] == null ) addView(viewType)
148
+ if (viewCaches [viewType] == null ) addView(viewType)
148
149
if (viewType != currentViewType) {
149
- val view = getView(viewType)
150
- view.visibility = View .VISIBLE
151
- if (animation != null ) {
152
- animation.onStartHideAnimation(currentView, currentViewType)
153
- animation.onStartShowAnimation(view, getViewDelegate<ViewDelegate >(viewType)!! .viewType)
150
+ val nextView = getOrCreateView(viewType)
151
+ nextView.visibility = View .VISIBLE
152
+ val nextViewDelegate = getViewDelegate<ViewDelegate >(viewType)
153
+ nextViewDelegate?.onViewAttached(nextView)
154
+ getViewDelegate<ViewDelegate >(currentViewType)?.onViewDetached(nextView)
155
+ if (animatable != null && nextViewDelegate != null ) {
156
+ animatable.toggleViewsAnimation(nextView, currentView, viewType, currentViewType)
154
157
} else {
155
158
currentView.visibility = View .GONE
156
159
}
157
- this .currentView = view
160
+ this .currentView = nextView
158
161
}
159
162
}
160
163
currentViewType = viewType
@@ -167,7 +170,7 @@ class LoadingStateView @JvmOverloads constructor(
167
170
fun <T : ViewDelegate > getViewDelegate (viewType : Any ) = viewDelegates[viewType] as ? T
168
171
169
172
private fun addView (viewType : Any ) {
170
- val view = getView (viewType)
173
+ val view = getOrCreateView (viewType)
171
174
(view.parent as ? ViewGroup )?.removeView(view)
172
175
if (parent is ConstraintLayout && viewType == ViewType .CONTENT ) {
173
176
val params = view.layoutParams
@@ -179,24 +182,25 @@ class LoadingStateView @JvmOverloads constructor(
179
182
currentView = view
180
183
}
181
184
182
- private fun getView (viewType : Any ): View {
183
- if (viewCashes [viewType] == null ) {
185
+ private fun getOrCreateView (viewType : Any ): View {
186
+ if (viewCaches [viewType] == null ) {
184
187
val viewDelegate = requireNotNull(getViewDelegate(viewType)) { " Please register view delegate for $viewType type." }
185
188
val view = viewDelegate.onCreateView(LayoutInflater .from(contentParent.context), contentParent)
186
189
viewDelegate.onReloadListener = onReloadListener
187
- viewCashes [viewType] = view
190
+ viewCaches [viewType] = view
188
191
}
189
- return viewCashes [viewType]!!
192
+ return viewCaches [viewType]!!
190
193
}
191
194
192
195
abstract class ViewDelegate (val viewType : Any ) {
196
+ abstract fun onCreateView (inflater : LayoutInflater , parent : ViewGroup ): View
197
+ open fun onViewAttached (view : View ) = Unit
198
+ open fun onViewDetached (view : View ) = Unit
193
199
var onReloadListener: OnReloadListener ? = null
194
200
internal set
195
-
196
- abstract fun onCreateView (inflater : LayoutInflater , parent : ViewGroup ): View
197
201
}
198
202
199
- private inner class ContentViewDelegate : LoadingStateView . ViewDelegate (ViewType .CONTENT ) {
203
+ private inner class ContentViewDelegate : ViewDelegate (ViewType .CONTENT ) {
200
204
override fun onCreateView (inflater : LayoutInflater , parent : ViewGroup ) = contentView
201
205
}
202
206
@@ -210,7 +214,7 @@ class LoadingStateView @JvmOverloads constructor(
210
214
211
215
constructor (delegates: Array <out ViewDelegate >) : this (delegates.map {
212
216
register(it)
213
- getView (it.viewType)
217
+ getOrCreateView (it.viewType)
214
218
})
215
219
216
220
override fun onCreateDecorView (context : Context , inflater : LayoutInflater ) =
@@ -233,14 +237,16 @@ class LoadingStateView @JvmOverloads constructor(
233
237
fun T.invoke ()
234
238
}
235
239
236
- interface Animation {
237
- fun onStartShowAnimation (view : View , viewType : Any )
238
- fun onStartHideAnimation (view : View , viewType : Any )
240
+ interface Animatable {
241
+ fun toggleViewsAnimation (showView : View , hideView : View , showViewType : Any , hideViewType : Any )
239
242
}
240
243
241
244
companion object {
242
245
private var poolInitializer: Callback <PoolInitializer >? = null
243
246
247
+ @JvmStatic
248
+ var defaultAnimatable: Animatable ? = null
249
+
244
250
@JvmStatic
245
251
fun setViewDelegatePool (poolInitializer : Callback <PoolInitializer >) {
246
252
this .poolInitializer = poolInitializer
0 commit comments