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+
17+ package com.dylanc.loadingstateview
18+
19+ import android.app.Activity
20+ import android.view.View
21+ import android.view.ViewGroup
22+ import androidx.annotation.StringRes
23+ import androidx.fragment.app.Fragment
24+
25+ class LoadingStateImpl : LoadingState {
26+ private var loadingStateView: LoadingStateView ? = null
27+
28+ override fun Activity.decorateContentView (listener : OnReloadListener , isDecorated : Boolean ) {
29+ findViewById<ViewGroup >(android.R .id.content).getChildAt(0 ).decorate(listener, isDecorated)
30+ }
31+
32+ override fun View.decorate (listener : OnReloadListener , isDecorated : Boolean ): View =
33+ if (isDecorated) {
34+ LoadingStateView (this , listener).also { loadingStateView = it }.decorView
35+ } else {
36+ this
37+ }
38+
39+ override fun registerView (vararg viewDelegates : LoadingStateView .ViewDelegate ) {
40+ loadingStateView?.register(* viewDelegates)
41+ }
42+
43+ override fun Activity.setToolbar (@StringRes titleId : Int , navBtnType : NavBtnType , block : (ToolbarConfig .() -> Unit )? ) {
44+ setToolbar(getString(titleId), navBtnType, block)
45+ }
46+
47+ override fun Activity.setToolbar (title : String? , navBtnType : NavBtnType , block : (ToolbarConfig .() -> Unit )? ) {
48+ loadingStateView?.setHeaders(ToolbarViewDelegate (title, navBtnType, block))
49+ }
50+
51+ override fun Fragment.setToolbar (@StringRes titleId : Int , navBtnType : NavBtnType , block : (ToolbarConfig .() -> Unit )? ) {
52+ setToolbar(getString(titleId), navBtnType, block)
53+ }
54+
55+ override fun Fragment.setToolbar (title : String? , navBtnType : NavBtnType , block : (ToolbarConfig .() -> Unit )? ) {
56+ loadingStateView?.setHeaders(ToolbarViewDelegate (title, navBtnType, block))
57+ }
58+
59+ override fun Activity.setHeaders (vararg delegates : LoadingStateView .ViewDelegate ) {
60+ loadingStateView?.setHeaders(* delegates)
61+ }
62+
63+ override fun Fragment.setHeaders (vararg delegates : LoadingStateView .ViewDelegate ) {
64+ loadingStateView?.addChildHeaders(* delegates)
65+ }
66+
67+ override fun Activity.setDecorView (delegate : LoadingStateView .DecorViewDelegate ) {
68+ loadingStateView?.setDecorView(delegate)
69+ }
70+
71+ override fun Fragment.setDecorView (delegate : LoadingStateView .DecorViewDelegate ) {
72+ loadingStateView?.addChildDecorView(delegate)
73+ }
74+
75+ override fun showLoadingView () {
76+ loadingStateView?.showLoadingView()
77+ }
78+
79+ override fun showContentView () {
80+ loadingStateView?.showContentView()
81+ }
82+
83+ override fun showErrorView () {
84+ loadingStateView?.showErrorView()
85+ }
86+
87+ override fun showEmptyView () {
88+ loadingStateView?.showEmptyView()
89+ }
90+
91+ override fun showCustomView (viewType : Any ) {
92+ loadingStateView?.showView(viewType)
93+ }
94+
95+ override fun updateToolbar (block : ToolbarConfig .() -> Unit ) {
96+ updateView<ToolbarViewDelegate >(ViewType .TITLE ) { bind(config.apply (block)) }
97+ }
98+
99+ override fun <T : LoadingStateView .ViewDelegate > updateView (viewType : Any , block : T .() -> Unit ) {
100+ loadingStateView?.getViewDelegate<T >(viewType)?.apply (block)
101+ }
102+
103+ override fun ToolbarViewDelegate (title : String? , navBtnType : NavBtnType , block : (ToolbarConfig .() -> Unit )? ) =
104+ requireNotNull(loadingStateView?.getViewDelegate<ToolbarViewDelegate >(ViewType .TITLE )) {
105+ " ToolbarViewDelegate must be registered before."
106+ }.apply {
107+ config = ToolbarConfig (title, navBtnType).apply { block?.invoke(this ) }
108+ }
109+ }
0 commit comments