mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-30 22:34:02 +01:00 
			
		
		
		
	Delete Contributions Activity content to rewrite it
This commit is contained in:
		
							parent
							
								
									78f83230ee
								
							
						
					
					
						commit
						48a6c7a78b
					
				
					 3 changed files with 8 additions and 370 deletions
				
			
		|  | @ -1,379 +1,17 @@ | |||
| package fr.free.nrw.commons.contributions; | ||||
| 
 | ||||
| import android.accounts.Account; | ||||
| import android.content.ComponentName; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.content.ServiceConnection; | ||||
| import android.content.SharedPreferences; | ||||
| import android.database.Cursor; | ||||
| import android.database.DataSetObserver; | ||||
| import android.os.Bundle; | ||||
| import android.os.IBinder; | ||||
| import android.support.v4.app.FragmentManager; | ||||
| import android.support.v4.app.LoaderManager; | ||||
| import android.support.v4.content.CursorLoader; | ||||
| import android.support.v4.content.Loader; | ||||
| import android.support.v4.widget.CursorAdapter; | ||||
| import android.view.Menu; | ||||
| import android.view.MenuItem; | ||||
| import android.view.View; | ||||
| import android.widget.Adapter; | ||||
| import android.widget.AdapterView; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import javax.inject.Inject; | ||||
| import javax.inject.Named; | ||||
| 
 | ||||
| import butterknife.ButterKnife; | ||||
| import fr.free.nrw.commons.BuildConfig; | ||||
| import fr.free.nrw.commons.HandlerService; | ||||
| import fr.free.nrw.commons.Media; | ||||
| 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.media.MediaDetailPagerFragment; | ||||
| import fr.free.nrw.commons.mwapi.MediaWikiApi; | ||||
| import fr.free.nrw.commons.quiz.QuizChecker; | ||||
| import fr.free.nrw.commons.settings.Prefs; | ||||
| import fr.free.nrw.commons.upload.UploadService; | ||||
| import fr.free.nrw.commons.utils.ContributionUtils; | ||||
| import fr.free.nrw.commons.utils.ViewUtil; | ||||
| import io.reactivex.android.schedulers.AndroidSchedulers; | ||||
| import io.reactivex.disposables.CompositeDisposable; | ||||
| import io.reactivex.schedulers.Schedulers; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| import static android.content.ContentResolver.requestSync; | ||||
| import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED; | ||||
| import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS; | ||||
| import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI; | ||||
| import static fr.free.nrw.commons.settings.Prefs.UPLOADS_SHOWING; | ||||
| 
 | ||||
| public  class       ContributionsActivity | ||||
|         extends     AuthenticatedActivity | ||||
|         implements  LoaderManager.LoaderCallbacks<Cursor>, | ||||
|                     AdapterView.OnItemClickListener, | ||||
|                     MediaDetailPagerFragment.MediaDetailProvider, | ||||
|                     FragmentManager.OnBackStackChangedListener, | ||||
|                     ContributionsListFragment.SourceRefresher { | ||||
|         extends     AuthenticatedActivity { | ||||
| 
 | ||||
|     @Inject MediaWikiApi mediaWikiApi; | ||||
|     @Inject SessionManager sessionManager; | ||||
|     @Inject @Named("default_preferences") SharedPreferences prefs; | ||||
|     @Inject ContributionDao contributionDao; | ||||
| 
 | ||||
|     private Cursor allContributions; | ||||
|     private ContributionsListFragment contributionsList; | ||||
|     private MediaDetailPagerFragment mediaDetails; | ||||
|     private UploadService uploadService; | ||||
|     private boolean isUploadServiceConnected; | ||||
|     private ArrayList<DataSetObserver> observersWaitingForLoad = new ArrayList<>(); | ||||
| 
 | ||||
|     private CompositeDisposable compositeDisposable = new CompositeDisposable(); | ||||
| 
 | ||||
|     private ServiceConnection uploadServiceConnection = new ServiceConnection() { | ||||
|         @Override | ||||
|         public void onServiceConnected(ComponentName componentName, IBinder binder) { | ||||
|             uploadService = (UploadService) ((HandlerService.HandlerServiceLocalBinder) binder) | ||||
|                     .getService(); | ||||
|             isUploadServiceConnected = true; | ||||
|         protected void onAuthCookieAcquired(String authCookie) { | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public void onServiceDisconnected(ComponentName componentName) { | ||||
|             // this should never happen | ||||
|             Timber.e(new RuntimeException("UploadService died but the rest of the process did not!")); | ||||
|         protected void onAuthFailure() { | ||||
| 
 | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onDestroy() { | ||||
|         compositeDisposable.clear(); | ||||
|         getSupportFragmentManager().removeOnBackStackChangedListener(this); | ||||
|         super.onDestroy(); | ||||
|         if (isUploadServiceConnected) { | ||||
|             unbindService(uploadServiceConnection); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onResume() { | ||||
|         super.onResume(); | ||||
|         boolean isSettingsChanged = prefs.getBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, false); | ||||
|         prefs.edit().putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, false).apply(); | ||||
|         if (isSettingsChanged) { | ||||
|             refreshSource(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onAuthCookieAcquired(String authCookie) { | ||||
|         // Do a sync everytime we get here! | ||||
|         requestSync(sessionManager.getCurrentAccount(), BuildConfig.CONTRIBUTION_AUTHORITY, new Bundle()); | ||||
|         Intent uploadServiceIntent = new Intent(this, UploadService.class); | ||||
|         uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE); | ||||
|         startService(uploadServiceIntent); | ||||
|         bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE); | ||||
| 
 | ||||
|         allContributions = contributionDao.loadAllContributions(); | ||||
| 
 | ||||
|         getSupportLoaderManager().initLoader(0, null, this); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
|         super.onCreate(savedInstanceState); | ||||
|         setContentView(R.layout.activity_contributions); | ||||
|         ButterKnife.bind(this); | ||||
| 
 | ||||
|         // Activity can call methods in the fragment by acquiring a | ||||
|         // reference to the Fragment from FragmentManager, using findFragmentById() | ||||
|         FragmentManager supportFragmentManager = getSupportFragmentManager(); | ||||
|         contributionsList = (ContributionsListFragment)supportFragmentManager | ||||
|                 .findFragmentById(R.id.contributionsListFragment); | ||||
| 
 | ||||
|         supportFragmentManager.addOnBackStackChangedListener(this); | ||||
|         if (savedInstanceState != null) { | ||||
|             mediaDetails = (MediaDetailPagerFragment)supportFragmentManager | ||||
|                     .findFragmentById(R.id.contributionsFragmentContainer); | ||||
| 
 | ||||
|             getSupportLoaderManager().initLoader(0, null, this); | ||||
|         } | ||||
| 
 | ||||
|         requestAuthToken(); | ||||
|         initDrawer(); | ||||
|         setTitle(getString(R.string.title_activity_contributions)); | ||||
| 
 | ||||
| 
 | ||||
|         if(checkAccount()) { | ||||
|             new QuizChecker(this, | ||||
|                     sessionManager.getCurrentAccount().name, | ||||
|                     mediaWikiApi); | ||||
|         } | ||||
|         if(!BuildConfig.FLAVOR.equalsIgnoreCase("beta")){ | ||||
|             setUploadCount(); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onSaveInstanceState(Bundle outState) { | ||||
|         super.onSaveInstanceState(outState); | ||||
|         boolean mediaDetailsVisible = mediaDetails != null && mediaDetails.isVisible(); | ||||
|         outState.putBoolean("mediaDetailsVisible", mediaDetailsVisible); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Replace whatever is in the current contributionsFragmentContainer view with | ||||
|      * mediaDetailPagerFragment, and preserve previous state in back stack. | ||||
|      * Called when user selects a contribution. | ||||
|      */ | ||||
|     private void showDetail(int i) { | ||||
|         if (mediaDetails == null || !mediaDetails.isVisible()) { | ||||
|             mediaDetails = new MediaDetailPagerFragment(); | ||||
|             FragmentManager supportFragmentManager = getSupportFragmentManager(); | ||||
|             supportFragmentManager | ||||
|                     .beginTransaction() | ||||
|                     .replace(R.id.contributionsFragmentContainer, mediaDetails) | ||||
|                     .addToBackStack(null) | ||||
|                     .commit(); | ||||
|             supportFragmentManager.executePendingTransactions(); | ||||
|         } | ||||
|         mediaDetails.showImage(i); | ||||
|     } | ||||
| 
 | ||||
|     public void retryUpload(int i) { | ||||
|         allContributions.moveToPosition(i); | ||||
|         Contribution c = contributionDao.fromCursor(allContributions); | ||||
|         if (c.getState() == STATE_FAILED) { | ||||
|             uploadService.queue(UploadService.ACTION_UPLOAD_FILE, c); | ||||
|             Timber.d("Restarting for %s", c.toString()); | ||||
|         } else { | ||||
|             Timber.d("Skipping re-upload for non-failed %s", c.toString()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public void deleteUpload(int i) { | ||||
|         allContributions.moveToPosition(i); | ||||
|         Contribution c = contributionDao.fromCursor(allContributions); | ||||
|         if (c.getState() == STATE_FAILED) { | ||||
|             Timber.d("Deleting failed contrib %s", c.toString()); | ||||
|             // If upload fails and then user decides to cancel upload at all, which means contribution | ||||
|             // object will be deleted. So we have to delete temp file for that contribution. | ||||
|             ContributionUtils.removeTemporaryFile(c.getLocalUri()); | ||||
|             contributionDao.delete(c); | ||||
|         } else { | ||||
|             Timber.d("Skipping deletion for non-failed contrib %s", c.toString()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean onOptionsItemSelected(MenuItem item) { | ||||
|         switch (item.getItemId()) { | ||||
|             case android.R.id.home: | ||||
|                 if (mediaDetails.isVisible()) { | ||||
|                     getSupportFragmentManager().popBackStack(); | ||||
|                 } | ||||
|                 return true; | ||||
|             default: | ||||
|                 return super.onOptionsItemSelected(item); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onAuthFailure() { | ||||
|         finish(); // If authentication failed, we just exit | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onItemClick(AdapterView<?> adapterView, View view, int position, long item) { | ||||
|         showDetail(position); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean onCreateOptionsMenu(Menu menu) { | ||||
|         return super.onCreateOptionsMenu(menu); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { | ||||
|         int uploads = prefs.getInt(UPLOADS_SHOWING, 100); | ||||
|         return new CursorLoader(this, BASE_URI, | ||||
|                 ALL_FIELDS, "", null, | ||||
|                 ContributionDao.CONTRIBUTION_SORT + "LIMIT " + uploads); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { | ||||
|         contributionsList.changeProgressBarVisibility(false); | ||||
| 
 | ||||
|         if (contributionsList.getAdapter() == null) { | ||||
|             contributionsList.setAdapter(new ContributionsListAdapter(getApplicationContext(), | ||||
|                     cursor, 0, contributionDao)); | ||||
|         } else { | ||||
|             ((CursorAdapter) contributionsList.getAdapter()).swapCursor(cursor); | ||||
|         } | ||||
| 
 | ||||
|         contributionsList.clearSyncMessage(); | ||||
|         notifyAndMigrateDataSetObservers(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onLoaderReset(Loader<Cursor> cursorLoader) { | ||||
|         ((CursorAdapter) contributionsList.getAdapter()).swapCursor(null); | ||||
|     } | ||||
| 
 | ||||
|     //FIXME: Potential cause of wrong image display bug | ||||
|     @Override | ||||
|     public Media getMediaAtPosition(int i) { | ||||
|         if (contributionsList.getAdapter() == null) { | ||||
|             // not yet ready to return data | ||||
|             return null; | ||||
|         } else { | ||||
|             return contributionDao.fromCursor((Cursor) contributionsList.getAdapter().getItem(i)); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int getTotalMediaCount() { | ||||
|         if (contributionsList.getAdapter() == null) { | ||||
|             return 0; | ||||
|         } | ||||
|         return contributionsList.getAdapter().getCount(); | ||||
|     } | ||||
| 
 | ||||
|     @SuppressWarnings("ConstantConditions") | ||||
|     private void setUploadCount() { | ||||
|         compositeDisposable.add(mediaWikiApi | ||||
|                 .getUploadCount(sessionManager.getCurrentAccount().name) | ||||
|                 .subscribeOn(Schedulers.io()) | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|                 .subscribe(this::displayUploadCount, | ||||
|                         t -> Timber.e(t, "Fetching upload count failed") | ||||
|                 )); | ||||
|     } | ||||
| 
 | ||||
|     private void displayUploadCount(Integer uploadCount) { | ||||
|         if (isFinishing() | ||||
|                 || getSupportActionBar() == null | ||||
|                 || getResources() == null) { | ||||
|             return; | ||||
|         } | ||||
|          | ||||
|         getSupportActionBar().setSubtitle(getResources() | ||||
|                 .getQuantityString(R.plurals.contributions_subtitle, | ||||
|                         uploadCount, uploadCount)); | ||||
|     } | ||||
| 
 | ||||
|     public void betaSetUploadCount(int betaUploadCount) { | ||||
|         displayUploadCount(betaUploadCount); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void notifyDatasetChanged() { | ||||
|         // Do nothing for now | ||||
|     } | ||||
| 
 | ||||
|     private void notifyAndMigrateDataSetObservers() { | ||||
|         Adapter adapter = contributionsList.getAdapter(); | ||||
| 
 | ||||
|         // First, move the observers over to the adapter now that we have it. | ||||
|         for (DataSetObserver observer : observersWaitingForLoad) { | ||||
|             adapter.registerDataSetObserver(observer); | ||||
|         } | ||||
|         observersWaitingForLoad.clear(); | ||||
| 
 | ||||
|         // Now fire off a first notification... | ||||
|         for (DataSetObserver observer : observersWaitingForLoad) { | ||||
|             observer.onChanged(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void registerDataSetObserver(DataSetObserver observer) { | ||||
|         Adapter adapter = contributionsList.getAdapter(); | ||||
|         if (adapter == null) { | ||||
|             observersWaitingForLoad.add(observer); | ||||
|         } else { | ||||
|             adapter.registerDataSetObserver(observer); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void unregisterDataSetObserver(DataSetObserver observer) { | ||||
|         Adapter adapter = contributionsList.getAdapter(); | ||||
|         if (adapter == null) { | ||||
|             observersWaitingForLoad.remove(observer); | ||||
|         } else { | ||||
|             adapter.unregisterDataSetObserver(observer); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * to ensure user is logged in | ||||
|      * @return | ||||
|      */ | ||||
|     private boolean checkAccount() { | ||||
|         Account currentAccount = sessionManager.getCurrentAccount(); | ||||
|         if (currentAccount == null) { | ||||
|             Timber.d("Current account is null"); | ||||
|             ViewUtil.showLongToast(this, getResources().getString(R.string.user_not_logged_in)); | ||||
|             sessionManager.forceLogin(this); | ||||
|             return false; | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onBackStackChanged() { | ||||
|         initBackButton(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void refreshSource() { | ||||
|         getSupportLoaderManager().restartLoader(0, null, this); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -91,7 +91,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment { | |||
|         this.contributionsList.setAdapter(adapter); | ||||
| 
 | ||||
|         if(BuildConfig.FLAVOR.equalsIgnoreCase("beta")){ | ||||
|             ((ContributionsActivity) getActivity()).betaSetUploadCount(adapter.getCount()); | ||||
|             // TODO: Neslihan open later ((ContributionsActivity) getActivity()).betaSetUploadCount(adapter.getCount()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -166,12 +166,12 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple | |||
|                 return true; | ||||
|             case R.id.menu_retry_current_image: | ||||
|                 // Retry | ||||
|                 ((ContributionsActivity) getActivity()).retryUpload(pager.getCurrentItem()); | ||||
|                 // TODO: Neslihan open later ((ContributionsActivity) getActivity()).retryUpload(pager.getCurrentItem()); | ||||
|                 getActivity().getSupportFragmentManager().popBackStack(); | ||||
|                 return true; | ||||
|             case R.id.menu_cancel_current_image: | ||||
|                 // todo: delete image | ||||
|                 ((ContributionsActivity) getActivity()).deleteUpload(pager.getCurrentItem()); | ||||
|                 // TODO: Neslihan open later ((ContributionsActivity) getActivity()).deleteUpload(pager.getCurrentItem()); | ||||
|                 getActivity().getSupportFragmentManager().popBackStack(); | ||||
|                 return true; | ||||
|             default: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 neslihanturan
						neslihanturan