diff --git a/doric/build.gradle b/doric/build.gradle index fa8f9b0c..33327fad 100644 --- a/doric/build.gradle +++ b/doric/build.gradle @@ -40,8 +40,13 @@ dependencies { implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.1' implementation 'com.google.code.gson:gson:2.8.6' implementation "com.google.android.material:material:1.0.0" - testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + + + def nav_version = "2.1.0" + + implementation "androidx.navigation:navigation-fragment:$nav_version" + implementation "androidx.navigation:navigation-ui:$nav_version" } diff --git a/doric/src/main/java/pub/doric/DoricActivity.java b/doric/src/main/java/pub/doric/DoricActivity.java index ad225b6d..266d04e6 100644 --- a/doric/src/main/java/pub/doric/DoricActivity.java +++ b/doric/src/main/java/pub/doric/DoricActivity.java @@ -26,8 +26,6 @@ import androidx.appcompat.app.AppCompatActivity; * @CreateDate: 2019-11-19 */ public class DoricActivity extends AppCompatActivity { - private DoricFragment doricFragment; - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -35,19 +33,10 @@ public class DoricActivity extends AppCompatActivity { if (savedInstanceState == null) { String scheme = getIntent().getStringExtra("scheme"); String alias = getIntent().getStringExtra("alias"); - doricFragment = DoricFragment.newInstance(scheme, alias); + DoricFragment doricFragment = DoricFragment.newInstance(scheme, alias); getSupportFragmentManager().beginTransaction() .add(R.id.container, doricFragment) .commit(); } } - - @Override - public void onBackPressed() { - if (doricFragment.canPop()) { - doricFragment.pop(); - } else { - super.onBackPressed(); - } - } } diff --git a/doric/src/main/java/pub/doric/DoricFragment.java b/doric/src/main/java/pub/doric/DoricFragment.java index e64117b3..3d7f5661 100644 --- a/doric/src/main/java/pub/doric/DoricFragment.java +++ b/doric/src/main/java/pub/doric/DoricFragment.java @@ -20,18 +20,19 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; - -import pub.doric.navigator.IDoricNavigator; +import androidx.navigation.NavController; +import androidx.navigation.Navigation; /** * @Description: pub.doric * @Author: pengfei.zhou * @CreateDate: 2019-11-23 */ -public class DoricFragment extends Fragment implements IDoricNavigator { +public class DoricFragment extends Fragment { public static DoricFragment newInstance(String scheme, String alias) { Bundle args = new Bundle(); @@ -42,6 +43,23 @@ public class DoricFragment extends Fragment implements IDoricNavigator { return fragment; } + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + OnBackPressedCallback callback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host); + if (!navController.popBackStack()) { + if (getActivity() != null) { + getActivity().finish(); + } + } + } + }; + requireActivity().getOnBackPressedDispatcher().addCallback(this, callback); + } + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -51,34 +69,6 @@ public class DoricFragment extends Fragment implements IDoricNavigator { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - Bundle argument = getArguments(); - if (argument != null) { - String alias = argument.getString("alias"); - String scheme = argument.getString("scheme"); - push(scheme, alias); - } } - @Override - public void push(String scheme, String alias) { - getChildFragmentManager().beginTransaction() - .add(R.id.root, DoricPanelFragment.newInstance(scheme, alias)) - .addToBackStack(scheme) - .commit(); - } - - @Override - public void pop() { - if (canPop()) { - getChildFragmentManager().popBackStack(); - } else { - if (getActivity() != null) { - getActivity().finish(); - } - } - } - - public boolean canPop() { - return getChildFragmentManager().getBackStackEntryCount() > 1; - } } diff --git a/doric/src/main/java/pub/doric/DoricPanelFragment.java b/doric/src/main/java/pub/doric/DoricPanelFragment.java index 12653e8d..c6b7c7a8 100644 --- a/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -23,6 +23,8 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.navigation.NavController; +import androidx.navigation.Navigation; import pub.doric.async.AsyncResult; import pub.doric.loader.DoricJSLoaderManager; @@ -35,18 +37,8 @@ import pub.doric.utils.DoricLog; * @Author: pengfei.zhou * @CreateDate: 2019-11-23 */ -public class DoricPanelFragment extends Fragment { +public class DoricPanelFragment extends Fragment implements IDoricNavigator { private DoricPanel doricPanel; - private BaseDoricNavBar navBar; - - public static DoricPanelFragment newInstance(String scheme, String alias) { - Bundle args = new Bundle(); - args.putString("scheme", scheme); - args.putString("alias", alias); - DoricPanelFragment fragment = new DoricPanelFragment(); - fragment.setArguments(args); - return fragment; - } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -58,8 +50,12 @@ public class DoricPanelFragment extends Fragment { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); doricPanel = view.findViewById(R.id.doric_panel); - navBar = view.findViewById(R.id.doric_nav_bar); Bundle argument = getArguments(); + if (argument == null) { + if (getActivity() != null && getActivity().getIntent() != null) { + argument = getActivity().getIntent().getExtras(); + } + } if (argument == null) { DoricLog.e("DoricPanelFragment argument is null"); return; @@ -71,10 +67,8 @@ public class DoricPanelFragment extends Fragment { public void onResult(String result) { doricPanel.config(result, alias); DoricContext context = doricPanel.getDoricContext(); - Fragment fragment = getParentFragment(); - if (fragment instanceof IDoricNavigator) { - context.setDoricNavigator((IDoricNavigator) fragment); - } + context.setDoricNavigator(DoricPanelFragment.this); + BaseDoricNavBar navBar = requireActivity().getWindow().getDecorView().findViewById(R.id.doric_nav_bar); context.setDoricNavBar(navBar); } @@ -89,4 +83,22 @@ public class DoricPanelFragment extends Fragment { } }); } + + @Override + public void push(String scheme, String alias) { + Bundle argument = new Bundle(); + argument.putString("scheme", scheme); + argument.putString("alias", alias); + getNavController() + .navigate(R.id.action_doricPanelFragment_to_doricPanelFragment, argument); + } + + @Override + public void pop() { + getNavController().popBackStack(); + } + + private NavController getNavController() { + return Navigation.findNavController(getView()); + } } diff --git a/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java b/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java index f1b9a7b8..ff21d8d0 100644 --- a/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java +++ b/doric/src/main/java/pub/doric/plugin/NavBarPlugin.java @@ -61,13 +61,6 @@ public class NavBarPlugin extends DoricJavaPlugin { JSObject jsObject = jsDecoder.decode().asObject(); boolean hidden = jsObject.getProperty("hidden").asBoolean().value(); navBar.setHidden(hidden); - View v = getDoricContext().getRootNode().getNodeView(); - ViewGroup.LayoutParams params = v.getLayoutParams(); - if (params instanceof ViewGroup.MarginLayoutParams) { - ((ViewGroup.MarginLayoutParams) params).topMargin = - hidden ? 0 - : ((View) navBar).getMeasuredHeight(); - } promise.resolve(); } catch (ArchiveException e) { e.printStackTrace(); diff --git a/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java b/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java index 372529ec..b28f9251 100644 --- a/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java +++ b/doric/src/main/java/pub/doric/plugin/NavigatorPlugin.java @@ -18,10 +18,13 @@ package pub.doric.plugin; import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JavaValue; +import pub.doric.Doric; import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; import pub.doric.navigator.IDoricNavigator; import pub.doric.utils.ThreadMode; @@ -37,7 +40,7 @@ public class NavigatorPlugin extends DoricJavaPlugin { } @DoricMethod(thread = ThreadMode.UI) - public void push(JSDecoder jsDecoder) { + public void push(JSDecoder jsDecoder, DoricPromise promise) { IDoricNavigator navigator = getDoricContext().getDoricNavigator(); if (navigator != null) { try { @@ -45,17 +48,24 @@ public class NavigatorPlugin extends DoricJavaPlugin { navigator.push(jsObject.getProperty("scheme").asString().value(), jsObject.getProperty("alias").asString().value() ); + promise.resolve(); } catch (ArchiveException e) { e.printStackTrace(); + promise.reject(new JavaValue(e.getLocalizedMessage())); } + } else { + promise.reject(new JavaValue("Navigator not implemented")); } } @DoricMethod(thread = ThreadMode.UI) - public void pop() { + public void pop(DoricPromise promise) { IDoricNavigator navigator = getDoricContext().getDoricNavigator(); if (navigator != null) { navigator.pop(); + promise.resolve(); + } else { + promise.reject(new JavaValue("Navigator not implemented")); } } } diff --git a/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java b/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java index e7d44a06..14e7b94c 100644 --- a/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java +++ b/doric/src/main/java/pub/doric/shader/flowlayout/FlowLayoutNode.java @@ -16,8 +16,6 @@ package pub.doric.shader.flowlayout; import android.graphics.Rect; -import android.os.SystemClock; -import android.view.MotionEvent; import android.view.View; import androidx.annotation.NonNull; diff --git a/doric/src/main/res/layout/doric_fragment.xml b/doric/src/main/res/layout/doric_fragment.xml index 7af124c8..539a884b 100644 --- a/doric/src/main/res/layout/doric_fragment.xml +++ b/doric/src/main/res/layout/doric_fragment.xml @@ -1,7 +1,21 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/doric/src/main/res/layout/doric_framgent_panel.xml b/doric/src/main/res/layout/doric_framgent_panel.xml index 237f04c3..dba2e8e6 100644 --- a/doric/src/main/res/layout/doric_framgent_panel.xml +++ b/doric/src/main/res/layout/doric_framgent_panel.xml @@ -1,18 +1,7 @@ - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/doric/src/main/res/navigation/doric_navigation.xml b/doric/src/main/res/navigation/doric_navigation.xml new file mode 100644 index 00000000..61bff7b0 --- /dev/null +++ b/doric/src/main/res/navigation/doric_navigation.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file