feat: 添加DI框架Hilt,重构基类部分,添加module_home模块示例用法

This commit is contained in:
Quyunshuo
2021-05-25 17:47:55 +08:00
parent eb8ec75f04
commit 3ca38ee5b2
40 changed files with 316 additions and 118 deletions

1
module_home/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1 @@
apply("../buildGradleScript/module_home.gradle")

View File

21
module_home/proguard-rules.pro vendored Normal file
View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quyunshuo.module.home">
<application>
<!-- Main 首页 -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -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<String> {
delay(1000L)
emit("Hello Hilt")
}
}

View File

@@ -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<String>()
/**
* 模拟获取数据
*/
fun getData() {
viewModelScope.launch(Dispatchers.IO) {
mRepository.getData()
.catch { Log.d("qqq", "getData: $it") }
.collect { data.postValue(it) }
}
}
}

View File

@@ -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<HomeActivityMainBinding>() {
/**
* 通过 viewModels() + Hilt 获取 ViewModel 实例
*/
private val mViewModel by viewModels<HomeViewModel>()
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()
}
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/vTvHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -0,0 +1 @@
<resources></resources>

View File

@@ -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)
}
}