ModulesFragmentMgr.java 4.38 KB
package com.polysoft.nafmii.fragment;

import android.text.TextUtils;

import androidx.fragment.app.Fragment;
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.webview.IWebview;

import java.util.List;

public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedListener {

    private FragmentManager manager;
    private int containerId;
    private ModulesEnum currentModule;
    private OnChangeModuleListener changeListener;

    public ModulesFragmentMgr(FragmentManager manager, int containerId) {
        this.manager = manager;
        this.containerId = containerId;
    }

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

    public void showInitFragment(ModulesEnum[] modules) {
        for (int i = 0; i < modules.length; i++) {
            this.addModuleFragment(modules[i]);
        }

        WebViewFragment fragment = this.findFragment(modules[0]);
        FragmentTransaction transaction = this.manager.beginTransaction();
        transaction.show(fragment).commitNow();
        this.setCurrentModule(modules[0]);
    }

    public WebViewFragment getCurrentFragment() {
        return this.findFragment(this.currentModule);
    }

    public boolean onBackStack() {

        WebViewFragment fragment = getCurrentFragment();
        IWebview webview = fragment.getWebview();
        if (null != webview && webview.canGoBack()) {
            return false;
        }

        if (this.manager.getBackStackEntryCount() > 0) {
            this.manager.addOnBackStackChangedListener(this);
            this.manager.popBackStack();
            return false;
        }
        return true;
    }

    public void showModule(ModulesEnum module) {
        if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
            WebViewFragment fragment = this.findFragment(module);
            FragmentTransaction transaction = this.manager.beginTransaction();
            transaction.hide(this.getCurrentFragment());
            transaction.addToBackStack("").show(fragment).commit();
        }
        this.setCurrentModule(module);
    }

    public void showModule(MenuBean bean) {
        ModulesEnum module = ModulesEnum.findModule(bean.getModuleName());
        Fragment fragment = this.findFragment(module);
        if (!TextUtils.isEmpty(bean.getUrl())) {
            ((IWebview) fragment).loadUrl(bean.getUrl());
        }
        if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
            FragmentTransaction transaction = this.manager.beginTransaction();
            transaction.hide(this.getCurrentFragment());
            transaction.addToBackStack("").show(fragment).commit();
        }
        this.setCurrentModule(module);
    }

    public WebViewFragment findFragment(ModulesEnum module) {
        WebViewFragment fragment = (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
        if (null == fragment) {
            return this.addModuleFragment(module);
        }
        return fragment;
    }

    private WebViewFragment addModuleFragment(ModulesEnum module) {
        WebViewFragment fragment = (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
        if (null == fragment) {
            FragmentTransaction transaction = this.manager.beginTransaction();
            fragment = WebViewFragment.newInstances(module.webUrl);
            transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow();
        }
        return fragment;
    }

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

    @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()) {
                setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
                return;
            }
        }
    }


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