Enable support toolbar for ProfileActivity and added a couple of unit… (#5188)

* Enable support toolbar for ProfileActivity and added a couple of unit tests

* Nit fixes

* Nit Fixes

* Minor styling changes in ContributionsFragment
This commit is contained in:
Priyank Shankar 2023-04-03 12:32:16 +05:30 committed by GitHub
parent fc672dbd9b
commit 07616485ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 50 deletions

View file

@ -196,6 +196,10 @@ public class ContributionsFragment
@Override @Override
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
// Removing contributions menu items for ProfileActivity
if (getActivity() instanceof ProfileActivity) { return; }
inflater.inflate(R.menu.contribution_activity_notification_menu, menu); inflater.inflate(R.menu.contribution_activity_notification_menu, menu);
MenuItem notificationsMenuItem = menu.findItem(R.id.notifications); MenuItem notificationsMenuItem = menu.findItem(R.id.notifications);
@ -339,23 +343,23 @@ public class ContributionsFragment
if (fragment.isAdded() && otherFragment != null) { if (fragment.isAdded() && otherFragment != null) {
transaction.hide(otherFragment); transaction.hide(otherFragment);
transaction.show(fragment); transaction.show(fragment);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG); transaction.addToBackStack(tag);
transaction.commit(); transaction.commit();
getChildFragmentManager().executePendingTransactions(); getChildFragmentManager().executePendingTransactions();
} else if (fragment.isAdded() && otherFragment == null) { } else if (fragment.isAdded() && otherFragment == null) {
transaction.show(fragment); transaction.show(fragment);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG); transaction.addToBackStack(tag);
transaction.commit(); transaction.commit();
getChildFragmentManager().executePendingTransactions(); getChildFragmentManager().executePendingTransactions();
}else if (!fragment.isAdded() && otherFragment != null ) { }else if (!fragment.isAdded() && otherFragment != null ) {
transaction.hide(otherFragment); transaction.hide(otherFragment);
transaction.add(R.id.root_frame, fragment, tag); transaction.add(R.id.root_frame, fragment, tag);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG); transaction.addToBackStack(tag);
transaction.commit(); transaction.commit();
getChildFragmentManager().executePendingTransactions(); getChildFragmentManager().executePendingTransactions();
} else if (!fragment.isAdded()) { } else if (!fragment.isAdded()) {
transaction.replace(R.id.root_frame, fragment, tag); transaction.replace(R.id.root_frame, fragment, tag);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG); transaction.addToBackStack(tag);
transaction.commit(); transaction.commit();
getChildFragmentManager().executePendingTransactions(); getChildFragmentManager().executePendingTransactions();
} }
@ -641,7 +645,7 @@ public class ContributionsFragment
@Override @Override
public void showDetail(int position, boolean isWikipediaButtonDisplayed) { public void showDetail(int position, boolean isWikipediaButtonDisplayed) {
if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) { if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) {
mediaDetailPagerFragment = new MediaDetailPagerFragment(); mediaDetailPagerFragment = new MediaDetailPagerFragment(false, true);
if(isUserProfile) { if(isUserProfile) {
((ProfileActivity)getActivity()).setScroll(false); ((ProfileActivity)getActivity()).setScroll(false);
} }

View file

@ -78,10 +78,7 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
return removedItems; return removedItems;
} }
public MediaDetailPagerFragment() { private MediaDetailPagerFragment() {}; // Constructor calls made to be explicit
this(false, false);
}
@SuppressLint("ValidFragment") @SuppressLint("ValidFragment")
public MediaDetailPagerFragment(Boolean editable, boolean isFeaturedImage) { public MediaDetailPagerFragment(Boolean editable, boolean isFeaturedImage) {
this.editable = editable; this.editable = editable;
@ -107,11 +104,13 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
adapter = new MediaDetailAdapter(getChildFragmentManager()); adapter = new MediaDetailAdapter(getChildFragmentManager());
if (getActivity() != null) { // ActionBar is now supported in both activities - if this crashes something is quite wrong
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) { if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
} }
else {
throw new AssertionError("Action bar should not be null!");
} }
// If fragment is associated with ProfileActivity, then hide the tabLayout // If fragment is associated with ProfileActivity, then hide the tabLayout
@ -119,6 +118,11 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
((ProfileActivity)getActivity()).tabLayout.setVisibility(View.GONE); ((ProfileActivity)getActivity()).tabLayout.setVisibility(View.GONE);
} }
// Else if fragment is associated with MainActivity then hide that tab layout
else if (getActivity() instanceof MainActivity) {
((MainActivity)getActivity()).hideTabs();
}
pager.setAdapter(adapter); pager.setAdapter(adapter);
if (savedInstanceState != null) { if (savedInstanceState != null) {
@ -127,9 +131,7 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();
} }
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
if (getActivity() instanceof MainActivity) {
((MainActivity)getActivity()).hideTabs();
}
return view; return view;
} }

View file

@ -13,6 +13,7 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -50,6 +51,9 @@ public class ProfileActivity extends BaseActivity {
@BindView(R.id.tab_layout) @BindView(R.id.tab_layout)
public TabLayout tabLayout; public TabLayout tabLayout;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Inject @Inject
SessionManager sessionManager; SessionManager sessionManager;
@ -83,9 +87,14 @@ public class ProfileActivity extends BaseActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile); setContentView(R.layout.activity_profile);
ButterKnife.bind(this); ButterKnife.bind(this);
setTitle(sessionManager.getUserName());
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(view -> {
onSupportNavigateUp();
});
userName = getIntent().getStringExtra(KEY_USERNAME); userName = getIntent().getStringExtra(KEY_USERNAME);
setTitle(userName);
shouldShowContributions = getIntent().getBooleanExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, false); shouldShowContributions = getIntent().getBooleanExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, false);
supportFragmentManager = getSupportFragmentManager(); supportFragmentManager = getSupportFragmentManager();
@ -140,14 +149,13 @@ public class ProfileActivity extends BaseActivity {
fragmentList.add(leaderboardFragment); fragmentList.add(leaderboardFragment);
titleList.add(getResources().getString(R.string.leaderboard_tab_title).toUpperCase()); titleList.add(getResources().getString(R.string.leaderboard_tab_title).toUpperCase());
if (shouldShowContributions) { contributionsFragment = new ContributionsFragment();
contributionsFragment = new ContributionsFragment(); Bundle contributionsListBundle = new Bundle();
Bundle contributionsListBundle = new Bundle(); contributionsListBundle.putString(KEY_USERNAME, userName);
contributionsListBundle.putString(KEY_USERNAME, userName); contributionsFragment.setArguments(contributionsListBundle);
contributionsFragment.setArguments(contributionsListBundle); fragmentList.add(contributionsFragment);
fragmentList.add(contributionsFragment); titleList.add(getString(R.string.contributions_fragment).toUpperCase());
titleList.add(getString(R.string.contributions_fragment).toUpperCase());
}
viewPagerAdapter.setTabData(fragmentList, titleList); viewPagerAdapter.setTabData(fragmentList, titleList);
viewPagerAdapter.notifyDataSetChanged(); viewPagerAdapter.notifyDataSetChanged();

View file

@ -5,33 +5,44 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<RelativeLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout <include
android:id="@+id/toolbar_layout" layout="@layout/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content" />
android:background="?attr/mainBackground">
<com.google.android.material.tabs.TabLayout <RelativeLayout
android:id="@+id/tab_layout" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/toolbar" android:background="?attr/mainBackground">
android:background="?attr/tabBackground"
app:tabIndicatorColor="?attr/tabIndicatorColor"
app:tabMode="fixed"
app:tabSelectedTextColor="?attr/tabSelectedTextColor"
app:tabTextColor="?attr/tabTextColor" />
</com.google.android.material.appbar.AppBarLayout>
<fr.free.nrw.commons.explore.ParentViewPager <com.google.android.material.tabs.TabLayout
android:id="@+id/viewPager" android:id="@+id/tab_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:layout_below="@id/toolbar_layout" /> android:layout_below="@id/toolbar"
</RelativeLayout> android:background="?attr/tabBackground"
app:tabIndicatorColor="?attr/tabIndicatorColor"
app:tabMode="fixed"
app:tabSelectedTextColor="?attr/tabSelectedTextColor"
app:tabTextColor="?attr/tabTextColor" />
</com.google.android.material.appbar.AppBarLayout>
<fr.free.nrw.commons.explore.ParentViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar_layout" />
</RelativeLayout>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout> </androidx.drawerlayout.widget.DrawerLayout>

View file

@ -48,13 +48,21 @@
android:text="@string/limited_connection_explanation"/> android:text="@string/limited_connection_explanation"/>
</LinearLayout> </LinearLayout>
<FrameLayout
android:id="@+id/explore_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="visible">
<FrameLayout <FrameLayout
android:id="@+id/root_frame" android:id="@+id/root_frame"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="@dimen/miniscule_margin" android:layout_marginTop="@dimen/miniscule_margin"
android:background="#000" android:background="#000">
>
</FrameLayout>
</FrameLayout> </FrameLayout>
</LinearLayout> </LinearLayout>

View file

@ -67,7 +67,7 @@ class MediaDetailPagerFragmentUnitTests {
val activity = Robolectric.buildActivity(SearchActivity::class.java).create().get() val activity = Robolectric.buildActivity(SearchActivity::class.java).create().get()
fragment = MediaDetailPagerFragment() fragment = MediaDetailPagerFragment(false, true)
fragment = MediaDetailPagerFragment(false, false) fragment = MediaDetailPagerFragment(false, false)
fragment = MediaDetailPagerFragment(false, false, 0) fragment = MediaDetailPagerFragment(false, false, 0)
fragmentManager = activity.supportFragmentManager fragmentManager = activity.supportFragmentManager

View file

@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.os.Looper import android.os.Looper
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.widget.Toolbar
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import fr.free.nrw.commons.R import fr.free.nrw.commons.R
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
@ -106,4 +107,17 @@ class ProfileActivityTest {
fun testOnSupportNavigateUp() { fun testOnSupportNavigateUp() {
activity.onSupportNavigateUp() activity.onSupportNavigateUp()
} }
@Test
fun testToolbarNotNull() {
val toolbar = activity.findViewById<Toolbar>(R.id.toolbar)
Assert.assertNotNull(toolbar)
}
@Test
fun testOptionsMenu() {
val menu: Menu = RoboMenu(mockContext)
activity.onCreateOptionsMenu(menu)
Assert.assertEquals(1, menu.size())
}
} }