/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gnd.ui.home;

import static com.google.android.gnd.util.ImmutableListCollector.toImmutableList;
import static java8.util.stream.StreamSupport.stream;

import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import com.google.android.gnd.R;
import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.layer.Layer;
import com.google.android.gnd.ui.common.AbstractDialogFragment;
import com.google.common.collect.ImmutableList;
import dagger.hilt.android.AndroidEntryPoint;
import java8.util.Objects;
import javax.inject.Inject;
import timber.log.Timber;

@AndroidEntryPoint
public class AddFeatureDialogFragment extends AbstractDialogFragment {

  private static final String TAG = AddFeatureDialogFragment.class.getSimpleName();

  @Nullable private Project project;
  @Nullable private LayerSelectedListener listener;

  @Inject
  public AddFeatureDialogFragment() {}

  public void show(
      Project project, FragmentManager fragmentManager, LayerSelectedListener listener) {
    this.project = project;
    this.listener = listener;
    show(fragmentManager, TAG);
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    try {
      return createDialog(Objects.requireNonNull(project));
    } catch (RuntimeException e) {
      Timber.e(e);
      return fail(Objects.requireNonNullElse(e.getMessage(), "Unknown error"));
    }
  }

  private Dialog createDialog(Project project) {
    ImmutableList<Layer> layers =
        stream(project.getLayers())
            .sorted((pt1, pt2) -> pt1.getName().compareTo(pt2.getName()))
            .collect(toImmutableList());
    String[] items = stream(layers).map(Layer::getName).toArray(String[]::new);

    // TODO: Add icons.
    return new AlertDialog.Builder(requireContext())
        .setTitle(R.string.add_feature_select_type_dialog_title)
        .setNegativeButton(R.string.cancel, (dialog, id) -> dismiss())
        .setItems(items, (dialog, idx) -> onSelectLayer(layers.get(idx)))
        .create();
  }

  private void onSelectLayer(Layer layer) {
    if (listener != null) {
      listener.onSelected(layer);
    }
  }

  public interface LayerSelectedListener {
    void onSelected(Layer layer);
  }
}

@@ -892,32 +892,68 @@
 pp.AlertDialog;%0A
+import androidx.core.util.Consumer;%0A
 import androidx.
@@ -1586,36 +1586,35 @@
 ate 
-LayerSelectedListener listen
+Consumer%3CLayer%3E layerConsum
 er;%0A
@@ -1744,36 +1744,35 @@
 er, 
-LayerSelectedListener listen
+Consumer%3CLayer%3E layerConsum
 er) 
@@ -1815,24 +1815,34 @@
 is.l
-istener = listen
+ayerConsumer = layerConsum
 er;%0A
@@ -2036,39 +2036,30 @@
 log(
-Ob
+pro
 ject
-s.requireNonNull(project)
+, layerConsumer
 );%0A 
@@ -2247,16 +2247,47 @@
  project
+, Consumer%3CLayer%3E layerConsumer
 ) %7B%0A    
@@ -2800,28 +2800,37 @@
 g, i
-d
+nde
 x) -%3E 
-onSelectLayer
+layerConsumer.accept
 (lay
@@ -2838,17 +2838,19 @@
 rs.get(i
-d
+nde
 x)))%0A   
@@ -2876,82 +2876,54 @@
 %0A%0A  
-private void onSelectLayer(Layer layer) %7B%0A    if (listener != null
+@Override%0A  public void onDestroyView(
 ) %7B%0A    
   li
@@ -2922,124 +2922,75 @@
     
- 
- listener.onSelected(layer);%0A    %7D%0A  %7D%0A%0A  public interface LayerSelectedListener %7B%0A    void onSelected(Layer layer)
+super.onDestroyView();%0A    project = null;%0A    layerConsumer = null
 ;%0A  

Use Consumer<Layer> and remove references on destroy