package se.eelde.granter;

import android.content.res.Resources;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;

public class Granter {
    private static final String PERMISSIONS_FRAGMENT_TAG = "permissions_fragment_tag";

    private final FragmentManager fragmentManager;
    private final ArrayList<String> permissions;
    private final int requestCode;
    private final String rationale;
    private String systemSettingRationale;

    private Granter(FragmentManager fragmentManager, ArrayList<String> permissions, int requestCode, String rationale, String systemSettingRationale) {
        this.fragmentManager = fragmentManager;
        this.permissions = permissions;
        this.requestCode = requestCode;
        this.rationale = rationale;
        this.systemSettingRationale = systemSettingRationale;
    }

    public void show() {
        fragmentManager.beginTransaction().add(GranterFragment.newInstance(permissions, requestCode, rationale, systemSettingRationale), PERMISSIONS_FRAGMENT_TAG).commit();
    }

    @SuppressWarnings("unused")
    public static class Builder {
        private final FragmentManager fragmentManager;
        private final ArrayList<String> permissions = new ArrayList<>();
        private final Resources resources;
        private int requestCode = 7896;
        private String rationale = null;
        private String systemSettingRationale = null;

        public Builder(AppCompatActivity activity) {
            fragmentManager = activity.getSupportFragmentManager();
            resources = activity.getResources();
        }

        public Builder(Fragment fragment) {
            fragmentManager = fragment.getChildFragmentManager();
            resources = fragment.getResources();
        }

        public Builder requestCode(int requestCode) {
            this.requestCode = requestCode;
            return this;
        }

        public Builder addPermission(String... permissions) {
            this.permissions.addAll(Arrays.asList(permissions));
            return this;
        }

        public Builder rationale(String rationale) {
            this.rationale = rationale;
            return this;
        }

        public Builder rationale(@StringRes int rationale) {
            this.rationale = resources.getString(rationale);
            return this;
        }

        public Builder systemSettingRationale(String systemSettingRationale) {
            this.systemSettingRationale = systemSettingRationale;
            return this;
        }

        public Builder systemSettingRationale(@StringRes int systemSettingRationale) {
            this.systemSettingRationale = resources.getString(systemSettingRationale);
            return this;
        }

        public Granter build() {
            return new Granter(fragmentManager, permissions, requestCode, rationale, systemSettingRationale);
        }
    }
}

@@ -54,24 +54,68 @@
 .Resources;%0A
+import android.support.annotation.Nullable;%0A
 import andro
@@ -234,16 +234,67 @@
 anager;%0A
+import android.support.v4.app.FragmentTransaction;%0A
 import a
@@ -1137,42 +1137,344 @@
     
-fragmentManager.beginTransaction()
+@Nullable%0A        Fragment oldGranterFragment = fragmentManager.findFragmentByTag(PERMISSIONS_FRAGMENT_TAG);%0A%0A        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();%0A        if (oldGranterFragment != null) %7B%0A            fragmentTransaction.remove(oldGranterFragment);%0A        %7D%0A%0A        fragmentTransaction
 .add
@@ -1589,16 +1589,33 @@
 ENT_TAG)
+%0A                
 .commit(

Make sure we only have one GranterFragment