Skip to content

Commit

Permalink
Updates UI on refresh complete
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 15, 2023
1 parent 2d7d8b7 commit ebeca76
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 172 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ android {
buildFeatures {
viewBinding = true
}
dataBinding {
enabled = true
enabledForTests = true
}
namespace 'org.openobservatory.ooniprobe'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.text.TextUtilsCompat;
import androidx.core.view.ViewCompat;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;

import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.activity.overview.OverviewViewModel;
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;
import org.openobservatory.ooniprobe.test.suite.WebsitesSuite;
import org.openobservatory.ooniprobe.test.test.AbstractTest;

import java.util.Locale;
Expand All @@ -42,6 +44,7 @@ public class OverviewActivity extends AbstractActivity {

@Inject
PreferenceManager preferenceManager;
private OverviewViewModel viewModel;

public static Intent newIntent(Context context, AbstractSuite testSuite) {
return new Intent(context, OverviewActivity.class).putExtra(TEST, testSuite);
Expand All @@ -56,38 +59,30 @@ public static Intent newIntent(Context context, AbstractSuite testSuite) {
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(testSuite.getTitle());
binding.icon.setImageResource(testSuite.getIcon());
binding.customUrl.setVisibility(testSuite.getName().equals(WebsitesSuite.NAME) ? View.VISIBLE : View.GONE);
viewModel = new ViewModelProvider(this).get(OverviewViewModel.class);
binding.setViewmodel(viewModel);
binding.setLifecycleOwner(this);
onTestSuiteChanged();
if(testSuite.isTestEmpty(preferenceManager)){
binding.run.setAlpha(0.5F);
binding.run.setEnabled(false);
}
if (testSuite.getName().equals(ExperimentalSuite.NAME)) {
Markwon.setMarkdown(binding.desc, testSuite.getDesc1());
if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL)
binding.desc.setTextDirection(View.TEXT_DIRECTION_RTL);
} else {
Markwon.setMarkdown(binding.desc, testSuite.getDesc1());
}

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)
binding.lastTime.setText(R.string.Dashboard_Overview_LastRun_Never);
else
binding.lastTime.setText(DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime()));

setUpOnCLickListeners();
}

private void onTestSuiteChanged() {
setTitle(testSuite.getTitle());
viewModel.onTestSuiteChanged(testSuite);
binding.executePendingBindings();
}


private void setUpOnCLickListeners() {
binding.run.setOnClickListener(view -> onRunClick());
binding.customUrl.setOnClickListener(view -> customUrlClick());
Expand All @@ -112,7 +107,7 @@ private void initiateRefresh() {
OverviewActivity.this
),
descriptor -> {
if (descriptor.getVersion() > descriptorToUpdate.getVersion()){
if (descriptorToUpdate.shouldUpdate(descriptor)){
if (descriptorToUpdate.isAutoUpdate()) {
updateDescriptor(descriptor, descriptorToUpdate);
} else {
Expand Down Expand Up @@ -153,7 +148,24 @@ private void noUpdatesAvailable() {
}

private void updateViewFromDescriptor(TestDescriptor descriptor) {
// TODO use view model to update screen
this.testSuite = descriptor.getTestSuite(this);
this.onTestSuiteChanged();
}

@BindingAdapter(value = {"richText", "testSuiteName"})
public static void setRichText(TextView view, String richText,String testSuiteName) {
if (testSuiteName.equals(ExperimentalSuite.NAME)) {
Markwon.setMarkdown(view, richText);
if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL)
view.setTextDirection(View.TEXT_DIRECTION_RTL);
} else {
Markwon.setMarkdown(view, richText);
}
}

@BindingAdapter({"resource"})
public static void setImageViewResource(ImageView imageView, int resource) {
imageView.setImageResource(resource);
}

void onRunClick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.openobservatory.ooniprobe.activity.overview

import android.text.format.DateUtils
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.openobservatory.ooniprobe.model.database.Result
import org.openobservatory.ooniprobe.test.suite.AbstractSuite
import org.openobservatory.ooniprobe.test.suite.OONIRunSuite
import org.openobservatory.ooniprobe.test.suite.WebsitesSuite

class OverviewViewModel : ViewModel() {
var suite: MutableLiveData<AbstractSuite?> = MutableLiveData<AbstractSuite?>()
var name = MutableLiveData<String>()
var title = MutableLiveData<String>()
var icon = MutableLiveData<Int>()
var dataUsage = MutableLiveData<String>()
var lastTime = MutableLiveData<String>()
var author = MutableLiveData<String?>()
var description = MutableLiveData<String>()
fun onTestSuiteChanged(suite: AbstractSuite) {
this.suite.value = suite
name.value = suite.name
title.value = suite.title
icon.value = suite.icon
dataUsage.value = suite.dataUsage.toString()
// TODO: (aanorbel) Use runID for fetching value, all ooni-run tests register as one with same name.
lastTime.value = Result.getLastResult(suite.name)?.start_time?.time?.let {
DateUtils.getRelativeTimeSpanString(
it
)?.toString()
}

description.value = suite.desc1
if (suite is OONIRunSuite) {
author.value = String.format("Author : %s", suite.descriptor.author)
}
}
val authorVisibility: Int
get() = if (author.value != null) View.VISIBLE else View.GONE

val customUrlVisibility: Int
get() = if (name.value == WebsitesSuite.NAME) View.VISIBLE else View.GONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Result doWork() {

TestDescriptor updatedDescriptor = TestDescriptorManager.fetchDescriptorFromRunId(descriptor.getRunId(), applicationContext);

if (updatedDescriptor.getVersion() > descriptor.getVersion()) {
if (descriptor.shouldUpdate(updatedDescriptor)) {

updatedDescriptor.setAutoUpdate(descriptor.isAutoUpdate());
updatedDescriptor.setAutoRun(descriptor.isAutoRun());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static TestDescriptor fetchDescriptorFromRunId(long runId, Context contex
.withIcon(descriptor.getIcon())
.withArchived(response.archived)
.withAuthor(descriptor.getAuthor())
.withVersion(response.version)
.withCreationTime(response.creationTime)
.withTranslationCreationTime(response.translationCreationTime)
.withNettests(descriptor.getNettests())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openobservatory.ooniprobe.test.test.WebConnectivity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -62,8 +63,11 @@ public class TestDescriptor extends BaseModel implements Serializable {
@Column(name = "auto_update")
private boolean autoUpdate;

@Column
private int version;
@Column(name = "creation_time")
private Date creationTime;

@Column(name = "translation_creation_time")
private Date translationCreationTime;
@Column(typeConverter = NettestConverter.class)
private List nettests;

Expand Down Expand Up @@ -163,12 +167,20 @@ public void setAutoUpdate(boolean autoUpdate) {
this.autoUpdate = autoUpdate;
}

public int getVersion() {
return version;
public Date getCreationTime() {
return creationTime;
}

public void setVersion(int version) {
this.version = version;
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}

public Date getTranslationCreationTime() {
return translationCreationTime;
}

public void setTranslationCreationTime(Date translationCreationTime) {
this.translationCreationTime = translationCreationTime;
}

public List getNettests() {
Expand Down Expand Up @@ -204,6 +216,11 @@ public static Where<TestDescriptor> selectAllAvailable() {
.where(TestDescriptor_Table.archived.eq(false));
}

public boolean shouldUpdate(TestDescriptor updatedDescriptor) {
return updatedDescriptor.creationTime.after(creationTime)
|| updatedDescriptor.translationCreationTime.after(translationCreationTime);
}


public static final class Builder {
private long runId;
Expand All @@ -216,7 +233,10 @@ public static final class Builder {
private String icon;
private String author;
private boolean archived;
private int version;
private boolean autoRun;
private boolean autoUpdate;
private Date creationTime;
private Date translationCreationTime;
private List nettests;

private Builder() {
Expand Down Expand Up @@ -276,8 +296,23 @@ public Builder withArchived(boolean archived) {
return this;
}

public Builder withVersion(int version) {
this.version = version;
public Builder withAutoRun(boolean autoRun) {
this.autoRun = autoRun;
return this;
}

public Builder withAutoUpdate(boolean autoUpdate) {
this.autoUpdate = autoUpdate;
return this;
}

public Builder withCreationTime(Date creationTime) {
this.creationTime = creationTime;
return this;
}

public Builder withTranslationCreationTime(Date translationCreationTime) {
this.translationCreationTime = translationCreationTime;
return this;
}

Expand All @@ -298,7 +333,10 @@ public TestDescriptor build() {
testDescriptor.setIcon(icon);
testDescriptor.setAuthor(author);
testDescriptor.setArchived(archived);
testDescriptor.setVersion(version);
testDescriptor.setAutoRun(autoRun);
testDescriptor.setAutoUpdate(autoUpdate);
testDescriptor.setCreationTime(creationTime);
testDescriptor.setTranslationCreationTime(translationCreationTime);
testDescriptor.setNettests(nettests);
return testDescriptor;
}
Expand Down
Loading

0 comments on commit ebeca76

Please sign in to comment.