/*
 * 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.editobservation;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gnd.R;
import com.google.android.material.textfield.TextInputEditText;

public class MultipleChoiceFieldLayout extends FrameLayout {

  @Nullable
  private Runnable showDialogListener;

  public MultipleChoiceFieldLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public void setOnShowDialogListener(Runnable showDialogListener) {
    this.showDialogListener = showDialogListener;
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();


    TextInputEditText editText = findViewById(R.id.multiple_choice_input_edit_text);

    editText.setOnFocusChangeListener(
        (v, hasFocus) -> {
          if (hasFocus) {
            if (showDialogListener == null){
              throw new NullPointerException("showDialogListener must be not be null");
            }
            showDialogListener.run();
          }
        });

    findViewById(R.id.multiple_choice_tap_target)
        .setOnClickListener(
            v -> {
              if (editText.isFocused()) {
                if (showDialogListener == null){
                  throw new NullPointerException("showDialogListener must be not be null");
                }
                showDialogListener.run();
              } else {
                editText.requestFocus();
              }
            });
  }
}

@@ -911,16 +911,61 @@
 ditText;
+%0Aimport com.google.common.base.Preconditions;
 %0A%0Apublic
@@ -1031,18 +1031,16 @@
 Nullable
-%0A 
  private
@@ -1398,17 +1398,16 @@
 ate();%0A%0A
-%0A
     Text
@@ -1580,35 +1580,58 @@
 ) %7B%0A            
-if 
+Preconditions.checkNotNull
 (showDialogListe
@@ -1629,34 +1629,25 @@
 alogListener
- == null)%7B
+,
 %0A           
@@ -1653,39 +1653,10 @@
     
-throw new NullPointerException(
+  
 %22sho
@@ -1694,30 +1694,16 @@
 null%22);%0A
-            %7D%0A
         
@@ -1913,19 +1913,42 @@
         
-if 
+Preconditions.checkNotNull
 (showDia
@@ -1962,18 +1962,9 @@
 ener
- == null)%7B
+,
 %0A   
@@ -1982,39 +1982,10 @@
     
-throw new NullPointerException(
+  
 %22sho
@@ -2027,26 +2027,8 @@
 %22);%0A
-                %7D%0A
     

Fix formatting and add checkNotNull