package fr.free.nrw.commons.nearby;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

import dagger.android.support.AndroidSupportInjection;
import dagger.android.support.DaggerFragment;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionController;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.utils.UriDeserializer;
import timber.log.Timber;

import static android.app.Activity.RESULT_OK;

public class NearbyListFragment extends DaggerFragment {
    private static final Type LIST_TYPE = new TypeToken<List<Place>>() {
    }.getType();
    private static final Type CUR_LAT_LNG_TYPE = new TypeToken<LatLng>() {
    }.getType();
    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(Uri.class, new UriDeserializer())
            .create();

    private NearbyAdapterFactory adapterFactory;
    private RecyclerView recyclerView;
    private ContributionController controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        Timber.d("NearbyListFragment created");
        View view = inflater.inflate(R.layout.fragment_nearby, container, false);
        recyclerView = view.findViewById(R.id.listView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        controller = new ContributionController(this);
        adapterFactory = new NearbyAdapterFactory(this, controller);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // Check that this is the first time view is created,
        // to avoid double list when screen orientation changed
        List<Place> placeList = Collections.emptyList();
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            String gsonPlaceList = bundle.getString("PlaceList", "[]");
            placeList = gson.fromJson(gsonPlaceList, LIST_TYPE);

            String gsonLatLng = bundle.getString("CurLatLng");
            LatLng curLatLng = gson.fromJson(gsonLatLng, CUR_LAT_LNG_TYPE);

            placeList = NearbyController.loadAttractionsFromLocationToPlaces(curLatLng, placeList);
        }
        recyclerView.setAdapter(adapterFactory.create(placeList));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            Timber.d("OnActivityResult() parameters: Req code: %d Result code: %d Data: %s",
                    requestCode, resultCode, data);
            controller.handleImagePicked(requestCode, data, true);
        } else {
            Timber.e("OnActivityResult() parameters: Req code: %d Result code: %d Data: %s",
                    requestCode, resultCode, data);
        }
    }

}
@@ -112,41 +112,126 @@
 oid.
-net.Uri;%0Aimport android.os.Bundle
+content.pm.PackageManager;%0Aimport android.net.Uri;%0Aimport android.os.Bundle;%0Aimport android.support.annotation.NonNull
 ;%0Aim
@@ -978,16 +978,84 @@
 SULT_OK;
+%0Aimport static android.content.pm.PackageManager.PERMISSION_GRANTED;
 %0A%0Apublic
@@ -3254,32 +3254,1042 @@
 eList));%0A    %7D%0A%0A
+%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode, @NonNull String%5B%5D permissions, @NonNull int%5B%5D grantResults) %7B%0A        Timber.d(%22onRequestPermissionsResult: req code = %22 + %22 perm = %22 + permissions + %22 grant =%22 + grantResults);%0A%0A        switch (requestCode) %7B%0A            // 1 = %22Read external storage%22 allowed when gallery selected%0A            case 1: %7B%0A                if (grantResults.length %3E 0 && grantResults%5B0%5D == PERMISSION_GRANTED) %7B%0A                    Timber.d(%22Call controller.startGalleryPick()%22);%0A                    controller.startGalleryPick();%0A                %7D%0A            %7D%0A            break;%0A%0A            // 3 = %22Write external storage%22 allowed when camera selected%0A            case 3: %7B%0A                if (grantResults.length %3E 0 && grantResults%5B0%5D == PackageManager.PERMISSION_GRANTED) %7B%0A                    Timber.d(%22Call controller.startCameraCapture()%22);%0A                    controller.startCameraCapture();%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A%0A
     @Override%0A  
@@ -4860,10 +4860,9 @@
 %7D%0A    %7D%0A
-%0A
 %7D

Add onRequestPermissionsResult to NearbyListFragment