Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add GroupSceneCompatUtility, provides the ability to bind Gr…
…oupScene to Fragment directly
- Loading branch information
Showing
10 changed files
with
272 additions
and
12 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
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
39 changes: 39 additions & 0 deletions
39
demo/src/main/java/com/bytedance/scenedemo/migrate/GroupSceneBindToFragmentSample.kt
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,39 @@ | ||
package com.bytedance.scenedemo.migrate | ||
|
||
import android.os.Bundle | ||
import android.support.v4.app.Fragment | ||
import android.support.v7.app.AppCompatActivity | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.FrameLayout | ||
import com.bytedance.scene.GroupSceneDelegate | ||
import com.bytedance.scene.ui.GroupSceneCompatUtility | ||
import com.bytedance.scene.utlity.ViewIdGenerator | ||
import com.bytedance.scenedemo.group.viewpager.ViewPagerSample | ||
|
||
/** | ||
* Created by JiangQi on 9/5/18. | ||
*/ | ||
class GroupSceneBindToFragmentSample : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
supportFragmentManager.beginTransaction().add(android.R.id.content, AFragment()).commitNow() | ||
} | ||
|
||
class AFragment : Fragment() { | ||
private val viewId = ViewIdGenerator.generateViewId() | ||
private var delegate: GroupSceneDelegate? = null | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
val frameLayout = FrameLayout(activity!!) | ||
frameLayout.id = viewId | ||
return frameLayout | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
delegate = GroupSceneCompatUtility.setupWithFragment(this, ViewPagerSample::class.java, viewId).build() | ||
} | ||
} | ||
} |
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
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
208 changes: 208 additions & 0 deletions
208
library/scene_ui/src/main/java/com/bytedance/scene/ui/GroupSceneCompatUtility.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,208 @@ | ||
/* | ||
* 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.ui; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.IdRes; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentTransaction; | ||
import android.view.View; | ||
import com.bytedance.scene.*; | ||
import com.bytedance.scene.group.GroupScene; | ||
import com.bytedance.scene.utlity.SceneInstanceUtility; | ||
import com.bytedance.scene.utlity.ThreadUtility; | ||
import com.bytedance.scene.utlity.Utility; | ||
|
||
import java.util.HashSet; | ||
import java.util.WeakHashMap; | ||
|
||
public class GroupSceneCompatUtility { | ||
private GroupSceneCompatUtility() { | ||
} | ||
|
||
public static final class Builder { | ||
@NonNull | ||
private final Fragment mFragment; | ||
@NonNull | ||
private final Class<? extends GroupScene> mRootSceneClazz; | ||
@Nullable | ||
private Bundle mRootSceneArguments; | ||
@IdRes | ||
private final int mIdRes; | ||
private boolean mSupportRestore = false; | ||
@Nullable | ||
private SceneComponentFactory mRootSceneComponentFactory; | ||
@NonNull | ||
private String mTag = NavigationSceneCompatUtility.LIFE_CYCLE_FRAGMENT_TAG; | ||
private boolean mImmediate = true; | ||
|
||
private Builder(@NonNull Fragment fragment, @NonNull Class<? extends GroupScene> rootSceneClazz, @IdRes int containerId) { | ||
this.mFragment = Utility.requireNonNull(fragment, "Fragment can't be null"); | ||
this.mRootSceneClazz = Utility.requireNonNull(rootSceneClazz, "Root Scene class can't be null"); | ||
this.mIdRes = containerId; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneCompatUtility.Builder rootSceneArguments(@Nullable Bundle rootSceneArguments) { | ||
this.mRootSceneArguments = rootSceneArguments; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneCompatUtility.Builder supportRestore(boolean supportRestore) { | ||
this.mSupportRestore = supportRestore; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneCompatUtility.Builder rootSceneComponentFactory(@Nullable SceneComponentFactory rootSceneComponentFactory) { | ||
this.mRootSceneComponentFactory = rootSceneComponentFactory; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneCompatUtility.Builder tag(@NonNull String tag) { | ||
this.mTag = Utility.requireNonNull(tag, "Tag can't be null"); | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneCompatUtility.Builder immediate(boolean immediate) { | ||
this.mImmediate = immediate; | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public GroupSceneDelegate build() { | ||
return setupWithFragment(this.mFragment, this.mIdRes, this.mRootSceneClazz, this.mRootSceneArguments, this.mRootSceneComponentFactory, this.mSupportRestore, this.mTag, this.mImmediate); | ||
} | ||
} | ||
|
||
@NonNull | ||
public static GroupSceneCompatUtility.Builder setupWithFragment(@NonNull final Fragment fragment, | ||
@NonNull Class<? extends GroupScene> rootScene, | ||
@IdRes int containerId) { | ||
return new GroupSceneCompatUtility.Builder(fragment, rootScene, containerId); | ||
} | ||
|
||
@NonNull | ||
private static GroupSceneDelegate setupWithFragment(@NonNull final Fragment fragment, | ||
@IdRes int containerId, | ||
@NonNull Class<? extends Scene> sceneClazz, | ||
@Nullable Bundle sceneArguments, | ||
@Nullable SceneComponentFactory rootSceneComponentFactory, | ||
final boolean supportRestore, | ||
@NonNull final String tag, | ||
final boolean immediate) { | ||
ThreadUtility.checkUIThread(); | ||
if (tag == null) { | ||
throw new IllegalArgumentException("tag cant be null"); | ||
} | ||
NavigationSceneCompatUtility.checkDuplicateTag(fragment, tag); | ||
|
||
final FragmentManager fragmentManager = fragment.getChildFragmentManager(); | ||
LifeCycleCompatFragment lifeCycleFragment = (LifeCycleCompatFragment) fragmentManager.findFragmentByTag(tag); | ||
if (lifeCycleFragment != null && !supportRestore) { | ||
FragmentTransaction transaction = fragmentManager.beginTransaction(); | ||
transaction.remove(lifeCycleFragment); | ||
FragmentUtility.commitFragment(transaction, immediate); | ||
lifeCycleFragment = null; | ||
} | ||
|
||
ViewFinder viewFinder = new FragmentViewFinder(fragment); | ||
GroupScene groupScene = null; | ||
if (rootSceneComponentFactory != null) { | ||
groupScene = (GroupScene) rootSceneComponentFactory.instantiateScene(fragment.getClass().getClassLoader(), sceneClazz.getName(), sceneArguments); | ||
} | ||
if (groupScene == null) { | ||
groupScene = (GroupScene) SceneInstanceUtility.getInstanceFromClass(sceneClazz, sceneArguments); | ||
} | ||
|
||
ScopeHolderCompatFragment targetScopeHolderFragment = null; | ||
SceneLifecycleDispatcher<GroupScene> dispatcher = null; | ||
if (lifeCycleFragment != null) { | ||
final ScopeHolderCompatFragment scopeHolderFragment = ScopeHolderCompatFragment.install(fragment, tag, false, immediate); | ||
targetScopeHolderFragment = scopeHolderFragment; | ||
|
||
dispatcher = new SceneLifecycleDispatcher<>(containerId, viewFinder, groupScene, scopeHolderFragment, supportRestore); | ||
lifeCycleFragment.setSceneContainerLifecycleCallback(dispatcher); | ||
} else { | ||
final ScopeHolderCompatFragment scopeHolderFragment = ScopeHolderCompatFragment.install(fragment, tag, !supportRestore, immediate); | ||
lifeCycleFragment = LifeCycleCompatFragment.newInstance(supportRestore); | ||
|
||
dispatcher = new SceneLifecycleDispatcher<>(containerId, viewFinder, groupScene, scopeHolderFragment, supportRestore); | ||
lifeCycleFragment.setSceneContainerLifecycleCallback(dispatcher); | ||
|
||
FragmentTransaction transaction = fragmentManager.beginTransaction(); | ||
transaction.add(containerId, lifeCycleFragment, tag); | ||
FragmentUtility.commitFragment(transaction, immediate); | ||
targetScopeHolderFragment = scopeHolderFragment; | ||
} | ||
|
||
final LifeCycleCompatFragment finalLifeCycleFragment = lifeCycleFragment; | ||
final ScopeHolderCompatFragment finalTargetScopeHolderFragment = targetScopeHolderFragment; | ||
final GroupScene finalGroupScene = groupScene; | ||
final GroupSceneDelegate proxy = new GroupSceneDelegate() { | ||
private boolean mAbandoned = false; | ||
|
||
@Nullable | ||
@Override | ||
public GroupScene getGroupScene() { | ||
if (this.mAbandoned) { | ||
return null; | ||
} | ||
return finalGroupScene; | ||
} | ||
|
||
@Override | ||
public void abandon() { | ||
if (this.mAbandoned) { | ||
return; | ||
} | ||
this.mAbandoned = true; | ||
final View view = finalGroupScene.getView(); | ||
FragmentTransaction transaction = fragmentManager.beginTransaction().remove(finalLifeCycleFragment).remove(finalTargetScopeHolderFragment); | ||
if (immediate) { | ||
fragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() { | ||
@Override | ||
public void onFragmentDetached(FragmentManager fm, Fragment f) { | ||
super.onFragmentDetached(fm, f); | ||
if (f != finalLifeCycleFragment) { | ||
return; | ||
} | ||
fragmentManager.unregisterFragmentLifecycleCallbacks(this); | ||
NavigationSceneCompatUtility.removeTag(fragment, tag); | ||
} | ||
}, false); | ||
FragmentUtility.commitFragment(transaction, true); | ||
if (view != null) { | ||
Utility.removeFromParentView(view); | ||
} | ||
} else { | ||
FragmentUtility.commitFragment(transaction, false); | ||
NavigationSceneCompatUtility.removeTag(fragment, tag); | ||
if (view != null) { | ||
Utility.removeFromParentView(view); | ||
} | ||
} | ||
} | ||
}; | ||
return proxy; | ||
} | ||
} |
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