代码拉取完成,页面将自动刷新
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.8.0" apply false
id 'org.jetbrains.kotlin.multiplatform' version '1.8.0' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.0' apply false
id 'com.google.devtools.ksp' version '1.8.21-1.0.11' apply false
}
plugins {
id 'kotlinx-serialization'
id 'com.google.devtools.ksp'
}
ksp.incremental=false
buildFeatures {
viewBinding = true
}
implementation "io.github.catchpig.kmvvm:mvvm:last_version"
ksp "io.github.catchpig.kmvvm:compiler:last_version"
需要使用下载功能,请单独添加如下依赖
implementation "io.github.catchpig.kmvvm:download:last_version"
maven {
// mavenCentral的快照地址
url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
}
interface IGlobalConfig {
/**
* 标题栏高度
* @return Int
*/
@DimenRes
fun getTitleHeight(): Int
/**
* 标题栏的返回按钮资源
* @return Int
*/
@DrawableRes
fun getTitleBackIcon(): Int
/**
* 标题栏背景颜色
* @return Int
*/
@ColorRes
fun getTitleBackground(): Int
/**
* 标题栏文本颜色
* @return Int
*/
@ColorRes
fun getTitleTextColor(): Int
/**
* 标题字体样式
* @return Int
*/
@TextStyle
fun getTitleTextStyle(): Int
/**
* 标题栏下方是否需要横线
* @return Boolean
*/
fun isShowTitleLine(): Boolean
/**
* 标题栏下方横线颜色
* @return Int
*/
@ColorRes
fun getTitleLineColor(): Int
/**
* loading的颜色
* @return Int
*/
@ColorRes
fun getLoadingColor(): Int
/**
* loading的背景颜色
* @return Int
*/
@ColorRes
fun getLoadingBackground(): Int
/**
* RecyclerView的空页面ViewBinding
* @param parent ViewGroup
* @return ViewBinding
*/
fun getRecyclerEmptyBanding(parent: ViewGroup): ViewBinding
/**
* 网络请求失败的显示页面
* @param layoutInflater LayoutInflater
* @param any Any BaseActivity or BaseFragment
* @return ViewBinding
*/
fun getFailedBinding(layoutInflater: LayoutInflater, any: Any): ViewBinding?
/**
* 失败页面,需要重新加载的点击事件的id
* @return Int
*/
@IdRes
fun onFailedReloadClickId(): Int
/**
* 刷新每页加载个数
* @return Int
*/
fun getPageSize(): Int
/**
* 刷新起始页的index(有些后台设置的0,有些后台设置1)
*/
fun getStartPageIndex(): Int
}
使用示例:
@GlobalConfig
class MvvmGlobalConfig : IGlobalConfig {
override fun getTitleHeight(): Int {
return R.dimen.title_bar_height
}
override fun getTitleBackIcon(): Int {
return R.drawable.back_black
}
override fun getTitleBackground(): Int {
return R.color.colorPrimary
}
override fun getTitleTextColor(): Int {
return R.color.white
}
override fun getTitleTextStyle(): Int {
return TextStyle.BOLD
}
override fun isShowTitleLine(): Boolean {
return true
}
override fun getTitleLineColor(): Int {
return R.color.color_black
}
override fun getLoadingColor(): Int {
return R.color.color_black
}
override fun getLoadingBackground(): Int {
return R.color.white
}
override fun getRecyclerEmptyBanding(parent: ViewGroup): ViewBinding {
return LayoutEmptyBinding.inflate(LayoutInflater.from(parent.context), parent, false)
}
override fun getFailedBinding(layoutInflater: LayoutInflater, any: Any): ViewBinding? {
return when (any) {
is BaseActivity<*> -> {
LayoutActivityErrorBinding.inflate(layoutInflater)
}
is BaseFragment<*> -> {
LayoutFragmentErrorBinding.inflate(layoutInflater)
}
else -> {
null
}
}
}
override fun onFailedReloadClickId(): Int {
return R.id.failed_reload
}
override fun getPageSize(): Int {
return 16
}
override fun getStartPageIndex(): Int {
return 1
}
}
使用示例
Title其他注解参数,请看下方注解详情
//设置标题的文字
@Title(R.string.child_title)
class ChildActivity : BaseVMActivity<ActivityChildBinding, ChildViewModel>()
如果标题栏文字要根据接口显示不同的文字,也有接口设置
class ChildActivity : BaseVMActivity<ActivityChildBinding, ChildViewModel>() {
@OnClickFirstDrawable(R.drawable.more)
fun clickFirstDrawable(v: View) {
updateTitle("更改标题")
}
}
使用示例
StatusBar其他注解参数,请看下方注解详情
//弃用注解
@StatusBar(hide = true)
class FullScreenActivity : BaseActivity<ActivityFullScreenBinding>()
使用示例
注解修饰的方法只能可以带View参数,也可以不带View参数,看自身的需求
@Title(R.string.child_title)
class ChildActivity : BaseVMActivity<ActivityChildBinding, ChildViewModel>() {
@OnClickFirstDrawable(R.drawable.more)
fun clickFirstDrawable(v: View) {
SnackbarManager.show(bodyBinding.root, "第一个图标按钮点击生效")
updateTitle("nihao")
}
@OnClickFirstText(R.string.more)
fun clickFirstText() {
SnackbarManager.show(bodyBinding.root, "第一个文字按钮点击生效")
updateTitle("12354")
}
@OnClickSecondDrawable(R.drawable.more)
fun clickSecondDrawable(v: View) {
SnackbarManager.show(bodyBinding.root, "第二个图标按钮点击生效")
updateTitle("nihao")
}
@OnClickSecondText(R.string.more)
fun clickSecondText() {
SnackbarManager.show(bodyBinding.root, "第二个文字按钮点击生效")
updateTitle("12354")
}
}
使用示例
@OnClickSecondDrawable(R.drawable.more)
fun clickSecondDrawable(v: View) {
snackBar("第二个图标按钮点击生效")
}
/**
* 加载失败后展示失败页面,点击自定义失败页面的刷新按钮,重新请求数据
* @param autoFirstLoad Boolean 第一次是否自动加载
* @param block [@kotlin.ExtensionFunctionType] Function1<View, Unit>
*/
fun onFailedReload(autoFirstLoad: Boolean = true, block: View.() -> Unit){
.....
}
override fun initFlow() {
onFailedReload {
loadingViewError(bodyBinding.root)
}
}
fun loadingViewError(v: View) {
viewModel.loadingViewError().lifecycleLoadingView(this, showFailedView = true) {
snackBar(this)
}
}
使用示例
snackbar.setOnClickListener {
snackBar("提示框")
}
/**
* 加载失败后展示失败页面,点击自定义失败页面的刷新按钮,重新请求数据
* @param autoFirstLoad Boolean 第一次是否自动加载
* @param block [@kotlin.ExtensionFunctionType] Function1<View, Unit>
*/
fun onFailedReload(autoFirstLoad: Boolean = true, block: View.() -> Unit){
.....
}
override fun initFlow() {
onFailedReload {
loadBanners()
}
}
private fun loadBanners(){
viewModel.queryBanners().lifecycleLoadingDialog(this, true) {
val images = mutableListOf<String>()
this.forEach {
images.add(it.imagePath)
}
bodyBinding.banner.run {
setImages(images)
start()
}
}
}
使用示例
class UserAdapter(iPageControl: IPageControl) :
RecyclerAdapter<User, ItemUserBinding>(iPageControl) {
override fun viewBinding(parent: ViewGroup): ItemUserBinding {
return ItemUserBinding.inflate(LayoutInflater.from(parent.context), parent, false)
}
override fun bindViewHolder(holder: CommonViewHolder<ItemUserBinding>, m: User, position: Int) {
holder.viewBanding {
name.text = m.name
}
}
}
不用关心分页的逻辑,分页的刷新逻辑实现都在RefreshLayoutWrapper
<declare-styleable name="RefreshRecyclerView">
<attr name="recycler_background" format="color" />
</declare-styleable>
使用示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="50dp"
android:background="@color/colorPrimary" android:gravity="center" android:text="文章"
android:textColor="@color/color_white"/>
<com.catchpig.mvvm.widget.refresh.RefreshRecyclerView android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:recycler_background="#445467">
</com.catchpig.mvvm.widget.refresh.RefreshRecyclerView>
</LinearLayout>
bodyBinding.refresh.run {
setOnRefreshLoadMoreListener { nextPageIndex ->
viewModel.queryArticles(nextPageIndex).lifecycleRefresh(this@ArticleFragment,this)
}
}
如果rxJava为true,必须要引入RxJava的依赖包和adapter-rxjava3依赖包
implementation("com.squareup.retrofit2:adapter-rxjava3:$retrofit2_version")
//rxjava3
implementation "io.reactivex.rxjava3:rxjava:$rxjava_version"
implementation "io.reactivex.rxjava3:rxandroid:$rxandroid_version"
使用示例
@ServiceApi(
baseUrl = "https://www.wanandroid.com/",
responseConverter = ResponseBodyConverter::class,
interceptors = [RequestInterceptor::class],
debugInterceptors = [OkHttpProfilerInterceptor::class],
rxJava = true
)
interface WanAndroidService {
@GET("banner/json")
suspend fun banner(): List<Banner>
}
object WanAndroidRepository {
private val wanAndroidService = NetManager.getService(WanAndroidService::class.java)
fun getBanners(): Flow<MutableList<Banner>> {
//这里如果用flowOf的话,方法上面必须加上suspend关键字
return flow {
emit(wanAndroidService.queryBanner())
}
}
}
class IndexViewModel : BaseViewModel() {
fun queryBanners(): Flow<MutableList<Banner>> {
return WanAndroidRepository.getBanners()
}
}
//Activity或者Fragment
viewModel.queryBanners().lifecycle(this) {
val images = mutableListOf<String>()
this.forEach {
images.add(it.imagePath)
}
bodyBinding.banner.run {
setImages(images)
start()
}
}
{
code: "SUCCESS",
errorMsg: "成功",
data: ...
}
代码示例
class ResponseBodyConverter :
BaseResponseBodyConverter() {
override fun getResultClass(): KClass<out BaseResponseData<JsonElement>> {
return Result::class
}
override fun handlerErrorCode(errorCode: String, msg: String): Exception {
return NullPointerException()
}
}
-keep class com.catchpig.annotation.enums.**
-keep class com.google.android.material.snackbar.Snackbar {*;}
-keep @com.catchpig.annotation.ServiceApi class * {*;}
-keep public class **.databinding.*Binding {*;}
-keep class **.*_Compiler {*;}
#序列化混淆
-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
static <1>$Companion Companion;
}
# Keep `serializer()` on companion objects (both default and named) of serializable classes.
-if @kotlinx.serialization.Serializable class ** {
static **$* *;
}
-keepclassmembers class <1>$<3> {
kotlinx.serialization.KSerializer serializer(...);
}
# Keep `INSTANCE.serializer()` of serializable objects.
-if @kotlinx.serialization.Serializable class ** {
public static ** INSTANCE;
}
-keepclassmembers class <1> {
public static <1> INSTANCE;
kotlinx.serialization.KSerializer serializer(...);
}
# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。