Skip to content

Commit

Permalink
refactor: refactor Scene.getNavigationScene and Scene.requireNavigati…
Browse files Browse the repository at this point in the history
…onScene, prepare to splite navigation library
  • Loading branch information
qii committed Apr 17, 2021
1 parent 84ee1fb commit 9fa0bd2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
27 changes: 8 additions & 19 deletions library/scene/src/main/java/com/bytedance/scene/Scene.java
Expand Up @@ -39,6 +39,7 @@
import androidx.lifecycle.ViewModelStoreOwner;

import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scene.parcel.ParcelConstants;
import com.bytedance.scene.utlity.ViewRefUtility;
import com.bytedance.scene.utlity.SceneInternalException;
Expand Down Expand Up @@ -134,7 +135,11 @@ public abstract class Scene implements LifecycleOwner, ViewModelStoreOwner {
private Scene mParentScene;
private Scope.RootScopeFactory mRootScopeFactory = Scope.DEFAULT_ROOT_SCOPE_FACTORY;
private Scope mScope;
private NavigationScene mNavigationScene;
/**
* use NavigationSceneGetter instead
*/
@Deprecated
private Object mNavigationScene;
private State mState = State.NONE;
private final StringBuilder mStateHistoryBuilder = new StringBuilder(mState.name);
private Bundle mArguments;
Expand Down Expand Up @@ -280,11 +285,6 @@ void rest() {
public void dispatchAttachScene(@Nullable Scene parentScene) {
if (parentScene != null) {
this.mParentScene = parentScene;
if (this.mParentScene instanceof NavigationScene) {
this.mNavigationScene = (NavigationScene) this.mParentScene;
} else {
this.mNavigationScene = this.mParentScene.getNavigationScene();
}
}
mCalled = false;
onAttach();
Expand Down Expand Up @@ -839,23 +839,12 @@ public final Scene requireParentScene() {

@Nullable
public final NavigationScene getNavigationScene() {
return this.mNavigationScene;
return NavigationSceneGetter.getNavigationScene(this);
}

@NonNull
public final NavigationScene requireNavigationScene() {
NavigationScene navigationScene = getNavigationScene();
if (navigationScene == null) {
Context context = getApplicationContext();
if (context == null) {
throw new IllegalStateException("Scene " + this + " is not attached to any Scene");
} else if (this instanceof NavigationScene) {
throw new IllegalStateException("Scene " + this + " is root Scene");
} else {
throw new IllegalStateException("The root of the Scene hierarchy is not NavigationScene");
}
}
return navigationScene;
return NavigationSceneGetter.requireNavigationScene(this);
}

@MainThread
Expand Down
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2019 ByteDance Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bytedance.scene.navigation;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.lifecycle.*;
import com.bytedance.scene.Scene;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.utlity.ThreadUtility;

import java.util.WeakHashMap;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

/**
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public class NavigationSceneGetter {
@Nullable
public static NavigationScene getNavigationScene(@NonNull final Scene scene) {
ThreadUtility.checkUIThread();
return findNavigationScene(scene);
}

@Nullable
private static NavigationScene findNavigationScene(@Nullable Scene scene) {
while (scene != null) {
Scene parentScene = scene.getParentScene();
if (parentScene instanceof NavigationScene) {
return (NavigationScene) parentScene;
}
scene = parentScene;
}
return null;
}

@NonNull
public static NavigationScene requireNavigationScene(@NonNull Scene scene) {
ThreadUtility.checkUIThread();
NavigationScene navigationScene = getNavigationScene(scene);
if (navigationScene == null) {
Context context = scene.getApplicationContext();
if (context == null) {
throw new IllegalStateException("Scene " + scene + " is not attached to any Scene");
} else if (scene instanceof NavigationScene) {
throw new IllegalStateException("Scene " + scene + " is root Scene");
} else {
throw new IllegalStateException("The root of the Scene hierarchy is not NavigationScene");
}
}
return navigationScene;
}
}

0 comments on commit 9fa0bd2

Please sign in to comment.