Skip to content

Commit

Permalink
add support for item update by swipe to refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 10, 2023
1 parent 221d446 commit a981699
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 112 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ dependencies {
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
implementation 'com.google.guava:guava:30.1.1-android'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
// WorkManager dependency
implementation "androidx.work:work-runtime:2.8.1"
implementation 'androidx.work:work-runtime:2.8.1'

// Third-party
annotationProcessor 'com.github.Raizlabs.DBFlow:dbflow-processor:4.2.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
Expand All @@ -12,8 +13,11 @@

import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.TaskExecutor;
import org.openobservatory.ooniprobe.databinding.ActivityOverviewBinding;
import org.openobservatory.ooniprobe.domain.TestDescriptorManager;
import org.openobservatory.ooniprobe.model.database.Result;
import org.openobservatory.ooniprobe.model.database.TestDescriptor;
import org.openobservatory.ooniprobe.test.suite.AbstractSuite;
import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite;
import org.openobservatory.ooniprobe.test.suite.OONIRunSuite;
Expand All @@ -28,6 +32,7 @@

public class OverviewActivity extends AbstractActivity {
private static final String TEST = "test";
private static final String TAG = OverviewActivity.class.getSimpleName();

private ActivityOverviewBinding binding;
private AbstractSuite testSuite;
Expand Down Expand Up @@ -66,6 +71,10 @@ public static Intent newIntent(Context context, AbstractSuite testSuite) {
if (testSuite.getName().equals(OONIRunSuite.NAME)) {
binding.author.setText(String.format("Author : %s",((OONIRunSuite)testSuite).getDescriptor().getAuthor()));
binding.author.setVisibility(View.VISIBLE);

binding.swipeRefresh.setOnRefreshListener(this::initiateRefresh);
} else {
binding.swipeRefresh.setEnabled(false);
}
Result lastResult = Result.getLastResult(testSuite.getName());
if (lastResult == null)
Expand All @@ -88,6 +97,46 @@ private void setUpOnCLickListeners() {
binding.runtime.setText(getString(R.string.twoParam, getString(testSuite.getDataUsage()), getString(R.string.Dashboard_Card_Seconds, testSuite.getRuntime(preferenceManager).toString())));
}


private void initiateRefresh() {
Log.i(TAG, "initiateRefresh");
TestDescriptor descriptorToUpdate = ((OONIRunSuite)testSuite).getDescriptor();

TaskExecutor executor = new TaskExecutor();
executor.executeTask(
() -> TestDescriptorManager.fetchDescriptorFromRunId(
descriptorToUpdate.getRunId(),
OverviewActivity.this
),
descriptor -> {
if (descriptor.getVersion() > descriptorToUpdate.getVersion()){
prepareForUpdates(descriptor, descriptorToUpdate);
} else {
noUpdatesAvailable();
}
binding.swipeRefresh.setRefreshing(false);
return null;
});
}

private void prepareForUpdates(TestDescriptor descriptor, TestDescriptor descriptorToUpdate) {
binding.refresh.setOnClickListener(v -> {
descriptor.setAutoUpdate(descriptorToUpdate.isAutoUpdate());
descriptor.setAutoRun(descriptorToUpdate.isAutoRun());
descriptor.save();
binding.refresh.setVisibility(View.GONE);
updateViewFromDescriptor(descriptor);
});
binding.refresh.setVisibility(android.view.View.VISIBLE);
}

private void noUpdatesAvailable() {

}

private void updateViewFromDescriptor(TestDescriptor descriptor) {
}

void onRunClick() {
if(!testSuite.isTestEmpty(preferenceManager)){
RunningActivity.runAsForegroundService(this, testSuite.asArray(), this::bindTestService, preferenceManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ private void scheduleWorkers() {
UpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest.from(UpdateDescriptorsWorker.class)
).then(OneTimeWorkRequest.from(UpdateDescriptorsWorker.class))
// Call enqueue to kick things off
.enqueue();*/
).enqueue();*/
}

protected AppComponent buildDagger() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openobservatory.ooniprobe.common

import android.os.Handler
import android.os.Looper
import java.util.concurrent.Callable
import java.util.concurrent.Executors

abstract class ProgressTask<P, R> {
abstract fun runTask(progressToken: OnTaskProgressUpdate<P>): R
}

typealias Task<R> = Callable<R>

typealias OnTaskProgressUpdate<P> = (P) -> Unit

typealias OnTaskComplete<R> = (R) -> Unit

class TaskExecutor {
private val executor = Executors.newSingleThreadExecutor()
private val handler = Handler(Looper.getMainLooper())

fun <R> executeTask(task: Task<R>, onComplete: OnTaskComplete<R>) {
executor.execute {
val result = task.call()
handler.post {
onComplete(result)
}
}
}

fun <P, R> executeProgressTask(
progressTask: ProgressTask<P, R>,
onProgress: OnTaskProgressUpdate<P>,
onComplete: OnTaskComplete<R>
) {
executor.execute {
val result = progressTask.runTask(progressToken = { progress ->
handler.post {
onProgress(progress)
}
})

handler.post {
onComplete(result)
}
}
}
}
Loading

0 comments on commit a981699

Please sign in to comment.