Sfoglia il codice sorgente

Cleanup Lifecycle module a bit, add Lifecycle module info to the README

Aidan Follestad 6 anni fa
parent
commit
18997f4256

+ 36 - 0
README.md

@@ -8,6 +8,7 @@
 [ ![Input](https://api.bintray.com/packages/drummer-aidan/maven/material-dialogs%3Ainput/images/download.svg) ](https://bintray.com/drummer-aidan/maven/material-dialogs%3Ainput/_latestVersion)
 [ ![Files](https://api.bintray.com/packages/drummer-aidan/maven/material-dialogs%3Afiles/images/download.svg) ](https://bintray.com/drummer-aidan/maven/material-dialogs%3Afiles/_latestVersion)
 [ ![Color](https://api.bintray.com/packages/drummer-aidan/maven/material-dialogs%3Acolor/images/download.svg) ](https://bintray.com/drummer-aidan/maven/material-dialogs%3Acolor/_latestVersion)
+[ ![Color](https://api.bintray.com/packages/drummer-aidan/maven/material-dialogs%3Alifecycle/images/download.svg) ](https://bintray.com/drummer-aidan/maven/material-dialogs%3Alifecycle/_latestVersion)
 
 #### [View Releases and Changelogs](https://github.com/afollestad/material-dialogs/releases)
 
@@ -69,6 +70,11 @@
     1. [Basics](#basics-4)
     2. [Sub Colors](#sub-colors) 
 
+# Table of Contents - Lifecycle
+
+1. [Gradle Dependency](#gradle-dependency-4)
+2. [Usage](#usage)
+
 ---
 
 # Core
@@ -1081,3 +1087,33 @@ MaterialDialog(this).show {
 ```
 
 Omitting `showAlphaSelector` will hide the alpha (transparency) selector.
+
+---
+
+# Lifecycle
+
+## Gradle Dependency
+
+[ ![Lifecycle](https://api.bintray.com/packages/drummer-aidan/maven/material-dialogs%3Alifecycle/images/download.svg) ](https://bintray.com/drummer-aidan/maven/material-dialogs%3Alifecycle/_latestVersion)
+
+The `lifecycle` module contains extensions to make dialogs work with AndroidX lifecycles.
+
+```gradle
+dependencies {
+  ...
+  implementation 'com.afollestad.material-dialogs:lifecycle:2.0.3'
+}
+```
+
+## Usage
+
+```kotlin
+MaterialDialog(this).show {
+  ...
+  lifecycleOwner(owner)
+}
+```
+
+When the given lifecycle owner is destroyed, the dialog is automatically dismissed. Lifecycle 
+owners include Activities and Fragments from AndroidX, along with any class that implements the
+`LifecycleOwner` interface.

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

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

+ 6 - 6
lifecycle/src/main/java/com/afollestad/materialdialogs/lifecycle/LCObserver.kt → lifecycle/src/main/java/com/afollestad/materialdialogs/lifecycle/DialogLifecycleObserver.kt

@@ -13,16 +13,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+@file:Suppress("unused")
+
 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()
-    }
+/** @author @jordyamc */
+internal class DialogLifecycleObserver(private val dismiss: () -> Unit) : LifecycleObserver {
+  @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
+  fun onDestroy() = dismiss()
 }

+ 12 - 10
lifecycle/src/main/java/com/afollestad/materialdialogs/lifecycle/LifecycleExt.kt

@@ -13,24 +13,26 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+@file:Suppress("unused")
+
 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.
+ * Attach the dialog to a lifecycle and dismiss it when the lifecycle is destroyed.
+ * Uses the given [owner] lifecycle if provided, else falls back to the Context of the dialog
+ * window if it can.
  *
  * @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
+  val observer = DialogLifecycleObserver(::dismiss)
+  val lifecycleOwner = owner ?: (windowContext as? LifecycleOwner
+      ?: throw IllegalStateException(
+          "$windowContext is not a LifecycleOwner."
+      ))
+  lifecycleOwner.lifecycle.addObserver(observer)
+  return this
 }