Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujal-Gupta-SG committed Jan 4, 2025
1 parent 9377b7c commit d39a57f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.inject.Inject;
import javax.inject.Named;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

/**
* The presenter class for Contributions
Expand All @@ -45,7 +44,7 @@ public class ContributionsListPresenter implements UserActionListener {
// Timer for polling new contributions
private Handler pollingHandler;
private Runnable pollingRunnable;
private long pollingInterval = 1 * 60 * 1000L; // Poll every minute
private long pollingInterval = 24 * 60 * 60 * 1000L; // Poll every day

@Inject
ContributionsListPresenter(
Expand Down Expand Up @@ -136,7 +135,7 @@ public void refreshList(final SwipeRefreshLayout swipeRefreshLayout) {
}

/**
* Start polling for new contributions every 15 minutes.
* Start polling for new contributions every 24 hour.
*/
private void startPollingForNewContributions() {
if (pollingHandler != null) {
Expand All @@ -147,7 +146,7 @@ private void startPollingForNewContributions() {
pollingRunnable = new Runnable() {
@Override
public void run() {
fetchNewContributions(); // Fetch new contributions in background
checkForNewContributions();
pollingHandler.postDelayed(this, pollingInterval); // Repeat after the interval
}
};
Expand All @@ -164,22 +163,33 @@ private void stopPollingForNewContributions() {
pollingRunnable = null;
}
}
private String lastKnownIdentifier = null; // Declare and initialize

/**
* Check for new contributions by comparing the latest contribution identifier.
*/
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
});

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>() {
private void fetchAllContributions() {
contributionsRemoteDataSource.fetchAllContributions(new ContributionsRemoteDataSource.LoadCallback<Contribution>() {
@Override
public void onResult(List<Contribution> newContributions) {
if (newContributions != null && !newContributions.isEmpty()) {
appendContributions(newContributions); // Add new contributions
existingContributions.clear();
existingContributions.addAll(newContributions);
liveData.postValue(existingContributions); // Update liveData with the new list
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class ContributionsRemoteDataSource
params: LoadInitialParams<Int>,
callback: LoadInitialCallback<Contribution>,
) {
fetchContributions(callback)
fetchAllContributions(callback)
}

override fun loadAfter(
params: LoadParams<Int>,
callback: LoadCallback<Contribution>,
) {
fetchContributions(callback)
fetchAllContributions(callback)
}

override fun loadBefore(
Expand All @@ -46,7 +46,7 @@ class ContributionsRemoteDataSource
/**
* Fetches contributions using the MediaWiki API
*/
public fun fetchContributions(callback: LoadCallback<Contribution>) {
fun fetchAllContributions(callback: LoadCallback<Contribution>) {
if (userName.isNullOrEmpty()) {
Timber.e("Failed to fetch contributions: userName is null or empty")
return
Expand All @@ -61,16 +61,41 @@ class ContributionsRemoteDataSource
Contribution(media = it, state = Contribution.STATE_COMPLETED)
}
}.subscribeOn(ioThreadScheduler)
.subscribe({
callback.onResult(it)
}) { error: Throwable ->
.subscribe({ contributions ->
// Pass the contributions to the callback
callback.onResult(contributions)
}) { error: Throwable ->
Timber.e(
"Failed to fetch contributions: %s",
error.message,
)
},
)
}
/**
* 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() {
compositeDisposable.dispose()
Expand Down

0 comments on commit d39a57f

Please sign in to comment.