Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -196,6 +196,10 @@ private void initWLMCampaign() {

@Override
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {

// Removing contributions menu items for ProfileActivity
if (getActivity() instanceof ProfileActivity) { return;}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space before }


inflater.inflate(R.menu.contribution_activity_notification_menu, menu);

MenuItem notificationsMenuItem = menu.findItem(R.id.notifications);
Expand Down Expand Up @@ -339,23 +343,23 @@ private void showFragment(Fragment fragment, String tag, Fragment otherFragment)
if (fragment.isAdded() && otherFragment != null) {
transaction.hide(otherFragment);
transaction.show(fragment);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG);
transaction.addToBackStack(tag);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
} else if (fragment.isAdded() && otherFragment == null) {
transaction.show(fragment);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG);
transaction.addToBackStack(tag);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}else if (!fragment.isAdded() && otherFragment != null ) {
transaction.hide(otherFragment);
transaction.add(R.id.root_frame, fragment, tag);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG);
transaction.addToBackStack(tag);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
} else if (!fragment.isAdded()) {
transaction.replace(R.id.root_frame, fragment, tag);
transaction.addToBackStack(CONTRIBUTION_LIST_FRAGMENT_TAG);
transaction.addToBackStack(tag);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
Expand Down Expand Up @@ -641,7 +645,7 @@ public void viewPagerNotifyDataSetChanged() {
@Override
public void showDetail(int position, boolean isWikipediaButtonDisplayed) {
if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) {
mediaDetailPagerFragment = new MediaDetailPagerFragment();
mediaDetailPagerFragment = new MediaDetailPagerFragment(false, true);
if(isUserProfile) {
((ProfileActivity)getActivity()).setScroll(false);
}
Expand Down
Expand Up @@ -78,10 +78,7 @@ public ArrayList<Integer> getRemovedItems() {
return removedItems;
}

public MediaDetailPagerFragment() {
this(false, false);
}

private MediaDetailPagerFragment() {}; // Constructor calls made to be explicit
@SuppressLint("ValidFragment")
public MediaDetailPagerFragment(Boolean editable, boolean isFeaturedImage) {
this.editable = editable;
Expand All @@ -107,18 +104,25 @@ public View onCreateView(LayoutInflater inflater,

adapter = new MediaDetailAdapter(getChildFragmentManager());

if (getActivity() != null) {
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// ActionBar is now supported in both activities - if this crashes something is quite wrong
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
else {
throw new AssertionError("Action bar should not be null!");
}

// If fragment is associated with ProfileActivity, then hide the tabLayout
if (getActivity() instanceof ProfileActivity) {
((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);

if (savedInstanceState != null) {
Expand All @@ -127,9 +131,7 @@ public View onCreateView(LayoutInflater inflater,
getActivity().invalidateOptionsMenu();
}
adapter.notifyDataSetChanged();
if (getActivity() instanceof MainActivity) {
((MainActivity)getActivity()).hideTabs();
}

return view;
}

Expand Down
26 changes: 17 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java
Expand Up @@ -13,6 +13,7 @@
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class ProfileActivity extends BaseActivity {
@BindView(R.id.tab_layout)
public TabLayout tabLayout;

@BindView(R.id.toolbar)
Toolbar toolbar;

@Inject
SessionManager sessionManager;

Expand Down Expand Up @@ -83,9 +87,14 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ButterKnife.bind(this);
setTitle(sessionManager.getUserName());

setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(view -> {
onSupportNavigateUp();
});

userName = getIntent().getStringExtra(KEY_USERNAME);
setTitle(userName);
shouldShowContributions = getIntent().getBooleanExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, false);

supportFragmentManager = getSupportFragmentManager();
Expand Down Expand Up @@ -140,14 +149,13 @@ private void setTabs() {
fragmentList.add(leaderboardFragment);
titleList.add(getResources().getString(R.string.leaderboard_tab_title).toUpperCase());

if (shouldShowContributions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past, why was this if (shouldShowContributions) { added? You can use git blame to find out.

contributionsFragment = new ContributionsFragment();
Bundle contributionsListBundle = new Bundle();
contributionsListBundle.putString(KEY_USERNAME, userName);
contributionsFragment.setArguments(contributionsListBundle);
fragmentList.add(contributionsFragment);
titleList.add(getString(R.string.contributions_fragment).toUpperCase());
}
contributionsFragment = new ContributionsFragment();
Bundle contributionsListBundle = new Bundle();
contributionsListBundle.putString(KEY_USERNAME, userName);
contributionsFragment.setArguments(contributionsListBundle);
fragmentList.add(contributionsFragment);
titleList.add(getString(R.string.contributions_fragment).toUpperCase());

viewPagerAdapter.setTabData(fragmentList, titleList);
viewPagerAdapter.notifyDataSetChanged();

Expand Down
53 changes: 32 additions & 21 deletions app/src/main/res/layout/activity_profile.xml
Expand Up @@ -5,33 +5,44 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/toolbar_layout"
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/mainBackground">
android:layout_height="wrap_content" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
<RelativeLayout
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_height="wrap_content"
android:layout_below="@id/toolbar"
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>
android:background="?attr/mainBackground">

<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>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
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>
12 changes: 10 additions & 2 deletions app/src/main/res/layout/fragment_contributions.xml
Expand Up @@ -48,13 +48,21 @@
android:text="@string/limited_connection_explanation"/>
</LinearLayout>

<FrameLayout
android:id="@+id/explore_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="visible">

<FrameLayout
android:id="@+id/root_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/miniscule_margin"
android:background="#000"
>
android:background="#000">

</FrameLayout>
</FrameLayout>

</LinearLayout>
Expand Up @@ -67,7 +67,7 @@ class MediaDetailPagerFragmentUnitTests {

val activity = Robolectric.buildActivity(SearchActivity::class.java).create().get()

fragment = MediaDetailPagerFragment()
fragment = MediaDetailPagerFragment(false, true)
fragment = MediaDetailPagerFragment(false, false)
fragment = MediaDetailPagerFragment(false, false, 0)
fragmentManager = activity.supportFragmentManager
Expand Down
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.os.Looper
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.Toolbar
import androidx.test.core.app.ApplicationProvider
import fr.free.nrw.commons.R
import fr.free.nrw.commons.TestCommonsApplication
Expand Down Expand Up @@ -106,4 +107,17 @@ class ProfileActivityTest {
fun testOnSupportNavigateUp() {
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())
}

}