mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-30 22:34:02 +01:00 
			
		
		
		
	Replace Guava-implemented getUploadCount with RxJava2 version
				
					
				
			This commit is contained in:
		
							parent
							
								
									9658f18fea
								
							
						
					
					
						commit
						09d94f3e06
					
				
					 10 changed files with 59 additions and 274 deletions
				
			
		|  | @ -11,7 +11,6 @@ import android.database.DataSetObserver; | |||
| import android.os.Bundle; | ||||
| import android.os.IBinder; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.v4.app.FragmentManager; | ||||
| import android.support.v4.app.LoaderManager; | ||||
| import android.support.v4.content.CursorLoader; | ||||
|  | @ -23,10 +22,6 @@ import android.view.View; | |||
| import android.widget.Adapter; | ||||
| import android.widget.AdapterView; | ||||
| 
 | ||||
| import com.google.common.util.concurrent.FutureCallback; | ||||
| import com.google.common.util.concurrent.Futures; | ||||
| import com.google.common.util.concurrent.ListenableFuture; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import butterknife.ButterKnife; | ||||
|  | @ -38,7 +33,9 @@ import fr.free.nrw.commons.auth.AuthenticatedActivity; | |||
| import fr.free.nrw.commons.media.MediaDetailPagerFragment; | ||||
| import fr.free.nrw.commons.settings.Prefs; | ||||
| import fr.free.nrw.commons.upload.UploadService; | ||||
| import fr.free.nrw.commons.utils.ExecutorUtils; | ||||
| import io.reactivex.android.schedulers.AndroidSchedulers; | ||||
| import io.reactivex.disposables.CompositeDisposable; | ||||
| import io.reactivex.schedulers.Schedulers; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| public  class       ContributionsActivity | ||||
|  | @ -68,6 +65,8 @@ public  class       ContributionsActivity | |||
|      */ | ||||
|     private String CONTRIBUTION_SORT = Contribution.Table.COLUMN_STATE + " DESC, " + Contribution.Table.COLUMN_UPLOADED + " DESC , (" + Contribution.Table.COLUMN_TIMESTAMP + " * " + Contribution.Table.COLUMN_STATE + ")"; | ||||
| 
 | ||||
|     private CompositeDisposable compositeDisposable = new CompositeDisposable(); | ||||
| 
 | ||||
|     private ServiceConnection uploadServiceConnection = new ServiceConnection() { | ||||
|         @Override | ||||
|         public void onServiceConnected(ComponentName componentName, IBinder binder) { | ||||
|  | @ -84,6 +83,7 @@ public  class       ContributionsActivity | |||
| 
 | ||||
|     @Override | ||||
|     protected void onDestroy() { | ||||
|         compositeDisposable.clear(); | ||||
|         getSupportFragmentManager().removeOnBackStackChangedListener(this); | ||||
|         super.onDestroy(); | ||||
|         if(isUploadServiceConnected) { | ||||
|  | @ -110,6 +110,8 @@ public  class       ContributionsActivity | |||
|         super.onPause(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onAuthCookieAcquired(String authCookie) { | ||||
|         // Do a sync everytime we get here! | ||||
|  | @ -268,24 +270,21 @@ public  class       ContributionsActivity | |||
|     } | ||||
| 
 | ||||
|     private void setUploadCount() { | ||||
|         UploadCountClient uploadCountClient = new UploadCountClient(); | ||||
|         CommonsApplication application = CommonsApplication.getInstance(); | ||||
|         ListenableFuture<Integer> future = uploadCountClient | ||||
|                 .getUploadCount(application.getCurrentAccount().name); | ||||
|         Futures.addCallback(future, new FutureCallback<Integer>() { | ||||
|             @Override | ||||
|             public void onSuccess(Integer uploadCount) { | ||||
|                 getSupportActionBar().setSubtitle(getResources() | ||||
|                         .getQuantityString(R.plurals.contributions_subtitle, | ||||
|                                 uploadCount, | ||||
|                                 uploadCount)); | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public void onFailure(@NonNull Throwable t) { | ||||
|                 Timber.e(t, "Fetching upload count failed"); | ||||
|             } | ||||
|         }, ExecutorUtils.uiExecutor()); | ||||
|         compositeDisposable.add( | ||||
|                 RxJava2Tasks.getUploadCount(application.getCurrentAccount().name) | ||||
|                         .subscribeOn(Schedulers.io()) | ||||
|                         .observeOn(AndroidSchedulers.mainThread()) | ||||
|                         .subscribe( | ||||
|                                 uploadCount -> | ||||
|                                         getSupportActionBar().setSubtitle(getResources() | ||||
|                                                 .getQuantityString(R.plurals.contributions_subtitle, | ||||
|                                                         uploadCount, | ||||
|                                                         uploadCount)), | ||||
|                                 throwable -> Timber.e(throwable, "Fetching upload count failed") | ||||
|                         ) | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  |  | |||
|  | @ -0,0 +1,30 @@ | |||
| package fr.free.nrw.commons.contributions; | ||||
| 
 | ||||
| import java.io.BufferedReader; | ||||
| import java.io.InputStreamReader; | ||||
| import java.net.HttpURLConnection; | ||||
| import java.net.URL; | ||||
| import java.util.Locale; | ||||
| 
 | ||||
| import fr.free.nrw.commons.PageTitle; | ||||
| import io.reactivex.Single; | ||||
| 
 | ||||
| class RxJava2Tasks { | ||||
| 
 | ||||
|     private static final String UPLOAD_COUNT_URL_TEMPLATE = | ||||
|             "https://tools.wmflabs.org/urbanecmbot/uploadsbyuser/uploadsbyuser.py?user=%s"; | ||||
| 
 | ||||
|     static Single<Integer> getUploadCount(String userName) { | ||||
|         return Single.fromCallable(() -> { | ||||
|                     URL url = new URL(String.format(Locale.ENGLISH, UPLOAD_COUNT_URL_TEMPLATE, | ||||
|                                 new PageTitle(userName).getText())); | ||||
|                     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | ||||
|                     BufferedReader bufferedReader = new BufferedReader(new | ||||
|                             InputStreamReader(urlConnection.getInputStream())); | ||||
|                     String uploadCount = bufferedReader.readLine(); | ||||
|                     bufferedReader.close(); | ||||
|                     urlConnection.disconnect(); | ||||
|                     return Integer.parseInt(uploadCount); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|  | @ -1,54 +0,0 @@ | |||
| package fr.free.nrw.commons.contributions; | ||||
| 
 | ||||
| import com.google.common.util.concurrent.ListenableFuture; | ||||
| import com.google.common.util.concurrent.SettableFuture; | ||||
| 
 | ||||
| import java.io.BufferedReader; | ||||
| import java.io.InputStreamReader; | ||||
| import java.net.HttpURLConnection; | ||||
| import java.net.URL; | ||||
| import java.util.Locale; | ||||
| 
 | ||||
| import fr.free.nrw.commons.PageTitle; | ||||
| import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler; | ||||
| import fr.free.nrw.commons.concurrency.ThreadPoolExecutorService; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| class UploadCountClient { | ||||
|     private ThreadPoolExecutorService threadPoolExecutor; | ||||
| 
 | ||||
|     UploadCountClient() { | ||||
|         threadPoolExecutor = new ThreadPoolExecutorService.Builder("bg-pool") | ||||
|                 .setPoolSize(Runtime.getRuntime().availableProcessors()) | ||||
|                 .setExceptionHandler(new BackgroundPoolExceptionHandler()) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     private static final String UPLOAD_COUNT_URL_TEMPLATE = | ||||
|             "https://tools.wmflabs.org/urbanecmbot/uploadsbyuser/uploadsbyuser.py?user=%s"; | ||||
| 
 | ||||
|     ListenableFuture<Integer> getUploadCount(final String userName) { | ||||
|         final SettableFuture<Integer> future = SettableFuture.create(); | ||||
|         threadPoolExecutor.schedule(() -> { | ||||
|             URL url; | ||||
|             try { | ||||
|                 url = new URL(String.format(Locale.ENGLISH, UPLOAD_COUNT_URL_TEMPLATE, | ||||
|                         new PageTitle(userName).getText())); | ||||
|                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | ||||
|                 try { | ||||
|                     BufferedReader bufferedReader = new BufferedReader(new | ||||
|                             InputStreamReader(urlConnection.getInputStream())); | ||||
|                     String uploadCount = bufferedReader.readLine(); | ||||
|                     bufferedReader.close(); | ||||
|                     future.set(Integer.parseInt(uploadCount)); | ||||
|                 } finally { | ||||
|                     urlConnection.disconnect(); | ||||
|                 } | ||||
|             } catch (Exception e) { | ||||
|                 Timber.e("Error getting upload count Error", e); | ||||
|                 future.setException(e); | ||||
|             } | ||||
|         }); | ||||
|         return future; | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Mikel
						Mikel