|
| 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 static androidx.lifecycle.Lifecycle.Event.ON_DESTROY; |
| 20 | +import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE; |
| 21 | +import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; |
| 22 | +import static com.facebook.litho.LithoLifecycleProvider.LithoLifecycle.DESTROYED; |
| 23 | +import static com.facebook.litho.LithoLifecycleProvider.LithoLifecycle.HINT_INVISIBLE; |
| 24 | +import static com.facebook.litho.LithoLifecycleProvider.LithoLifecycle.HINT_VISIBLE; |
| 25 | + |
| 26 | +import androidx.lifecycle.LifecycleObserver; |
| 27 | +import androidx.lifecycle.LifecycleOwner; |
| 28 | +import androidx.lifecycle.OnLifecycleEvent; |
| 29 | +import com.facebook.infer.annotation.Nullsafe; |
| 30 | +import javax.annotation.Nullable; |
| 31 | + |
| 32 | +/** |
| 33 | + * This LithoLifecycleProvider implementation dispatches to the registered observers the lifecycle |
| 34 | + * state changes triggered by the provided LifecycleOwner. For example, if a Fragment is passed as |
| 35 | + * param, the observers will be registered to listen to all of the fragment's lifecycle state |
| 36 | + * changes. |
| 37 | + */ |
| 38 | +@Nullsafe(Nullsafe.Mode.LOCAL) |
| 39 | +public class AOSPLithoLifecycleProvider |
| 40 | + implements LithoLifecycleProvider, LifecycleObserver, AOSPLifecycleOwnerProvider { |
| 41 | + private LithoLifecycleProviderDelegate mLithoLifecycleProviderDelegate; |
| 42 | + private LifecycleOwner mLifecycleOwner; |
| 43 | + |
| 44 | + public AOSPLithoLifecycleProvider(LifecycleOwner lifecycleOwner) { |
| 45 | + mLithoLifecycleProviderDelegate = new LithoLifecycleProviderDelegate(); |
| 46 | + mLifecycleOwner = lifecycleOwner; |
| 47 | + lifecycleOwner.getLifecycle().addObserver(this); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public LithoLifecycle getLifecycleStatus() { |
| 52 | + return mLithoLifecycleProviderDelegate.getLifecycleStatus(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void moveToLifecycle(LithoLifecycle lithoLifecycle) { |
| 57 | + mLithoLifecycleProviderDelegate.moveToLifecycle(lithoLifecycle); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void addListener(LithoLifecycleListener listener) { |
| 62 | + mLithoLifecycleProviderDelegate.addListener(listener); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void removeListener(LithoLifecycleListener listener) { |
| 67 | + mLithoLifecycleProviderDelegate.removeListener(listener); |
| 68 | + } |
| 69 | + |
| 70 | + @OnLifecycleEvent(ON_RESUME) |
| 71 | + private void onVisible() { |
| 72 | + moveToLifecycle(HINT_VISIBLE); |
| 73 | + } |
| 74 | + |
| 75 | + @OnLifecycleEvent(ON_PAUSE) |
| 76 | + private void onInvisible() { |
| 77 | + moveToLifecycle(HINT_INVISIBLE); |
| 78 | + } |
| 79 | + |
| 80 | + @OnLifecycleEvent(ON_DESTROY) |
| 81 | + private void onDestroy() { |
| 82 | + moveToLifecycle(DESTROYED); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + @Nullable |
| 87 | + public LifecycleOwner getLifecycleOwner() { |
| 88 | + return mLifecycleOwner; |
| 89 | + } |
| 90 | +} |
0 commit comments