diff --git a/Quick/src/main/java/com/wpf/app/quick/helper/binddatahelper/Request2View.kt b/Quick/src/main/java/com/wpf/app/quick/helper/binddatahelper/Request2View.kt deleted file mode 100644 index ffd2cecd..00000000 --- a/Quick/src/main/java/com/wpf/app/quick/helper/binddatahelper/Request2View.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.wpf.app.quick.helper.binddatahelper - -import android.view.View -import com.wpf.app.quickbind.annotations.BindD2VHelper -import com.wpf.app.quickrecyclerview.data.QuickItemData -import com.wpf.app.quickrecyclerview.listeners.RequestAndCallbackWithView - -object Request2View: BindD2VHelper> { - override fun initView(view: View, data: RequestAndCallbackWithView) { - - } - -} \ No newline at end of file diff --git a/QuickBind/src/main/java/com/wpf/app/quickbind/bindview/QuickRequestData.kt b/QuickBind/src/main/java/com/wpf/app/quickbind/bindview/QuickRequestData.kt new file mode 100644 index 00000000..2af9c7c2 --- /dev/null +++ b/QuickBind/src/main/java/com/wpf/app/quickbind/bindview/QuickRequestData.kt @@ -0,0 +1,25 @@ +package com.wpf.app.quickbind.bindview + +import android.view.View +import androidx.annotation.LayoutRes +import com.wpf.app.quickbind.QuickBind +import com.wpf.app.quickutil.bind.Bind + +open class QuickRequestData @JvmOverloads constructor( + @LayoutRes open val layoutId: Int = 0, + @Transient open val layoutView: View? = null +) : Bind { + + @Transient + private var mView: View? = null + + open fun onCreateView(view: View) { + mView = view + QuickBind.bind(this) + } + + override fun getView(): View? { + return mView + } + +} \ No newline at end of file diff --git a/QuickBind/src/main/java/com/wpf/app/quickbind/helper/binddatahelper/Request2View.kt b/QuickBind/src/main/java/com/wpf/app/quickbind/helper/binddatahelper/Request2View.kt new file mode 100644 index 00000000..3b324d6f --- /dev/null +++ b/QuickBind/src/main/java/com/wpf/app/quickbind/helper/binddatahelper/Request2View.kt @@ -0,0 +1,20 @@ +package com.wpf.app.quickbind.helper.binddatahelper + +import android.view.View +import com.wpf.app.quickbind.bindview.QuickRequestData +import com.wpf.app.quickbind.annotations.BindD2VHelper +import com.wpf.app.quickbind.interfaces.Request2ViewWithView +import com.wpf.app.quickutil.Callback + +object Request2View: BindD2VHelper> { + + override fun initView(view: View, data: Request2ViewWithView) { + (data as Request2ViewWithView).requestAndCallback(view, callback = object : Callback { + override fun callback(data: QuickRequestData?) { + data?.onCreateView(view) + } + + }) + } + +} \ No newline at end of file diff --git a/QuickBind/src/main/java/com/wpf/app/quickbind/interfaces/Request2View.kt b/QuickBind/src/main/java/com/wpf/app/quickbind/interfaces/Request2View.kt new file mode 100644 index 00000000..3963f7d8 --- /dev/null +++ b/QuickBind/src/main/java/com/wpf/app/quickbind/interfaces/Request2View.kt @@ -0,0 +1,44 @@ +package com.wpf.app.quickbind.interfaces + +import android.view.View +import com.wpf.app.quickbind.bindview.QuickRequestData +import com.wpf.app.quickutil.Callback + + +interface Request2View: Request2ViewWithView { + + /** + * 接口请求 + */ + fun requestAndCallback(callback: Callback) + + override fun requestAndCallback(view: View, callback: Callback) { + requestAndCallback(callback) + } +} + +interface Request2ViewWithView { + + /** + * 接口请求 + */ + fun requestAndCallback(view: View, callback: Callback) +} + +fun request2View( + callbackF: (callback: Callback) -> Unit +) = object : Request2View { + + override fun requestAndCallback(callback: Callback) { + callbackF.invoke(callback) + } +} + +fun request2ViewWithView( + callbackF: (view: View, callback: Callback) -> Unit +) = object : Request2ViewWithView { + + override fun requestAndCallback(view: View, callback: Callback) { + callbackF.invoke(view, callback) + } +} \ No newline at end of file diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt index 28f07e8a..ffaef7b5 100644 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt @@ -5,8 +5,8 @@ import android.util.AttributeSet import com.wpf.app.quickrecyclerview.data.QuickItemData import com.wpf.app.quickrecyclerview.data.RequestData import com.wpf.app.quickrecyclerview.listeners.RefreshView -import com.wpf.app.quickrecyclerview.listeners.RequestDataAndCallbackWithView -import com.wpf.app.quickutil.Callback +import com.wpf.app.quickrecyclerview.listeners.Request2ListWithView +import com.wpf.app.quickutil.CallbackList /** * Created by 王朋飞 on 2022/7/13. @@ -26,17 +26,17 @@ open class QuickRefreshRecyclerView @JvmOverloads constructor( @JvmField var mRequestData = RequestData(0) - private var mDataChangeListener: RequestDataAndCallbackWithView? = + private var mDataChangeListener: Request2ListWithView? = null - fun setDataChangeListener(dataChangeListener: RequestDataAndCallbackWithView) { + fun setDataChangeListener(dataChangeListener: Request2ListWithView) { mDataChangeListener = dataChangeListener } override fun onRefresh() { mRequestData.refresh() - (mDataChangeListener as? RequestDataAndCallbackWithView) - ?.requestAndCallback(this, mRequestData, object : Callback { + (mDataChangeListener as? Request2ListWithView) + ?.requestAndCallback(this, mRequestData, object : CallbackList { override fun callback(data: List?) { setNewData(data) mRequestData.loadDataSize(data?.size ?: 0) @@ -49,8 +49,8 @@ open class QuickRefreshRecyclerView @JvmOverloads constructor( override fun onLoadMore() { mRequestData.loadMore() - (mDataChangeListener as? RequestDataAndCallbackWithView) - ?.requestAndCallback(this, mRequestData, object : Callback { + (mDataChangeListener as? Request2ListWithView) + ?.requestAndCallback(this, mRequestData, object : CallbackList { override fun callback(data: List?) { appendList(data) mRequestData.loadDataSize(data?.size ?: 0) diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt index 40fcbee5..ee64b547 100644 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt @@ -7,19 +7,19 @@ import com.wpf.app.quickrecyclerview.QuickRefreshRecyclerView import com.wpf.app.quickrecyclerview.data.QuickItemData import com.wpf.app.quickrecyclerview.data.RequestData import com.wpf.app.quickrecyclerview.listeners.RefreshView -import com.wpf.app.quickrecyclerview.listeners.RequestDataAndCallbackWithView -import com.wpf.app.quickutil.Callback +import com.wpf.app.quickrecyclerview.listeners.Request2ListWithView +import com.wpf.app.quickutil.CallbackList import java.lang.reflect.ParameterizedType object Request2RefreshView : - BindD2VHelper> { + BindD2VHelper> { override fun initView( view: View, - data: RequestDataAndCallbackWithView + data: Request2ListWithView ) { if (view is QuickRefreshRecyclerView) { - view.setDataChangeListener(data as RequestDataAndCallbackWithView) + view.setDataChangeListener(data as Request2ListWithView) } else if (view is RefreshView && view.getAdapter() is QuickAdapter) { val quickAdapter = view.getAdapter() as QuickAdapter val requestData: RequestData = try { @@ -27,14 +27,14 @@ object Request2RefreshView : } catch (ignore: Exception) { RequestData() } - val realData = data as RequestDataAndCallbackWithView + val realData = data as Request2ListWithView view.refreshView = object : RefreshView { override var refreshView: RefreshView? = this override fun onRefresh() { super.onRefresh() requestData.refresh() - realData.requestAndCallback(view, requestData, object : Callback { + realData.requestAndCallback(view, requestData, object : CallbackList { override fun callback(data: List?) { quickAdapter.mDataList?.clear() quickAdapter.appendList(data) @@ -50,7 +50,7 @@ object Request2RefreshView : override fun onLoadMore() { super.onLoadMore() requestData.loadMore() - realData.requestAndCallback(view, requestData, object : Callback { + realData.requestAndCallback(view, requestData, object : CallbackList { override fun callback(data: List?) { quickAdapter.appendList(data) requestData.loadDataSize(data?.size ?: 0) diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt new file mode 100644 index 00000000..90082037 --- /dev/null +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt @@ -0,0 +1,78 @@ +package com.wpf.app.quickrecyclerview.listeners + +import com.wpf.app.quickrecyclerview.data.QuickItemData +import com.wpf.app.quickrecyclerview.data.RequestData +import com.wpf.app.quickutil.CallbackList + +/** + * Created by 王朋飞 on 2022/7/13. + * + */ + +interface Request2List: Request2ListWithView { + + /** + * 接口请求 + */ + fun requestAndCallback(requestData: Request, callback: CallbackList) + + override fun requestAndCallback(view: Any, requestData: Request, callback: CallbackList) { + requestAndCallback(requestData, callback) + } +} + +interface Request2ListWithView { + + /** + * 接口请求 + */ + fun requestAndCallback(view: View, requestData: Request, callback: CallbackList) + + /** + * 刷新结束 + * 返回true表示刷新结束后自己刷新adapter + */ + fun refreshFinish(): Boolean { return false } + + /** + * 加载结束 + * 返回true表示刷新结束后自己刷新adapter + */ + fun loadMoreFinish(): Boolean { return false } +} + +fun requestData2List( + callbackF: (requestData: Request, callback: CallbackList) -> Unit +) = object : Request2List { + + override fun requestAndCallback(requestData: Request, callback: CallbackList) { + callbackF.invoke(requestData, callback) + } +} + +fun requestData2ListWithView( + callbackF: (view: View, requestData: Request, callback: CallbackList) -> Unit +) = object : Request2ListWithView { + + override fun requestAndCallback(view: View, requestData: Request, callback: CallbackList) { + callbackF.invoke(view, requestData, callback) + } +} + +fun request2List( + callbackF: (requestData: RequestData, callback: CallbackList) -> Unit +) = object : Request2List { + + override fun requestAndCallback(requestData: RequestData, callback: CallbackList) { + callbackF.invoke(requestData, callback) + } +} + +fun request2ListWithView( + callbackF: (view: View, requestData: RequestData, callback: CallbackList) -> Unit +) = object : Request2ListWithView { + + override fun requestAndCallback(view: View, requestData: RequestData, callback: CallbackList) { + callbackF.invoke(view, requestData, callback) + } +} \ No newline at end of file diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestAndItemCallback.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestAndItemCallback.kt deleted file mode 100644 index 502033fa..00000000 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestAndItemCallback.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.wpf.app.quickrecyclerview.listeners - -import com.wpf.app.quickrecyclerview.data.QuickItemData -import com.wpf.app.quickrecyclerview.data.RequestData - -/** - * Created by 王朋飞 on 2022/7/13. - * - */ -interface RequestAndItemCallback: RequestDataAndCallback \ No newline at end of file diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestDataAndCallback.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestDataAndCallback.kt deleted file mode 100644 index 617c1d8b..00000000 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/RequestDataAndCallback.kt +++ /dev/null @@ -1,120 +0,0 @@ -package com.wpf.app.quickrecyclerview.listeners - -import android.view.View -import com.wpf.app.quickrecyclerview.data.QuickItemData -import com.wpf.app.quickrecyclerview.data.RequestData -import com.wpf.app.quickutil.Callback - -/** - * Created by 王朋飞 on 2022/7/13. - * - */ -interface RequestAndCallback: RequestAndCallbackWithView { - - /** - * 接口请求 - */ - fun requestAndCallback(callback: Callback) - - override fun requestAndCallback(view: View, callback: Callback) { - requestAndCallback(callback) - } -} - -interface RequestAndCallbackWithView: RequestDataAndCallbackWithView { - - /** - * 接口请求 - */ - fun requestAndCallback(view: View, callback: Callback) - - override fun requestAndCallback(view: View, requestData: RequestData, callback: Callback) { - requestAndCallback(view, callback) - } -} - -fun request2View( - callbackF: (callback: Callback) -> Unit -) = object : RequestAndCallback { - - override fun requestAndCallback(callback: Callback) { - callbackF.invoke(callback) - } -} - -fun request2ViewWithView( - callbackF: (view: View, callback: Callback) -> Unit -) = object : RequestAndCallbackWithView { - - override fun requestAndCallback(view: View, callback: Callback) { - callbackF.invoke(view, callback) - } -} - -interface RequestDataAndCallback: RequestDataAndCallbackWithView { - - /** - * 接口请求 - */ - fun requestAndCallback(requestData: Request, callback: Callback) - - override fun requestAndCallback(view: Any, requestData: Request, callback: Callback) { - requestAndCallback(requestData, callback) - } -} - -interface RequestDataAndCallbackWithView { - - /** - * 接口请求 - */ - fun requestAndCallback(view: View, requestData: Request, callback: Callback) - - /** - * 刷新结束 - * 返回true表示刷新结束后自己刷新adapter - */ - fun refreshFinish(): Boolean { return false } - - /** - * 加载结束 - * 返回true表示刷新结束后自己刷新adapter - */ - fun loadMoreFinish(): Boolean { return false } -} - -fun requestData2List( - callbackF: (requestData: Request, callback: Callback) -> Unit -) = object : RequestDataAndCallback { - - override fun requestAndCallback(requestData: Request, callback: Callback) { - callbackF.invoke(requestData, callback) - } -} - -fun requestData2ListWithView( - callbackF: (view: View, requestData: Request, callback: Callback) -> Unit -) = object : RequestDataAndCallbackWithView { - - override fun requestAndCallback(view: View, requestData: Request, callback: Callback) { - callbackF.invoke(view, requestData, callback) - } -} - -fun request2List( - callbackF: (requestData: RequestData, callback: Callback) -> Unit -) = object : RequestDataAndCallback { - - override fun requestAndCallback(requestData: RequestData, callback: Callback) { - callbackF.invoke(requestData, callback) - } -} - -fun request2ListWithView( - callbackF: (view: View, requestData: RequestData, callback: Callback) -> Unit -) = object : RequestDataAndCallbackWithView { - - override fun requestAndCallback(view: View, requestData: RequestData, callback: Callback) { - callbackF.invoke(view, requestData, callback) - } -} \ No newline at end of file diff --git a/QuickUtil/src/main/java/com/wpf/app/quickutil/Callback.kt b/QuickUtil/src/main/java/com/wpf/app/quickutil/Callback.kt index a8eb0ff0..db214102 100644 --- a/QuickUtil/src/main/java/com/wpf/app/quickutil/Callback.kt +++ b/QuickUtil/src/main/java/com/wpf/app/quickutil/Callback.kt @@ -6,5 +6,5 @@ package com.wpf.app.quickutil */ interface Callback { - fun callback(data: List?) + fun callback(data: Data?) } \ No newline at end of file diff --git a/QuickUtil/src/main/java/com/wpf/app/quickutil/CallbackList.kt b/QuickUtil/src/main/java/com/wpf/app/quickutil/CallbackList.kt new file mode 100644 index 00000000..240843ea --- /dev/null +++ b/QuickUtil/src/main/java/com/wpf/app/quickutil/CallbackList.kt @@ -0,0 +1,10 @@ +package com.wpf.app.quickutil + +/** + * Created by 王朋飞 on 2022/7/13. + * + */ +interface CallbackList { + + fun callback(data: List?) +} \ No newline at end of file diff --git "a/app/src/main/java/com/wpf/app/quick/demo/http/model/\351\246\226\351\241\265\346\226\207\347\253\240.kt" "b/app/src/main/java/com/wpf/app/quick/demo/http/model/\351\246\226\351\241\265\346\226\207\347\253\240.kt" index db878700..10535fa1 100644 --- "a/app/src/main/java/com/wpf/app/quick/demo/http/model/\351\246\226\351\241\265\346\226\207\347\253\240.kt" +++ "b/app/src/main/java/com/wpf/app/quick/demo/http/model/\351\246\226\351\241\265\346\226\207\347\253\240.kt" @@ -3,16 +3,26 @@ package com.wpf.app.quick.demo.http.model import android.annotation.SuppressLint import com.wpf.app.quick.annotations.BindData2View import com.wpf.app.quick.demo.R +import com.wpf.app.quickbind.bindview.QuickRequestData import com.wpf.app.quickbind.helper.binddatahelper.Text2TextView +import com.wpf.app.quickbind.interfaces.runOnContextWithSelf import com.wpf.app.quickrecyclerview.data.QuickBindData -class 首页文章 { +class 首页文章 : QuickRequestData( + R.layout.activity_request_test +) { val curPage: Int? = null val datas: List<文章>? = null - class 文章: QuickBindData(layoutId = R.layout.holder_refresh_item) { + class 文章 : QuickBindData(layoutId = R.layout.holder_refresh_item) { @SuppressLint("NonConstantResourceId") @BindData2View(id = R.id.title, helper = Text2TextView::class) - val title: String?= null + val title: String? = null + } + + @SuppressLint("NonConstantResourceId") + @BindData2View(id = R.id.info, helper = Text2TextView::class) + val info = runOnContextWithSelf { _, self -> + "请求成功:" + self.curPage + "---" + self.datas?.toString() } } \ No newline at end of file diff --git a/app/src/main/java/com/wpf/app/quick/demo/viewmodel/RequestTestViewModel.kt b/app/src/main/java/com/wpf/app/quick/demo/viewmodel/RequestTestViewModel.kt index 630a13e4..a1593305 100644 --- a/app/src/main/java/com/wpf/app/quick/demo/viewmodel/RequestTestViewModel.kt +++ b/app/src/main/java/com/wpf/app/quick/demo/viewmodel/RequestTestViewModel.kt @@ -2,37 +2,51 @@ package com.wpf.app.quick.demo.viewmodel import android.annotation.SuppressLint import android.widget.TextView -import com.google.android.material.button.MaterialButton import com.google.gson.Gson import com.wpf.app.quick.annotations.BindView import com.wpf.app.quick.demo.R import com.wpf.app.quick.demo.RequestTestActivity import com.wpf.app.quick.demo.http.request import com.wpf.app.quick.activity.viewmodel.QuickViewModel -import com.wpf.app.quicknetwork.helper.GsonHelper +import com.wpf.app.quick.annotations.BindData2View +import com.wpf.app.quickbind.helper.binddatahelper.Request2View +import com.wpf.app.quickbind.interfaces.request2View import com.wpf.app.quickutil.LogUtil class RequestTestViewModel: QuickViewModel() { - @SuppressLint("NonConstantResourceId", "StaticFieldLeak") - @BindView(R.id.info) - val info: TextView? = null - override fun onViewCreated(baseView: RequestTestActivity) { - r首页文章() + } - private fun r首页文章() { +// @SuppressLint("NonConstantResourceId", "StaticFieldLeak") +// @BindView(R.id.info) +// val info: TextView? = null +// + +// +// private fun r首页文章() { +// request { +// 首页文章列表(0) +// }.success { +// val result = Gson().toJson(it?.data) ?: "" +// LogUtil.e("接口返回成功$result") +// info?.text = result +// }.fail { +// val result = Gson().toJson(it?.data) ?: "" +// LogUtil.e("接口返回失败:${it?.errorMsg}$result") +// info?.text = result +// } +// } + + //代替上面注释的逻辑 + @SuppressLint("NonConstantResourceId", "StaticFieldLeak") + @BindData2View(id = R.id.info, helper = Request2View::class) + val request2View = request2View { callback -> request { 首页文章列表(0) }.success { - val result = Gson().toJson(it?.data) ?: "" - LogUtil.e("接口返回成功$result") - info?.text = result - }.fail { - val result = Gson().toJson(it?.data) ?: "" - LogUtil.e("接口返回失败:${it?.errorMsg}$result") - info?.text = result + callback.callback(it?.data) } } } \ No newline at end of file