Skip to content

Commit 250f7f2

Browse files
Optimize some method names
1 parent a8cf311 commit 250f7f2

File tree

20 files changed

+234
-110
lines changed

20 files changed

+234
-110
lines changed

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,77 +22,65 @@ import android.view.ViewGroup
2222
import androidx.annotation.StringRes
2323
import androidx.fragment.app.Fragment
2424

25-
interface LoadingState : LoadingStateView.OnReloadListener {
26-
fun Activity.decorateContentView(isDecorated: Boolean = true)
27-
fun View.decorateWithLoadingState(isDecorated: Boolean = true): View
25+
interface LoadingState {
26+
fun Activity.decorateContentView(listener: OnReloadListener, isDecorated: Boolean = true)
27+
fun View.decorate(listener: OnReloadListener, isDecorated: Boolean = true): View
2828
fun registerView(vararg viewDelegates: LoadingStateView.ViewDelegate)
2929
fun Activity.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType = NavBtnType.ICON, block: (ToolbarConfig.() -> Unit)? = null)
3030
fun Activity.setToolbar(title: String? = null, navBtnType: NavBtnType = NavBtnType.ICON, block: (ToolbarConfig.() -> Unit)? = null)
3131
fun Fragment.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType = NavBtnType.ICON, block: (ToolbarConfig.() -> Unit)? = null)
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 <T : LoadingStateView.ViewDelegate> updateView(viewType: Any, block: T.() -> Unit)
36-
fun Activity.updateToolbar(block: ToolbarConfig.() -> Unit)
37-
fun Fragment.updateToolbar(block: ToolbarConfig.() -> Unit)
3835
fun showLoadingView()
3936
fun showContentView()
4037
fun showErrorView()
4138
fun showEmptyView()
4239
fun showCustomView(viewType: Any)
40+
fun updateToolbar(block: ToolbarConfig.() -> Unit)
41+
fun <T : LoadingStateView.ViewDelegate> updateView(viewType: Any, block: T.() -> Unit)
4342
}
4443

4544
class LoadingStateImpl : LoadingState {
4645
private var loadingStateView: LoadingStateView? = null
4746

48-
override fun Activity.decorateContentView(isDecorated: Boolean) {
49-
findViewById<ViewGroup>(android.R.id.content).getChildAt(0)
50-
.decorateWithLoadingState(isDecorated)
47+
override fun Activity.decorateContentView(listener: OnReloadListener, isDecorated: Boolean) {
48+
findViewById<ViewGroup>(android.R.id.content).getChildAt(0).decorate(listener, isDecorated)
5149
}
5250

53-
override fun View.decorateWithLoadingState(isDecorated: Boolean): View {
54-
return if (isDecorated) {
55-
LoadingStateView(this).apply {
56-
setOnReloadListener(::onReload)
57-
loadingStateView = this
58-
}.decorView
51+
override fun View.decorate(listener: OnReloadListener, isDecorated: Boolean): View =
52+
if (isDecorated) {
53+
LoadingStateView(this, listener).also { loadingStateView = it }.decorView
5954
} else {
6055
this
6156
}
62-
}
6357

6458
override fun registerView(vararg viewDelegates: LoadingStateView.ViewDelegate) {
6559
loadingStateView?.register(*viewDelegates)
6660
}
6761

68-
override fun Activity.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) =
62+
override fun Activity.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) {
6963
setToolbar(getString(titleId), navBtnType, block)
64+
}
7065

71-
override fun Activity.setToolbar(title: String?, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) =
66+
override fun Activity.setToolbar(title: String?, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) {
7267
setHeaders(ToolbarViewDelegate(title, navBtnType, block))
68+
}
7369

74-
override fun Fragment.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) =
70+
override fun Fragment.setToolbar(@StringRes titleId: Int, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) {
7571
setToolbar(getString(titleId), navBtnType, block)
72+
}
7673

77-
override fun Fragment.setToolbar(title: String?, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) =
74+
override fun Fragment.setToolbar(title: String?, navBtnType: NavBtnType, block: (ToolbarConfig.() -> Unit)?) {
7875
setHeaders(ToolbarViewDelegate(title, navBtnType, block))
76+
}
7977

8078
override fun Activity.setHeaders(vararg delegates: LoadingStateView.ViewDelegate) {
81-
loadingStateView?.setDecorHeader(*delegates)
79+
loadingStateView?.setHeaders(*delegates)
8280
}
8381

8482
override fun Fragment.setHeaders(vararg delegates: LoadingStateView.ViewDelegate) {
85-
loadingStateView?.addChildDecorHeader(*delegates)
86-
}
87-
88-
override fun Activity.updateToolbar(block: ToolbarConfig.() -> Unit) =
89-
updateView<ToolbarViewDelegate>(ViewType.TITLE) { bind(config.apply(block)) }
90-
91-
override fun Fragment.updateToolbar(block: ToolbarConfig.() -> Unit) =
92-
updateView<ToolbarViewDelegate>(ViewType.TITLE) { bind(config.apply(block)) }
93-
94-
override fun <T : LoadingStateView.ViewDelegate> updateView(viewType: Any, block: T.() -> Unit) {
95-
loadingStateView?.getViewDelegate<T>(viewType)?.apply(block)
83+
loadingStateView?.addChildHeaders(*delegates)
9684
}
9785

9886
override fun showLoadingView() {
@@ -115,5 +103,11 @@ class LoadingStateImpl : LoadingState {
115103
loadingStateView?.showView(viewType)
116104
}
117105

118-
override fun onReload() = Unit
106+
override fun updateToolbar(block: ToolbarConfig.() -> Unit) {
107+
updateView<ToolbarViewDelegate>(ViewType.TITLE) { bind(config.apply(block)) }
108+
}
109+
110+
override fun <T : LoadingStateView.ViewDelegate> updateView(viewType: Any, block: T.() -> Unit) {
111+
loadingStateView?.getViewDelegate<T>(viewType)?.apply(block)
112+
}
119113
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2019. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
@file:Suppress("unused")
218

319
package com.dylanc.loadingstateview

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2019. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
@file:Suppress("unused")
218

319
package com.dylanc.loadingstateview

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

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,24 @@ import java.util.*
3232
/**
3333
* @author Dylan Cai
3434
*/
35-
class LoadingStateView(private val contentView: View) {
35+
class LoadingStateView @JvmOverloads constructor(
36+
private val contentView: View,
37+
var onReloadListener: OnReloadListener? = null
38+
) {
3639
lateinit var decorView: View private set
3740
lateinit var currentViewType: Any private set
3841
private lateinit var contentParent: ViewGroup
3942
private val parent: ViewGroup?
4043
private var currentView: View? = null
41-
private var onReloadListener: OnReloadListener? = null
4244
private var viewDelegates: HashMap<Any, ViewDelegate> = HashMap()
4345
private val viewCashes: HashMap<Any, View> = HashMap()
4446

4547
/**
46-
* Constructs a LoadingStateView with a activity and a content view delegate
47-
*
48-
* @param activity the activity
48+
* Constructs a LoadingStateView with an activity
4949
*/
50-
constructor(activity: Activity) :
51-
this(activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0))
50+
@JvmOverloads
51+
constructor(activity: Activity, listener: OnReloadListener? = null) :
52+
this(activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0), listener)
5253

5354
init {
5455
viewDelegatePool?.apply { ViewDelegatePool(this@LoadingStateView).invoke() }
@@ -57,6 +58,17 @@ class LoadingStateView(private val contentView: View) {
5758
setDecorView(LinearDecorViewDelegate(emptyList()))
5859
}
5960

61+
/**
62+
* Adds one or more views to decorate content in the header.
63+
*
64+
* @param delegates the view delegates of creating view
65+
*/
66+
fun setHeaders(vararg delegates: ViewDelegate) =
67+
setDecorView(LinearDecorViewDelegate(delegates.map {
68+
register(it)
69+
getView(it.viewType)
70+
}))
71+
6072
/**
6173
* Sets an view delegate for decorating content view.
6274
*
@@ -82,12 +94,12 @@ class LoadingStateView(private val contentView: View) {
8294
}
8395

8496
/**
85-
* Adds one or more views to decorate content in the header.
97+
* Adds child decorative header between the content and the decorative view.
8698
*
8799
* @param delegates the view delegates of creating view
88100
*/
89-
fun setDecorHeader(vararg delegates: ViewDelegate) =
90-
setDecorView(LinearDecorViewDelegate(delegates.map {
101+
fun addChildHeaders(vararg delegates: ViewDelegate) =
102+
addChildDecorView(LinearDecorViewDelegate(delegates.map {
91103
register(it)
92104
getView(it.viewType)
93105
}))
@@ -106,17 +118,6 @@ class LoadingStateView(private val contentView: View) {
106118
showView(ViewType.CONTENT)
107119
}
108120

109-
/**
110-
* Adds child decorative header between the content and the decorative view.
111-
*
112-
* @param delegates the view delegates of creating view
113-
*/
114-
fun addChildDecorHeader(vararg delegates: ViewDelegate) =
115-
addChildDecorView(LinearDecorViewDelegate(delegates.map {
116-
register(it)
117-
getView(it.viewType)
118-
}))
119-
120121
private fun DecorViewDelegate.createDecorView() =
121122
onCreateDecorView(LayoutInflater.from(contentView.context)).also { decorView ->
122123
contentView.layoutParams?.let { decorView.layoutParams = it }
@@ -125,21 +126,12 @@ class LoadingStateView(private val contentView: View) {
125126
/**
126127
* Registers the view delegate of creating view before showing view.
127128
*
128-
* @param viewDelegate the view delegate of creating view
129+
* @param delegates the view delegate of creating view
129130
*/
130131
fun register(vararg delegates: ViewDelegate) {
131132
delegates.forEach { viewDelegates[it.viewType] = it }
132133
}
133134

134-
/**
135-
* Called if you need to handle reload event, you can get the listener of reloading data from view holder.
136-
*
137-
* @param onReloadListener the listener of reloading data
138-
*/
139-
fun setOnReloadListener(onReloadListener: OnReloadListener) {
140-
this.onReloadListener = onReloadListener
141-
}
142-
143135
@JvmOverloads
144136
fun showLoadingView(animation: Animation? = null) = showView(ViewType.LOADING, animation)
145137

@@ -163,21 +155,25 @@ class LoadingStateView(private val contentView: View) {
163155
if (currentView == null) {
164156
addView(viewType)
165157
} else {
166-
viewCashes[viewType]?.let { addView(viewType) }
158+
if (viewCashes[viewType] == null) addView(viewType)
167159
if (viewType != currentViewType) {
168-
getView(viewType).visibility = View.VISIBLE
160+
val view = getView(viewType)
161+
view.visibility = View.VISIBLE
169162
if (animation != null) {
170163
animation.onStartHideAnimation(currentView, currentViewType)
171-
animation.onStartShowAnimation(getView(viewType), getViewDelegate<ViewDelegate>(viewType).viewType)
164+
animation.onStartShowAnimation(view, getViewDelegate<ViewDelegate>(viewType).viewType)
172165
} else {
173166
currentView.visibility = View.GONE
174167
}
175-
this.currentView = getView(viewType)
168+
this.currentView = view
176169
}
177170
}
178171
currentViewType = viewType
179172
}
180173

174+
@Suppress("UNCHECKED_CAST")
175+
fun <T : ViewDelegate> getViewDelegate(viewType: Any) = viewDelegates[viewType] as T
176+
181177
private fun addView(viewType: Any) {
182178
val view = getView(viewType)
183179
if (view.parent != null) {
@@ -195,21 +191,14 @@ class LoadingStateView(private val contentView: View) {
195191

196192
private fun getView(viewType: Any): View {
197193
if (viewCashes[viewType] == null) {
198-
addViewHolder(viewType)
194+
val viewDelegate: ViewDelegate = getViewDelegate(viewType)
195+
val view = viewDelegate.onCreateView(LayoutInflater.from(contentParent.context), contentParent)
196+
viewDelegate.onReloadListener = onReloadListener
197+
viewCashes[viewType] = view
199198
}
200199
return viewCashes[viewType]!!
201200
}
202201

203-
@Suppress("UNCHECKED_CAST")
204-
fun <T : ViewDelegate> getViewDelegate(viewType: Any) = viewDelegates[viewType] as T
205-
206-
private fun addViewHolder(viewType: Any) {
207-
val viewDelegate: ViewDelegate = getViewDelegate(viewType)
208-
val view = viewDelegate.onCreateView(LayoutInflater.from(contentParent.context), contentParent)
209-
viewDelegate.onReloadListener = onReloadListener
210-
viewCashes[viewType] = view
211-
}
212-
213202
abstract class ViewDelegate(val viewType: Any) {
214203
var onReloadListener: OnReloadListener? = null
215204
internal set
@@ -223,7 +212,6 @@ class LoadingStateView(private val contentView: View) {
223212

224213
abstract class DecorViewDelegate {
225214
abstract fun onCreateDecorView(inflater: LayoutInflater): View
226-
227215
abstract fun getContentParent(decorView: View): ViewGroup
228216
}
229217

@@ -246,17 +234,12 @@ class LoadingStateView(private val contentView: View) {
246234
fun register(vararg delegates: ViewDelegate) = stateView.register(*delegates)
247235
}
248236

249-
fun interface OnReloadListener {
250-
fun onReload()
251-
}
252-
253237
fun interface Callback<in T> {
254238
fun T.invoke()
255239
}
256240

257241
interface Animation {
258242
fun onStartShowAnimation(view: View, viewType: Any)
259-
260243
fun onStartHideAnimation(view: View, viewType: Any)
261244
}
262245

@@ -270,6 +253,10 @@ class LoadingStateView(private val contentView: View) {
270253
}
271254
}
272255

256+
fun interface OnReloadListener {
257+
fun onReload()
258+
}
259+
273260
enum class ViewType {
274261
TITLE, LOADING, CONTENT, ERROR, EMPTY
275262
}

sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.dylanc.loadingstateview.LoadingStateView;
2222
import com.dylanc.loadingstateview.ViewType;
23+
import com.dylanc.loadingstateview.sample.java.animation.FadeAnimation;
2324
import com.dylanc.loadingstateview.sample.java.delegate.EmptyViewDelegate;
2425
import com.dylanc.loadingstateview.sample.java.delegate.ErrorViewDelegate;
2526
import com.dylanc.loadingstateview.sample.java.delegate.LoadingViewDelegate;

sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/base/BaseActivity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* 这是耦合度较低的封装方式,没有任何抽象方法,可以很方便地将基类里的代码拷贝到其它项目的基类里使用。
28-
*
28+
* <p>
2929
* 使用该基类时注意以下事项:
3030
* 1. 显示对应视图之前需要注册适配器,可以设置全局适配器,某个页面想修改样式时再注册个新的适配器。
3131
* 2. 设置标题栏的方法应该根据项目需要进行编写,下面提供了参考示例。
@@ -45,11 +45,10 @@ public void setContentView(int layoutResID) {
4545
public void setContentView(int layoutResID, @IdRes int contentViewId) {
4646
super.setContentView(layoutResID);
4747
if (contentViewId == 0) {
48-
loadingStateView = new LoadingStateView(this);
48+
loadingStateView = new LoadingStateView(this, this::onReload);
4949
} else {
5050
loadingStateView = new LoadingStateView(findViewById(contentViewId));
5151
}
52-
loadingStateView.setOnReloadListener(this::onReload);
5352
}
5453

5554
/**
@@ -64,7 +63,7 @@ public void setToolbar(String title, NavIconType type) {
6463
}
6564

6665
public void setToolbar(String title, NavIconType type, int menuId) {
67-
loadingStateView.setDecorHeader(new ToolbarViewDelegate(title, type, menuId, this::onOptionsItemSelected));
66+
loadingStateView.setHeaders(new ToolbarViewDelegate(title, type, menuId, this::onOptionsItemSelected));
6867
}
6968

7069
public void showLoadingView() {

0 commit comments

Comments
 (0)