From 66e00c7868a030d673d01a5cfa2065bb788d8c1c Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Wed, 6 Nov 2019 17:09:43 +0100 Subject: [PATCH] Avoid removing history items when updating history --- .../org/mozilla/vrbrowser/browser/engine/Session.java | 10 +++------- .../org/mozilla/vrbrowser/ui/views/HistoryView.java | 11 +++++++++++ .../mozilla/vrbrowser/ui/widgets/WindowWidget.java | 10 ++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java index a667a898a..17e883a92 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java @@ -9,7 +9,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; -import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; import android.view.Surface; @@ -47,7 +46,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.LinkedList; -import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -1236,11 +1234,9 @@ public void onHistoryStateChange(@NonNull GeckoSession aSession, @NonNull GeckoS } else { mQueuedCalls.add(() -> { - new Handler(mContext.getMainLooper()).postDelayed(() -> { - if (mHistoryDelegate != null) { - mHistoryDelegate.onHistoryStateChange(aSession, historyList); - } - }, 100); + if (mHistoryDelegate != null) { + mHistoryDelegate.onHistoryStateChange(aSession, historyList); + } }); } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java index ad1884e6a..b514901de 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/HistoryView.java @@ -41,7 +41,11 @@ import java.util.Comparator; import java.util.GregorianCalendar; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; import mozilla.components.concept.storage.VisitInfo; @@ -277,6 +281,12 @@ public void onAuthenticationProblems() { } }; + @NotNull + public static Predicate distinctByUrl(Function keyExtractor) { + Set seen = ConcurrentHashMap.newKeySet(); + return t -> seen.add(keyExtractor.apply(t)); + } + private void updateHistory() { Calendar date = new GregorianCalendar(); date.set(Calendar.HOUR_OF_DAY, 0); @@ -292,6 +302,7 @@ private void updateHistory() { List orderedItems = items.stream() .sorted(Comparator.comparing(VisitInfo::getVisitTime) .reversed()) + .filter(distinctByUrl(VisitInfo::getUrl)) .collect(Collectors.toList()); addSection(orderedItems, getResources().getString(R.string.history_section_today), Long.MAX_VALUE, todayLimit); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index b55031e61..b5dc5731f 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -1575,15 +1575,9 @@ public GeckoResult onVisited(@NonNull GeckoSession geckoSession, @NonNu } } - SessionStore.get().getHistoryStore().deleteVisitsFor(url).thenAcceptAsync(result -> { - SessionStore.get().getHistoryStore().recordVisit(url, pageVisit); - SessionStore.get().getHistoryStore().recordObservation(url, new PageObservation(url)); + SessionStore.get().getHistoryStore().recordVisit(url, pageVisit); + SessionStore.get().getHistoryStore().recordObservation(url, new PageObservation(url)); - }, mUIThreadExecutor).exceptionally(throwable -> { - Log.d(LOGTAG, "Error deleting history: " + throwable.getLocalizedMessage()); - throwable.printStackTrace(); - return null; - }); return GeckoResult.fromValue(true); }