[Android Kotlin-Flow]Kotlin-Flow

作者: Darren老师 | 来源:发表于2022-06-29 17:32 被阅读0次

    前言:

    Flow是kotlin提供的一个重要的异步工具,类似于RxJava的响应式编程框架,使用协程封装成生产者-消费者模式,上流来负责生产,下流来接收消耗。

    关于这个项目:

    • 如果您想开始使用 Kotlin Flow,那么这个项目就是为您准备的。

    • 本项目已实现 Android 中 Kotlin Flow 的常见用例。

    • 这是您学习 Kotlin Flow for Android 开发的一站式解决方案。

    • 通过该项目中的示例学习 Kotlin Flow 的步骤

      • 首先,要学习 Kotlin Flow 的概念
      • 了解 Kotlin Flow 中的终端运算符。
      • 在 Kotlin 中使用 Flow Builder 创建流。
      • Kotlin Flow 中的异常处理。
      • Kotlin 中的 StateFlow API。
      • 使用 Kotlin 流运算符实现即时搜索。
      • 具有指数退避延迟的 Kotlin 流重试运算符。
      • 用于并行多个网络调用的 Zip 运算符。
      • 然后,只需克隆、构建、运行项目并开始通过示例学习 Kotlin Flow。

    这个 Kotlin Flow 示例项目将帮助您学习以下 Android 应用程序开发:

    • 什么是 Kotlin 流?
    • 如何在 Android 中使用 Kotlin Flow?
    • 关于如何在 Android 中实现 Kotlin 流程的分步指南?
    • 在 Kotlin Flow 中完成简单的任务
    • 使用 filter、map、reduce、flatMapConcat、zip 等运算符。
    • Kotlin Flow 中的异常
    • 如何在 Flow 中使用 onCompletion?
    • 在 Flow 中使用重试运算符重试任务
    • 在流程中使用指数退避重试任务
    • 将 Kotlin Flow 与 Retrofit 结合使用。
    • 将 Kotlin Flow 与房间数据库结合使用。
    • 使用 Kotlin Flow 并行进行两个网络调用。
    • 使用 Kotlin Flow 连续执行任务。
    • Flow 中的单元测试。

    Android 开发的 Kotlin 流程示例:

    • 单一网络调用: 了解如何使用 Kotlin Flow 进行网络调用。这是 Android 应用程序开发中一个非常简单的用例。
    • 活动代码
    包 com.mindorks.kotlinFlow.learn.retrofit.single 
    
    导入 android.os.Bundle
    导入 android.view.View
    导入 android.widget.Toast
    导入 androidx.appcompat.app.AppCompatActivity
    导入 androidx.lifecycle.Observer
    导入 androidx.lifecycle.ViewModelProviders
    导入 androidx.recyclerview.widget.DividerItemDecoration
    导入 androidx.recyclerview.widget.LinearLayoutManager
    导入 com.mindorks.kotlinFlow.R
    导入 com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    导入 com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    导入 com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    导入 com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    导入 com.mindorks.kotlinFlow.data.model.ApiUser
    导入 com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    导入 com.mindorks.kotlinFlow.utils.Status
    导入 com.mindorks.kotlinFlow.utils.ViewModelFactory
    导入 kotlinx.android.synthetic.main.activity_recycler_view.*
    
    类 SingleNetworkCallActivity : AppCompatActivity () {
    
        私有 lateinit  var viewModel : SingleNetworkCallViewModel
        私有 lateinit  var适配器: ApiUserAdapter
        
        覆盖 fun  onCreate ( savedInstanceState :  Bundle ? ) {
            超级.onCreate(savedInstanceState)
            设置内容视图(R.layout.activity_recycler_view)
            设置界面()
            设置视图模型()
            设置观察者()
        }
        
        私人 乐趣 setupUI () {
            recyclerView.layoutManager =  LinearLayoutManager (这个)
            适配器=
                ApiUserAdapter (
                    数组列表()
                )
            recyclerView.addItemDecoration(
                分隔项装饰(
                    recyclerView.context,
                    (recyclerView.layoutManager作为 LinearLayoutManager ).orientation
                )
            )
            recyclerView.adapter =适配器
        }
        
        私人 乐趣 setupObserver () {
            viewModel.getUsers().observe(这个,观察者{
                当(it.status){
                    状态。成功 -> {
                        progressBar.visibility = 查看。消失了
                        it.data?. 让{ 用户->渲染列表(用户) }
                        recyclerView.visibility = 查看。可见的
                    }
                    状态。加载 -> {
                        progressBar.visibility = 查看。可见的
                        recyclerView.visibility = 查看。消失了
                    }
                    状态。错误 -> {
                        //处理错误
                        progressBar.visibility = 查看。消失了
                        Toast .makeText( this , it.message, Toast . LENGTH_SHORT ).show()
                    }
                }
            })
        }
        
        私人 乐趣 渲染列表(用户:列表< ApiUser >){ 
            适配器.addData(用户)
            适配器.notifyDataSetChanged()
        }
        
        私人 乐趣 setupViewModel () {
            viewModel =  ViewModelProviders .of (
                这个,
                视图模型工厂(
                    ApiHelperImpl ( RetrofitBuilder .apiService),
                    DatabaseHelperImpl ( DatabaseBuilder .getInstance(applicationContext))
                )
            ).get( SingleNetworkCallViewModel ::类.java)
        }
    
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retrofit.single
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.InternalCoroutinesApi
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.launch
    
    class SingleNetworkCallViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<ApiUser>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                apiHelper.getUsers()
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<ApiUser>>> {
            return users
        }
    
    }
    
    • 系列网络调用: 了解如何使用 Kotlin Flow 进行系列网络调用。当您要进行依赖于另一个网络调用的网络调用时,这很有用。
    • 活动代码
    package com.mindorks.kotlinFlow.learn.retrofit.series
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class SeriesNetworkCallsActivity : AppCompatActivity() {
    
        private lateinit var viewModel: SeriesNetworkCallsViewModel
        private lateinit var adapter: ApiUserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                ApiUserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<ApiUser>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(SeriesNetworkCallsViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retrofit.series
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.flow.flatMapConcat
    import kotlinx.coroutines.flow.flowOn
    import kotlinx.coroutines.launch
    
    class SeriesNetworkCallsViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<ApiUser>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                val allUsersFromApi = mutableListOf<ApiUser>()
                apiHelper.getUsers()
                    .flatMapConcat { usersFromApi ->
                        allUsersFromApi.addAll(usersFromApi)
                        apiHelper.getMoreUsers()
                    }
                    .flowOn(Dispatchers.Default)
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect { moreUsersFromApi ->
                        allUsersFromApi.addAll(moreUsersFromApi)
                        users.postValue(Resource.success(allUsersFromApi))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<ApiUser>>> {
            return users
        }
    
    }
    
    • 并行网络调用: 了解如何使用 Kotlin Flow 并行进行网络调用。当您要进行依赖于另一个网络调用的网络调用时,这很有用。
    • 活动代码
    package com.mindorks.kotlinFlow.learn.retrofit.parallel
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class ParallelNetworkCallsActivity : AppCompatActivity() {
    
        private lateinit var viewModel: ParallelNetworkCallsViewModel
        private lateinit var adapter: ApiUserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                ApiUserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<ApiUser>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(ParallelNetworkCallsViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retrofit.parallel
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.flow.flowOn
    import kotlinx.coroutines.flow.zip
    import kotlinx.coroutines.launch
    
    class ParallelNetworkCallsViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<ApiUser>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                apiHelper.getUsers()
                    .zip(apiHelper.getMoreUsers()) { usersFromApi, moreUsersFromApi ->
                        val allUsersFromApi = mutableListOf<ApiUser>()
                        allUsersFromApi.addAll(usersFromApi)
                        allUsersFromApi.addAll(moreUsersFromApi)
                        return@zip allUsersFromApi
                    }
                    .flowOn(Dispatchers.Default)
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<ApiUser>>> {
            return users
        }
    
    }
    
    • 房间数据库操作:了解如何使用 Kotlin Flow 在数据库中获取或插入实体。当您在 Android 应用程序中使用房间数据库时,这很有用。
    • 活动代码
    package com.mindorks.kotlinFlow.learn.room
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.local.entity.User
    import com.mindorks.kotlinFlow.learn.base.UserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class RoomDBActivity : AppCompatActivity() {
    
        private lateinit var viewModel: RoomDBViewModel
        private lateinit var adapter: UserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                UserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<User>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(RoomDBViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.room
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.local.entity.User
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    
    class RoomDBViewModel(private val apiHelper: ApiHelper, private val dbHelper: DatabaseHelper) :
        ViewModel() {
    
        private val users = MutableLiveData<Resource<List<User>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                dbHelper.getUsers()
                    .flatMapConcat { usersFromDb ->
                        if (usersFromDb.isEmpty()) {
                            return@flatMapConcat apiHelper.getUsers()
                                .map { apiUserList ->
                                    val userList = mutableListOf<User>()
                                    for (apiUser in apiUserList) {
                                        val user = User(
                                            apiUser.id,
                                            apiUser.name,
                                            apiUser.email,
                                            apiUser.avatar
                                        )
                                        userList.add(user)
                                    }
                                    userList
                                }
                                .flatMapConcat { usersToInsertInDB ->
                                    dbHelper.insertAll(usersToInsertInDB)
                                        .flatMapConcat {
                                            flow {
                                                emit(usersToInsertInDB)
                                            }
                                        }
                                }
                        } else {
                            return@flatMapConcat flow {
                                emit(usersFromDb)
                            }
                        }
                    }
                    .flowOn(Dispatchers.Default)
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<User>>> {
            return users
        }
    
    }
    
    • 长时间运行的任务:了解如何使用 Kotlin Flow 执行长时间运行的任务。如果您想使用 Kotlin Flow 在后台线程中执行任何任务,那么这很有用。
    • 活动代码
    package com.mindorks.kotlinFlow.learn.task.onetask
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_long_running_task.*
    import kotlinx.android.synthetic.main.activity_recycler_view.progressBar
    
    class LongRunningTaskActivity : AppCompatActivity() {
    
        private lateinit var viewModel: LongRunningTaskViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_long_running_task)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startLongRunningTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(LongRunningTaskViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.task.onetask
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.delay
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    
    class LongRunningTaskViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startLongRunningTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                // do a long running task
                doLongRunningTask()
                    .flowOn(Dispatchers.Default)
                    .catch {
                        status.postValue(Resource.error("Something Went Wrong", null))
                    }
                    .collect {
                        status.postValue(Resource.success("Task Completed"))
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
        private fun doLongRunningTask(): Flow<Int> {
            return flow {
                // your code for doing a long running task
                // Added delay to simulate
                delay(5000)
                emit(0)
            }
        }
    }
    
    • 两个长时间运行的任务:了解如何使用 Kotlin Flow 并行运行两个长时间运行的任务。
    • 活动代码
    package com.mindorks.kotlinFlow.learn.task.twotasks
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_long_running_task.*
    import kotlinx.android.synthetic.main.activity_recycler_view.progressBar
    
    class TwoLongRunningTasksActivity : AppCompatActivity() {
    
        private lateinit var viewModel: TwoLongRunningTasksViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_long_running_task)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startLongRunningTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(TwoLongRunningTasksViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.task.twotasks
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.delay
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    
    class TwoLongRunningTasksViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startLongRunningTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                doLongRunningTaskOne()
                    .zip(doLongRunningTaskTwo()) { resultOne, resultTwo ->
                        return@zip resultOne + resultTwo
                    }
                    .flowOn(Dispatchers.Default)
                    .catch { e ->
                        status.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        status.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
        private fun doLongRunningTaskTwo(): Flow<String> {
            return flow {
                // your code for doing a long running task
                // Added delay to simulate
                delay(5000)
                emit("Two")
            }
        }
    
        private fun doLongRunningTaskOne(): Flow<String> {
            return flow {
                // your code for doing a long running task
                // Added delay to simulate
                delay(5000)
                emit("One")
            }
        }
    
    }
    
    • Catch 错误处理:了解如何使用 Catch 处理 Kotlin Flow 中的错误。

    • 活动代码

    package com.mindorks.kotlinFlow.learn.errorhandling.catch
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class CatchActivity : AppCompatActivity() {
    
        private lateinit var viewModel: CatchViewModel
        private lateinit var adapter: ApiUserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                ApiUserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<ApiUser>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(CatchViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.errorhandling.catch
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.launch
    
    class CatchViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<ApiUser>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                apiHelper.getUsersWithError()
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<ApiUser>>> {
            return users
        }
    
    }
    
    • EmitAll 错误处理:了解如何使用 emitAll 处理 Kotlin Flow 中的错误。

    • 活动代码

    package com.mindorks.kotlinFlow.learn.errorhandling.emitall
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class EmitAllActivity : AppCompatActivity() {
    
        private lateinit var viewModel: EmitAllViewModel
        private lateinit var adapter: ApiUserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                ApiUserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<ApiUser>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(EmitAllViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.errorhandling.emitall
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.model.ApiUser
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    
    class EmitAllViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<ApiUser>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                apiHelper.getUsers()
                    .zip(
                        apiHelper.getUsersWithError()
                            .catch { emitAll(flowOf(emptyList())) }) { usersFromApi, moreUsersFromApi ->
                        val allUsersFromApi = mutableListOf<ApiUser>()
                        allUsersFromApi.addAll(usersFromApi)
                        allUsersFromApi.addAll(moreUsersFromApi)
                        return@zip allUsersFromApi
                    }
                    .flowOn(Dispatchers.Default)
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<ApiUser>>> {
            return users
        }
    
    }
    
    • 完成:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.completion
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.learn.base.ApiUserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_long_running_task.*
    import kotlinx.android.synthetic.main.activity_recycler_view.progressBar
    
    class CompletionActivity : AppCompatActivity() {
    
        private lateinit var viewModel: CompletionViewModel
        private lateinit var adapter: ApiUserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_completion)
            setupViewModel()
            setupObserver()
        }
    
        private fun setupObserver() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(CompletionViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.completion
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.flow.onCompletion
    import kotlinx.coroutines.launch
    
    class CompletionViewModel(
        private val apiHelper: ApiHelper,
        private val dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        init{
            fetchUsers()
        }
        private fun fetchUsers() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                apiHelper.getUsers()
                    .catch { e ->
                        status.postValue(Resource.error(e.toString(), null))
                    }
                    .onCompletion {
                        status.postValue(Resource.success("Task Completed"))
                    }
                    .collect {
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
        
    }
    
    • 减少:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.reduce
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_long_running_task.*
    
    class ReduceActivity : AppCompatActivity() {
    
        private lateinit var viewModel: ReduceViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_long_running_task)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startReduceTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(ReduceViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.reduce
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.flow.asFlow
    import kotlinx.coroutines.flow.reduce
    import kotlinx.coroutines.launch
    
    class ReduceViewModel(
        val apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startReduceTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                val result = (1..5).asFlow()
                    .reduce { a, b -> a + b }
    
                status.postValue(Resource.success(result.toString()))
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
    }
    
    • 地图:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.map
    
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.LinearLayoutManager
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.data.local.entity.User
    import com.mindorks.kotlinFlow.learn.base.UserAdapter
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_recycler_view.*
    
    class MapActivity : AppCompatActivity() {
    
        private lateinit var viewModel: MapViewModel
        private lateinit var adapter: UserAdapter
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler_view)
            setupUI()
            setupViewModel()
            setupObserver()
        }
    
        private fun setupUI() {
            recyclerView.layoutManager = LinearLayoutManager(this)
            adapter =
                UserAdapter(
                    arrayListOf()
                )
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
            recyclerView.adapter = adapter
        }
    
        private fun setupObserver() {
            viewModel.getUsers().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        it.data?.let { users -> renderList(users) }
                        recyclerView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        recyclerView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }
    
        private fun renderList(users: List<User>) {
            adapter.addData(users)
            adapter.notifyDataSetChanged()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(MapViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.map
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.data.local.entity.User
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.flow.catch
    import kotlinx.coroutines.flow.collect
    import kotlinx.coroutines.flow.map
    import kotlinx.coroutines.launch
    
    
    class MapViewModel(
        val apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val users = MutableLiveData<Resource<List<User>>>()
    
        init {
            fetchUsers()
        }
    
        private fun fetchUsers() {
            viewModelScope.launch {
                users.postValue(Resource.loading(null))
                apiHelper.getUsers()
                    .map { apiUserList ->
                        val userList = mutableListOf<User>()
                        for (apiUser in apiUserList) {
                            val user = User(
                                apiUser.id,
                                apiUser.name,
                                apiUser.email,
                                apiUser.avatar
                            )
                            userList.add(user)
                        }
                        userList
                    }
                    .catch { e ->
                        users.postValue(Resource.error(e.toString(), null))
                    }
                    .collect {
                        users.postValue(Resource.success(it))
                    }
            }
        }
    
        fun getUsers(): LiveData<Resource<List<User>>> {
            return users
        }
    }
    
    • 筛选:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.filter
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_long_running_task.*
    import kotlinx.android.synthetic.main.activity_recycler_view.progressBar
    
    class FilterActivity : AppCompatActivity() {
    
        private lateinit var viewModel: FilterViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_long_running_task)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startFilterTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(FilterViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.filter
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.flow.asFlow
    import kotlinx.coroutines.flow.filter
    import kotlinx.coroutines.flow.toList
    import kotlinx.coroutines.launch
    
    class FilterViewModel(
        apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
        private val status = MutableLiveData<Resource<String>>()
    
        fun startFilterTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                val result = mutableListOf<Int>()
                (1..5).asFlow()
                    .filter {
                        it % 2 == 0
                    }
                    .toList(result)
    
                status.postValue(Resource.success(result.toString()))
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
    }
    
    • 搜索功能:使用 Kotlin Flow Operators 实现搜索 - Debounce、Filter、DistinctUntilChanged、FlatMapLatest。

      • 活动代码
    package com.mindorks.kotlinFlow.learn.search
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.utils.getQueryTextChangeStateFlow
    import kotlinx.android.synthetic.main.activity_search.*
    import kotlinx.coroutines.*
    import kotlinx.coroutines.flow.*
    import kotlin.coroutines.CoroutineContext
    
    class SearchActivity : AppCompatActivity(), CoroutineScope {
    
        override val coroutineContext: CoroutineContext
            get() = Dispatchers.Main + job
    
        private lateinit var job: Job
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_search)
            job = Job()
            setUpSearchStateFlow()
        }
    
        override fun onDestroy() {
            job.cancel()
            super.onDestroy()
        }
    
        private fun setUpSearchStateFlow() {
            launch {
                searchView.getQueryTextChangeStateFlow()
                    .debounce(300)
                    .filter { query ->
                        if (query.isEmpty()) {
                            textViewResult.text = ""
                            return@filter false
                        } else {
                            return@filter true
                        }
                    }
                    .distinctUntilChanged()
                    .flatMapLatest { query ->
                        dataFromNetwork(query)
                            .catch {
                                emitAll(flowOf(""))
                            }
                    }
                    .flowOn(Dispatchers.Default)
                    .collect { result ->
                        textViewResult.text = result
                    }
            }
        }
    
        /**
         * Simulation of network data
         */
        private fun dataFromNetwork(query: String): Flow<String> {
            return flow {
                delay(2000)
                emit(query)
            }
        }
    
    }
    
    • 重试:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.retry
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_retry.*
    
    class RetryActivity : AppCompatActivity() {
    
        private lateinit var viewModel: RetryViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_retry)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(RetryViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retry
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.delay
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    import java.io.IOException
    
    class RetryViewModel(
        val apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                // do a long running task
                doLongRunningTask()
                    .flowOn(Dispatchers.Default)
                    .retry(retries = 3) { cause ->
                        if (cause is IOException) {
                            delay(2000)
                            return@retry true
                        } else {
                            return@retry false
                        }
                    }
                    .catch {
                        status.postValue(Resource.error("Something Went Wrong", null))
                    }
                    .collect {
                        status.postValue(Resource.success("Task Completed"))
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
        private fun doLongRunningTask(): Flow<Int> {
            return flow {
                // your code for doing a long running task
                // Added delay, random number, and exception to simulate
    
                delay(2000)
    
                val randomNumber = (0..2).random()
    
                if (randomNumber == 0) {
                    throw IOException()
                } else if (randomNumber == 1) {
                    throw IndexOutOfBoundsException()
                }
    
                delay(2000)
                emit(0)
            }
        }
    
    }
    
    • 重试时间:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.retrywhen
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_retry.*
    
    class RetryWhenActivity : AppCompatActivity() {
    
        private lateinit var viewModel: RetryWhenViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_retry)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(RetryWhenViewModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retrywhen
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.delay
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    import java.io.IOException
    
    class RetryWhenViewModel(
        val apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                // do a long running task
                doLongRunningTask()
                    .flowOn(Dispatchers.Default)
                    .retryWhen { cause, attempt ->
                        if (cause is IOException && attempt < 3) {
                            delay(2000)
                            return@retryWhen true
                        } else {
                            return@retryWhen false
                        }
                    }
                    .catch {
                        status.postValue(Resource.error("Something Went Wrong", null))
                    }
                    .collect {
                        status.postValue(Resource.success("Task Completed"))
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
        private fun doLongRunningTask(): Flow<Int> {
            return flow {
                // your code for doing a long running task
                // Added delay, random number, and exception to simulate
    
                delay(2000)
    
                val randomNumber = (0..2).random()
    
                if (randomNumber == 0) {
                    throw IOException()
                } else if (randomNumber == 1) {
                    throw IndexOutOfBoundsException()
                }
    
                delay(2000)
                emit(0)
            }
        }
    
    }
    
    • 使用指数退避重试:

    • 活动代码

    package com.mindorks.kotlinFlow.learn.retryexponentialbackoff
    
    import android.os.Bundle
    import android.view.View
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.lifecycle.Observer
    import androidx.lifecycle.ViewModelProviders
    import com.mindorks.kotlinFlow.R
    import com.mindorks.kotlinFlow.data.api.ApiHelperImpl
    import com.mindorks.kotlinFlow.data.api.RetrofitBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseBuilder
    import com.mindorks.kotlinFlow.data.local.DatabaseHelperImpl
    import com.mindorks.kotlinFlow.utils.Status
    import com.mindorks.kotlinFlow.utils.ViewModelFactory
    import kotlinx.android.synthetic.main.activity_retry.*
    
    class RetryExponentialBackoffActivity : AppCompatActivity() {
    
        private lateinit var viewModel: RetryExponentialBackoffModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_retry)
            setupViewModel()
            setupLongRunningTask()
        }
    
        private fun setupLongRunningTask() {
            viewModel.getStatus().observe(this, Observer {
                when (it.status) {
                    Status.SUCCESS -> {
                        progressBar.visibility = View.GONE
                        textView.text = it.data
                        textView.visibility = View.VISIBLE
                    }
                    Status.LOADING -> {
                        progressBar.visibility = View.VISIBLE
                        textView.visibility = View.GONE
                    }
                    Status.ERROR -> {
                        //Handle Error
                        progressBar.visibility = View.GONE
                        Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            viewModel.startTask()
        }
    
        private fun setupViewModel() {
            viewModel = ViewModelProviders.of(
                this,
                ViewModelFactory(
                    ApiHelperImpl(RetrofitBuilder.apiService),
                    DatabaseHelperImpl(DatabaseBuilder.getInstance(applicationContext))
                )
            ).get(RetryExponentialBackoffModel::class.java)
        }
    }
    
    • 视图模型代码
    package com.mindorks.kotlinFlow.learn.retryexponentialbackoff
    
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import com.mindorks.kotlinFlow.data.api.ApiHelper
    import com.mindorks.kotlinFlow.data.local.DatabaseHelper
    import com.mindorks.kotlinFlow.utils.Resource
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.delay
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    import java.io.IOException
    
    class RetryExponentialBackoffModel(
        val apiHelper: ApiHelper,
        dbHelper: DatabaseHelper
    ) : ViewModel() {
    
        private val status = MutableLiveData<Resource<String>>()
    
        fun startTask() {
            viewModelScope.launch {
                status.postValue(Resource.loading(null))
                // do a long running task
                var currentDelay = 1000L
                val delayFactor = 2
                doLongRunningTask()
                    .flowOn(Dispatchers.Default)
                    .retry(retries = 3) { cause ->
                        if (cause is IOException) {
                            delay(currentDelay)
                            currentDelay = (currentDelay * delayFactor)
                            return@retry true
                        } else {
                            return@retry false
                        }
                    }
                    .catch {
                        status.postValue(Resource.error("Something Went Wrong", null))
                    }
                    .collect {
                        status.postValue(Resource.success("Task Completed"))
                    }
            }
        }
    
        fun getStatus(): LiveData<Resource<String>> {
            return status
        }
    
        private fun doLongRunningTask(): Flow<Int> {
            return flow {
                // your code for doing a long running task
                // Added delay, random number, and exception to simulate
    
                delay(2000)
    
                val randomNumber = (0..2).random()
    
                if (randomNumber == 0) {
                    throw IOException()
                } else if (randomNumber == 1) {
                    throw IndexOutOfBoundsException()
                }
    
                delay(2000)
                emit(0)
            }
        }
    
    }
    

    作者:amitshekhariitbhu
    链接:https://github.com/MindorksOpenSource/Kotlin-Flow-Android-Examples

    相关文章

      网友评论

        本文标题:[Android Kotlin-Flow]Kotlin-Flow

        本文链接:https://www.haomeiwen.com/subject/nhntbrtx.html