|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 | + |
| 17 | +package com.facebook.litho |
| 18 | + |
| 19 | +import androidx.lifecycle.Lifecycle |
| 20 | +import androidx.lifecycle.Lifecycle.Event.ON_DESTROY |
| 21 | +import androidx.lifecycle.Lifecycle.Event.ON_PAUSE |
| 22 | +import androidx.lifecycle.Lifecycle.Event.ON_RESUME |
| 23 | +import androidx.lifecycle.LifecycleObserver |
| 24 | +import androidx.lifecycle.LifecycleOwner |
| 25 | +import androidx.lifecycle.OnLifecycleEvent |
| 26 | +import com.facebook.litho.LithoLifecycleProvider.LithoLifecycle |
| 27 | + |
| 28 | +/** |
| 29 | + * This LithoLifecycleProvider implementation dispatches to the registered observers the lifecycle |
| 30 | + * state changes triggered by the provided LifecycleOwner. For example, if a Fragment is passed as |
| 31 | + * param, the observers will be registered to listen to all of the fragment's lifecycle state |
| 32 | + * changes. |
| 33 | + */ |
| 34 | +open class AOSPLithoLifecycleProvider(lifecycleOwner: LifecycleOwner) : |
| 35 | + LithoLifecycleProvider, LifecycleObserver, AOSPLifecycleOwnerProvider { |
| 36 | + |
| 37 | + init { |
| 38 | + lifecycleOwner.lifecycle.addObserver(this) |
| 39 | + } |
| 40 | + |
| 41 | + private val lithoLifecycleProviderDelegate: LithoLifecycleProviderDelegate = |
| 42 | + LithoLifecycleProviderDelegate() |
| 43 | + |
| 44 | + private val _lifecycleOwner: LifecycleOwner = lifecycleOwner |
| 45 | + |
| 46 | + override val lifecycleStatus: LithoLifecycle |
| 47 | + get() = lithoLifecycleProviderDelegate.lifecycleStatus |
| 48 | + |
| 49 | + override fun moveToLifecycle(lithoLifecycle: LithoLifecycle) { |
| 50 | + lithoLifecycleProviderDelegate.moveToLifecycle(lithoLifecycle) |
| 51 | + } |
| 52 | + |
| 53 | + override fun addListener(listener: LithoLifecycleListener) { |
| 54 | + lithoLifecycleProviderDelegate.addListener(listener) |
| 55 | + } |
| 56 | + |
| 57 | + override fun removeListener(listener: LithoLifecycleListener) { |
| 58 | + lithoLifecycleProviderDelegate.removeListener(listener) |
| 59 | + } |
| 60 | + |
| 61 | + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) |
| 62 | + private fun onVisible() { |
| 63 | + moveToLifecycle(LithoLifecycle.HINT_VISIBLE) |
| 64 | + } |
| 65 | + |
| 66 | + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) |
| 67 | + private fun onInvisible() { |
| 68 | + moveToLifecycle(LithoLifecycle.HINT_INVISIBLE) |
| 69 | + } |
| 70 | + |
| 71 | + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) |
| 72 | + private fun onDestroy() { |
| 73 | + moveToLifecycle(LithoLifecycle.DESTROYED) |
| 74 | + } |
| 75 | + |
| 76 | + override val lifecycleOwner: LifecycleOwner? |
| 77 | + get() = _lifecycleOwner |
| 78 | +} |
0 commit comments