From 413a18f6d39bf274369d7a1ee3279e20d3a608a7 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 10 Aug 2019 18:29:04 +0800 Subject: Make navigation bar and status bar transparent to display posts, comments, subreddits and users under them. Minor UI tweaks for bottom sheet fragments. --- .../AppBarStateChangeListener.java | 41 ++++++ .../infinityforreddit/CommentsListingFragment.java | 16 +- .../FlairBottomSheetFragment.java | 7 + .../FollowedUsersListingFragment.java | 14 ++ .../infinityforreddit/MainActivity.java | 48 +++++- .../infinityforreddit/PostFragment.java | 14 ++ .../PostTypeBottomSheetFragment.java | 7 + .../SearchPostSortTypeBottomSheetFragment.java | 7 + .../infinityforreddit/SearchResultActivity.java | 36 +++++ ...serAndSubredditSortTypeBottomSheetFragment.java | 7 + .../SortTypeBottomSheetFragment.java | 7 + .../SubredditListingFragment.java | 14 ++ .../SubscribedSubredditsListingFragment.java | 14 ++ .../SubscribedThingListingActivity.java | 36 +++++ .../infinityforreddit/UserListingFragment.java | 14 ++ .../ViewSubredditDetailActivity.java | 40 +++++ .../infinityforreddit/ViewUserDetailActivity.java | 163 +++++++++++---------- 17 files changed, 405 insertions(+), 80 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AppBarStateChangeListener.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppBarStateChangeListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppBarStateChangeListener.java new file mode 100644 index 00000000..dcb034bf --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppBarStateChangeListener.java @@ -0,0 +1,41 @@ +package ml.docilealligator.infinityforreddit; + +import com.google.android.material.appbar.AppBarLayout; + +abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener { + // State + public enum State { + EXPANDED, + COLLAPSED, + IDLE + } + + private AppBarStateChangeListener.State mCurrentState = AppBarStateChangeListener.State.IDLE; + + @Override + public final void onOffsetChanged(AppBarLayout appBarLayout, int i) { + if (i == 0) { + if (mCurrentState != AppBarStateChangeListener.State.EXPANDED) { + onStateChanged(appBarLayout, AppBarStateChangeListener.State.EXPANDED); + } + mCurrentState = AppBarStateChangeListener.State.EXPANDED; + } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) { + if (mCurrentState != AppBarStateChangeListener.State.COLLAPSED) { + onStateChanged(appBarLayout, AppBarStateChangeListener.State.COLLAPSED); + } + mCurrentState = AppBarStateChangeListener.State.COLLAPSED; + } else { + if (mCurrentState != AppBarStateChangeListener.State.IDLE) { + onStateChanged(appBarLayout, AppBarStateChangeListener.State.IDLE); + } + mCurrentState = AppBarStateChangeListener.State.IDLE; + } + } + + /** + * Notifies on state change + * @param appBarLayout Layout + * @param state Collapse state + */ + abstract void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java index dc584f00..63eefa89 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingFragment.java @@ -3,6 +3,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -76,6 +79,17 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mGlide = Glide.with(activity); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mCommentRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + mCommentRecyclerView.setLayoutManager(new LinearLayoutManager(activity)); mAdapter = new CommentsListingRecyclerViewAdapter(activity, mOauthRetrofit, @@ -85,7 +99,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mCommentRecyclerView.setAdapter(mAdapter); - CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale, username); + CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, resources.getConfiguration().locale, username); mCommentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class); mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java index 5ccec346..bd8af127 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -67,6 +69,11 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { ((Infinity) mAcitivity.getApplication()).getAppComponent().inject(this); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + mAdapter = new FlairBottomSheetRecyclerViewAdapter(flair -> ((FlairSelectionCallback) mAcitivity).flairSelected(flair)); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(mAdapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java index 53fb9506..ffd2cf95 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java @@ -2,6 +2,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -59,6 +62,17 @@ public class FollowedUsersListingFragment extends Fragment { ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + mGlide = Glide.with(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 07e545b4..3fd746aa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -2,11 +2,16 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -15,6 +20,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; @@ -62,6 +68,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0; @BindView(R.id.drawer_layout) DrawerLayout drawer; + @BindView(R.id.coordinator_layout_main_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_main_activity) AppBarLayout appBarLayout; @BindView(R.id.view_pager_main_activity) ViewPager viewPager; @BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.toolbar) Toolbar toolbar; @@ -119,6 +127,41 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe ((Infinity) getApplication()).getAppComponent().inject(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + Resources resources = getResources(); + + if(resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + View decorView = window.getDecorView(); + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if(state == State.COLLAPSED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } else if(state == State.EXPANDED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); + } + + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); + params.bottomMargin = resources.getDimensionPixelSize(navBarResourceId); + fab.setLayoutParams(params); + } + } + } + postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); @@ -390,10 +433,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) { - Intent intent = getIntent(); - finish(); + Intent intent = new Intent(this, MainActivity.class); startActivity(intent); - overridePendingTransition(0, 0); + finish(); } super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 2506dde8..851d351a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -4,6 +4,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; @@ -125,6 +128,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator { window = activity.getWindow(); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mPostRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + mGlide = Glide.with(activity); lazyModeRunnable = new Runnable() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTypeBottomSheetFragment.java index c8dd0d28..338821c0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTypeBottomSheetFragment.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -48,6 +50,11 @@ public class PostTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen Activity activity = getActivity(); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + textTypeLinearLayout.setOnClickListener(view -> { ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_TEXT); dismiss(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java index f54cf5b4..d0b2bf6b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchPostSortTypeBottomSheetFragment.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -44,6 +46,11 @@ public class SearchPostSortTypeBottomSheetFragment extends RoundedBottomSheetDia Activity activity = getActivity(); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + relevanceTypeTextView.setOnClickListener(view -> { ((SearchSortTypeSelectionCallback) activity).searchSortTypeSelected(PostDataSource.SORT_TYPE_RELEVANCE); dismiss(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index 7e663e03..d528488d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -1,10 +1,16 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; +import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -14,6 +20,7 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.tabs.TabLayout; import javax.inject.Inject; @@ -36,6 +43,7 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos private String mQuery; private String mSubredditName; + @BindView(R.id.appbar_search_result_activity) AppBarLayout appBarLayout; @BindView(R.id.toolbar_search_result_activity) Toolbar toolbar; @BindView(R.id.tab_layout_search_result_activity) TabLayout tabLayout; @BindView(R.id.view_pager_search_result_activity) ViewPager viewPager; @@ -57,6 +65,34 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos ((Infinity) getApplication()).getAppComponent().inject(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + Resources resources = getResources(); + + if(resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + View decorView = window.getDecorView(); + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if(state == State.COLLAPSED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } else if(state == State.EXPANDED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); + } + } + } + setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java index ae652fd4..5579d220 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchUserAndSubredditSortTypeBottomSheetFragment.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -43,6 +45,11 @@ public class SearchUserAndSubredditSortTypeBottomSheetFragment extends RoundedBo Activity activity = getActivity(); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + int position = getArguments().getInt(EXTRA_FRAGMENT_POSITION); relevanceTypeTextView.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java index 76ebbea4..b4825274 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SortTypeBottomSheetFragment.java @@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -49,6 +51,11 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen Activity activity = getActivity(); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + if(getArguments().getBoolean(EXTRA_NO_BEST_TYPE)) { bestTypeTextView.setVisibility(View.GONE); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java index 058143bb..bf6e3894 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -3,6 +3,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -78,6 +81,17 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun ButterKnife.bind(this, rootView); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mSubredditListingRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + mLinearLayoutManager = new LinearLayoutManager(getActivity()); mSubredditListingRecyclerView.setLayoutManager(mLinearLayoutManager); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index d4c877d8..15da8748 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -2,6 +2,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -63,6 +66,17 @@ public class SubscribedSubredditsListingFragment extends Fragment { ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); mGlide = Glide.with(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index 1f35288d..85ad1196 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -1,8 +1,14 @@ package ml.docilealligator.infinityforreddit; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.MenuItem; +import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -12,6 +18,7 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.tabs.TabLayout; import java.util.ArrayList; @@ -33,6 +40,7 @@ public class SubscribedThingListingActivity extends AppCompatActivity { private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; + @BindView(R.id.appbar_subscribed_thing_listing_activity) AppBarLayout appBarLayout; @BindView(R.id.toolbar_subscribed_thing_listing_activity) Toolbar toolbar; @BindView(R.id.tab_layout_subscribed_thing_listing_activity) TabLayout tabLayout; @BindView(R.id.view_pager_subscribed_thing_listing_activity) ViewPager viewPager; @@ -60,6 +68,34 @@ public class SubscribedThingListingActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + Resources resources = getResources(); + + if(resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + View decorView = window.getDecorView(); + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if(state == State.COLLAPSED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } else if(state == State.EXPANDED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); + } + } + } + setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java index 04fe267d..46971ffe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -74,6 +77,17 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato ButterKnife.bind(this, rootView); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + mUserListingRecyclerView.setPadding(0, 0, 0, resources.getDimensionPixelSize(navBarResourceId)); + } + } + } + mLinearLayoutManager = new LinearLayoutManager(getActivity()); mUserListingRecyclerView.setLayoutManager(mLinearLayoutManager); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 4c734251..efbc07a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -1,12 +1,17 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.TextView; import androidx.annotation.NonNull; @@ -99,6 +104,41 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So ((Infinity) getApplication()).getAppComponent().inject(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + Resources resources = getResources(); + + if(resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || resources.getBoolean(R.bool.isTablet)) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + View decorView = window.getDecorView(); + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if (state == State.COLLAPSED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } else if (state == State.EXPANDED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); + } + + int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (navBarResourceId > 0) { + CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); + params.bottomMargin = resources.getDimensionPixelSize(navBarResourceId); + fab.setLayoutParams(params); + } + } + } + subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); if(savedInstanceState == null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 1901f179..f88b5ac8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -1,12 +1,17 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.TextView; import androidx.annotation.NonNull; @@ -51,16 +56,26 @@ public class ViewUserDetailActivity extends AppCompatActivity { private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; - @BindView(R.id.coordinator_layout_view_user_detail_activity) CoordinatorLayout coordinatorLayout; - @BindView(R.id.view_pager_view_user_detail_activity) ViewPager viewPager; - @BindView(R.id.appbar_layout_view_user_detail) AppBarLayout appBarLayout; - @BindView(R.id.tab_layout_view_user_detail_activity) TabLayout tabLayout; - @BindView(R.id.collapsing_toolbar_layout_view_user_detail_activity) CollapsingToolbarLayout collapsingToolbarLayout; - @BindView(R.id.banner_image_view_view_user_detail_activity) GifImageView bannerImageView; - @BindView(R.id.icon_gif_image_view_view_user_detail_activity) GifImageView iconGifImageView; - @BindView(R.id.user_name_text_view_view_user_detail_activity) TextView userNameTextView; - @BindView(R.id.subscribe_user_chip_view_user_detail_activity) Chip subscribeUserChip; - @BindView(R.id.karma_text_view_view_user_detail_activity) TextView karmaTextView; + @BindView(R.id.coordinator_layout_view_user_detail_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.view_pager_view_user_detail_activity) + ViewPager viewPager; + @BindView(R.id.appbar_layout_view_user_detail) + AppBarLayout appBarLayout; + @BindView(R.id.tab_layout_view_user_detail_activity) + TabLayout tabLayout; + @BindView(R.id.collapsing_toolbar_layout_view_user_detail_activity) + CollapsingToolbarLayout collapsingToolbarLayout; + @BindView(R.id.banner_image_view_view_user_detail_activity) + GifImageView bannerImageView; + @BindView(R.id.icon_gif_image_view_view_user_detail_activity) + GifImageView iconGifImageView; + @BindView(R.id.user_name_text_view_view_user_detail_activity) + TextView userNameTextView; + @BindView(R.id.subscribe_user_chip_view_user_detail_activity) + Chip subscribeUserChip; + @BindView(R.id.karma_text_view_view_user_detail_activity) + TextView karmaTextView; private SectionsPagerAdapter sectionsPagerAdapter; @@ -106,7 +121,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { username = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); - if(savedInstanceState == null) { + if (savedInstanceState == null) { getCurrentAccountAndInitializeViewPager(); } else { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); @@ -115,7 +130,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); - if(!mNullAccessToken && mAccessToken == null) { + if (!mNullAccessToken && mAccessToken == null) { getCurrentAccountAndInitializeViewPager(); } else { initializeViewPager(); @@ -140,8 +155,8 @@ public class ViewUserDetailActivity extends AppCompatActivity { toolbar.setTitle(title); setSupportActionBar(toolbar); - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = statusBarHeight; + ViewGroup.MarginLayoutParams toolbarLayoutParams = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + toolbarLayoutParams.topMargin = statusBarHeight; expandedTabTextColor = getResources().getColor(R.color.tabLayoutWithExpandedCollapsingToolbarTextColor); expandedTabBackgroundColor = getResources().getColor(R.color.tabLayoutWithExpandedCollapsingToolbarTabBackground); @@ -151,20 +166,54 @@ public class ViewUserDetailActivity extends AppCompatActivity { collapsedTabBackgroundColor = getResources().getColor(R.color.tabLayoutWithCollapsedCollapsingToolbarTabBackground); collapsedTabIndicatorColor = getResources().getColor(R.color.tabLayoutWithCollapsedCollapsingToolbarTabIndicator); - appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { - @Override - void onStateChanged(AppBarLayout appBarLayout, State state) { - if(state == State.EXPANDED) { - tabLayout.setTabTextColors(expandedTabTextColor, expandedTabTextColor); - tabLayout.setSelectedTabIndicatorColor(expandedTabIndicatorColor); - tabLayout.setBackgroundColor(expandedTabBackgroundColor); - } else if(state == State.COLLAPSED) { - tabLayout.setTabTextColors(collapsedTabTextColor, collapsedTabTextColor); - tabLayout.setSelectedTabIndicatorColor(collapsedTabIndicatorColor); - tabLayout.setBackgroundColor(collapsedTabBackgroundColor); + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 + && (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT + || resources.getBoolean(R.bool.isTablet))) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + View decorView = window.getDecorView(); + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if (state == State.COLLAPSED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + tabLayout.setTabTextColors(collapsedTabTextColor, collapsedTabTextColor); + tabLayout.setSelectedTabIndicatorColor(collapsedTabIndicatorColor); + tabLayout.setBackgroundColor(collapsedTabBackgroundColor); + } else if (state == State.EXPANDED) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + tabLayout.setTabTextColors(expandedTabTextColor, expandedTabTextColor); + tabLayout.setSelectedTabIndicatorColor(expandedTabIndicatorColor); + tabLayout.setBackgroundColor(expandedTabBackgroundColor); + } } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); } - }); + } else { + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + void onStateChanged(AppBarLayout appBarLayout, State state) { + if (state == State.EXPANDED) { + tabLayout.setTabTextColors(expandedTabTextColor, expandedTabTextColor); + tabLayout.setSelectedTabIndicatorColor(expandedTabIndicatorColor); + tabLayout.setBackgroundColor(expandedTabBackgroundColor); + } else if (state == State.COLLAPSED) { + tabLayout.setTabTextColors(collapsedTabTextColor, collapsedTabTextColor); + tabLayout.setSelectedTabIndicatorColor(collapsedTabIndicatorColor); + tabLayout.setBackgroundColor(collapsedTabBackgroundColor); + } + } + }); + } subscribedUserDao = mRedditDataRoomDatabase.subscribedUserDao(); glide = Glide.with(this); @@ -172,8 +221,8 @@ public class ViewUserDetailActivity extends AppCompatActivity { userViewModel = ViewModelProviders.of(this, new UserViewModel.Factory(getApplication(), mRedditDataRoomDatabase, username)) .get(UserViewModel.class); userViewModel.getUserLiveData().observe(this, userData -> { - if(userData != null) { - if(userData.getBanner().equals("")) { + if (userData != null) { + if (userData.getBanner().equals("")) { bannerImageView.setOnClickListener(view -> { //Do nothing since the user has no banner image }); @@ -188,7 +237,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { }); } - if(userData.getIconUrl().equals("")) { + if (userData.getIconUrl().equals("")) { glide.load(getDrawable(R.drawable.subreddit_default_icon)) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(216, 0))) .into(iconGifImageView); @@ -211,12 +260,12 @@ public class ViewUserDetailActivity extends AppCompatActivity { }); } - if(userData.isCanBeFollowed()) { + if (userData.isCanBeFollowed()) { subscribeUserChip.setVisibility(View.VISIBLE); subscribeUserChip.setOnClickListener(view -> { - if(subscriptionReady) { + if (subscriptionReady) { subscriptionReady = false; - if(subscribeUserChip.getText().equals(getResources().getString(R.string.follow))) { + if (subscribeUserChip.getText().equals(getResources().getString(R.string.follow))) { UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, username, mAccountName, subscribedUserDao, new UserFollowing.UserFollowingListener() { @Override @@ -275,7 +324,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { String userFullName = "u/" + userData.getName(); userNameTextView.setText(userFullName); - if(!title.equals(userFullName)) { + if (!title.equals(userFullName)) { getSupportActionBar().setTitle(userFullName); } String karma = getString(R.string.karma_info, userData.getKarma()); @@ -286,7 +335,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { private void getCurrentAccountAndInitializeViewPager() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { - if(account == null) { + if (account == null) { mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); @@ -304,7 +353,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { } private void fetchUserInfo() { - if(!mFetchUserInfoSuccess) { + if (!mFetchUserInfoSuccess) { FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData) { @@ -326,7 +375,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { getMenuInflater().inflate(R.menu.view_user_detail_activity, menu); mMenu = menu; MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity); - if(isInLazyMode) { + if (isInLazyMode) { lazyModeItem.setTitle(R.string.action_stop_lazy_mode); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); collapsingToolbarLayout.setLayoutParams(params); @@ -389,7 +438,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { } private void makeSnackbar(int resId, boolean retry) { - if(retry) { + if (retry) { Snackbar.make(coordinatorLayout, resId, Snackbar.LENGTH_SHORT).setAction(R.string.retry, view -> fetchUserInfo()).show(); } else { @@ -397,44 +446,6 @@ public class ViewUserDetailActivity extends AppCompatActivity { } } - public abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener { - // State - public enum State { - EXPANDED, - COLLAPSED, - IDLE - } - - private State mCurrentState = State.IDLE; - - @Override - public final void onOffsetChanged(AppBarLayout appBarLayout, int i) { - if (i == 0) { - if (mCurrentState != State.EXPANDED) { - onStateChanged(appBarLayout, State.EXPANDED); - } - mCurrentState = State.EXPANDED; - } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) { - if (mCurrentState != State.COLLAPSED) { - onStateChanged(appBarLayout, State.COLLAPSED); - } - mCurrentState = State.COLLAPSED; - } else { - if (mCurrentState != State.IDLE) { - onStateChanged(appBarLayout, State.IDLE); - } - mCurrentState = State.IDLE; - } - } - - /** - * Notifies on state change - * @param appBarLayout Layout - * @param state Collapse state - */ - abstract void onStateChanged(AppBarLayout appBarLayout, State state); - } - private static class InsertUserDataAsyncTask extends AsyncTask { interface InsertUserDataAsyncTaskListener { @@ -524,7 +535,7 @@ public class ViewUserDetailActivity extends AppCompatActivity { } public void refresh() { - if(viewPager.getCurrentItem() == 0) { + if (viewPager.getCurrentItem() == 0) { postFragment.refresh(); } else { commentsListingFragment.refresh(); -- cgit v1.2.3