Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add ViewUtility.findSceneByView(View) View.requireScene() Vi…
…ew.requireNavigationScene()
- Loading branch information
Showing
8 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
library/scene/src/main/java/com/bytedance/scene/utlity/ViewUtility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.utlity; | ||
|
||
import android.support.annotation.Nullable; | ||
import android.view.View; | ||
import android.view.ViewParent; | ||
import com.bytedance.scene.R; | ||
import com.bytedance.scene.Scene; | ||
|
||
public class ViewUtility { | ||
@Nullable | ||
public static Scene findSceneByView(@Nullable View view) { | ||
while (view != null) { | ||
Scene scene = (Scene) view.getTag(R.id.bytedance_scene_view_scene_tag); | ||
if (scene != null) { | ||
return scene; | ||
} else { | ||
ViewParent viewParent = view.getParent(); | ||
if (viewParent instanceof View) { | ||
view = (View) viewParent; | ||
} else { | ||
view = null; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<item type="id" name="navigation_scene_content" /> | ||
<item type="id" name="bytedance_scene_view_scene_tag" /> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
library/scene/src/test/java/com/bytedance/scene/ViewUtilityTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.bytedance.scene; | ||
|
||
import android.arch.lifecycle.Lifecycle; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.util.Pair; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.FrameLayout; | ||
import com.bytedance.scene.group.GroupScene; | ||
import com.bytedance.scene.navigation.NavigationScene; | ||
import com.bytedance.scene.utlity.ViewUtility; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class ViewUtilityTests { | ||
@Test | ||
public void testSceneContext() { | ||
final TestScene testScene = new TestScene(); | ||
|
||
Pair<SceneLifecycleManager<NavigationScene>, NavigationScene> pair = NavigationSourceUtility.createFromInitSceneLifecycleManager(testScene); | ||
View view = testScene.requireView(); | ||
assertEquals(ViewUtility.findSceneByView(view), testScene); | ||
|
||
FrameLayout frameLayout = (FrameLayout) view; | ||
View childView = new View(testScene.requireSceneContext()); | ||
frameLayout.addView(childView); | ||
assertEquals(ViewUtility.findSceneByView(childView), testScene); | ||
} | ||
|
||
@Test | ||
public void testActivityContext() { | ||
final TestScene2 testScene = new TestScene2(); | ||
|
||
Pair<SceneLifecycleManager<NavigationScene>, NavigationScene> pair = NavigationSourceUtility.createFromInitSceneLifecycleManager(testScene); | ||
View view = testScene.requireView(); | ||
assertEquals(ViewUtility.findSceneByView(view), testScene); | ||
|
||
FrameLayout frameLayout = (FrameLayout) view; | ||
View childView = new View(testScene.requireSceneContext()); | ||
frameLayout.addView(childView); | ||
assertEquals(ViewUtility.findSceneByView(childView), testScene); | ||
} | ||
|
||
public static class TestScene extends GroupScene { | ||
@NonNull | ||
@Override | ||
public ViewGroup onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
return new FrameLayout(requireSceneContext()); | ||
} | ||
} | ||
|
||
public static class TestScene2 extends GroupScene { | ||
@NonNull | ||
@Override | ||
public ViewGroup onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
return new FrameLayout(requireActivity()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters