GithubHelp home page GithubHelp logo

wangjjj / jetpackmvvm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hegaojian/jetpackmvvm

1.0 1.0 2.0 680 KB

一个Jetpack结合MVVM的快速开发框架

Home Page: https://github.com/hegaojian/JetpackMvvm

License: Apache License 2.0

Kotlin 79.42% Java 20.58%

jetpackmvvm's Introduction

Platform GitHub license GitHub license

JetPackMvvm

  • 基于MVVM模式集成谷歌官方推荐的JetPack组件库:LiveData、ViewModel、Lifecycle组件
  • 使用kotlin语言,添加大量拓展函数,简化代码
  • 加入Retorfit网络请求,协程,帮你简化各种操作,让你快速请求网络

演示Demo

已用该库重构了我之前的玩安卓项目,利用Navigation组件以单Activity+Fragment架构编写,优化了很多代码,对比之前的mvp项目,开发效率与舒适度要提高了不少,想看之前MVP的项目可以去 https://github.com/hegaojian/WanAndroid

效果图展示

项目效果图

APK下载:

1.如何使用

  • 1.1 启用dataBinding 如果你需要用到dataBinding,请在主工程的build中加入,不用可以忽略这一步: 不想用dataBinding的继承BaseVmActivity,想用dataBinding的继承BaseVmDbActivity
  dataBinding{
        enabled = true
  }
  • 1.2 依赖
dependencies {
  ...
  implementation 'me.hegj:JetpackMvvm:1.0.0'
}

2.登录示例(可以看demo,预先创建你的基类,并实现相关的方法)

  • 2.1 LoginFragment继承基类传入相关泛型
class LoginFragment:BaseFragment<LoginRegisterViewModel, FragmentLoginBinding>() {

    override fun layoutId() = R.layout.fragment_login

    override fun initView() {
        login_sub.setOnClickListener {
            mViewModel.login()
        }
    }
    
    override fun createObserver() {
        //监听请求结果
        mViewModel.loginResult.observe(this, Observer { viewState ->
            parseState(viewState,{
                //登录成功 退出,,
            },{
                //登录失败
                showMessage(it.errorMsg)
            })
        })
    }
}
  • 2.2 创建LoginViewModel继承BaseViewModel
class LoginViewModel:BaseViewModel() {
    
     //用户名
    var username = StringObservableField()
     //密码
    var password = StringObservableField()
    
    private val loginRpository:LoginRepository by lazy{ LoginRepository() }

    var loginResult = MutableLiveData<ViewState<UserInfo>>()

    fun login(){
        launchRequest({loginRpository.login(username.get(),password.get())},loginResult)
    }
}

  • 3.3 创建数据仓库,不管数据是请求网络还是本地数据库拉取都在这里实现
class LoginRepository {

    //登录 自带协程
    suspend fun login(username: String, password: String): ApiResponse<UserInfo> {
        return NetworkApi.service.login(username, password)
    }
    

License

 Copyright 2019, hegaojian(何高建)       
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at 
 
       http://www.apache.org/licenses/LICENSE-2.0 

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

jetpackmvvm's People

Contributors

hegaojian avatar

Stargazers

Cocoon avatar

Watchers

James Cloos avatar

Forkers

sayokia andy91310

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.