coolweathercoroutines

Introduction: 酷欧天气的协程版本实现,采用了 MVVM 架构。
More: Author   ReportBugs   
Tags:

一句话总结

协程是什么

  1. 协程是一种编程思想,不局限于语言。

Kotlin 协程在 Android 中有什么用?

  1. 用同步的方式写异步代码。使得代码更优雅。

延伸阅读, 学习协程

https://mp.weixin.qq.com/s/Tv-jEjJAn_gZ_M3qBG4Azw

欧酷天气-协程 MVVM 版本

简介

学习一个新东西,最好的就是找一个项目练练手了。早一阵子学习了郭神 coolweatherjetpack 这个项目,发现郭神的这个项目显得不是那么的 MVVM,在 View 层做了很多逻辑操作。这样还是会显得 View 层的比较臃肿。于是站在巨人的肩膀上,酷欧天气,协程 MVVM 版本就这样诞生了,整体思想基于郭神的 coolweatherjetpack 的版本,甚至很多代码比如页面布局我都是直接 Copy 郭神的。再次感谢郭神。

项目运行截图

赶快 clone 下来看看吧,如果这个项目帮到了你,希望能给我一个 star

代码粗略讲解,以 Weather 模块为例

viewModel

val weather = MutableLiveData<Weather>()
fun fetchWeather(weatherId: String) {
    // launch 表示开启一个协程环境,类似于 JS 中的 Async
    launch{
        val fetchWeather = repository.fetchWeather(weatherId, MainActivity.KEY) // 这里是运行在 IO 线程中,进行网络请求
        weather.value = fetchWeather.weather!![0] // 这里是主线程了,用于视图更新
    }
}

WeatherRepository

  1. viewModel 中调用了一个用 suspend 标记的方法,其中方法体中执行了 request 方法。下面让我们看下 request 方法。
    suspend fun fetchWeather(weatherId: String, key: String) = request{ service.getWeather(weatherId, key) }
    
  2. BaseRepository 是所有 Repository 的基类,里面有用 suspend 关键字标记的 request 方法,并且用 WithContext 指定此方法运行在 IO 环境中。
    open class BaseRepository {
     suspend fun <T> request(block: suspend () -> T): T {
         return withContext(Dispatchers.IO) {
             block()
         }
     }
    }
    
  3. 连起来看就是表示,在 IO 线程中,通过网络请求到数据,并且返回。

WeatherActivity

1.在 Activity 中订阅了 ViewModel 中的 liveData,用于视图更新。

weather.observe(this@WeatherActivity, Observer{
    swipeRefresh.isRefreshing = false
    showWeatherInfo(it)
})

大感谢

求 star

https://github.com/LoverJoker/coolweathercoroutines

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools