├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── layout │ │ │ └── activity_main.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── hadilq │ │ └── liveevent │ │ └── sample │ │ ├── LiveEventViewModel.kt │ │ └── MainActivity.kt └── build.gradle.kts ├── live-event ├── .gitignore ├── src │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── hadilq │ │ │ └── liveevent │ │ │ ├── LiveEventConfig.kt │ │ │ └── LiveEvent.kt │ └── test │ │ └── java │ │ └── com │ │ └── hadilq │ │ └── liveevent │ │ ├── TestLifecycleOwner.kt │ │ └── LiveEventTest.kt └── build.gradle.kts ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml └── .gitignore ├── .github └── workflows │ ├── pr-check.yaml │ └── main-check.yaml ├── CHANGELOG.md ├── settings.gradle.kts ├── gradle.properties ├── README.md ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /live-event/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LiveEvent 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | build-system/keystore.gradle 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hadilq/LiveEvent/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /live-event/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | compiler.xml 2 | caches 3 | libraries 4 | modules.xml 5 | workspace.xml 6 | navEditor.xml 7 | assetWizardSettings.xml 8 | gradle.xml 9 | jarRepositories.xml 10 | misc.xml 11 | runConfigurations.xml 12 | vcs.xml 13 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.github/workflows/pr-check.yaml: -------------------------------------------------------------------------------- 1 | name: PR Health Check 2 | on: 3 | push: 4 | branches-ignore: 5 | - main 6 | 7 | workflow_dispatch: 8 | inputs: 9 | logLevel: 10 | description: 'Log level' 11 | required: true 12 | default: 'warning' 13 | tags: 14 | description: 'Test scenario tags' 15 | jobs: 16 | check: 17 | name: Run tests and checks 18 | runs-on: ubuntu-latest 19 | steps: 20 | - uses: actions/checkout@v2 21 | - uses: actions/setup-java@v1 22 | with: 23 | java-version: '8' 24 | - name: Run all checks 25 | run: ./gradlew check 26 | -------------------------------------------------------------------------------- /live-event/src/main/java/com/hadilq/liveevent/LiveEventConfig.kt: -------------------------------------------------------------------------------- 1 | package com.hadilq.liveevent 2 | 3 | enum class LiveEventConfig { 4 | /** 5 | * Supports multi-observers on all cases the same. 6 | */ 7 | Normal, 8 | 9 | /** 10 | * Prefer the first observer when user emit the event, register observer, then the `onStart` 11 | * get called. In this case the _first observer_ will receive the _last event_. 12 | * 13 | * This scenario is specially useful when you want to emit the event in the `init` of 14 | * `ViewModel`, and expect the first observer receive it after `onStart`. 15 | */ 16 | PreferFirstObserver 17 | } 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 1.3.0 5 | ---- 6 | 7 | _2021-06-26_ 8 | 9 | - Create `LiveEventConfig` to handle `PreferFirstObserver` behavior. 10 | 11 | 1.2.3 12 | ---- 13 | 14 | _2021-04-15_ 15 | 16 | - Avoid adding existing observer. 17 | 18 | 1.2.2 19 | ---- 20 | 21 | _2021-04-14_ 22 | 23 | - Make it work with Android! 24 | 25 | 1.2.1 26 | ---- 27 | 28 | _2021-04-13_ 29 | 30 | - Change the group id from `com.github.hadilq.liveevent` to `com.github.hadilq` 31 | - Change the module name from `liveevent` to `live-event` 32 | - Update Kotlin 33 | - Update gradle and gradle plugin 34 | - Fix a reported lint error 35 | 36 | 37 | 1.2.0 38 | ---- 39 | 40 | Have no record 41 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2019 Hadi Lashkari Ghouchani 3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | include(":app", ":live-event") 17 | -------------------------------------------------------------------------------- /.github/workflows/main-check.yaml: -------------------------------------------------------------------------------- 1 | name: Health Check 2 | on: 3 | push: 4 | branches: 5 | - main 6 | 7 | workflow_dispatch: 8 | inputs: 9 | logLevel: 10 | description: 'Log level' 11 | required: true 12 | default: 'warning' 13 | tags: 14 | description: 'Test scenario tags' 15 | jobs: 16 | check: 17 | name: Run tests and checks 18 | runs-on: ubuntu-latest 19 | steps: 20 | - uses: actions/checkout@v2 21 | - uses: actions/setup-java@v1 22 | with: 23 | java-version: '8' 24 | - name: Run all checks 25 | run: ./gradlew check 26 | - name: Publish SNAPSHOT 27 | env: 28 | OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} 29 | OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} 30 | run: ./gradlew :live-event:publish 31 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # Kotlin code style for this project: "official" or "obsolete": 15 | kotlin.code.style=official 16 | android.useAndroidX=true -------------------------------------------------------------------------------- /app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.android.application") 3 | kotlin("android") 4 | kotlin("android.extensions") 5 | kotlin("kapt") 6 | } 7 | 8 | android { 9 | compileSdkVersion(Versions.compileSdkVersion) 10 | defaultConfig { 11 | applicationId = "com.hadilq.liveevent.sample" 12 | minSdkVersion(Versions.minSdkVersion) 13 | targetSdkVersion(Versions.targetSdkVersion) 14 | versionCode = 1 15 | versionName = "1.0" 16 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | } 19 | 20 | dependencies { 21 | kapt(Depends.archComponentsCompiler) 22 | 23 | // implementation(project(":live-event")) 24 | implementation("${Versions.groupId}:${Versions.artifactId}:${Versions.libVersion}") 25 | 26 | implementation(Depends.kotlin) 27 | implementation(Depends.supportCompat) 28 | implementation(Depends.constraintLayout) 29 | implementation(Depends.archComponents) 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadilq/liveevent/sample/LiveEventViewModel.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2019 Hadi Lashkari Ghouchani 3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hadilq.liveevent.sample 17 | 18 | import androidx.lifecycle.LiveData 19 | import androidx.lifecycle.ViewModel 20 | import com.hadilq.liveevent.LiveEvent 21 | 22 | class LiveEventViewModel : ViewModel() { 23 | private val clickedState = LiveEvent() 24 | val state: LiveData = clickedState 25 | 26 | fun clicked() { 27 | clickedState.value = "${if (clickedState.value == null) "" else clickedState.value} clicked!" 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 21 | 22 |