Move Contributions Fragment related codes to test activity

This commit is contained in:
neslihanturan 2019-08-29 10:26:05 +03:00
parent ef0472b4fb
commit eeb03bf5c2
4 changed files with 88 additions and 22 deletions

View file

@ -49,6 +49,7 @@ import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.explore.categories.ExploreActivity; import fr.free.nrw.commons.explore.categories.ExploreActivity;
import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.nearby.NearbyTestFragmentLayersActivity;
import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.utils.ConfigUtils; import fr.free.nrw.commons.utils.ConfigUtils;
import fr.free.nrw.commons.utils.ViewUtil; import fr.free.nrw.commons.utils.ViewUtil;
@ -451,7 +452,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
} }
public void startMainActivity() { public void startMainActivity() {
NavigationBaseActivity.startActivityWithFlags(this, MainActivity.class, Intent.FLAG_ACTIVITY_SINGLE_TOP); NavigationBaseActivity.startActivityWithFlags(this, NearbyTestFragmentLayersActivity.class, Intent.FLAG_ACTIVITY_SINGLE_TOP);
finish(); finish();
} }

View file

@ -45,6 +45,7 @@ import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import fr.free.nrw.commons.nearby.NearbyController; import fr.free.nrw.commons.nearby.NearbyController;
import fr.free.nrw.commons.nearby.NearbyNotificationCardView; import fr.free.nrw.commons.nearby.NearbyNotificationCardView;
import fr.free.nrw.commons.nearby.NearbyTestFragmentLayersActivity;
import fr.free.nrw.commons.nearby.Place; import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.settings.Prefs; import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.upload.UploadService; import fr.free.nrw.commons.upload.UploadService;
@ -257,8 +258,8 @@ public class ContributionsFragment
operations on first time fragment attached to an activity. Then they will be retained operations on first time fragment attached to an activity. Then they will be retained
until fragment life time ends. until fragment life time ends.
*/ */
if (((MainActivity)getActivity()).isAuthCookieAcquired && !isFragmentAttachedBefore) { if (((NearbyTestFragmentLayersActivity)getActivity()).isAuthCookieAcquired && !isFragmentAttachedBefore) {
onAuthCookieAcquired(((MainActivity)getActivity()).uploadServiceIntent); onAuthCookieAcquired(((NearbyTestFragmentLayersActivity)getActivity()).uploadServiceIntent);
isFragmentAttachedBefore = true; isFragmentAttachedBefore = true;
} }
@ -270,7 +271,7 @@ public class ContributionsFragment
*/ */
public void showContributionsListFragment() { public void showContributionsListFragment() {
// show tabs on contribution list is visible // show tabs on contribution list is visible
((MainActivity) getActivity()).showTabs(); ((NearbyTestFragmentLayersActivity) getActivity()).showTabs();
// show nearby card view on contributions list is visible // show nearby card view on contributions list is visible
if (nearbyNotificationCardView != null) { if (nearbyNotificationCardView != null) {
if (store.getBoolean("displayNearbyCardView", true)) { if (store.getBoolean("displayNearbyCardView", true)) {
@ -291,7 +292,7 @@ public class ContributionsFragment
*/ */
public void showMediaDetailPagerFragment() { public void showMediaDetailPagerFragment() {
// hide tabs on media detail view is visible // hide tabs on media detail view is visible
((MainActivity)getActivity()).hideTabs(); ((NearbyTestFragmentLayersActivity)getActivity()).hideTabs();
// hide nearby card view on media detail is visible // hide nearby card view on media detail is visible
nearbyNotificationCardView.setVisibility(View.GONE); nearbyNotificationCardView.setVisibility(View.GONE);
@ -301,7 +302,7 @@ public class ContributionsFragment
@Override @Override
public void onBackStackChanged() { public void onBackStackChanged() {
((MainActivity)getActivity()).initBackButton(); ((NearbyTestFragmentLayersActivity)getActivity()).initBackButton();
} }
/** /**
@ -351,7 +352,7 @@ public class ContributionsFragment
private void setUploadCount() { private void setUploadCount() {
compositeDisposable.add(okHttpJsonApiClient compositeDisposable.add(okHttpJsonApiClient
.getUploadCount(((MainActivity)getActivity()).sessionManager.getCurrentAccount().name) .getUploadCount(((NearbyTestFragmentLayersActivity)getActivity()).sessionManager.getCurrentAccount().name)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::displayUploadCount, .subscribe(this::displayUploadCount,
@ -365,7 +366,7 @@ public class ContributionsFragment
return; return;
} }
((MainActivity)getActivity()).setNumOfUploads(uploadCount); ((NearbyTestFragmentLayersActivity)getActivity()).setNumOfUploads(uploadCount);
} }
@ -420,7 +421,7 @@ public class ContributionsFragment
onLocationPermissionGranted(); onLocationPermissionGranted();
} else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) } else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
&& store.getBoolean("displayLocationPermissionForCardView", true) && store.getBoolean("displayLocationPermissionForCardView", true)
&& (((MainActivity) getActivity()).viewPager.getCurrentItem() == CONTRIBUTIONS_TAB_POSITION)) { && (((NearbyTestFragmentLayersActivity) getActivity()).viewPager.getCurrentItem() == CONTRIBUTIONS_TAB_POSITION)) {
nearbyNotificationCardView.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION; nearbyNotificationCardView.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION;
showNearbyCardPermissionRationale(); showNearbyCardPermissionRationale();
} }

View file

@ -73,17 +73,17 @@ public class NearbyNotificationCardView extends SwipableCardView {
@Override @Override
protected void onAttachedToWindow() { protected void onAttachedToWindow() {
super.onAttachedToWindow(); super.onAttachedToWindow();
// If you don't setVisibility after getting layout params, then you will se an empty space in place of nearby NotificationCardView /* // If you don't setVisibility after getting layout params, then you will se an empty space in place of nearby NotificationCardView
if (((MainActivity)getContext()).defaultKvStore.getBoolean("displayNearbyCardView", true) && this.cardViewVisibilityState == NearbyNotificationCardView.CardViewVisibilityState.READY) { if (((MainActivity)getContext()).defaultKvStore.getBoolean("displayNearbyCardView", true) && this.cardViewVisibilityState == NearbyNotificationCardView.CardViewVisibilityState.READY) {
this.setVisibility(VISIBLE); this.setVisibility(VISIBLE);
} else { } else {
this.setVisibility(GONE); this.setVisibility(GONE);
} }*/
} }
private void setActionListeners(Place place) { private void setActionListeners(Place place) {
this.setOnClickListener(view -> { /* this.setOnClickListener(view -> {
MainActivity m = (MainActivity) getContext(); MainActivity m = (MainActivity) getContext();
// Change to nearby tab // Change to nearby tab
@ -91,16 +91,16 @@ public class NearbyNotificationCardView extends SwipableCardView {
// Center the map to the place // Center the map to the place
//((NearbyFragment) m.contributionsActivityPagerAdapter.getItem(NEARBY_TAB_POSITION)).centerMapToPlace(place); //((NearbyFragment) m.contributionsActivityPagerAdapter.getItem(NEARBY_TAB_POSITION)).centerMapToPlace(place);
}); });*/
} }
@Override public boolean onSwipe(View view) { @Override public boolean onSwipe(View view) {
view.setVisibility(GONE); /* view.setVisibility(GONE);
// Save shared preference for nearby card view accordingly // Save shared preference for nearby card view accordingly
((MainActivity) getContext()).defaultKvStore.putBoolean("displayNearbyCardView", false); ((MainActivity) getContext()).defaultKvStore.putBoolean("displayNearbyCardView", false);
ViewUtil.showLongToast(getContext(), ViewUtil.showLongToast(getContext(),
getResources().getString(R.string.nearby_notification_dismiss_message)); getResources().getString(R.string.nearby_notification_dismiss_message));
return true; */return true;
} }
/** /**

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.nearby; package fr.free.nrw.commons.nearby;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -11,32 +12,71 @@ import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.contributions.ContributionsFragment;
import fr.free.nrw.commons.contributions.UnswipableViewPager; import fr.free.nrw.commons.contributions.UnswipableViewPager;
import fr.free.nrw.commons.theme.NavigationBaseActivity; import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.upload.UploadService;
import timber.log.Timber; import timber.log.Timber;
public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity { import static android.content.ContentResolver.requestSync;
public class NearbyTestFragmentLayersActivity extends AuthenticatedActivity {
@Inject
public SessionManager sessionManager;
@BindView(R.id.tab_layout) @BindView(R.id.tab_layout)
TabLayout tabLayout; TabLayout tabLayout;
@BindView(R.id.pager) @BindView(R.id.pager)
UnswipableViewPager viewPager; public UnswipableViewPager viewPager;
public static final int CONTRIBUTIONS_TAB_POSITION = 0; public static final int CONTRIBUTIONS_TAB_POSITION = 0;
public static final int NEARBY_TAB_POSITION = 1; public static final int NEARBY_TAB_POSITION = 1;
public Intent uploadServiceIntent;
public boolean isAuthCookieAcquired = false;
public ContributionsActivityPagerAdapter contributionsActivityPagerAdapter;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_test_fragment_layers); setContentView(R.layout.activity_nearby_test_fragment_layers);
ButterKnife.bind(this); ButterKnife.bind(this);
requestAuthToken();
initDrawer();
setTitle(getString(R.string.navigation_item_home)+"2"); // Should I create a new string variable with another name instead?
}
@Override
protected void onAuthCookieAcquired(String authCookie) {
// Do a sync everytime we get here!
requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle());
uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent);
addTabsAndFragments(); addTabsAndFragments();
isAuthCookieAcquired = true;
if (contributionsActivityPagerAdapter.getItem(0) != null) {
((ContributionsFragment)contributionsActivityPagerAdapter.getItem(0)).onAuthCookieAcquired(uploadServiceIntent);
}
}
@Override
protected void onAuthFailure() {
} }
private void addTabsAndFragments() { private void addTabsAndFragments() {
ContributionsActivityPagerAdapter contributionsActivityPagerAdapter = new ContributionsActivityPagerAdapter(getSupportFragmentManager()); contributionsActivityPagerAdapter = new ContributionsActivityPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(contributionsActivityPagerAdapter); viewPager.setAdapter(contributionsActivityPagerAdapter);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.contributions_fragment))); tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.contributions_fragment)));
@ -125,12 +165,12 @@ public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity {
public Fragment getItem(int position) { public Fragment getItem(int position) {
switch (position){ switch (position){
case 0: case 0:
NearbyTestLayersFragment retainedContributionsFragment = getContributionsFragment(0); ContributionsFragment retainedContributionsFragment = getContributionsFragment(0);
if (retainedContributionsFragment != null) { if (retainedContributionsFragment != null) {
return retainedContributionsFragment; return retainedContributionsFragment;
} else { } else {
// If we reach here, retainedContributionsFragment is null // If we reach here, retainedContributionsFragment is null
return new NearbyTestLayersFragment(); return new ContributionsFragment();
} }
@ -152,9 +192,9 @@ public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity {
* @param position index of tabs, in our case 0 or 1 * @param position index of tabs, in our case 0 or 1
* @return * @return
*/ */
private NearbyTestLayersFragment getContributionsFragment(int position) { private ContributionsFragment getContributionsFragment(int position) {
String tag = makeFragmentName(R.id.pager, position); String tag = makeFragmentName(R.id.pager, position);
return (NearbyTestLayersFragment)fragmentManager.findFragmentByTag(tag); return (ContributionsFragment)fragmentManager.findFragmentByTag(tag);
} }
/** /**
@ -180,4 +220,28 @@ public class NearbyTestFragmentLayersActivity extends NavigationBaseActivity {
} }
public void hideTabs() {
changeDrawerIconToBackButton();
if (tabLayout != null) {
tabLayout.setVisibility(View.GONE);
}
}
public void showTabs() {
changeDrawerIconToDefault();
if (tabLayout != null) {
tabLayout.setVisibility(View.VISIBLE);
}
}
/**
* Adds number of uploads next to tab text "Contributions" then it will look like
* "Contributions (NUMBER)"
* @param uploadCount
*/
public void setNumOfUploads(int uploadCount) {
tabLayout.getTabAt(0).setText(getResources().getString(R.string.contributions_fragment) +" "+ getResources()
.getQuantityString(R.plurals.contributions_subtitle,
uploadCount, uploadCount));
}
} }