From 644f2ddff8bbf75f33807a3b6531b2a5251743c4 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Wed, 19 May 2021 20:09:49 +0900 Subject: [PATCH] Add some null checks and formatting. --- .../fragments/main/ProfileFragment.java | 101 +++++++++--------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 66f5ade5..8fc98002 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -140,7 +140,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final ActionBar actionBar = fragmentActivity.getSupportActionBar(); if (actionBar != null && !TextUtils.isEmpty(username)) { final String finalUsername = username.startsWith("@") ? username.substring(1) - : username; + : username; actionBar.setTitle(finalUsername); actionBar.setSubtitle(null); } @@ -765,12 +765,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final long followingCount = profileModel.getFollowingCount(); final String postCount = String.valueOf(profileModel.getMediaCount()); - - SpannableStringBuilder span = new SpannableStringBuilder(getResources().getQuantityString(R.plurals.main_posts_count, - profileModel.getMediaCount() > 2000000000L - ? 2000000000 - : (int) profileModel.getMediaCount(), - postCount)); + + SpannableStringBuilder span = new SpannableStringBuilder(getResources().getQuantityString( + R.plurals.main_posts_count, + profileModel.getMediaCount() > 2000000000L ? 2000000000 : (int) profileModel.getMediaCount(), + postCount) + ); span.setSpan(new RelativeSizeSpan(1.2f), 0, postCount.length(), 0); span.setSpan(new StyleSpan(Typeface.BOLD), 0, postCount.length(), 0); profileDetailsBinding.mainPostCount.setText(span); @@ -778,9 +778,11 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final String followersCountStr = String.valueOf(followersCount); final int followersCountStrLen = followersCountStr.length(); - span = new SpannableStringBuilder(getResources().getQuantityString(R.plurals.main_posts_followers, + span = new SpannableStringBuilder(getResources().getQuantityString( + R.plurals.main_posts_followers, followersCount > 2000000000L ? 2000000000 : (int) followersCount, - followersCountStr)); + followersCountStr) + ); span.setSpan(new RelativeSizeSpan(1.2f), 0, followersCountStrLen, 0); span.setSpan(new StyleSpan(Typeface.BOLD), 0, followersCountStrLen, 0); profileDetailsBinding.mainFollowers.setText(span); @@ -788,15 +790,14 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final String followingCountStr = String.valueOf(followingCount); final int followingCountStrLen = followingCountStr.length(); - span = new SpannableStringBuilder(getString(R.string.main_posts_following, - followingCountStr)); + span = new SpannableStringBuilder(getString(R.string.main_posts_following, followingCountStr)); span.setSpan(new RelativeSizeSpan(1.2f), 0, followingCountStrLen, 0); span.setSpan(new StyleSpan(Typeface.BOLD), 0, followingCountStrLen, 0); profileDetailsBinding.mainFollowing.setText(span); profileDetailsBinding.mainFollowing.setVisibility(View.VISIBLE); profileDetailsBinding.mainFullName.setText(TextUtils.isEmpty(profileModel.getFullName()) ? profileModel.getUsername() - : profileModel.getFullName()); + : profileModel.getFullName()); final String biography = profileModel.getBiography(); if (TextUtils.isEmpty(biography)) { @@ -815,9 +816,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final String originalText = autoLinkItem.getOriginalText().trim(); navigateToProfile(originalText); }); - profileDetailsBinding.mainBiography.addOnEmailClickListener(autoLinkItem -> Utils.openEmailAddress(getContext(), - autoLinkItem.getOriginalText() - .trim())); + profileDetailsBinding.mainBiography.addOnEmailClickListener(autoLinkItem -> Utils.openEmailAddress( + getContext(), autoLinkItem.getOriginalText().trim())); profileDetailsBinding.mainBiography .addOnURLClickListener(autoLinkItem -> Utils.openURL(getContext(), autoLinkItem.getOriginalText().trim())); profileDetailsBinding.mainBiography.setOnLongClickListener(v -> { @@ -899,6 +899,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe return true; }); } + final MotionScene.Transition transition = root.getTransition(R.id.transition); if (!isReallyPrivate()) { if (isLoggedIn) { profileDetailsBinding.mainFollowing.setClickable(true); @@ -924,13 +925,13 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe binding.privatePage2.setVisibility(View.VISIBLE); binding.postsRecyclerView.setVisibility(View.GONE); binding.swipeRefreshLayout.setRefreshing(false); - root.getTransition(R.id.transition).setEnable(false); + if (transition != null) { + transition.setEnable(false); + } } - - if (profileModel.getMediaCount() == 0) { - root.getTransition(R.id.transition).setEnable(false); + if (profileModel.getMediaCount() == 0 && transition != null) { + transition.setEnable(false); } - } private void setupButtons(final long profileId) { @@ -1037,21 +1038,21 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } }); storiesService.fetchHighlights(profileId, - new ServiceCallback>() { - @Override - public void onSuccess(final List result) { - if (result != null) { - profileDetailsBinding.highlightsList.setVisibility(View.VISIBLE); - highlightsViewModel.getList().postValue(result); - } else profileDetailsBinding.highlightsList.setVisibility(View.GONE); - } + new ServiceCallback>() { + @Override + public void onSuccess(final List result) { + if (result != null) { + profileDetailsBinding.highlightsList.setVisibility(View.VISIBLE); + highlightsViewModel.getList().postValue(result); + } else profileDetailsBinding.highlightsList.setVisibility(View.GONE); + } - @Override - public void onFailure(final Throwable t) { - profileDetailsBinding.highlightsList.setVisibility(View.GONE); - Log.e(TAG, "Error", t); - } - }); + @Override + public void onFailure(final Throwable t) { + profileDetailsBinding.highlightsList.setVisibility(View.GONE); + Log.e(TAG, "Error", t); + } + }); } private void setupCommonListeners() { @@ -1116,14 +1117,14 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe }); profileDetailsBinding.btnLiked.setOnClickListener(v -> { final NavDirections action = ProfileFragmentDirections.actionProfileFragmentToSavedViewerFragment(profileModel.getUsername(), - profileModel.getPk(), - PostItemType.LIKED); + profileModel.getPk(), + PostItemType.LIKED); NavHostFragment.findNavController(this).navigate(action); }); profileDetailsBinding.btnTagged.setOnClickListener(v -> { final NavDirections action = ProfileFragmentDirections.actionProfileFragmentToSavedViewerFragment(profileModel.getUsername(), - profileModel.getPk(), - PostItemType.TAGGED); + profileModel.getPk(), + PostItemType.TAGGED); NavHostFragment.findNavController(this).navigate(action); }); if (!disableDm) { @@ -1162,7 +1163,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe // show stories final NavDirections action = ProfileFragmentDirections .actionProfileFragmentToStoryViewerFragment(StoryViewerOptions.forUser(profileModel.getPk(), - profileModel.getFullName())); + profileModel.getFullName())); NavHostFragment.findNavController(this).navigate(action); return; } @@ -1188,12 +1189,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe if (profileModel != null) { final FragmentManager fragmentManager = getParentFragmentManager(); final ProfilePicDialogFragment fragment = ProfilePicDialogFragment.getInstance(profileModel.getPk(), - username, - profileModel.getProfilePicUrl()); + username, + profileModel.getProfilePicUrl()); final FragmentTransaction ft = fragmentManager.beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) - .add(fragment, "profilePicDialog") - .commit(); + .add(fragment, "profilePicDialog") + .commit(); } } @@ -1206,13 +1207,13 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private void setupPosts() { binding.postsRecyclerView.setViewModelStoreOwner(this) - .setLifeCycleOwner(this) - .setPostFetchService(new ProfilePostFetchService(profileModel, isLoggedIn)) - .setLayoutPreferences(layoutPreferences) - .addFetchStatusChangeListener(fetching -> updateSwipeRefreshState()) - .setFeedItemCallback(feedItemCallback) - .setSelectionModeCallback(selectionModeCallback) - .init(); + .setLifeCycleOwner(this) + .setPostFetchService(new ProfilePostFetchService(profileModel, isLoggedIn)) + .setLayoutPreferences(layoutPreferences) + .addFetchStatusChangeListener(fetching -> updateSwipeRefreshState()) + .setFeedItemCallback(feedItemCallback) + .setSelectionModeCallback(selectionModeCallback) + .init(); binding.postsRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {