Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

ma-jian/MvvmKotlin

Repository files navigation

部分代码已经过时,不再更新

Activity Result API 方式启动的路由管理器 Router

多组件的MvvmPoet


MvvmKotlin

 只使用3个注解也可以轻松完成Mvvm框架的搭建,本项目根据Google 新框架jetpack配合dagger2 、Apt构建完成。

 项目整体架构参照Google的模式,基本保持一致。

 如图:

Android Jetpack

Respository (数据层)

Respository(数据处理层): 是用来处理网络数据、数据库、本地等数据的逻辑。

自定义的Repository只需继承BaseRepository (BaseRepository 由APT自动生成,并且提供所有网络(Retrofit)接口实例)

在BaseRepository中可以轻松获取各个接口数据。
  • 处理retrofit接口 添加注解@ApiFactory即可

Retrofit interface

  • APT自动生成retrofit接口工厂类,用于处理请求数据,内涵大量关联类的注释。

Retrofit interface

  • APT自动生成BaseRepository,并且通过dagger注入所有接口工厂,拥有所有retrofit接口工厂的实例

Retrofit interface

ViewModel (Model层)

ViewModel模块无须关心自身Factory的生产过程。只需在构造函数中传入自身所需要的Respository(数据层)使用即可,

ViewModle构造中可同时传入多个数据源,Factory将自动获取数据实例传入ViewModel中。
  • ViewModel构造中传入需要的数据源获取数据

Retrofit interface

  • APT将自动生成ViewModel对应的ViewModelFactory,并将model需要的数据源传入

Retrofit interface

Activity/Fragment (View层)

利用注解 @InjectViewModel @BindViewModel 配合APT自动完成ViewModel 与Respository 的注入过程,

利用控制反转轻松实现Activity/Fragment与ViewModel的关联,类似ButterKnife。

同时View可支持多个ViewModel的注入
  • APT自动生成ViewModelFactoryTools类,在bind方法中将View与ViewModel的绑定。

ViewModelFactory Tools

  • View中绑定ViewModel的个数取决于View中 @BindViewModel的个数

View

  • 在View基类中获取ViewModelFactoryTools的实例,用于绑定View与ViewModel,并且给View中带有 @BindViewModel注解的 ViewModel赋值。

ViewModel

Gif

Video

Thanks

Libraries Used

  • Foundation - Components for core system capabilities, Kotlin extensions and support for multidex and automated testing.
    • AppCompat - Degrade gracefully on older versions of Android.
    • Android KTX - Write more concise, idiomatic Kotlin code.
    • Test - An Android testing framework for unit and runtime UI tests.
  • Architecture - A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.
    • Data Binding - Declaratively bind observable data to UI elements.
    • Lifecycles - Create a UI that automatically responds to lifecycle events.
    • LiveData - Build data objects that notify views when the underlying database changes.
    • Navigation - Handle everything needed for in-app navigation.
    • Room - Access your app's SQLite database with in-app objects and compile-time checks.
    • ViewModel - Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.
    • WorkManager - Manage your Android background jobs.
  • UI - Details on why and how to use UI Components in your apps - together or separate
  • Third party
    • Glide for image loading
    • Kotlin Coroutines for managing background threads with simplified code and reducing needs for callbacks

License

Copyright 2018 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

About

MVVM、Dagger2、Jetpack、APT、AOP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published