浏览代码

Implement lifecycle so before the owner is destroyed the dialog dismiss itself (#1746)

By @jordyamc
Jordy Alexis Mendoza Caballero 6 年之前
父节点
当前提交
50802bde06

+ 1 - 0
dependencies.gradle

@@ -17,6 +17,7 @@ ext.versions = [
     androidxAnnotation  : '1.0.2',
     androidxRecyclerView: '1.0.0',
     androidxMaterial    : '1.0.0',
+    lifecycle           : '2.0.0',
 
     // Kotlin
     kotlin              : '1.3.21',

+ 1 - 0
lifecycle/.gitignore

@@ -0,0 +1 @@
+/build

+ 35 - 0
lifecycle/build.gradle

@@ -0,0 +1,35 @@
+apply plugin: 'com.android.library'
+apply plugin: 'kotlin-android'
+apply from: '../dependencies.gradle'
+
+ext.shard = 'lifecycle'
+apply from: '../bintrayconfig.gradle'
+
+android {
+    compileSdkVersion versions.compileSdk
+    buildToolsVersion versions.buildTools
+
+    defaultConfig {
+        minSdkVersion versions.minSdk
+        targetSdkVersion versions.compileSdk
+        versionCode versions.publishVersionCode
+        versionName versions.publishVersion
+    }
+
+    sourceSets {
+        main.res.srcDirs = [
+                'src/main/res',
+                'src/main/res-public'
+        ]
+    }
+}
+
+dependencies {
+    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + versions.kotlin
+    implementation project(':core')
+
+    implementation 'androidx.lifecycle:lifecycle-runtime:' + versions.lifecycle
+    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:' + versions.lifecycle
+}
+
+apply from: '../spotless.gradle'

+ 2 - 0
lifecycle/src/main/AndroidManifest.xml

@@ -0,0 +1,2 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.afollestad.materialdialogs.lifecycle"/>

+ 28 - 0
lifecycle/src/main/java/com/afollestad/materialdialogs/lifecycle/LCObserver.kt

@@ -0,0 +1,28 @@
+/**
+ * Designed and developed by Aidan Follestad (@afollestad)
+ *
+ * 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.afollestad.materialdialogs.lifecycle
+
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleObserver
+import androidx.lifecycle.OnLifecycleEvent
+
+class LCObserver(private val callback: () -> Unit) : LifecycleObserver {
+    /** dismiss automatically when lifecycle owner is destroyed. */
+    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
+    fun onDestroy() {
+        callback.invoke()
+    }
+}

+ 36 - 0
lifecycle/src/main/java/com/afollestad/materialdialogs/lifecycle/LifecycleExt.kt

@@ -0,0 +1,36 @@
+/**
+ * Designed and developed by Aidan Follestad (@afollestad)
+ *
+ * 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.afollestad.materialdialogs.lifecycle
+
+import androidx.lifecycle.LifecycleOwner
+import com.afollestad.materialdialogs.MaterialDialog
+
+/**
+ * Implement lifecycle in dialog to avoid leaks when the activity is destroyed
+ * when the dialog is showing.
+ *
+ * @param owner Optional lifecycle owner, if its null use windowContext.
+ */
+fun MaterialDialog.lifecycleOwner(owner: LifecycleOwner? = null): MaterialDialog {
+    val observer = LCObserver {
+        dismiss()
+    }
+    if (owner != null)
+        owner.lifecycle.addObserver(observer)
+    else
+        (windowContext as? LifecycleOwner)?.lifecycle?.addObserver(observer)
+    return this
+}

+ 1 - 1
settings.gradle

@@ -1 +1 @@
-include ':core', ':sample', ':files', ':color', ':input'
+include ':core', ':sample', ':files', ':color', ':input', ':lifecycle'