Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishkumar468 authored and neslihanturan committed Nov 25, 2019
1 parent 9b15a32 commit 3f0c3bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fr.free.nrw.commons.contributions;

import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
import static fr.free.nrw.commons.contributions.MainActivity.CONTRIBUTIONS_TAB_POSITION;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
Expand All @@ -13,19 +17,12 @@
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentManager.OnBackStackChangedListener;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.HandlerService;
Expand Down Expand Up @@ -59,12 +56,11 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;

import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
import static fr.free.nrw.commons.contributions.MainActivity.CONTRIBUTIONS_TAB_POSITION;
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;

public class ContributionsFragment
extends CommonsDaggerSupportFragment
implements
Expand Down Expand Up @@ -259,10 +255,9 @@ public void onAttach(Context context) {
operations on first time fragment attached to an activity. Then they will be retained
until fragment life time ends.
*/
if (!isFragmentAttachedBefore) {
onAuthCookieAcquired(((MainActivity)getActivity()).uploadServiceIntent);
if (!isFragmentAttachedBefore && getActivity() != null) {
onAuthCookieAcquired();
isFragmentAttachedBefore = true;

}
}

Expand Down Expand Up @@ -308,19 +303,24 @@ public void onBackStackChanged() {

/**
* Called when onAuthCookieAcquired is called on authenticated parent activity
* @param uploadServiceIntent
*/
void onAuthCookieAcquired(Intent uploadServiceIntent) {
void onAuthCookieAcquired() {
// Since we call onAuthCookieAcquired method from onAttach, isAdded is still false. So don't use it

if (getActivity() != null) { // If fragment is attached to parent activity
getActivity().bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE);
getActivity().bindService(getUploadServiceIntent(), uploadServiceConnection, Context.BIND_AUTO_CREATE);
isUploadServiceConnected = true;
getActivity().getSupportLoaderManager().initLoader(0, null, contributionsPresenter);
}

}

public Intent getUploadServiceIntent(){
Intent intent = new Intent(getActivity(), UploadService.class);
intent.setAction(UploadService.ACTION_START_SERVICE);
return intent;
}

/**
* Replace whatever is in the current contributionsFragmentContainer view with
* mediaDetailPagerFragment, and preserve previous state in back stack.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
package fr.free.nrw.commons.contributions;

import static android.content.ContentResolver.requestSync;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.google.android.material.tabs.TabLayout;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager;
Expand All @@ -45,10 +39,10 @@
import fr.free.nrw.commons.upload.UploadService;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import javax.inject.Inject;
import timber.log.Timber;

import static android.content.ContentResolver.requestSync;

public class MainActivity extends NavigationBaseActivity implements FragmentManager.OnBackStackChangedListener {

@BindView(R.id.tab_layout)
Expand All @@ -68,8 +62,6 @@ public class MainActivity extends NavigationBaseActivity implements FragmentMana
QuizChecker quizChecker;


public Intent uploadServiceIntent;

public ContributionsActivityPagerAdapter contributionsActivityPagerAdapter;
public static final int CONTRIBUTIONS_TAB_POSITION = 0;
public static final int NEARBY_TAB_POSITION = 1;
Expand Down Expand Up @@ -117,13 +109,13 @@ private void initMain() {
//Do not remove this, this triggers the sync service
ContentResolver.setSyncAutomatically(sessionManager.getCurrentAccount(),BuildConfig.CONTRIBUTION_AUTHORITY,true);
requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle());
uploadServiceIntent = new Intent(this, UploadService.class);
Intent uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent);

addTabsAndFragments();
if (contributionsActivityPagerAdapter.getItem(0) != null) {
((ContributionsFragment)contributionsActivityPagerAdapter.getItem(0)).onAuthCookieAcquired(uploadServiceIntent);
((ContributionsFragment)contributionsActivityPagerAdapter.getItem(0)).onAuthCookieAcquired();
}
}

Expand All @@ -147,10 +139,8 @@ private void addTabsAndFragments() {
.show()
);

if (uploadServiceIntent != null) {
// If auth cookie already acquired notify contrib fragment so that it san operate auth required actions
((ContributionsFragment)contributionsActivityPagerAdapter.getItem(CONTRIBUTIONS_TAB_POSITION)).onAuthCookieAcquired(uploadServiceIntent);
}
((ContributionsFragment) contributionsActivityPagerAdapter
.getItem(CONTRIBUTIONS_TAB_POSITION)).onAuthCookieAcquired();
setTabAndViewPagerSynchronisation();
}

Expand Down

0 comments on commit 3f0c3bb

Please sign in to comment.