Skip to content
Permalink
Browse files

listen to workers modifing email and refresh current query

  • Loading branch information...
iNPUTmice committed Nov 7, 2019
1 parent 0456577 commit 2ed0e995dffc6ec1ef3c6efa72eae4c8aa6498af
@@ -159,6 +159,7 @@ public void removeFromMailbox(final String threadId, final IdentifiableMailboxWi
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RemoveFromMailboxWorker.class)
.setConstraints(CONNECTED_CONSTRAINT)
.setInputData(RemoveFromMailboxWorker.data(threadId, mailbox))
.addTag(MuaWorker.TAG_EMAIL_MODIFICATION)
.build();
final WorkManager workManager = WorkManager.getInstance(application);
workManager.enqueueUniqueWork(
@@ -179,6 +180,7 @@ public void archive(final String threadId) {
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ArchiveWorker.class)
.setConstraints(CONNECTED_CONSTRAINT)
.setInputData(ArchiveWorker.data(threadId))
.addTag(MuaWorker.TAG_EMAIL_MODIFICATION)
.build();
final WorkManager workManager = WorkManager.getInstance(application);
workManager.enqueueUniqueWork(
@@ -202,6 +204,7 @@ public void moveToInbox(final String threadId) {
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(MoveToInboxWorker.class)
.setConstraints(CONNECTED_CONSTRAINT)
.setInputData(AbstractMailboxModificationWorker.data(threadId))
.addTag(MuaWorker.TAG_EMAIL_MODIFICATION)
.build();
final WorkManager workManager = WorkManager.getInstance(application);
workManager.enqueueUniqueWork(
@@ -224,6 +227,7 @@ public void moveToTrash(final String threadId) {
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(MoveToTrashWorker.class)
.setConstraints(CONNECTED_CONSTRAINT)
.setInputData(MoveToTrashWorker.data(threadId))
.addTag(MuaWorker.TAG_EMAIL_MODIFICATION)
.build();
final WorkManager workManager = WorkManager.getInstance(application);
workManager.enqueueUniqueWork(
@@ -254,6 +258,7 @@ private void toggleKeyword(final String threadId, final String keyword, final bo
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ModifyKeywordWorker.class)
.setConstraints(CONNECTED_CONSTRAINT)
.setInputData(ModifyKeywordWorker.data(threadId, keyword, targetState))
.addTag(MuaWorker.TAG_EMAIL_MODIFICATION)
.build();
final WorkManager workManager = WorkManager.getInstance(application);
workManager.enqueueUniqueWork(
@@ -28,6 +28,9 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
@@ -39,6 +42,8 @@

public class QueryRepository extends LttrsRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(QueryRepository.class);


private final Set<String> runningQueries = new HashSet<>();
private final Set<String> runningPagingRequests = new HashSet<>();
@@ -108,6 +113,18 @@ public void refresh(final EmailQuery emailQuery) {
}, MoreExecutors.directExecutor());
}

public void refreshInBackground(final EmailQuery emailQuery) {
final String queryString = emailQuery.toQueryString();
synchronized (this) {
if (runningQueries.contains(queryString) || runningPagingRequests.contains(queryString)) {
LOGGER.debug("skipping background refresh");
return;
}
LOGGER.info("started background refresh");
mua.query(emailQuery);
}
}


public void requestNextPage(final EmailQuery emailQuery, String afterEmailId) {
final String queryString = emailQuery.toQueryString();
@@ -18,6 +18,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -30,6 +31,10 @@
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.work.WorkInfo;

import java.util.List;

import rs.ltt.android.MainNavigationDirections;
import rs.ltt.android.R;
import rs.ltt.android.databinding.FragmentThreadListBinding;
@@ -78,6 +83,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
//((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

viewModel.isRunningPagingRequest().observe(getViewLifecycleOwner(), threadOverviewAdapter::setLoading);

viewModel.getEmailModificationWorkInfo().observe(getViewLifecycleOwner(), this::emailModification);

threadOverviewAdapter.setOnFlaggedToggledListener(this);
threadOverviewAdapter.setOnThreadClickedListener(this);

@@ -90,6 +98,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
return binding.getRoot();
}

private void emailModification(boolean allDone) {
if (allDone) {
getQueryViewModel().refreshInBackground();
}
}

protected void onLabelOpened(Label label) {
final Activity activity = getActivity();
if (activity instanceof OnLabelOpened) {
@@ -22,24 +22,29 @@
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Transformations;
import androidx.paging.PagedList;
import androidx.work.WorkManager;

import rs.ltt.android.entity.ThreadOverviewItem;
import rs.ltt.android.repository.QueryRepository;
import rs.ltt.android.util.WorkInfoUtil;
import rs.ltt.android.worker.MuaWorker;
import rs.ltt.jmap.common.entity.query.EmailQuery;

public abstract class AbstractQueryViewModel extends AndroidViewModel {

final QueryRepository queryRepository;

private final LiveData<Boolean> emailModificationWorkInfo;
private LiveData<PagedList<ThreadOverviewItem>> threads;

private LiveData<Boolean> refreshing;

private LiveData<Boolean> runningPagingRequest;

AbstractQueryViewModel(@NonNull Application application) {
super(application);

final WorkManager workManager = WorkManager.getInstance(application);

this.queryRepository = new QueryRepository(application);
this.emailModificationWorkInfo = Transformations.map(workManager.getWorkInfosByTagLiveData(MuaWorker.TAG_EMAIL_MODIFICATION), WorkInfoUtil::allDone);
}

void init() {
@@ -72,13 +77,25 @@ void init() {
return liveData;
}

public LiveData<Boolean> getEmailModificationWorkInfo() {
return emailModificationWorkInfo;
}

public void onRefresh() {
final EmailQuery emailQuery = getQuery().getValue();
if (emailQuery != null) {
queryRepository.refresh(emailQuery);
}
}

public void refreshInBackground() {
final EmailQuery emailQuery = getQuery().getValue();
if (emailQuery != null) {
queryRepository.refreshInBackground(emailQuery);
}
}


protected abstract LiveData<EmailQuery> getQuery();

public void toggleFlagged(String threadId, boolean target) {
@@ -0,0 +1,49 @@
/*
* Copyright 2019 Daniel Gultsch
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rs.ltt.android.util;

import androidx.work.WorkInfo;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;

import org.checkerframework.checker.nullness.compatqual.NullableDecl;

import java.util.Collection;
import java.util.List;

public class WorkInfoUtil {


private static Collection<WorkInfo.State> transform(List<WorkInfo> info) {
return Collections2.transform(info, new Function<WorkInfo, WorkInfo.State>() {
@NullableDecl
@Override
public WorkInfo.State apply(@NullableDecl WorkInfo input) {
return input == null ? null : input.getState();
}
});
}

public static boolean allDone(List<WorkInfo> info) {
return Iterables.all(
transform(info),
s -> s != null && s != WorkInfo.State.ENQUEUED && s != WorkInfo.State.RUNNING
);
}

}
@@ -31,7 +31,6 @@

public abstract class AbstractMailboxModificationWorker extends MuaWorker {


private static final String THREAD_ID_KEY = "threadId";

protected final String threadId;
@@ -50,6 +49,7 @@ public Result doWork() {
try {
Boolean result = modify(emails).get();
Log.d("lttrs", getClass().getSimpleName() + ": made changes to " + threadId + ": " + result);
//TODO only if we haven’t made changes?
database.overwriteDao().deleteMailboxOverwritesByThread(threadId);
return Result.success();
} catch (ExecutionException e) {
@@ -34,6 +34,8 @@

public abstract class MuaWorker extends Worker {

public static final String TAG_EMAIL_MODIFICATION = "email_modification";

public static final String SYNC = "sync";
public static final String SYNC_LABELS = "sync_labels";

0 comments on commit 2ed0e99

Please sign in to comment.
You can’t perform that action at this time.