mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Fixed #6084
This commit is contained in:
		
							parent
							
								
									411184fde8
								
							
						
					
					
						commit
						f2de40d3b8
					
				
					 2 changed files with 85 additions and 1 deletions
				
			
		|  | @ -2,8 +2,12 @@ package fr.free.nrw.commons.contributions; | ||||||
| 
 | 
 | ||||||
| import static fr.free.nrw.commons.di.CommonsApplicationModule.IO_THREAD; | import static fr.free.nrw.commons.di.CommonsApplicationModule.IO_THREAD; | ||||||
| 
 | 
 | ||||||
|  | import android.os.Handler; | ||||||
|  | import android.os.Looper; | ||||||
|  | 
 | ||||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||||
| import androidx.lifecycle.LiveData; | import androidx.lifecycle.LiveData; | ||||||
|  | import androidx.lifecycle.MutableLiveData; | ||||||
| import androidx.paging.DataSource; | import androidx.paging.DataSource; | ||||||
| import androidx.paging.DataSource.Factory; | import androidx.paging.DataSource.Factory; | ||||||
| import androidx.paging.LivePagedListBuilder; | import androidx.paging.LivePagedListBuilder; | ||||||
|  | @ -12,7 +16,9 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | ||||||
| import fr.free.nrw.commons.contributions.ContributionsListContract.UserActionListener; | import fr.free.nrw.commons.contributions.ContributionsListContract.UserActionListener; | ||||||
| import io.reactivex.Scheduler; | import io.reactivex.Scheduler; | ||||||
| import io.reactivex.disposables.CompositeDisposable; | import io.reactivex.disposables.CompositeDisposable; | ||||||
|  | import java.util.ArrayList; | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
|  | import java.util.List; | ||||||
| import javax.inject.Inject; | import javax.inject.Inject; | ||||||
| import javax.inject.Named; | import javax.inject.Named; | ||||||
| import kotlin.Unit; | import kotlin.Unit; | ||||||
|  | @ -32,6 +38,15 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
| 
 | 
 | ||||||
|     LiveData<PagedList<Contribution>> contributionList; |     LiveData<PagedList<Contribution>> contributionList; | ||||||
| 
 | 
 | ||||||
|  |     private MutableLiveData<List<Contribution>> liveData = new MutableLiveData<>(); | ||||||
|  | 
 | ||||||
|  |     private List<Contribution> existingContributions = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |     // Timer for polling new contributions | ||||||
|  |     private Handler pollingHandler; | ||||||
|  |     private Runnable pollingRunnable; | ||||||
|  |     private long pollingInterval = 1 * 60 * 1000L; // Poll every 15 seconds | ||||||
|  | 
 | ||||||
|     @Inject |     @Inject | ||||||
|     ContributionsListPresenter( |     ContributionsListPresenter( | ||||||
|         final ContributionBoundaryCallback contributionBoundaryCallback, |         final ContributionBoundaryCallback contributionBoundaryCallback, | ||||||
|  | @ -87,6 +102,15 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         contributionList = livePagedListBuilder.build(); |         contributionList = livePagedListBuilder.build(); | ||||||
|  |         contributionList.observeForever(pagedList -> { | ||||||
|  |             if (pagedList != null) { | ||||||
|  |                 existingContributions.clear(); | ||||||
|  |                 existingContributions.addAll(pagedList); | ||||||
|  |                 liveData.setValue(existingContributions); // Update liveData with the latest list | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         // Start polling for new contributions | ||||||
|  |         startPollingForNewContributions(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  | @ -94,8 +118,10 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|         compositeDisposable.clear(); |         compositeDisposable.clear(); | ||||||
|         contributionsRemoteDataSource.dispose(); |         contributionsRemoteDataSource.dispose(); | ||||||
|         contributionBoundaryCallback.dispose(); |         contributionBoundaryCallback.dispose(); | ||||||
|  |         stopPollingForNewContributions(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * It is used to refresh list. |      * It is used to refresh list. | ||||||
|      * |      * | ||||||
|  | @ -109,4 +135,57 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|             return Unit.INSTANCE; |             return Unit.INSTANCE; | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |     /** | ||||||
|  |      * Start polling for new contributions every 15 minutes. | ||||||
|  |      */ | ||||||
|  |     private void startPollingForNewContributions() { | ||||||
|  |         if (pollingHandler != null) { | ||||||
|  |             stopPollingForNewContributions(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         pollingHandler = new Handler(Looper.getMainLooper()); | ||||||
|  |         pollingRunnable = new Runnable() { | ||||||
|  |             @Override | ||||||
|  |             public void run() { | ||||||
|  |                 fetchNewContributions(); // Fetch new contributions in background | ||||||
|  |                 pollingHandler.postDelayed(this, pollingInterval); // Repeat after the interval | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |         pollingHandler.post(pollingRunnable); // Start polling immediately | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Stop the polling task when the view is detached or the activity is paused. | ||||||
|  |      */ | ||||||
|  |     private void stopPollingForNewContributions() { | ||||||
|  |         if (pollingHandler != null && pollingRunnable != null) { | ||||||
|  |             pollingHandler.removeCallbacks(pollingRunnable); | ||||||
|  |             pollingHandler = null; | ||||||
|  |             pollingRunnable = null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void appendContributions(List<Contribution> newContributions) { | ||||||
|  |         if (newContributions != null && !newContributions.isEmpty()) { | ||||||
|  |             existingContributions.addAll(newContributions); | ||||||
|  |             liveData.postValue(existingContributions); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     /** | ||||||
|  |      * Fetch new contributions from the server and append them to the existing list. | ||||||
|  |      */ | ||||||
|  |     private void fetchNewContributions() { | ||||||
|  |         contributionsRemoteDataSource.fetchContributions(new ContributionsRemoteDataSource.LoadCallback<Contribution>() { | ||||||
|  |             @Override | ||||||
|  |             public void onResult(List<Contribution> newContributions) { | ||||||
|  |                 if (newContributions != null && !newContributions.isEmpty()) { | ||||||
|  |                     appendContributions(newContributions); // Add new contributions | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -46,7 +46,12 @@ class ContributionsRemoteDataSource | ||||||
|         /** |         /** | ||||||
|          * Fetches contributions using the MediaWiki API |          * Fetches contributions using the MediaWiki API | ||||||
|          */ |          */ | ||||||
|         private fun fetchContributions(callback: LoadCallback<Contribution>) { |         public fun fetchContributions(callback: LoadCallback<Contribution>) { | ||||||
|  |             if (userName.isNullOrEmpty()) { | ||||||
|  |                 Timber.e("Failed to fetch contributions: userName is null or empty") | ||||||
|  |                 return | ||||||
|  |             } | ||||||
|  |             Timber.d("Fetching contributions for user: $userName") | ||||||
|             compositeDisposable.add( |             compositeDisposable.add( | ||||||
|                 mediaClient |                 mediaClient | ||||||
|                     .getMediaListForUser(userName!!) |                     .getMediaListForUser(userName!!) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Sujal-Gupta-SG
						Sujal-Gupta-SG