Jelajahi Sumber

引入协程

詹子聪 5 tahun lalu
induk
melakukan
856a40413e

+ 6 - 0
app/build.gradle

@@ -1,4 +1,5 @@
 apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
 //apply plugin: 'com.hujiang.android-aspectjx'
 
 
@@ -61,11 +62,16 @@ dependencies {
 
     implementation project(path: ':common')
     implementation project(path: ':mvp')
+    implementation "androidx.core:core-ktx:+"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
 
 //    compile 'org.aspectj:aspectjrt:1.8.+'
     // 动态权限申请firefly1126/android_permission_aspectjx
 //    compile 'com.firefly1126.permissionaspect:permissionaspect:1.0.1'
 }
+repositories {
+    mavenCentral()
+}
 
 //aspectjx {
 //    exclude  "android.support",'androidx','com.google','com.squareup.leakcanary','com.squareup.leakcanary.core','com.alipay','org.apache','com.tencent'

+ 13 - 0
app/src/main/java/com/miekir/newmvp/ui/home/article/presenter/AutoPresenter.kt

@@ -0,0 +1,13 @@
+package com.miekir.newmvp.ui.home.article.presenter
+
+import com.miekir.mvp.presenter.BasePresenterKt
+
+class AutoPresenter : BasePresenterKt() {
+    fun doWork() {
+        launchTask({
+            //协程体
+        }, {
+            //异常回调
+        })
+    }
+}

+ 2 - 0
build.gradle

@@ -1,6 +1,7 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
+    ext.kotlin_version = '1.4.21'
     repositories {
         google()
         jcenter()
@@ -8,6 +9,7 @@ buildscript {
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.1'
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 //        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
     }
 }

+ 5 - 4
mvp/src/main/java/com/miekir/mvp/presenter/BasePresenterKt.kt

@@ -1,19 +1,20 @@
 package com.miekir.mvp.presenter
 
-import androidx.lifecycle.AndroidViewModel
+import androidx.lifecycle.ViewModel
 import androidx.lifecycle.rxLifeScope
-import com.blankj.utilcode.util.Utils
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Job
 
-open class BasePresenterKt : AndroidViewModel(Utils.getApp()) {
+//open class BasePresenterKt : AndroidViewModel(Utils.getApp()) {
+open class BasePresenterKt : ViewModel() {
     /**
      * 所有网络请求都在 rxLifeScope 域中启动,当页面销毁时会自动
      * 取消所有协程
      * @param block 网络请求
      * @param onError 失败回调
+     *
      */
-    fun launchUI(block: suspend CoroutineScope.() -> Unit, onError: ((Throwable) -> Unit)? = null): Job {
+    fun launchTask(block: suspend CoroutineScope.() -> Unit, onError: ((Throwable) -> Unit)? = null): Job {
         return rxLifeScope.launch(block, onError)
     }
 }