Skip to content

Commit e0e9075

Browse files
committed
使用协程简化异步代码
1 parent 538e042 commit e0e9075

File tree

11 files changed

+54
-105
lines changed

11 files changed

+54
-105
lines changed

app/build.gradle

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId "com.fmt.github"
1313
minSdkVersion 15
1414
targetSdkVersion 28
15-
versionCode 6
16-
versionName "1.6"
15+
versionCode 7
16+
versionName "1.7"
1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
}
1919
signingConfigs {
@@ -50,15 +50,18 @@ dependencies {
5050
androidTestImplementation 'androidx.test:runner:1.2.0'
5151
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5252
implementation 'androidx.recyclerview:recyclerview:1.1.0'
53-
implementation 'com.google.android.material:material:1.0.0'
54-
55-
//Android KTX 是一组 Kotlin 扩展程序,属于 Android Jetpack 系列
56-
implementation 'androidx.core:core-ktx:1.1.0'
53+
implementation 'com.google.android.material:material:1.1.0'
5754

5855
//协程
5956
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0'
6057
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
58+
59+
//Android KTX 是一组 Kotlin 扩展程序,属于 Android Jetpack 系列
60+
implementation 'androidx.core:core-ktx:1.2.0'
61+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
6162
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
63+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
64+
6265

6366
//okhttp + retrofit(2.6.0支持协程)
6467
implementation("com.squareup.okhttp3:logging-interceptor:4.0.0")

app/src/main/java/com/fmt/github/WelcomeActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fmt.github
22

33
import android.content.Intent
4+
import androidx.lifecycle.lifecycleScope
45
import com.afollestad.assent.*
56
import com.fmt.github.base.activity.BaseDataBindActivity
67
import com.fmt.github.databinding.ActivityWelcomeBinding
@@ -47,7 +48,7 @@ class WelcomeActivity : BaseDataBindActivity<ActivityWelcomeBinding>() {
4748
}
4849

4950
private fun checkIsLogin() {
50-
launch {
51+
lifecycleScope.launch {
5152
delay(500)//挂起,但不会阻塞,后续通过resumeWith恢复执行
5253
val userList = mUserDao.getAll()
5354
(userList.isEmpty()).yes {

app/src/main/java/com/fmt/github/base/activity/BaseActivity.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package com.fmt.github.base.activity
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5-
import kotlinx.coroutines.CoroutineScope
6-
import kotlinx.coroutines.MainScope
7-
import kotlinx.coroutines.cancel
85

9-
abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
6+
abstract class BaseActivity : AppCompatActivity(){
107

118
override fun onCreate(savedInstanceState: Bundle?) {
129
super.onCreate(savedInstanceState)
@@ -26,10 +23,4 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope()
2623
open fun initData() {
2724

2825
}
29-
30-
override fun onDestroy() {
31-
super.onDestroy()
32-
cancel()
33-
}
34-
3526
}

app/src/main/java/com/fmt/github/base/activity/BaseVMActivity.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ import com.fmt.github.base.viewmodel.ErrorState
88
import com.fmt.github.base.viewmodel.LoadState
99
import com.fmt.github.base.viewmodel.SuccessState
1010
import com.fmt.github.ext.errorToast
11-
import kotlinx.coroutines.CoroutineScope
12-
import kotlinx.coroutines.MainScope
13-
import kotlinx.coroutines.cancel
1411

15-
/**
16-
* 封装带有协程基类,使用代理类完成
17-
*/
18-
abstract class BaseVMActivity : AppCompatActivity(), CoroutineScope by MainScope() {
12+
abstract class BaseVMActivity : AppCompatActivity(){
1913

2014
override fun onCreate(savedInstanceState: Bundle?) {
2115
super.onCreate(savedInstanceState)
@@ -68,9 +62,4 @@ abstract class BaseVMActivity : AppCompatActivity(), CoroutineScope by MainScope
6862
open fun handleError() {
6963

7064
}
71-
72-
override fun onDestroy() {
73-
super.onDestroy()
74-
cancel()//取消协程任务
75-
}
7665
}

app/src/main/java/com/fmt/github/ext/InputManagerExt.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.view.inputmethod.InputMethodManager
66

77
fun hideKeyboard(view: View) {
88
val manager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
9-
if (view == null) return
109
view.windowToken?.let {
1110
manager.hideSoftInputFromWindow(it, 0)
1211
}

app/src/main/java/com/fmt/github/home/activity/HomeActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.databinding.DataBindingUtil
1010
import androidx.fragment.app.Fragment
1111
import androidx.fragment.app.FragmentStatePagerAdapter
1212
import androidx.lifecycle.Observer
13+
import androidx.lifecycle.lifecycleScope
1314
import com.fmt.github.R
1415
import com.fmt.github.base.activity.BaseVMActivity
1516
import com.fmt.github.base.viewmodel.BaseViewModel
@@ -51,7 +52,7 @@ class HomeActivity : BaseVMActivity(), NavigationView.OnNavigationItemSelectedLi
5152
override fun getViewModel(): BaseViewModel = mViewModel
5253

5354
private fun initUserInfo() {
54-
launch {
55+
lifecycleScope.launch {//Androidx的协程支持LifecycleScope、ViewModelScope
5556
mUser = mUserDao.getAll()[0]
5657
initHeaderLayout()
5758
initViewPager()

app/src/main/java/com/fmt/github/home/viewmodel/HomeViewModel.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package com.fmt.github.home.viewmodel
22

33
import androidx.lifecycle.LiveData
4-
import androidx.lifecycle.MutableLiveData
4+
import androidx.lifecycle.liveData
55
import com.fmt.github.base.viewmodel.BaseViewModel
66
import com.fmt.github.ext.yes
77
import com.fmt.github.user.repository.UserRepository
88

9-
class HomeViewModel(private val mUserRepository : UserRepository) : BaseViewModel() {
9+
class HomeViewModel(private val mUserRepository: UserRepository) : BaseViewModel() {
1010

11-
fun deleteAuthorization(id: Int): LiveData<Boolean> {
12-
val mutableLiveData = MutableLiveData<Boolean>()
13-
launch {
14-
val response = mUserRepository.deleteAuthorization(id)
15-
(response.code() == 204).yes {
16-
mutableLiveData.value = true
17-
}
11+
fun deleteAuthorization(id: Int): LiveData<Boolean> = liveData {
12+
val response = mUserRepository.deleteAuthorization(id)
13+
(response.code() == 204).yes {
14+
emit(true)
1815
}
19-
return mutableLiveData
2016
}
2117

2218
fun deleteUser() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.fmt.github.repos.model
22

3-
data class ReposListModel(val items: MutableList<ReposItemModel>)
3+
data class ReposListModel(val items: List<ReposItemModel>)
Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.fmt.github.repos.viewmodel
22

33
import androidx.lifecycle.LiveData
4-
import androidx.lifecycle.MutableLiveData
4+
import androidx.lifecycle.liveData
55
import com.fmt.github.base.viewmodel.BaseViewModel
66
import com.fmt.github.ext.yes
77
import com.fmt.github.repos.model.ReposItemModel
@@ -14,47 +14,31 @@ class ReposViewModel(private val mReposRepository: ReposRepository) : BaseViewMo
1414
sort: String,
1515
order: String,
1616
page: Int
17-
): LiveData<List<ReposItemModel>> {
18-
val mutableLiveData = MutableLiveData<List<ReposItemModel>>()
19-
launch {
20-
mutableLiveData.value = mReposRepository.searchRepos(query, sort, order, page).items
21-
}
22-
return mutableLiveData
17+
): LiveData<List<ReposItemModel>> = liveData {
18+
emit(mReposRepository.searchRepos(query, sort, order, page).items)//挂起函数
2319
}
2420

25-
fun checkRepoStarred(owner: String, repo: String): LiveData<Boolean> {
26-
val mutableLiveData = MutableLiveData<Boolean>()
27-
launch {
28-
val response = mReposRepository.checkRepoStarred(owner, repo)
29-
if (response.code() == 204) {
30-
mutableLiveData.value = true
31-
} else if (response.code() == 404) {
32-
mutableLiveData.value = false
33-
}
21+
fun checkRepoStarred(owner: String, repo: String): LiveData<Boolean> = liveData {
22+
val response = mReposRepository.checkRepoStarred(owner, repo)
23+
if (response.code() == 204) {
24+
emit(true)
25+
} else if (response.code() == 404) {
26+
emit(false)
3427
}
35-
return mutableLiveData
3628
}
3729

38-
fun starRepo(owner: String, repo: String): LiveData<Boolean> {
39-
val mutableLiveData = MutableLiveData<Boolean>()
40-
launch {
41-
val response = mReposRepository.starRepo(owner, repo)
42-
(response.code() == 204).yes {
43-
mutableLiveData.value = true
44-
}
30+
fun starRepo(owner: String, repo: String): LiveData<Boolean> = liveData {
31+
val response = mReposRepository.starRepo(owner, repo)
32+
(response.code() == 204).yes {
33+
emit(true)
4534
}
46-
return mutableLiveData
4735
}
4836

49-
fun unStarRepo(owner: String, repo: String): LiveData<Boolean> {
50-
val mutableLiveData = MutableLiveData<Boolean>()
51-
launch {
52-
val response = mReposRepository.unStarRepo(owner, repo)
53-
(response.code() == 204).yes {
54-
mutableLiveData.value = true
55-
}
37+
fun unStarRepo(owner: String, repo: String): LiveData<Boolean> = liveData {
38+
val response = mReposRepository.unStarRepo(owner, repo)
39+
(response.code() == 204).yes {
40+
emit(true)
5641
}
57-
return mutableLiveData
5842
}
5943

6044
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.fmt.github.user.model
22

3-
data class UserListModel(val items:MutableList<UserModel>)
3+
data class UserListModel(val items:List<UserModel>)

0 commit comments

Comments
 (0)