/*
 * Copyright 2016 Phil Shadlyn
 *
 * 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
 *
 *    http://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.physphil.android.unitconverterultimate.ui;

import android.content.Context;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

/**
 * Behaviour to hide/show FAB on scroll
 * Created by Phizz on 16-09-11.
 */
public class ScrollAwareFabBehaviour extends FloatingActionButton.Behavior {

    public ScrollAwareFabBehaviour(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout,
                                       final FloatingActionButton child,
                                       final View directTargetChild, final View target, final int nestedScrollAxes) {
        // Ensure we react to vertical scrolling
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child,
                directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(final CoordinatorLayout coordinatorLayout,
                               final FloatingActionButton child,
                               final View target, final int dxConsumed, final int dyConsumed,
                               final int dxUnconsumed, final int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
                dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            child.hide();
        }
        else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            child.show();
        }
    }
}

@@ -677,16 +677,52 @@
 ontext;%0A
+import androidx.annotation.NonNull;%0A
 import a
@@ -1252,36 +1252,48 @@
 artNestedScroll(
-fina
+%0A        @NonNul
 l CoordinatorLay
@@ -1327,43 +1327,15 @@
     
-                               fina
+@NonNul
 l Fl
@@ -1372,166 +1372,113 @@
     
-                               final View directTargetChild, final View target, final int nestedScrollAxes) %7B%0A        // Ensure we react to vertical scrolling
+@NonNull View directTargetChild,%0A        @NonNull View target,%0A        int axes,%0A        int type%0A    ) %7B
 %0A   
@@ -1489,29 +1489,17 @@
  return 
-nestedScrollA
+a
 xes == V
@@ -1528,16 +1528,19 @@
 VERTICAL
+ %7C%7C
 %0A       
@@ -1547,15 +1547,8 @@
     
-     %7C%7C
  sup
@@ -1595,32 +1595,16 @@
 , child,
-%0A               
  directT
@@ -1627,24 +1627,18 @@
 et, 
-nestedScrollAxes
+axes, type
 );%0A 
@@ -1688,20 +1688,32 @@
 dScroll(
-fina
+%0A        @NonNul
 l Coordi
@@ -1755,35 +1755,15 @@
     
-                       fina
+@NonNul
 l Fl
@@ -1800,35 +1800,15 @@
     
-                       fina
+@NonNul
 l Vi
@@ -1817,22 +1817,24 @@
  target,
- final
+%0A       
  int dxC
@@ -1837,30 +1837,32 @@
  dxConsumed,
- final
+%0A       
  int dyConsu
@@ -1878,37 +1878,8 @@
     
-                       final 
 int 
@@ -1895,14 +1895,16 @@
 med,
- final
+%0A       
  int
@@ -1916,16 +1916,39 @@
 consumed
+,%0A        int type%0A    
 ) %7B%0A    
@@ -2033,24 +2033,8 @@
 med,
-%0A               
  dxU
@@ -2056,19 +2056,26 @@
 consumed
+, type
 );%0A
+%0A
         
@@ -2243,16 +2243,44 @@
 ld.hide(
+fabVisibilityChangedListener
 );%0A     
@@ -2283,24 +2283,16 @@
        %7D
-%0A       
  else if
@@ -2478,11 +2478,615 @@
  %7D%0A    %7D
+%0A%0A    private FloatingActionButton.OnVisibilityChangedListener fabVisibilityChangedListener = new FloatingActionButton.OnVisibilityChangedListener() %7B%0A        @Override%0A        public void onHidden(FloatingActionButton fab) %7B%0A            // Starting with support lib 25, hiding the FAB sets its visibility to View.GONE instead of INVISIBLE. This messes up the scrolling%0A            // https://stackoverflow.com/questions/41153619/floating-action-button-not-visible-on-scrolling-after-updating-google-support%0A            super.onHidden(fab);%0A            fab.setVisibility(View.INVISIBLE);%0A        %7D%0A    %7D;
 %0A%7D%0A

fix FabBehaviour so it reappears after scrolling