feat:use Navigation to manage DoricPanelFragment

This commit is contained in:
pengfei.zhou 2019-12-09 18:01:40 +08:00
parent 394800150e
commit eb12c2c4c5
10 changed files with 103 additions and 89 deletions

View File

@ -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"
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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();

View File

@ -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"));
}
}
}

View File

@ -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;

View File

@ -1,7 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
<pub.doric.navbar.BaseDoricNavBar
android:id="@+id/doric_nav_bar"
android:layout_width="match_parent"
android:layout_height="44dp" />
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/doric_navigation" />
</LinearLayout>

View File

@ -1,18 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<pub.doric.DoricPanel xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/doric_panel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<pub.doric.DoricPanel
android:id="@+id/doric_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginTop="44dp" />
<pub.doric.navbar.BaseDoricNavBar
android:id="@+id/doric_nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</pub.doric.DoricPanel>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/doric_navigation"
app:startDestination="@id/doricPanelFragment">
<fragment
android:id="@+id/doricPanelFragment"
android:name="pub.doric.DoricPanelFragment"
android:label="DoricPanelFragment">
<action
android:id="@+id/action_doricPanelFragment_to_doricPanelFragment"
app:destination="@id/doricPanelFragment" />
</fragment>
</navigation>