|
| 1 | +package com.baimaisu.aloading.view |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.Context |
| 5 | +import android.util.Log |
| 6 | +import android.view.View |
| 7 | +import android.view.ViewGroup |
| 8 | +import android.widget.FrameLayout |
| 9 | + |
| 10 | +class Aloading(private var loadingViewClass: Class<out ILoadingView>) { |
| 11 | + companion object { |
| 12 | + private val loadingFactorys: MutableMap<Class<out ILoadingView>, Factory<out ILoadingView>> = mutableMapOf() |
| 13 | + |
| 14 | + private val viewGroupHandlers:MutableList<ViewGroupHandler> = mutableListOf() |
| 15 | + |
| 16 | + init { |
| 17 | + loadingFactorys[LoadingView::class.java] = default_factory |
| 18 | + viewGroupHandlers.add(ConstraintLayoutHandler) |
| 19 | + } |
| 20 | + |
| 21 | + private var defaultLoadingViewClass: Class<out ILoadingView> = LoadingView::class.java |
| 22 | + |
| 23 | + fun default(): Aloading { |
| 24 | + return Aloading(defaultLoadingViewClass) |
| 25 | + } |
| 26 | + |
| 27 | + fun with(loadingViewClass: Class<out ILoadingView>): Aloading { |
| 28 | + return Aloading(loadingViewClass) |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + class Registry { |
| 34 | + private constructor() |
| 35 | + companion object { |
| 36 | + fun setDefaultLoadingView(viewClass: Class<out ILoadingView>) { |
| 37 | + defaultLoadingViewClass = viewClass |
| 38 | + } |
| 39 | + |
| 40 | + fun <T : ILoadingView> register(viewClass: Class<T>, factory: Factory<T>) { |
| 41 | + loadingFactorys[viewClass] = factory |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + fun appendViewGroupHandler(handler:ViewGroupHandler){ |
| 46 | + viewGroupHandlers.add(handler) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + fun wrap(act: Activity): LoadViewAdapter { |
| 53 | + return ViewWrapper(loadingFactorys[loadingViewClass]!!.create(act)).wrap(act) |
| 54 | + } |
| 55 | + |
| 56 | + fun wrap(view: View): LoadViewAdapter { |
| 57 | + return ViewWrapper(loadingFactorys[loadingViewClass]!!.create(view.context)).wrap(view) |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + private inner class ViewWrapper(private var loadingView: ILoadingView) { |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + fun wrap(act: Activity): LoadViewAdapter { |
| 68 | + return wrap(act.findViewById<View>(android.R.id.content)) |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private fun wrapDefaultByFrameLayout(view: View): LoadViewAdapter { |
| 73 | + val wrapper = FrameLayout(view.context) |
| 74 | + val lp = view.layoutParams |
| 75 | + if (lp != null) { |
| 76 | + wrapper.layoutParams = lp |
| 77 | + } |
| 78 | + if (view.parent != null) { |
| 79 | + val parent = view.parent as ViewGroup |
| 80 | + val index = parent.indexOfChild(view) |
| 81 | + parent.removeView(view) |
| 82 | + parent.addView(wrapper, index) |
| 83 | + } |
| 84 | + val newLp = |
| 85 | + FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) |
| 86 | + wrapper.addView(view, newLp) |
| 87 | + wrapper.addView( |
| 88 | + loadingView.getView(), |
| 89 | + FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) |
| 90 | + ) |
| 91 | + loadingView.getView().bringToFront() |
| 92 | + return LoadViewAdapter(loadingView, wrapper) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * if(view not has parent){ |
| 97 | + * return wrapDefaultByFrameLayout(); |
| 98 | + * }else if(constraintLayoutClass.isAssignableFrom(view.getParent().getClass()){ |
| 99 | + * return wrapConstraintLayout() |
| 100 | + * }else if(***.class.isAssignableFrom(view.getParent().getClass()){ |
| 101 | + * return wrap***Layout(); |
| 102 | + * }else{ |
| 103 | + * return wrapDefaultByFrameLayout() |
| 104 | + * } |
| 105 | + */ |
| 106 | + fun wrap(view: View): LoadViewAdapter { |
| 107 | + if (FrameLayout::class.java.isAssignableFrom(view.javaClass)) { |
| 108 | + (view as FrameLayout).addView( |
| 109 | + loadingView.getView(), |
| 110 | + FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) |
| 111 | + ) |
| 112 | + loadingView.getView().bringToFront() |
| 113 | + return LoadViewAdapter(loadingView, view) |
| 114 | + } |
| 115 | + |
| 116 | + if(view.parent != null){ |
| 117 | + viewGroupHandlers.forEach { |
| 118 | + if(it.couldHandle(view)){ |
| 119 | + return LoadViewAdapter(loadingView,it.handle(view,view.parent as ViewGroup)) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return wrapDefaultByFrameLayout(view) |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments