make FAB hide on scroll in AccountActivity as well

This commit is contained in:
Conny Duck 2017-08-04 11:44:10 +02:00
parent e4c9b229d7
commit 099d76d919
6 changed files with 136 additions and 33 deletions

View file

@ -47,6 +47,7 @@ import android.widget.TextView;
import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.entity.Relationship;
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
import com.keylesspalace.tusky.interfaces.LinkListener;
import com.keylesspalace.tusky.pager.AccountPagerAdapter;
import com.keylesspalace.tusky.receiver.TimelineReceiver;
@ -64,7 +65,7 @@ import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class AccountActivity extends BaseActivity {
public class AccountActivity extends BaseActivity implements ActionButtonActivity {
private static final String TAG = "AccountActivity"; // logging tag
private enum FollowState {
@ -86,6 +87,8 @@ public class AccountActivity extends BaseActivity {
private TabLayout tabLayout;
private ImageView accountLockedView;
private View container;
private boolean hideFab;
private int oldOffset;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -127,6 +130,8 @@ public class AccountActivity extends BaseActivity {
actionBar.setDisplayShowHomeEnabled(true);
}
hideFab = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fabHide", false);
// Add a listener to change the toolbar icon color when it enters/exits its collapsed state.
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.account_app_bar_layout);
final CollapsingToolbarLayout collapsingToolbar =
@ -158,6 +163,16 @@ public class AccountActivity extends BaseActivity {
ThemeUtils.setDrawableTint(context, toolbar.getNavigationIcon(), attribute);
ThemeUtils.setDrawableTint(context, toolbar.getOverflowIcon(), attribute);
}
if(floatingBtn != null && hideFab) {
if (verticalOffset > oldOffset) {
floatingBtn.show();
}
if (verticalOffset < oldOffset) {
floatingBtn.hide();
}
}
oldOffset = verticalOffset;
}
});
@ -642,4 +657,11 @@ public class AccountActivity extends BaseActivity {
}
return super.onOptionsItemSelected(item);
}
@Nullable
@Override
public FloatingActionButton getActionButton() {
return floatingBtn;
}
}

View file

@ -23,6 +23,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.graphics.drawable.VectorDrawableCompat;
@ -37,6 +38,7 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
import com.keylesspalace.tusky.pager.TimelinePagerAdapter;
import com.keylesspalace.tusky.receiver.TimelineReceiver;
import com.keylesspalace.tusky.util.ThemeUtils;
@ -63,7 +65,7 @@ import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends BaseActivity {
public class MainActivity extends BaseActivity implements ActionButtonActivity {
private static final String TAG = "MainActivity"; // logging tag
private static final long DRAWER_ITEM_EDIT_PROFILE = 0;
private static final long DRAWER_ITEM_FAVOURITES = 1;
@ -77,7 +79,8 @@ public class MainActivity extends BaseActivity {
private static final long DRAWER_ITEM_SAVED_TOOT = 9;
protected static int COMPOSE_RESULT = 1;
public FloatingActionButton composeButton;
private FloatingActionButton composeButton;
private String loggedInAccountId;
private String loggedInAccountUsername;
private Stack<Integer> pageHistory;
@ -493,4 +496,10 @@ public class MainActivity extends BaseActivity {
private void onFetchUserInfoFailure(Exception exception) {
Log.e(TAG, "Failed to fetch user info. " + exception.getMessage());
}
@Nullable
@Override
public FloatingActionButton getActionButton() {
return composeButton;
}
}

View file

@ -17,9 +17,12 @@ package com.keylesspalace.tusky.fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DividerItemDecoration;
@ -42,6 +45,7 @@ import com.keylesspalace.tusky.BaseActivity;
import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.entity.Relationship;
import com.keylesspalace.tusky.interfaces.AccountActionListener;
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
import com.keylesspalace.tusky.network.MastodonApi;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.util.HttpHeaderLink;
@ -77,6 +81,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi
private int bottomFetches;
private boolean topLoading;
private int topFetches;
private boolean hideFab;
public static AccountListFragment newInstance(Type type) {
Bundle arguments = new Bundle();
@ -168,15 +173,55 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi
* activity is created, so everything needing to access the api object has to be delayed
* until here. */
api = activity.mastodonApi;
scrollListener = new EndlessOnScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
AccountListFragment.this.onLoadMore(view);
}
};
if (actionButtonPresent()) {
/* Use a modified scroll listener that both loads more statuses as it goes, and hides
* the follow button on down-scroll. */
ActionButtonActivity actionButtonActivity = (ActionButtonActivity) getActivity();
final FloatingActionButton composeButton = actionButtonActivity.getActionButton();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
hideFab = preferences.getBoolean("fabHide", false);
scrollListener = new EndlessOnScrollListener(layoutManager) {
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
super.onScrolled(view, dx, dy);
if (composeButton != null) {
if (hideFab) {
if (dy > 0 && composeButton.isShown()) {
composeButton.hide(); // hides the button if we're scrolling down
} else if (dy < 0 && !composeButton.isShown()) {
composeButton.show(); // shows it if we are scrolling up
}
} else if (!composeButton.isShown()) {
composeButton.show();
}
}
}
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
AccountListFragment.this.onLoadMore(view);
}
};
} else {
// Just use the basic scroll listener to load more accounts.
scrollListener = new EndlessOnScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
AccountListFragment.this.onLoadMore(view);
}
};
}
recyclerView.addOnScrollListener(scrollListener);
}
private boolean actionButtonPresent() {
return type == Type.FOLLOWS || type == Type.FOLLOWERS;
}
@Override
public void onDestroyView() {
if (jumpToTopAllowed()) {

View file

@ -172,7 +172,7 @@ public class NotificationsFragment extends SFragment implements
* guaranteed to be set until then.
* Use a modified scroll listener that both loads more notifications as it goes, and hides
* the compose button on down-scroll. */
final FloatingActionButton composeButton = activity.composeButton;
final FloatingActionButton composeButton = activity.getActionButton();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
activity);
preferences.registerOnSharedPreferenceChangeListener(this);
@ -182,14 +182,16 @@ public class NotificationsFragment extends SFragment implements
public void onScrolled(RecyclerView view, int dx, int dy) {
super.onScrolled(view, dx, dy);
if (hideFab) {
if (dy > 0 && composeButton.isShown()) {
composeButton.hide(); // hides the button if we're scrolling down
} else if (dy < 0 && !composeButton.isShown()) {
composeButton.show(); // shows it if we are scrolling up
if(composeButton != null) {
if (hideFab) {
if (dy > 0 && composeButton.isShown()) {
composeButton.hide(); // hides the button if we're scrolling down
} else if (dy < 0 && !composeButton.isShown()) {
composeButton.show(); // shows it if we are scrolling up
}
} else if (!composeButton.isShown()) {
composeButton.show();
}
} else if (!composeButton.isShown()) {
composeButton.show();
}
}

View file

@ -34,11 +34,11 @@ import android.view.View;
import android.view.ViewGroup;
import com.keylesspalace.tusky.BuildConfig;
import com.keylesspalace.tusky.MainActivity;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.adapter.FooterViewHolder;
import com.keylesspalace.tusky.adapter.TimelineAdapter;
import com.keylesspalace.tusky.entity.Status;
import com.keylesspalace.tusky.interfaces.ActionButtonActivity;
import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.network.MastodonApi;
import com.keylesspalace.tusky.receiver.TimelineReceiver;
@ -195,28 +195,29 @@ public class TimelineFragment extends SFragment implements
/* This is delayed until onActivityCreated solely because MainActivity.composeButton isn't
* guaranteed to be set until then. */
if (composeButtonPresent()) {
if (actionButtonPresent()) {
/* Use a modified scroll listener that both loads more statuses as it goes, and hides
* the follow button on down-scroll. */
MainActivity activity = (MainActivity) getActivity();
final FloatingActionButton composeButton = activity.composeButton;
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
activity);
ActionButtonActivity activity = (ActionButtonActivity) getActivity();
final FloatingActionButton composeButton = activity.getActionButton();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
hideFab = preferences.getBoolean("fabHide", false);
scrollListener = new EndlessOnScrollListener(layoutManager) {
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
super.onScrolled(view, dx, dy);
if (hideFab) {
if (dy > 0 && composeButton.isShown()) {
composeButton.hide(); // hides the button if we're scrolling down
} else if (dy < 0 && !composeButton.isShown()) {
composeButton.show(); // shows it if we are scrolling up
if (composeButton != null) {
if (hideFab) {
if (dy > 0 && composeButton.isShown()) {
composeButton.hide(); // hides the button if we're scrolling down
} else if (dy < 0 && !composeButton.isShown()) {
composeButton.show(); // shows it if we are scrolling up
}
} else if (!composeButton.isShown()) {
composeButton.show();
}
} else if (!composeButton.isShown()) {
composeButton.show();
}
}
}
@Override
@ -438,8 +439,8 @@ public class TimelineFragment extends SFragment implements
return kind != Kind.TAG && kind != Kind.FAVOURITES;
}
private boolean composeButtonPresent() {
return kind != Kind.TAG && kind != Kind.FAVOURITES && kind != Kind.USER;
private boolean actionButtonPresent() {
return kind != Kind.TAG && kind != Kind.FAVOURITES;
}
private void jumpToTop() {

View file

@ -0,0 +1,24 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.interfaces;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
public interface ActionButtonActivity {
@Nullable
FloatingActionButton getActionButton();
}