summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/util/DetailActivityScrollingViewBehavior.java
blob: 85e8ae4b2b27db4b160f6eb0a340fb5d7d7bde03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.fox.ttrss.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

public class DetailActivityScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {

    private SharedPreferences m_prefs;

    public DetailActivityScrollingViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);

        m_prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return super.layoutDependsOn(parent, child, dependency) ||
                dependency instanceof FloatingActionButton;
    }

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

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                  @NonNull View child, @NonNull View target, int dx, int dy,
                                  @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);

        if (m_prefs.getBoolean("enable_article_fab", true)) {
            if (dy > 0) {
                // User scrolled down -> hide the FAB
                List<View> dependencies = coordinatorLayout.getDependencies(child);
                for (View view : dependencies) {
                    if (view instanceof FloatingActionButton) {
                        ((FloatingActionButton) view).hide();
                    }
                }
            } else if (dy < 0) {
                // User scrolled up -> show the FAB
                List<View> dependencies = coordinatorLayout.getDependencies(child);
                for (View view : dependencies) {
                    if (view instanceof FloatingActionButton) {
                        ((FloatingActionButton) view).show();
                    }
                }
            }
        }

    }
}