{"id":21536184,"url":"https://github.com/sczerwinski/android-lifecycle","last_synced_at":"2025-04-10T01:51:19.485Z","repository":{"id":38206187,"uuid":"320645761","full_name":"sczerwinski/android-lifecycle","owner":"sczerwinski","description":"Extensions for Jetpack Lifecycle components","archived":false,"fork":false,"pushed_at":"2023-07-24T07:51:26.000Z","size":336,"stargazers_count":4,"open_issues_count":6,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T03:34:35.867Z","etag":null,"topics":["android","jetpack","jetpack-android","jetpack-lifecycle-components","lifecycle"],"latest_commit_sha":null,"homepage":"https://czerwinski.it/projects/android-lifecycle/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sczerwinski.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-12-11T17:54:33.000Z","updated_at":"2022-08-04T06:17:37.000Z","dependencies_parsed_at":"2023-02-18T04:16:14.273Z","dependency_job_id":null,"html_url":"https://github.com/sczerwinski/android-lifecycle","commit_stats":{"total_commits":132,"total_committers":3,"mean_commits":44.0,"dds":"0.21969696969696972","last_synced_commit":"98568465312d73778df1f38e0a44252aa40876e4"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sczerwinski%2Fandroid-lifecycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sczerwinski%2Fandroid-lifecycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sczerwinski%2Fandroid-lifecycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sczerwinski%2Fandroid-lifecycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sczerwinski","download_url":"https://codeload.github.com/sczerwinski/android-lifecycle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248142894,"owners_count":21054668,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","jetpack","jetpack-android","jetpack-lifecycle-components","lifecycle"],"created_at":"2024-11-24T03:18:24.356Z","updated_at":"2025-04-10T01:51:19.455Z","avatar_url":"https://github.com/sczerwinski.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build](https://github.com/sczerwinski/android-lifecycle/workflows/Build/badge.svg)][ci-build]\n\n# Extensions for Jetpack Lifecycle\n\n## LiveData Extensions\n\n[![Maven Central](https://img.shields.io/maven-central/v/it.czerwinski.android.lifecycle/lifecycle-livedata)][lifecycle-livedata-release]\n[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/it.czerwinski.android.lifecycle/lifecycle-livedata?server=https%3A%2F%2Foss.sonatype.org)][lifecycle-livedata-snapshot]\n\n\u003cdetails\u003e\n  \u003csummary\u003eKotlin\u003c/summary\u003e\n\n  ```kotlin\n  dependencies {\n      implementation(\"it.czerwinski.android.lifecycle:lifecycle-livedata:[VERSION]\")\n  }\n  ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eGroovy\u003c/summary\u003e\n\n  ```groovy\n  dependencies {\n      implementation 'it.czerwinski.android.lifecycle:lifecycle-livedata:[VERSION]'\n  }\n  ```\n\u003c/details\u003e\n\n### Types\n\n#### `ConstantLiveData`\n\n[LiveData] that always emits a single constant value.\n\n#### `GroupedLiveData`\n\n[MediatorLiveData] subclass which provides a separate [LiveData] per each result returned by `keySelector` function\nexecuted on subsequent values emitted by the source LiveData.\n\n### LiveData Factory Methods\n\n#### `intervalLiveData`\n\nReturns a [LiveData] emitting a sequence of integer values, spaced by a given `timeInMillis`.\n\n```kotlin\nval fixedIntervalLiveData: LiveData\u003cInt\u003e = intervalLiveData(timeInMillis = 1000L)\nval varyingIntervalLiveData: LiveData\u003cInt\u003e = intervalLiveData { index -\u003e (index + 1) * 1000L }\n```\n\n\n### LiveData Transformations\n\n#### `mapNotNull`\n\nReturns a [LiveData] emitting only the non-null results of applying the given `transform` function to each value\nemitted by this LiveData.\n\n```kotlin\nval userOptionLiveData: LiveData\u003cOption\u003cUser\u003e\u003e = // ...\nval userLiveData: LiveData\u003cUser\u003e = userOptionLiveData.mapNotNull { user -\u003e user.getOrNull() }\n```\n\n#### `filter`\n\nReturns a [LiveData] emitting only values from this LiveData matching the given `predicate`.\n\n```kotlin\nval resultLiveData: LiveData\u003cTry\u003cUser\u003e\u003e = // ...\nval successLiveData: LiveData\u003cTry\u003cUser\u003e\u003e = resultLiveData.filter { it.isSuccess }\n```\n\n#### `filterNotNull`\n\nReturns a [LiveData] emitting only non-null values from this LiveData.\n\n```kotlin\nval userLiveData: LiveData\u003cUser?\u003e = // ...\nval nonNullUserLiveData: LiveData\u003cUser\u003e = userLiveData.filterNotNull()\n```\n\n#### `filterIsInstance`\n\nReturns a [LiveData] emitting only values of the given type from this LiveData.\n\n```kotlin\nval resultLiveData: LiveData\u003cTry\u003cUser\u003e\u003e = // ...\nval failureLiveData: LiveData\u003cFailure\u003e = resultLiveData.filterIsInstance\u003cFailure\u003e()\n```\n\n#### `reduce`\n\nReturns a [LiveData] emitting accumulated value starting with the first value emitted by this LiveData and applying\n`operation` from left to right to current accumulator value and each value emitted by this.\n\n```kotlin\nval newOperationsCountLiveData: LiveData\u003cInt?\u003e = // ...\nval operationsCountLiveData: LiveData\u003cInt?\u003e =\n    newOperationsCountLiveData.reduce { acc, next -\u003e if (next == null) null else acc + next }\n```\n\n#### `reduceNotNull`\n\nReturns a [LiveData] emitting non-null accumulated value starting with the first non-null value emitted by this\nLiveData and applying `operation` from left to right to current accumulator value and each subsequent non-null value\nemitted by this LiveData.\n\n```kotlin\nval newOperationsCountLiveData: LiveData\u003cInt\u003e = // ...\nval operationsCountLiveData: LiveData\u003cInt\u003e =\n    newOperationsCountLiveData.reduceNotNull { acc, next -\u003e acc + next }\n```\n\n#### `throttleWithTimeout`\n\nReturns a [LiveData] emitting values from this LiveData, after dropping values followed by newer values before\n`timeInMillis` expires.\n\n```kotlin\nval isLoadingLiveData: LiveData\u003cBoolean\u003e = // ...\nval isLoadingThrottledLiveData: LiveData\u003cBoolean\u003e = isLoadingLiveData.throttleWithTimeout(\n    timeInMillis = 1000L,\n    context = viewModelScope.coroutineContext\n)\n```\n\n#### `delayStart`\n\nReturns a [LiveData] emitting values from this LiveData, after dropping values followed by newer values before\n`timeInMillis` expires since the result LiveData has been created.\n\n```kotlin\nval resultLiveData: LiveData\u003cResultData\u003e = // ...\nval delayedResultLiveData: LiveData\u003cResultData\u003e = resultLiveData.delayStart(\n    timeInMillis = 1000L,\n    context = viewModelScope.coroutineContext\n)\n```\n\n#### `merge`\n\nReturns a [LiveData] emitting each value emitted by any of the given LiveData.\n\n```kotlin\nval serverError: LiveData\u003cString\u003e = // ...\nval databaseError: LiveData\u003cString\u003e = // ...\nval error: LiveData\u003cString\u003e = serverError merge databaseError\n```\n\n```kotlin\nval serverError: LiveData\u003cString\u003e = // ...\nval databaseError: LiveData\u003cString\u003e = // ...\nval fileError: LiveData\u003cString\u003e = // ...\nval error: LiveData\u003cString\u003e = merge(serverError, databaseError, fileError)\n```\n\n#### `combineLatest`\n\nReturns a [LiveData] emitting pairs, triples or lists of latest values emitted by the given LiveData.\n\n```kotlin\nval userLiveData: LiveData\u003cUser\u003e = // ...\nval avatarUrlLiveData: LiveData\u003cString\u003e = // ...\nval userWithAvatar: LiveData\u003cPair\u003cUser?, String?\u003e\u003e = combineLatest(userLiveData, avatarUrlLiveData)\n```\n\n```kotlin\nval userLiveData: LiveData\u003cUser\u003e = // ...\nval avatarUrlLiveData: LiveData\u003cString\u003e = // ...\nval userWithAvatar: LiveData\u003cUserWithAvatar\u003e =\n    combineLatest(userLiveData, avatarUrlLiveData) { user, avatarUrl -\u003e\n        UserWithAvatar(user, avatarUrl)\n    }\n```\n\n#### `switch`\n\nConverts [LiveData] that emits other LiveData into a single LiveData that emits the items emitted by the most\nrecently emitted LiveData.\n\n```kotlin\nval sourcesLiveData: LiveData\u003cLiveData\u003cString\u003e\u003e = // ...\nval resultLiveData: LiveData\u003cString?\u003e = sourcesLiveData.switch()\n```\n\n#### `groupBy`\n\nReturns a `GroupedLiveData` providing a set of [LiveData], each emitting a different subset of values from this\nLiveData, based on the result of the given `keySelector` function.\n\n```kotlin\nval userLiveData: LiveData\u003cUser\u003e = // ...\nval userByStatusLiveData: GroupedLiveData\u003cUserStatus, User\u003e = errorLiveData.groupBy { user -\u003e user.status }\nval activeUserLiveData: LiveData\u003cUser\u003e = userByStatusLiveData[UserStatus.ACTIVE]\n```\n\n#### `defaultIfEmpty`\n\nReturns a [LiveData] that emits the values emitted by this LiveData or a specified default value if this LiveData has\nnot yet emitted any values at the time of observing.\n\n```kotlin\nval errorLiveData: LiveData\u003cString\u003e = // ...\nval statusLiveData: LiveData\u003cString?\u003e = errorLiveData.defaultIfEmpty(\"No errors\")\n```\n\n### MediatorLiveData Extensions\n\n#### `addDirectSource`\n\nStarts to listen the given source LiveData.\nWhenever source value is changed, it is set as a new value of this [MediatorLiveData].\n\n```kotlin\nmediatorLiveData.addDirectSource(liveData)\n```\nis equivalent to:\n```kotlin\nmediatorLiveData.addSource(liveData) { x -\u003e mediatorLiveData.value = x }\n```\n\n## Common LivaData Testing Utilities\n\n[![Maven Central](https://img.shields.io/maven-central/v/it.czerwinski.android.lifecycle/lifecycle-livedata-test-common)][lifecycle-livedata-test-common-release]\n[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/it.czerwinski.android.lifecycle/lifecycle-livedata-test-common?server=https%3A%2F%2Foss.sonatype.org)][lifecycle-livedata-test-common-snapshot]\n\nThis package is included in both `lifecycle-livedata-test-junit4` and `lifecycle-livedata-test-junit5`.\n\n\u003cdetails\u003e\n  \u003csummary\u003eKotlin\u003c/summary\u003e\n\n  ```kotlin\n  dependencies {\n      testImplementation(\"junit:junit:4.13.1\")\n      testImplementation(\"it.czerwinski.android.lifecycle:lifecycle-livedata-test-common:[VERSION]\")\n  }\n  ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eGroovy\u003c/summary\u003e\n\n  ```groovy\n  dependencies {\n      testImplementation 'junit:junit:4.13.1'\n      testImplementation 'it.czerwinski.android.lifecycle:lifecycle-livedata-test-common:[VERSION]'\n  }\n  ```\n\u003c/details\u003e\n\n### Testing Observed Values\n\n#### `TestObserver`\n\nA callback testing values emitted by [LiveData].\n\n```kotlin\nclass MyTestClass {\n\n    @Test\n    fun testMethod1() {\n        val liveData = MutableLiveData\u003cInt\u003e()\n\n        val observer = liveData.test()\n\n        observer.assertNoValues()\n    }\n\n    @Test\n    fun testMethod2() {\n        val liveData = MutableLiveData\u003cInt\u003e()\n\n        val observer = liveData.test()\n\n        liveData.postValue(1)\n        liveData.postValue(2)\n        liveData.postValue(3)\n\n        observer.assertValues(1, 2, 3)\n    }\n}\n```\n\n## LivaData Testing Utilities For JUnit4\n\n[![Maven Central](https://img.shields.io/maven-central/v/it.czerwinski.android.lifecycle/lifecycle-livedata-test-junit4)][lifecycle-livedata-test-junit4-release]\n[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/it.czerwinski.android.lifecycle/lifecycle-livedata-test-junit4?server=https%3A%2F%2Foss.sonatype.org)][lifecycle-livedata-test-junit4-snapshot]\n\n\u003cdetails\u003e\n  \u003csummary\u003eKotlin\u003c/summary\u003e\n\n  ```kotlin\n  dependencies {\n      testImplementation(\"junit:junit:4.13.1\")\n      testImplementation(\"it.czerwinski.android.lifecycle:lifecycle-livedata-test-junit4:[VERSION]\")\n  }\n  ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eGroovy\u003c/summary\u003e\n\n  ```groovy\n  dependencies {\n      testImplementation 'junit:junit:4.13.1'\n      testImplementation 'it.czerwinski.android.lifecycle:lifecycle-livedata-test-junit4:[VERSION]'\n  }\n  ```\n\u003c/details\u003e\n\n### JUnit4 Rules\n\n#### `TestCoroutineDispatcherRule`\n\nJUnit4 test rule that swaps main coroutine dispatcher with [UnconfinedTestDispatcher].\n\n```kotlin\nclass MyTestClass {\n\n    @Rule\n    @JvmField\n    val testCoroutineDispatcherRule = TestCoroutineDispatcherRule()\n\n    val testCoroutineScheduler get() = testCoroutineDispatcherRule.scheduler\n\n    // ...\n}\n```\n\n## LivaData Testing Utilities For JUnit5\n\n[![Maven Central](https://img.shields.io/maven-central/v/it.czerwinski.android.lifecycle/lifecycle-livedata-test-junit5)][lifecycle-livedata-test-junit5-release]\n[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/it.czerwinski.android.lifecycle/lifecycle-livedata-test-junit5?server=https%3A%2F%2Foss.sonatype.org)][lifecycle-livedata-test-junit5-snapshot]\n\n\u003cdetails\u003e\n  \u003csummary\u003eKotlin\u003c/summary\u003e\n\n  ```kotlin\n  dependencies {\n      testImplementation(\"org.junit.jupiter:junit-jupiter-api:5.7.0\")\n      testRuntimeOnly(\"org.junit.jupiter:junit-jupiter-engine:5.7.0\")\n      testImplementation(\"it.czerwinski.android.lifecycle:lifecycle-livedata-test-junit5:[VERSION]\")\n  }\n  ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eGroovy\u003c/summary\u003e\n\n  ```groovy\n  dependencies {\n      testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'\n      testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'\n      testImplementation 'it.czerwinski.android.lifecycle:lifecycle-livedata-test-junit5:[VERSION]'\n  }\n  ```\n\u003c/details\u003e\n\n### JUnit5 Extensions\n\n#### `InstantTaskExecutorExtension`\n\nJUnit5 extension that swaps the background executor used by the Architecture Components with a different one which\nexecutes each task synchronously.\n\nThis extension is analogous to [InstantTaskExecutorRule] for JUnit4.\n\n```kotlin\n@ExtendWith(InstantTaskExecutorExtension::class)\nclass MyTestClass {\n    // ...\n}\n```\n\n#### `TestCoroutineDispatcherExtension`\n\nJUnit5 extension that swaps main coroutine dispatcher with [UnconfinedTestDispatcher].\n\nAny test method parameter of type [TestCoroutineScheduler] will be resolved as the scheduler of the [TestCoroutineDispatcher]:\n\n```kotlin\n@ExtendWith(TestCoroutineDispatcherExtension::class)\nclass MyTestClass {\n\n    @Test\n    fun someTest(scheduler: TestCoroutineScheduler) {\n        // ...\n        scheduler.advanceTimeBy(delayTimeMillis = 1000L)\n        scheduler.runCurrent()\n        // ...\n    }\n}\n```\n\nIn case of parameterized tests, the scheduler can be passed as a parameter of a before method:\n\n```kotlin\n@ExtendWith(TestCoroutineDispatcherExtension::class)\nclass MyTestClass {\n\n    private lateinit var testCoroutineScheduler: TestCoroutineScheduler\n\n    @BeforeEach\n    fun setScheduler(scheduler: TestCoroutineScheduler) {\n        testCoroutineScheduler = scheduler\n    }\n\n    @ParameterizedTest\n    @MethodSource(\"testData\")\n    fun someParameterizedTest(input: Int) {\n        // ...\n    }\n}\n```\n\n\n[ci-build]: https://github.com/sczerwinski/android-lifecycle/actions?query=workflow%3ABuild\n[lifecycle-livedata-release]: https://repo1.maven.org/maven2/it/czerwinski/android/lifecycle/lifecycle-livedata/\n[lifecycle-livedata-test-common-release]: https://repo1.maven.org/maven2/it/czerwinski/android/lifecycle/lifecycle-livedata-test-common/\n[lifecycle-livedata-test-junit4-release]: https://repo1.maven.org/maven2/it/czerwinski/android/lifecycle/lifecycle-livedata-test-junit4/\n[lifecycle-livedata-test-junit5-release]: https://repo1.maven.org/maven2/it/czerwinski/android/lifecycle/lifecycle-livedata-test-junit5/\n[lifecycle-livedata-snapshot]: https://oss.sonatype.org/content/repositories/snapshots/it/czerwinski/android/lifecycle/lifecycle-livedata/\n[lifecycle-livedata-test-common-snapshot]: https://oss.sonatype.org/content/repositories/snapshots/it/czerwinski/android/lifecycle/lifecycle-livedata-test-common/\n[lifecycle-livedata-test-junit4-snapshot]: https://oss.sonatype.org/content/repositories/snapshots/it/czerwinski/android/lifecycle/lifecycle-livedata-test-junit4/\n[lifecycle-livedata-test-junit5-snapshot]: https://oss.sonatype.org/content/repositories/snapshots/it/czerwinski/android/lifecycle/lifecycle-livedata-test-junit5/\n\n[LiveData]: https://developer.android.com/reference/androidx/lifecycle/LiveData\n[MediatorLiveData]: https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData\n[InstantTaskExecutorRule]: https://developer.android.com/reference/androidx/arch/core/executor/testing/InstantTaskExecutorRule\n[TestCoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-dispatcher/\n[UnconfinedTestDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-unconfined-test-dispatcher.html\n[TestCoroutineScheduler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsczerwinski%2Fandroid-lifecycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsczerwinski%2Fandroid-lifecycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsczerwinski%2Fandroid-lifecycle/lists"}