Code Monkey home page Code Monkey logo

viewmodelautomation's Introduction

自动生成ViewModel、网络请求框架

一、项目地址:Github

  • 框架优势:通过提供的注解自动生成ViewModel层的LiveData、StateFlow,以及网络请求。节省您大量的开发时间
  • 交流方式:
    • 1、欢迎使用issues提建议或bug
    • 2、欢迎Star以兹鼓励

二、集成

  • 如果你的项目 Gradle 配置是在 7.0 以下,需要在 build.gradle 文件中加入(默认jdk1.8)

    allprojects {
        repositories {
            //远程仓库:https://jitpack.io
            maven { url 'https://jitpack.io' }
        }
    }
  • 如果你的 Gradle 配置是 7.0 及以上,则需要在 settings.gradle 文件中加入(默认jdk11)

    dependencyResolutionManagement {
        repositories {
            //远程仓库:https://jitpack.io
            maven { url 'https://jitpack.io' }
        }
    }
  • 在项目 app 模块下的 build.gradle 文件中加入远程依赖和kapt插件

plugins {
    id 'kotlin-kapt'
}


dependencies {
    //ktor网络框架
    implementation("io.ktor:ktor-client-core:1.6.0")
    //ktor扩展库
    implementation("com.github.hearthappy.viewmodelautomation:ktor-expand:2.0.6")
    //注解库
    compileOnly("com.github.hearthappy.viewmodelautomation:annotations:2.0.6")
    //处理注解自动生成库
    kapt("com.github.hearthappy.viewmodelautomation:processor:2.0.6")
}

三、使用示例(或参考Demo):

1、使用@BindStateFlow或@BindLiveData生成命名函数并绑定StateFlow/LiveData属性
@AndroidViewModel
@BindLiveData("getImages", ReImages::class, ResImages::class)
class MainActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
2、使用@Request注解生成网络请求(GET示例。其他类型参见,标题四:“HTTP请求”)
/**
 * GET:获取图片
 * @property page Int
 * @property size Int
 * @constructor
 */
@Request(urlString = "/getImages")
data class ReImages(val page: Int, val size: Int)
3、使用ViewModel
//Activity中添加
private val viewModel by viewModels<MainViewModel>()

//监听
lifecycleScope.launchWhenCreated {
  viewModel.getImagesStateFlow.collect {
    when (it) {
      is RequestState.LOADING-> progress.show()
      is RequestState.SUCCEED -> tvResult.showSucceedMsg(it.body.toString())
      is RequestState.FAILED -> tvResult.showFailedMsg(it)
      is RequestState.Throwable -> tvResult.showThrowableMsg(it)
      else -> Unit
    }
  }
}
4、生成MainViewModel
(1)完成步骤2基础上,点击AndroidStudio->Build->Make Project,编译生成MainViewModel类
(2)ViewModel生成路径:build/generated/source/kaptKotlin/debug/com/hearthappy/compiler/viewmodel/MainViewModel.kt

viewmodelautomation's People

Contributors

hearthappy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.