Skip to content

Commit

Permalink
feat: 增加 Activity 的利用 ViewModel 初始化一次的扩展功能
Browse files Browse the repository at this point in the history
  • Loading branch information
keepactive committed Oct 3, 2021
1 parent 83d9c8c commit 5045450
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 2 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ ext.versions = [
ext.libs = [
kotlin_stdlib : "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version",
constraintlayout : 'androidx.constraintlayout:constraintlayout:2.0.4',
activity : 'androidx.activity:activity:1.2.3',
activity : 'androidx.activity:activity:1.3.1',
appcompat : 'androidx.appcompat:appcompat:1.3.0',
android_core_ktx : "androidx.core:core-ktx:1.6.0",
acrivity_ktx : "androidx.activity:activity-ktx:1.3.1",
fragment_ktx : "androidx.fragment:fragment-ktx:1.3.5",
recyclerview : "androidx.recyclerview:recyclerview:1.2.0",
rxandroid : "io.reactivex.rxjava2:rxandroid:2.1.1",
Expand Down
1 change: 1 addition & 0 deletions lib-ktx/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
api libs.rxjava
api libs.rxandroid
api libs.recyclerview
api libs.acrivity_ktx
api libs.lifecycle_viewmodel_ktx
api libs.lifecycle_runtime_ktx

Expand Down
17 changes: 17 additions & 0 deletions lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Activities.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xiaojinzi.support.ktx

import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel

class InitOnceViewModel: ViewModel() {
var isInit: Boolean = false
}

fun FragmentActivity.initOnceUseViewModel(action: () -> Unit) {
val initViewModel: InitOnceViewModel by this.viewModels()
if (!initViewModel.isInit) {
initViewModel.isInit = true
action.invoke()
}
}

0 comments on commit 5045450

Please sign in to comment.