Browse Source

拆分函数

drake 4 years ago
parent
commit
18fa074677

+ 2 - 4
net/build.gradle

@@ -57,18 +57,16 @@ dependencies {
     api fileTree(dir: "libs", include: ["*.jar"])
     implementation "androidx.appcompat:appcompat:1.2.0"
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
-    testImplementation "junit:junit:4.13"
+    testImplementation "junit:junit:4.13.1"
     androidTestImplementation "androidx.test:runner:1.3.0"
     androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
 
-    implementation "com.github.liangjingkanji:Tooltip:1.0.4"
+    implementation "com.github.liangjingkanji:Tooltip:1.1.0"
     api project(path: ':kalle')
 
-    api 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0'
     compileOnly "com.github.liangjingkanji:BRV:$brv_version"
     compileOnly "com.github.bumptech.glide:glide:$glide_version"
 
-
     compileOnly "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
     compileOnly "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
 

+ 43 - 0
net/src/main/java/com/drake/net/utils/FlowUtils.kt

@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2018 Drake, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.drake.net.utils
+
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleOwner
+import com.drake.net.scope.AndroidScope
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.collect
+import kotlinx.coroutines.flow.distinctUntilChanged
+
+/**
+ * 收集Flow结果并过滤重复结果
+ */
+fun <T> Flow<List<T>>.listen(
+    lifecycleOwner: LifecycleOwner? = null,
+    lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
+    dispatcher: CoroutineDispatcher = Dispatchers.Main,
+    block: (List<T>) -> Unit
+): AndroidScope {
+    return AndroidScope(lifecycleOwner, lifeEvent, dispatcher).launch {
+        distinctUntilChanged().collect { data ->
+            block(data)
+        }
+    }
+}
+

+ 0 - 92
net/src/main/java/com/drake/net/utils/JetPack.kt

@@ -1,92 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.drake.net.utils
-
-import android.database.Cursor
-import androidx.fragment.app.Fragment
-import androidx.fragment.app.FragmentActivity
-import androidx.lifecycle.*
-import com.drake.net.scope.AndroidScope
-import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.flow.collect
-import kotlinx.coroutines.flow.distinctUntilChanged
-
-/**
- * 方便迭代Cursor, 针对数据库的便携函数
- */
-fun Cursor.foreach(block: Cursor.() -> Unit) {
-    if (count == 0) {
-        close()
-        return
-    }
-    moveToFirst()
-    do {
-        block()
-    } while (moveToNext())
-    close()
-}
-
-/**
- * 快速创建LiveData的观察者
- */
-fun <M> LifecycleOwner.observe(liveData: LiveData<M>?, block: M?.() -> Unit) {
-    liveData?.observe(this, Observer { it.block() })
-}
-
-/**
- * 收集Flow结果并过滤重复结果
- */
-fun <T> Flow<List<T>>.listen(
-    lifecycleOwner: LifecycleOwner? = null,
-    lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
-    dispatcher: CoroutineDispatcher = Dispatchers.Main,
-    block: (List<T>) -> Unit
-): AndroidScope {
-    return AndroidScope(lifecycleOwner, lifeEvent, dispatcher).launch {
-        distinctUntilChanged().collect { data ->
-            block(data)
-        }
-    }
-}
-
-/**
- * 继承这个类可以快速创建具备saveInstance的ViewModel
- */
-open class SavedViewModel(var saved: SavedStateHandle) : ViewModel()
-
-/**
- * 返回当前组件指定的ViewModel
- */
-inline fun <reified M : ViewModel> ViewModelStoreOwner.getViewModel(): M {
-    return ViewModelProvider(this).get(M::class.java)
-}
-
-/**
- * 返回当前组件指定的SavedViewModel
- */
-inline fun <reified M : ViewModel> FragmentActivity.getSavedModel(): M {
-    return ViewModelProvider(this, SavedStateViewModelFactory(application, this)).get(M::class.java)
-}
-
-/**
- * 返回当前组件指定的SavedViewModel
- */
-inline fun <reified M : ViewModel> Fragment.getSavedModel(): M {
-    return ViewModelProvider(this, SavedStateViewModelFactory(activity!!.application, this)).get(M::class.java)
-}