From 3ca38ee5b2570fe9c6eebe47fbefbcff5c382d15 Mon Sep 17 00:00:00 2001 From: Quyunshuo Date: Tue, 25 May 2021 17:47:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0DI=E6=A1=86=E6=9E=B6H?= =?UTF-8?q?ilt=EF=BC=8C=E9=87=8D=E6=9E=84=E5=9F=BA=E7=B1=BB=E9=83=A8?= =?UTF-8?q?=E5=88=86=EF=BC=8C=E6=B7=BB=E5=8A=A0module=5Fhome=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=A4=BA=E4=BE=8B=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++- app/src/main/AndroidManifest.xml | 1 + .../app/AppApplication.kt | 42 +++---------------- build.gradle | 2 + buildGradleScript/app.gradle | 7 +++- buildGradleScript/base/base_lib.gradle | 1 + buildGradleScript/base/base_module.gradle | 9 +++- buildGradleScript/lib_base.gradle | 4 ++ buildGradleScript/lib_common.gradle | 4 ++ buildGradleScript/lib_net.gradle | 4 ++ .../buildsrc/DependencyConfig.kt | 7 +++- .../buildsrc/ProjectPluginManager.kt | 3 +- gradle/wrapper/gradle-wrapper.properties | 4 +- .../base/app/BaseApplication.kt | 2 +- .../base/mvvm/m/BaseRepository.kt | 8 ++-- .../base/mvvm/v/BaseFrameActivity.kt | 16 +++---- .../base/mvvm/v/BaseFrameFragment.kt | 15 +++---- .../base/mvvm/v/BaseFrameNotMVVMActivity.kt | 8 ++-- .../base/mvvm/v/BaseFrameNotMVVMFragment.kt | 8 ++-- .../base/mvvm/v/FrameNotMVVMView.kt | 8 ++-- .../base/mvvm/v/FrameView.kt | 8 ++-- .../base/mvvm/vm/BaseViewModel.kt | 15 +++---- lib_common/src/main/AndroidManifest.xml | 2 +- .../common/ui/BaseActivity.kt | 11 +++-- .../common/ui/BaseFragment.kt | 11 +++-- .../common/ui/BaseNotMVVMActivity.kt | 8 ++-- .../common/ui/BaseNotMVVMFragment.kt | 8 ++-- module_home/.gitignore | 1 + module_home/build.gradle.kts | 1 + module_home/consumer-rules.pro | 0 module_home/proguard-rules.pro | 21 ++++++++++ .../module/home/ExampleInstrumentedTest.kt | 24 +++++++++++ module_home/src/main/AndroidManifest.xml | 16 +++++++ .../quyunshuo/module/home/HomeRepository.kt | 22 ++++++++++ .../quyunshuo/module/home/HomeViewModel.kt | 37 ++++++++++++++++ .../com/quyunshuo/module/home/MainActivity.kt | 37 ++++++++++++++++ .../main/res/layout/home_activity_main.xml | 23 ++++++++++ module_home/src/main/res/values/strings.xml | 1 + .../quyunshuo/module/home/ExampleUnitTest.kt | 17 ++++++++ settings.gradle.kts | 3 +- 40 files changed, 316 insertions(+), 118 deletions(-) create mode 100644 module_home/.gitignore create mode 100644 module_home/build.gradle.kts create mode 100644 module_home/consumer-rules.pro create mode 100644 module_home/proguard-rules.pro create mode 100644 module_home/src/androidTest/java/com/quyunshuo/module/home/ExampleInstrumentedTest.kt create mode 100644 module_home/src/main/AndroidManifest.xml create mode 100644 module_home/src/main/java/com/quyunshuo/module/home/HomeRepository.kt create mode 100644 module_home/src/main/java/com/quyunshuo/module/home/HomeViewModel.kt create mode 100644 module_home/src/main/java/com/quyunshuo/module/home/MainActivity.kt create mode 100644 module_home/src/main/res/layout/home_activity_main.xml create mode 100644 module_home/src/main/res/values/strings.xml create mode 100644 module_home/src/test/java/com/quyunshuo/module/home/ExampleUnitTest.kt diff --git a/README.md b/README.md index c4f8689..c24fc78 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ android { * [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) * [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) * [ViewBinding](https://developer.android.com/topic/libraries/view-binding) +* [Hilt](https://developer.android.com/jetpack/androidx/releases/hilt) * [Android KTX](https://developer.android.com/kotlin/ktx) * [OkHttp](https://github.com/square/okhttp):网络请求 * [Retrofit](https://github.com/square/retrofit):网络请求 @@ -364,4 +365,16 @@ fun test() { **资料:** -官方文档: [https://developer.android.com/topic/libraries/architecture/lifecycle](https://developer.android.com/topic/libraries/architecture/lifecycle) \ No newline at end of file +官方文档: [https://developer.android.com/topic/libraries/architecture/lifecycle](https://developer.android.com/topic/libraries/architecture/lifecycle) + +### Hilt + +**Hilt** 是 **Android** 的依赖项注入库,可减少在项目中执行手动依赖项注入的样板代码。执行手动依赖项注入要求您手动构造每个类及其依赖项,并借助容器重复使用和管理依赖项。 + +**Hilt** 通过为项目中的每个 **Android** 类提供容器并自动管理其生命周期,提供了一种在应用中使用 **DI(依赖项注入)**的标准方法。**Hilt** 在热门 **DI** 库 **Dagger** 的基础上构建而成,因而能够受益于 **Dagger** 的编译时正确性、运行时性能、可伸缩性和 **Android Studio** 支持。 + +**资料:** + +目前官方文档还没有更新正式版的,还是 **alpha** 版本的文档:[使用 Hilt 实现依赖项注入](https://developer.android.com/training/dependency-injection/hilt-android) + +**Dagger** 的 **Hilt** 文档目前是最新的:[Dagger-Hilt](https://dagger.dev/hilt/) \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6105beb..077c24d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ tools:ignore="ProtectedPermissions" /> : - AppCompatActivity(), FrameView { +abstract class BaseFrameActivity : AppCompatActivity(), FrameView { protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { BindingReflex.reflexViewBinding(javaClass, layoutInflater) } - protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) { - BindingReflex.reflexViewModel(javaClass, this) - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(mBinding.root) diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameFragment.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameFragment.kt index d6a5525..a8c1100 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameFragment.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameFragment.kt @@ -5,7 +5,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import androidx.lifecycle.ViewModel import androidx.viewbinding.ViewBinding import com.alibaba.android.arouter.launcher.ARouter import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex @@ -13,21 +12,17 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils /** - * @Author: QuYunShuo - * @Time: 2020/8/27 - * @Class: BaseFrameFragment - * @Remark: Fragment基类 与项目无关 + * Fragment基类 + * + * @author Qu Yunshuo + * @since 8/27/20 */ -abstract class BaseFrameFragment : Fragment(), FrameView { +abstract class BaseFrameFragment : Fragment(), FrameView { protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { BindingReflex.reflexViewBinding(javaClass, layoutInflater) } - protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) { - BindingReflex.reflexViewModel(javaClass, this) - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMActivity.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMActivity.kt index 44e68fa..ed46ed2 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMActivity.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMActivity.kt @@ -9,10 +9,10 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils /** - * @Author: QuYunShuo - * @Time: 2020/9/10 - * @Class: BaseFrameNotMVVMActivity - * @Remark: 不使用 MVVM 的 Activity 基类 + * 不使用 MVVM 的 Activity 基类 + * + * @author Qu Yunshuo + * @since /9/10/20 */ abstract class BaseFrameNotMVVMActivity : AppCompatActivity(), FrameNotMVVMView { diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMFragment.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMFragment.kt index 13a1388..254c78f 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMFragment.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/BaseFrameNotMVVMFragment.kt @@ -12,10 +12,10 @@ import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusRegister import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils /** - * @Author: QuYunShuo - * @Time: 2020/9/10 - * @Class: BaseFrameNotMVVMFragment - * @Remark: 不使用 MVVM 的 Fragment 基类 + * 不使用 MVVM 的 Fragment 基类 + * + * @author Qu Yunshuo + * @since 9/10/20 */ abstract class BaseFrameNotMVVMFragment : Fragment(), FrameNotMVVMView { diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameNotMVVMView.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameNotMVVMView.kt index 11ae08c..d4361ab 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameNotMVVMView.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameNotMVVMView.kt @@ -3,10 +3,10 @@ package com.quyunshuo.androidbaseframemvvm.base.mvvm.v import androidx.viewbinding.ViewBinding /** - * @Author: QuYunShuo - * @Time: 2020/10/13 - * @Class: FrameNotMVVMView - * @Remark: View层基类抽象 + * View层基类抽象 + * + * @author Qu Yunshuo + * @since 10/13/20 */ interface FrameNotMVVMView { /** diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameView.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameView.kt index 156a3a6..9c938e2 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameView.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/v/FrameView.kt @@ -3,10 +3,10 @@ package com.quyunshuo.androidbaseframemvvm.base.mvvm.v import androidx.viewbinding.ViewBinding /** - * @Author: QuYunShuo - * @Time: 2020/10/13 - * @Class: FrameView - * @Remark: View层基类抽象 + * View层基类抽象 + * + * @author Qu Yunshuo + * @since 10/13/20 */ interface FrameView { /** diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/vm/BaseViewModel.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/vm/BaseViewModel.kt index a466b4d..bf1e661 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/vm/BaseViewModel.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/mvvm/vm/BaseViewModel.kt @@ -2,23 +2,18 @@ package com.quyunshuo.androidbaseframemvvm.base.mvvm.vm import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import com.quyunshuo.androidbaseframemvvm.base.mvvm.m.BaseRepository /** - * @Author: QuYunShuo - * @Time: 2020/8/27 - * @Class: BaseViewModel - * @Remark: ViewModel 基类 + * ViewModel 基类 + * + * @author Qu Yunshuo + * @since 8/27/20 */ -abstract class BaseViewModel : ViewModel() { +abstract class BaseViewModel : ViewModel() { // Loading 状态 val isLoading = MutableLiveData(false) // 请求异常 val requestError = MutableLiveData() - - protected val mRepository: R by lazy { initRepository() } - - protected abstract fun initRepository(): R } \ No newline at end of file diff --git a/lib_common/src/main/AndroidManifest.xml b/lib_common/src/main/AndroidManifest.xml index 6e745f1..dca3f02 100644 --- a/lib_common/src/main/AndroidManifest.xml +++ b/lib_common/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ - + : BaseFrameActivity() \ No newline at end of file +abstract class BaseActivity : BaseFrameActivity() \ No newline at end of file diff --git a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseFragment.kt b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseFragment.kt index 2e24bf6..b3d44dd 100644 --- a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseFragment.kt +++ b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseFragment.kt @@ -1,13 +1,12 @@ package com.quyunshuo.androidbaseframemvvm.common.ui -import androidx.lifecycle.ViewModel import androidx.viewbinding.ViewBinding import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameFragment /** - * @Author: QuYunShuo - * @Time: 2020/8/27 - * @Class: BaseFragment - * @Remark: 项目相关的Fragment基类 + * Fragment基类 + * + * @author Qu Yunshuo + * @since 8/27/20 */ -abstract class BaseFragment : BaseFrameFragment() \ No newline at end of file +abstract class BaseFragment : BaseFrameFragment() \ No newline at end of file diff --git a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMActivity.kt b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMActivity.kt index 1e0beb5..bd7ea24 100644 --- a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMActivity.kt +++ b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMActivity.kt @@ -4,9 +4,9 @@ import androidx.viewbinding.ViewBinding import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameNotMVVMActivity /** - * @Author: QuYunShuo - * @Time: 2020/9/10 - * @Class: BaseNotMVVMActivity - * @Remark: 不是 MVVM 模式的基类 + * 不是 MVVM 模式的基类 + * + * @author Qu Yunshuo + * @since 9/10/20 */ abstract class BaseNotMVVMActivity : BaseFrameNotMVVMActivity() \ No newline at end of file diff --git a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMFragment.kt b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMFragment.kt index 3bd3a2c..96a40c7 100644 --- a/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMFragment.kt +++ b/lib_common/src/main/java/com/quyunshuo/androidbaseframemvvm/common/ui/BaseNotMVVMFragment.kt @@ -4,9 +4,9 @@ import androidx.viewbinding.ViewBinding import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameNotMVVMFragment /** - * @Author: QuYunShuo - * @Time: 2020/9/10 - * @Class: BaseNotMVVMFragment - * @Remark: 不是 MVVM 模式的基类 + * 不是 MVVM 模式的基类 + * + * @author Qu Yunshuo + * @since 9/10/20 */ abstract class BaseNotMVVMFragment : BaseFrameNotMVVMFragment() \ No newline at end of file diff --git a/module_home/.gitignore b/module_home/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/module_home/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/module_home/build.gradle.kts b/module_home/build.gradle.kts new file mode 100644 index 0000000..c2908fd --- /dev/null +++ b/module_home/build.gradle.kts @@ -0,0 +1 @@ +apply("../buildGradleScript/module_home.gradle") \ No newline at end of file diff --git a/module_home/consumer-rules.pro b/module_home/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/module_home/proguard-rules.pro b/module_home/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/module_home/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/module_home/src/androidTest/java/com/quyunshuo/module/home/ExampleInstrumentedTest.kt b/module_home/src/androidTest/java/com/quyunshuo/module/home/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..c35de01 --- /dev/null +++ b/module_home/src/androidTest/java/com/quyunshuo/module/home/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.quyunshuo.module.home + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.quyunshuo.module.home.test", appContext.packageName) + } +} \ No newline at end of file diff --git a/module_home/src/main/AndroidManifest.xml b/module_home/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d9e29be --- /dev/null +++ b/module_home/src/main/AndroidManifest.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/module_home/src/main/java/com/quyunshuo/module/home/HomeRepository.kt b/module_home/src/main/java/com/quyunshuo/module/home/HomeRepository.kt new file mode 100644 index 0000000..8f7d414 --- /dev/null +++ b/module_home/src/main/java/com/quyunshuo/module/home/HomeRepository.kt @@ -0,0 +1,22 @@ +package com.quyunshuo.module.home + +import com.quyunshuo.androidbaseframemvvm.base.mvvm.m.BaseRepository +import kotlinx.coroutines.delay +import javax.inject.Inject + +/** + * 首页M层 + * + * @author Qu Yunshuo + * @since 5/25/21 5:42 PM + */ +class HomeRepository @Inject constructor() : BaseRepository() { + + /** + * 模拟获取数据 + */ + suspend fun getData() = flowRequest { + delay(1000L) + emit("Hello Hilt") + } +} \ No newline at end of file diff --git a/module_home/src/main/java/com/quyunshuo/module/home/HomeViewModel.kt b/module_home/src/main/java/com/quyunshuo/module/home/HomeViewModel.kt new file mode 100644 index 0000000..07f2069 --- /dev/null +++ b/module_home/src/main/java/com/quyunshuo/module/home/HomeViewModel.kt @@ -0,0 +1,37 @@ +package com.quyunshuo.module.home + +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope +import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch +import javax.inject.Inject + +/** + * 首页的VM层 + * + * @property mRepository HomeRepository 仓库层 通过Hilt注入 + * + * @author Qu Yunshuo + * @since 5/25/21 5:41 PM + */ +@HiltViewModel +class HomeViewModel @Inject constructor(private val mRepository: HomeRepository) : BaseViewModel() { + + val data = MutableLiveData() + + /** + * 模拟获取数据 + */ + fun getData() { + viewModelScope.launch(Dispatchers.IO) { + mRepository.getData() + .catch { Log.d("qqq", "getData: $it") } + .collect { data.postValue(it) } + } + } +} \ No newline at end of file diff --git a/module_home/src/main/java/com/quyunshuo/module/home/MainActivity.kt b/module_home/src/main/java/com/quyunshuo/module/home/MainActivity.kt new file mode 100644 index 0000000..69ccbe2 --- /dev/null +++ b/module_home/src/main/java/com/quyunshuo/module/home/MainActivity.kt @@ -0,0 +1,37 @@ +package com.quyunshuo.module.home + +import android.graphics.Color +import androidx.activity.viewModels +import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity +import com.quyunshuo.module.home.databinding.HomeActivityMainBinding +import dagger.hilt.android.AndroidEntryPoint + +/** + * 首页 + * + * @author Qu Yunshuo + * @since 5/22/21 2:26 PM + */ +@AndroidEntryPoint +class MainActivity : BaseActivity() { + + /** + * 通过 viewModels() + Hilt 获取 ViewModel 实例 + */ + private val mViewModel by viewModels() + + override fun HomeActivityMainBinding.initView() {} + + override fun initLiveDataObserve() { + // 订阅数据 + mViewModel.data.observe(this, { + mBinding.vTvHello.text = it + mBinding.vTvHello.setTextColor(Color.BLUE) + }) + } + + override fun initRequestData() { + // 模拟获取数据 + mViewModel.getData() + } +} \ No newline at end of file diff --git a/module_home/src/main/res/layout/home_activity_main.xml b/module_home/src/main/res/layout/home_activity_main.xml new file mode 100644 index 0000000..3ecbc1c --- /dev/null +++ b/module_home/src/main/res/layout/home_activity_main.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/module_home/src/main/res/values/strings.xml b/module_home/src/main/res/values/strings.xml new file mode 100644 index 0000000..73862c4 --- /dev/null +++ b/module_home/src/main/res/values/strings.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/module_home/src/test/java/com/quyunshuo/module/home/ExampleUnitTest.kt b/module_home/src/test/java/com/quyunshuo/module/home/ExampleUnitTest.kt new file mode 100644 index 0000000..5bde5f9 --- /dev/null +++ b/module_home/src/test/java/com/quyunshuo/module/home/ExampleUnitTest.kt @@ -0,0 +1,17 @@ +package com.quyunshuo.module.home + +import org.junit.Test + +import org.junit.Assert.* + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 0a73e4b..af85f15 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,5 +2,6 @@ include( ":app", ":lib_base", ":lib_common", - ":lib_net" + ":lib_net", + ":module_home" ) \ No newline at end of file