mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Fixed #6084
This commit is contained in:
		
							parent
							
								
									9377b7c057
								
							
						
					
					
						commit
						d39a57f257
					
				
					 2 changed files with 53 additions and 18 deletions
				
			
		|  | @ -22,7 +22,6 @@ 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; | ||||||
| import kotlin.jvm.functions.Function0; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * The presenter class for Contributions |  * The presenter class for Contributions | ||||||
|  | @ -45,7 +44,7 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|     // Timer for polling new contributions |     // Timer for polling new contributions | ||||||
|     private Handler pollingHandler; |     private Handler pollingHandler; | ||||||
|     private Runnable pollingRunnable; |     private Runnable pollingRunnable; | ||||||
|     private long pollingInterval = 1 * 60 * 1000L; // Poll every minute |     private long pollingInterval = 24 * 60 * 60 * 1000L; // Poll every day | ||||||
| 
 | 
 | ||||||
|     @Inject |     @Inject | ||||||
|     ContributionsListPresenter( |     ContributionsListPresenter( | ||||||
|  | @ -136,7 +135,7 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Start polling for new contributions every 15 minutes. |      * Start polling for new contributions every 24 hour. | ||||||
|      */ |      */ | ||||||
|     private void startPollingForNewContributions() { |     private void startPollingForNewContributions() { | ||||||
|         if (pollingHandler != null) { |         if (pollingHandler != null) { | ||||||
|  | @ -147,7 +146,7 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|         pollingRunnable = new Runnable() { |         pollingRunnable = new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 fetchNewContributions(); // Fetch new contributions in background |                 checkForNewContributions(); | ||||||
|                 pollingHandler.postDelayed(this, pollingInterval); // Repeat after the interval |                 pollingHandler.postDelayed(this, pollingInterval); // Repeat after the interval | ||||||
|             } |             } | ||||||
|         }; |         }; | ||||||
|  | @ -164,22 +163,33 @@ public class ContributionsListPresenter implements UserActionListener { | ||||||
|             pollingRunnable = null; |             pollingRunnable = null; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |     private String lastKnownIdentifier = null; // Declare and initialize | ||||||
| 
 | 
 | ||||||
|     public void appendContributions(List<Contribution> newContributions) { |     /** | ||||||
|         if (newContributions != null && !newContributions.isEmpty()) { |      * Check for new contributions by comparing the latest contribution identifier. | ||||||
|             existingContributions.addAll(newContributions); |      */ | ||||||
|             liveData.postValue(existingContributions); |     private void checkForNewContributions() { | ||||||
|  |         contributionsRemoteDataSource.fetchLatestContributionIdentifier(latestIdentifier -> { | ||||||
|  |             if (latestIdentifier != null && !latestIdentifier.equals(lastKnownIdentifier)) { | ||||||
|  |                 lastKnownIdentifier = latestIdentifier; | ||||||
|  |                 fetchAllContributions(); // Fetch the full list of contributions | ||||||
|             } |             } | ||||||
|  |             return Unit.INSTANCE; // Explicitly return Unit for Kotlin compatibility | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Fetch new contributions from the server and append them to the existing list. |      * Fetch new contributions from the server and append them to the existing list. | ||||||
|      */ |      */ | ||||||
|     private void fetchNewContributions() { |     private void fetchAllContributions() { | ||||||
|         contributionsRemoteDataSource.fetchContributions(new ContributionsRemoteDataSource.LoadCallback<Contribution>() { |         contributionsRemoteDataSource.fetchAllContributions(new ContributionsRemoteDataSource.LoadCallback<Contribution>() { | ||||||
|             @Override |             @Override | ||||||
|             public void onResult(List<Contribution> newContributions) { |             public void onResult(List<Contribution> newContributions) { | ||||||
|                 if (newContributions != null && !newContributions.isEmpty()) { |                 if (newContributions != null && !newContributions.isEmpty()) { | ||||||
|                     appendContributions(newContributions); // Add new contributions |                     existingContributions.clear(); | ||||||
|  |                     existingContributions.addAll(newContributions); | ||||||
|  |                     liveData.postValue(existingContributions); // Update liveData with the new list | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  | @ -25,14 +25,14 @@ class ContributionsRemoteDataSource | ||||||
|             params: LoadInitialParams<Int>, |             params: LoadInitialParams<Int>, | ||||||
|             callback: LoadInitialCallback<Contribution>, |             callback: LoadInitialCallback<Contribution>, | ||||||
|         ) { |         ) { | ||||||
|             fetchContributions(callback) |             fetchAllContributions(callback) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         override fun loadAfter( |         override fun loadAfter( | ||||||
|             params: LoadParams<Int>, |             params: LoadParams<Int>, | ||||||
|             callback: LoadCallback<Contribution>, |             callback: LoadCallback<Contribution>, | ||||||
|         ) { |         ) { | ||||||
|             fetchContributions(callback) |             fetchAllContributions(callback) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         override fun loadBefore( |         override fun loadBefore( | ||||||
|  | @ -46,7 +46,7 @@ class ContributionsRemoteDataSource | ||||||
|         /** |         /** | ||||||
|          * Fetches contributions using the MediaWiki API |          * Fetches contributions using the MediaWiki API | ||||||
|          */ |          */ | ||||||
|         public fun fetchContributions(callback: LoadCallback<Contribution>) { |         fun fetchAllContributions(callback: LoadCallback<Contribution>) { | ||||||
|             if (userName.isNullOrEmpty()) { |             if (userName.isNullOrEmpty()) { | ||||||
|                 Timber.e("Failed to fetch contributions: userName is null or empty") |                 Timber.e("Failed to fetch contributions: userName is null or empty") | ||||||
|                 return |                 return | ||||||
|  | @ -61,8 +61,9 @@ class ContributionsRemoteDataSource | ||||||
|                             Contribution(media = it, state = Contribution.STATE_COMPLETED) |                             Contribution(media = it, state = Contribution.STATE_COMPLETED) | ||||||
|                         } |                         } | ||||||
|                     }.subscribeOn(ioThreadScheduler) |                     }.subscribeOn(ioThreadScheduler) | ||||||
|                     .subscribe({ |                     .subscribe({ contributions -> | ||||||
|                         callback.onResult(it) |                         // Pass the contributions to the callback | ||||||
|  |                         callback.onResult(contributions) | ||||||
|                     })  { error: Throwable -> |                     })  { error: Throwable -> | ||||||
|                         Timber.e( |                         Timber.e( | ||||||
|                             "Failed to fetch contributions: %s", |                             "Failed to fetch contributions: %s", | ||||||
|  | @ -71,6 +72,30 @@ class ContributionsRemoteDataSource | ||||||
|                     }, |                     }, | ||||||
|             ) |             ) | ||||||
|         } |         } | ||||||
|  |     /** | ||||||
|  |      * Fetches the latest contribution identifier only | ||||||
|  |      */ | ||||||
|  |     fun fetchLatestContributionIdentifier(callback: (String?) -> Unit) { | ||||||
|  |         if (userName.isNullOrEmpty()) { | ||||||
|  |             Timber.e("Failed to fetch latest contribution: userName is null or empty") | ||||||
|  |             return | ||||||
|  |         } | ||||||
|  |         Timber.d("Fetching latest contribution identifier for user: $userName") | ||||||
|  | 
 | ||||||
|  |         compositeDisposable.add( | ||||||
|  |             mediaClient.getMediaListForUser(userName!!) | ||||||
|  |                 .map { mediaList -> | ||||||
|  |                     mediaList.firstOrNull()?.pageId.toString() // Extract the first contribution's pageId | ||||||
|  |                 } | ||||||
|  |                 .subscribeOn(ioThreadScheduler) | ||||||
|  |                 .subscribe({ latestIdentifier -> | ||||||
|  |                     callback(latestIdentifier) | ||||||
|  |                 }) { error: Throwable -> | ||||||
|  |                     Timber.e("Failed to fetch latest contribution identifier: %s", error.message) | ||||||
|  |                     callback(null) | ||||||
|  |                 }, | ||||||
|  |         ) | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         fun dispose() { |         fun dispose() { | ||||||
|             compositeDisposable.dispose() |             compositeDisposable.dispose() | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Sujal-Gupta-SG
						Sujal-Gupta-SG