Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
1.升级kotlin至1.3.61版本。
Browse files Browse the repository at this point in the history
2.为brick添加lazy系列方法。
3.调用brick部分api命名。
  • Loading branch information
xiazunyang committed Dec 12, 2019
1 parent 8f7251b commit e86b345
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.lifecycle.Observer
import com.numeron.adapter.BindingHolder
import com.numeron.adapter.PagedBindingAdapter
import com.numeron.adapter.SpaceItemDecoration
import com.numeron.brick.createViewModel
import com.numeron.brick.lazyViewModel
import com.numeron.delegate.ActivityExtraDelegate
import com.numeron.starter.startActivity
import com.numeron.util.dp
Expand All @@ -31,9 +31,7 @@ class ArticleListActivity : BaseActivity(), ArticleListParamProvider {

override val chapterId: Int by ActivityExtraDelegate(0)

private val articleListViewModel: ArticleListViewModel by lazy {
createViewModel<ArticleListViewModel>(this)
}
private val articleListViewModel: ArticleListViewModel by lazyViewModel(this)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.lifecycle.Observer
import com.numeron.adapter.BindingHolder
import com.numeron.adapter.PagedBindingAdapter
import com.numeron.adapter.SpaceItemDecoration
import com.numeron.brick.createViewModel
import com.numeron.brick.lazyViewModel
import com.numeron.chameleon.Chameleon
import com.numeron.util.dp
import com.numeron.view.StatefulLayoutMessageObserver
Expand All @@ -20,9 +20,7 @@ import kotlinx.android.synthetic.main.activity_we_chat_author_layout.*

class WeChatAuthorActivity : BaseActivity() {

private val weChatAuthorViewModel by lazy {
createViewModel<WeChatAuthorViewModel>()
}
private val weChatAuthorViewModel: WeChatAuthorViewModel by lazyViewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.numeron.brick.ViewModel
import com.numeron.brick.stack
import com.numeron.brick.lazyAutowired
import com.numeron.wandroid.dao.ArticleDao
import com.numeron.wandroid.entity.ApiResponse
import com.numeron.wandroid.entity.Paged
Expand All @@ -26,7 +26,7 @@ interface ArticleListParamProvider {

class ArticleListViewModel(private val paramProvider: ArticleListParamProvider) : ViewModel() {

private val articleRepository = stack<ArticleRepository>()
private val articleRepository: ArticleRepository by lazyAutowired()

private val pagedConfig = PagedList.Config.Builder()
.setPageSize(20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.numeron.brick.ViewModel
import com.numeron.brick.stack
import com.numeron.brick.lazyAutowired
import com.numeron.wandroid.dao.WeChatAuthorDao
import com.numeron.wandroid.entity.ApiResponse
import com.numeron.wandroid.entity.db.WeChatAuthor
Expand All @@ -16,7 +16,7 @@ import retrofit2.http.GET

class WeChatAuthorViewModel : ViewModel() {

private val weChatAuthorRepository = stack<WeChatAuthorRepository>()
private val weChatAuthorRepository: WeChatAuthorRepository by lazyAutowired()

val loadStatusLiveData = MutableLiveData<Pair<State, String>>()
val weChatAuthorLiveData =
Expand Down
34 changes: 25 additions & 9 deletions brick/src/main/java/com/numeron/brick/Brick.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:JvmName("Brick")
@file:Suppress("FunctionName")

package com.numeron.brick

Expand Down Expand Up @@ -32,7 +33,7 @@ fun addRoom(vararg room: Any) {
* @return Model 实例
*/
@JvmOverloads
fun <M> stack(clazz: Class<M>, iRetrofit: Any? = null): M {
fun <M> autowired(clazz: Class<M>, iRetrofit: Any? = null): M {
return ModelFactory.create(clazz, iRetrofit)
}

Expand All @@ -42,10 +43,18 @@ fun <M> stack(clazz: Class<M>, iRetrofit: Any? = null): M {
* @param iRetrofit Any? 创建Model的实例时,用于创建Retrofit Api实例的Retrofit或其它工具类
* @return M Model的实例
*/
inline fun <reified M> stack(iRetrofit: Any? = null) = stack(M::class.java, iRetrofit)
inline fun <reified M> autowired(iRetrofit: Any? = null) = autowired(M::class.java, iRetrofit)


inline fun <reified M> lazyAutowired(iRetrofit: Any? = null): Lazy<M> {
return lazy {
autowired<M>(iRetrofit)
}
}


@JvmOverloads
fun <VM : ViewModel> createViewModel(clazz: Class<VM>, store: ViewModelStore, factory: ViewModelProvider.Factory = ViewModelFactory()): VM {
fun <VM : ViewModel> viewModel(clazz: Class<VM>, store: ViewModelStore, factory: ViewModelProvider.Factory = ViewModelFactory()): VM {
return ViewModelProvider(store, factory).get(clazz)
}

Expand All @@ -58,23 +67,30 @@ fun <VM : ViewModel> createViewModel(clazz: Class<VM>, store: ViewModelStore, fa
* @return VM 创建后的实例
*/
@JvmOverloads
fun <VM : ViewModel> ViewModelStoreOwner.createViewModel(
fun <VM : ViewModel> ViewModelStoreOwner.viewModel(
clazz: Class<VM>, factory: ViewModelProvider.Factory = ViewModelFactory()): VM {
return ViewModelProvider(this, factory).get(clazz)
}

/**
* [createViewModel] 的inline方法
* [viewModel] 的inline方法
*/
inline fun <reified VM : ViewModel> ViewModelStoreOwner.createViewModel(factory: ViewModelProvider.Factory): VM {
return createViewModel(VM::class.java, factory)
inline fun <reified VM : ViewModel> ViewModelStoreOwner.viewModel(factory: ViewModelProvider.Factory): VM {
return viewModel(VM::class.java, factory)
}

/**
* 如果ViewModel需要接收参数,建议使用此方法创建ViewModel实例
* @param arguments Array<out Any> ViewModel的参数
* @return VM 创建后的实例
*/
inline fun <reified VM : ViewModel> ViewModelStoreOwner.createViewModel(vararg arguments: Any): VM {
return createViewModel(ViewModelFactory(*arguments))

inline fun <reified VM : ViewModel> ViewModelStoreOwner.viewModel(vararg arguments: Any): VM {
return viewModel(ViewModelFactory(*arguments))
}

inline fun <reified VM : ViewModel> ViewModelStoreOwner.lazyViewModel(vararg arguments: Any): Lazy<VM> {
return lazy {
viewModel<VM>(*arguments)
}
}
1 change: 0 additions & 1 deletion brick/src/main/java/com/numeron/brick/ModelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
Expand Down

0 comments on commit e86b345

Please sign in to comment.