ModulesFragmentMgr.java 4.79 KB
package com.polysoft.nafmii.fragment.home;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.polysoft.nafmii.bean.MenuBean;
import com.polysoft.nafmii.enums.ModulesEnum;
import com.polysoft.nafmii.fragment.HomeWebviewFragment;
import com.polysoft.nafmii.fragment.WebViewFragment;
import com.polysoft.nafmii.webview.IWebview;

import java.util.List;

public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedListener {

    private final FragmentManager manager;
    private final int containerId;
    private final ModuleUrlHandle moduleHandle;

    private ModulesEnum currentModule;
    private OnChangeModuleListener changeListener;

    public ModulesFragmentMgr(FragmentActivity activity, int containerId) {
        this.manager = activity.getSupportFragmentManager();
        this.containerId = containerId;
        this.moduleHandle = new ModuleUrlHandle(activity);

        this.manager.addOnBackStackChangedListener(this);
    }

    public void addChangeModuleLsitener(OnChangeModuleListener listener) {
        this.changeListener = listener;
    }

    public void showInitFragment(ModulesEnum[] modules) {
        for (ModulesEnum module : modules) {
            this.findNewModuleFragment(module);
        }
        this.showModule(modules[0]);
    }

    public WebViewFragment getCurrentFragment() {
        if (null == this.currentModule) return null;

        return this.findFragment(this.currentModule);
    }

    public boolean onBackStack() {
        WebViewFragment fragment = this.getCurrentFragment();
        if (null == fragment) {
            return false;
        }
        IWebview webview = fragment.getWebview();
        if (null != webview && webview.canGoBack()) {
            return false;
        }
        if (this.manager.getBackStackEntryCount() > 0) {
            this.manager.popBackStack();
            return false;
        }
        return true;
    }

    @Override
    public void onBackStackChanged() {
        List<Fragment> fragments = this.manager.getFragments();
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            if (fragment.isVisible()) {
                this.setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
                return;
            }
        }
    }

    public void showModule(ModulesEnum module) {
        if (module == this.currentModule) {
            return;
        }
        if (null == this.currentModule) {
            this.showModuleFragment(this.findFragment(module));
            this.setCurrentModule(module);
            return;
        }
        this.showModule(this.moduleHandle.getModuleHandle(module));
    }

    public void showModule(MenuBean bean) {
        this.showModule(moduleHandle.getModuleHandle(bean));
    }


    public WebViewFragment findFragment(ModulesEnum module) {
        return (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
    }

    private void showModule(ModuleUrlHandle.IModuleHandle handle) {
        ModulesEnum module = handle.getModule();
        WebViewFragment fragment = this.findNewModuleFragment(module);

        if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
            this.showModuleFragment(fragment);
            if (null != fragment) {
                handle.loadUrl(fragment.getWebview());
            }
        } else {
            handle.loadUrl(fragment.getWebview());
        }
        this.setCurrentModule(module);
    }

    private WebViewFragment findNewModuleFragment(ModulesEnum module) {
        WebViewFragment fragment = this.findFragment(module);
        if (null == fragment) {
            FragmentTransaction transaction = this.manager.beginTransaction();
            fragment = HomeWebviewFragment.newInstance(this.moduleHandle.getModuleUrl(module));
            transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow();
        }
        return fragment;
    }

    private void showModuleFragment(Fragment fragment) {
        if (null == fragment) return;

        FragmentTransaction transaction = this.manager.beginTransaction();
        WebViewFragment currentFragment = this.getCurrentFragment();
        if (null != currentFragment) {
            transaction.hide(currentFragment);
        }
        transaction.show(fragment).commit();
    }

    private void setCurrentModule(ModulesEnum module) {
        this.currentModule = module;
        if (null != this.changeListener) {
            WebViewFragment fragment = this.getCurrentFragment();
            this.changeListener.changeModule(module, fragment);
        }
    }


    public interface OnChangeModuleListener {
        void changeModule(ModulesEnum module, Fragment fragment);
    }
}