What is the brick

  • BrickIt is a number of Java and Android libraries that I made public and maintained last year. The core brick framework is designed to help users create viewModels and repositoriesView-ViewModel-RepositoryLayer architecture library.

The problem of brick

  • In the early days of the Brick frame, reflection was implemented through reflection, and although reflection was not used very often and didn’t eat much performance, there were still a lot of people who didn’t like reflection; Therefore, I decided to use APT(Annotation processor) to refactor Brick.

How’s it going?

  • It has been passed at present@ProvideAnnotations generate get and lazy methods for the ViewModel.
  • The method of generating Repository layer is still being explored. Suggestions and methods are welcome.

How to use @provide annotations

  • Add the following code to the build.gradle file at the root of your project, ignore it if it already exists
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
Copy the code
  • Add the following code to your build.gradle file to enable kapt:
. apply plugin: 'kotlin-kapt'Copy the code
  • Add the following code to your build.gradle file to use brick:
dependencies { ... Kapt 'com. Gitee. Numeron. Brick: the compiler: 0.1.0 from' implementation 'com. Gitee. Numeron. Brick: the annotation: 0.1.0 from'}Copy the code
  • Use it on your ViewModel class@ProvideNotes, such as:
@Provide
class WeChatAuthorViewModel(val userId: Long, authorIdProvider: () -> String) : ViewModel()
Copy the code
  • Open Gradle in the right sidebar of AndroidStudio and go to:
[ProjectName] -> [ModuleName] -> Tasks -> Other -> kaptDebugKotlinCopy the code

Ctrl + F9 is used to create a class in the build folder of the module. The class name is ViewModel with an extra S after the name:

  • In your View layer (Activity or Fragment), just call the method in the generated class:
private val weChatAuthorViewModel by lazyWeChatAuthorViewModel(0, ::authoerId)
Copy the code

The generated classes are as follows:

package com.numeron.brick.contract import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelStoreOwner import java.lang.Class import kotlin.Boolean import kotlin.Function0 import kotlin.Lazy import kotlin.Long import kotlin.String import kotlin.Suppress fun ViewModelStoreOwner.lazyWeChatAuthorViewModel(userId: Long, authorIdProvider: Function0<String>): Lazy<WeChatAuthorViewModel> = LazyWeChatAuthorViewModel(this, userId, authorIdProvider) fun get(owner: ViewModelStoreOwner, userId: Long, authorIdProvider: Function0<String>): WeChatAuthorViewModel { val factory = WeChatAuthorViewModelFactory(userId, authorIdProvider) return ViewModelProvider(owner, factory).get(WeChatAuthorViewModel::class.java) } private class WeChatAuthorViewModelFactory( private val userId: Long, private val authorIdProvider: Function0<String> ) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun <VM : ViewModel> create(clazz: Class<VM>): VM = WeChatAuthorViewModel(userId, authorIdProvider) as VM } private class LazyWeChatAuthorViewModel( private val owner: ViewModelStoreOwner, private val userId: Long, private val authorIdProvider: Function0<String> ) : Lazy<WeChatAuthorViewModel> { private var _value: WeChatAuthorViewModel? = null override val value: WeChatAuthorViewModel get() { if(_value == null) { _value = get(owner, userId, authorIdProvider) } return _value!! } override fun isInitialized(): Boolean = _value ! = null }Copy the code
  • The generated class contains two methods and two private classes wrapped in the GET methodViewModelProviderThe use oflazyXXXViewModelIs a Kotlin extension method that returnsLazy<XXXViewModel>Type, which can be mediated directly by the by keywordvalProperties.
  • The two generated private classes contain one namedXXXViewModelFactoryClass, generally usedNewInstanceFactoryCreate viewModels with no arguments directly by reflection, but for viewModels with arguments in the constructor, we generally need to inherit fromViewModelProvider.FactoryCreate a new class to create the ViewModel. Here, we can generate arguments that match in the constructor directly, no longer by hand.
  • And the generated XXXViewModelFactory doesn’t call reflection, but instead creates an object directly through the constructor, because that’s what this version of brick was born forDon't use reflection.