package com.navigation.reactnative;

import android.content.Context;
import android.os.Build;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import java.util.ArrayList;
import java.util.List;

public class TabBarView extends ViewPager {

    public TabBarView(Context context) {
        super(context);
        addOnPageChangeListener(new TabChangeListener());
        setAdapter(new Adapter());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.requestLayout();
        post(measureAndLayout);
    }

    private final Runnable measureAndLayout = new Runnable() {
        @Override
        public void run() {
            measure(
                MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
            layout(getLeft(), getTop(), getRight(), getBottom());
        }
    };

    @Nullable
    @Override
    public Adapter getAdapter() {
        return (Adapter) super.getAdapter();
    }

    void addTab(TabBarItemView tab, int index) {
        if (getAdapter() != null)
            getAdapter().addTab(tab, index);
    }

    void removeTab(int index) {
        if (getAdapter() != null)
            getAdapter().removeTab(index);
    }

    private class Adapter extends FragmentPagerAdapter {
        private List<TabFragment> tabs = new ArrayList<>();

        Adapter() {
            super(((FragmentActivity) ((ReactContext) getContext()).getCurrentActivity()).getSupportFragmentManager());
        }

        void addTab(TabBarItemView tab, int index) {
            tabs.add(index, new TabFragment(tab));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                tab.setElevation(-1 * index);
            notifyDataSetChanged();
        }

        void removeTab(int index) {
            tabs.remove(index);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return tabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            return tabs.get(position);
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return !tabs.contains(object) ? POSITION_NONE : tabs.indexOf(object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            post(measureAndLayout);
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            if (!tabs.contains(object)) {
                FragmentTransaction transaction = ((FragmentActivity) ((ReactContext) getContext()).getCurrentActivity()).getSupportFragmentManager().beginTransaction();
                transaction.remove((Fragment) object);
                transaction.commit();
            }
        }
    }

    private class TabChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            WritableMap event = Arguments.createMap();
            event.putInt("tab", position);
            ReactContext reactContext = (ReactContext) getContext();
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(),"onTabSelected", event);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}

@@ -2300,32 +2300,80 @@
 taSetChanged();%0A
+            setOffscreenPageLimit(tabs.size());%0A
         %7D%0A%0A     
@@ -2463,32 +2463,80 @@
 taSetChanged();%0A
+            setOffscreenPageLimit(tabs.size());%0A
         %7D%0A%0A     
@@ -3264,54 +3264,8 @@
 ) %7B%0A
-            if (!tabs.contains(object)) %7B%0A    
     
@@ -3430,36 +3430,32 @@
 ();%0A            
-    
 transaction.remo
@@ -3481,36 +3481,32 @@
 t);%0A            
-    
 transaction.comm
@@ -3515,22 +3515,8 @@
 ();%0A
-            %7D%0A
     
@@ -3619,32 +3619,32 @@
       @Override%0A
-
         public v
@@ -3722,24 +3722,25 @@
 etPixels) %7B%0A
+%0A
         %7D%0A%0A 

Revert "Kept default offscreen page limit"