From 8fbbcbf0eeb63b0946c586d271257755328d4ba6 Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Tue, 16 Jul 2019 16:07:24 -0700 Subject: [PATCH] Introduce a centralized places.kt which owns storage This lets us access storage backends from different parts of the system at the same time. E.g. from both BookmarksStore as well as the upcoming Services sync integration. --- .../vrbrowser/VRBrowserApplication.java | 7 ++++ .../vrbrowser/browser/BookmarksStore.kt | 41 ++++++++----------- .../org/mozilla/vrbrowser/browser/Places.kt | 16 ++++++++ 3 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 app/src/common/shared/org/mozilla/vrbrowser/browser/Places.kt diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java index 3badef3fb8..e99d8b0b23 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java @@ -7,18 +7,21 @@ import android.app.Application; +import org.mozilla.vrbrowser.browser.Places; import org.mozilla.vrbrowser.db.AppDatabase; import org.mozilla.vrbrowser.telemetry.TelemetryWrapper; public class VRBrowserApplication extends Application { private AppExecutors mAppExecutors; + private Places mPlaces; @Override public void onCreate() { super.onCreate(); mAppExecutors = new AppExecutors(); + mPlaces = new Places(this); TelemetryWrapper.init(this); } @@ -30,4 +33,8 @@ public AppDatabase getDatabase() { public DataRepository getRepository() { return DataRepository.getInstance(getDatabase(), mAppExecutors); } + + public Places getPlaces() { + return mPlaces; + } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/BookmarksStore.kt b/app/src/common/shared/org/mozilla/vrbrowser/browser/BookmarksStore.kt index 435a378236..aaa7ecfbfb 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/BookmarksStore.kt +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/BookmarksStore.kt @@ -8,62 +8,54 @@ package org.mozilla.vrbrowser.browser import android.content.Context import android.os.Handler import android.os.Looper -import android.util.Log import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.future.future -import mozilla.appservices.places.BookmarkRoot; -import mozilla.components.browser.storage.sync.PlacesBookmarksStorage +import mozilla.appservices.places.BookmarkRoot import mozilla.components.concept.storage.BookmarkNode -import mozilla.components.concept.storage.BookmarksStorage +import org.mozilla.vrbrowser.VRBrowserApplication import java.util.concurrent.CompletableFuture -class BookmarksStore constructor(aContext: Context) { - private var mContext: Context - private var mStorage: BookmarksStorage - private var mListeners = ArrayList() +class BookmarksStore constructor(val context: Context) { + private val listeners = ArrayList() + private val storage = (context.applicationContext as VRBrowserApplication).places.bookmarks interface BookmarkListener { fun onBookmarksUpdated() } - init { - mContext = aContext - mStorage = PlacesBookmarksStorage(aContext) - } - fun addListener(aListener: BookmarkListener) { - if (!mListeners.contains(aListener)) { - mListeners.add(aListener) + if (!listeners.contains(aListener)) { + listeners.add(aListener) } } fun removeListener(aListener: BookmarkListener) { - mListeners.remove(aListener) + listeners.remove(aListener) } fun removeAllListeners() { - mListeners.clear() + listeners.clear() } fun getBookmarks(): CompletableFuture?> = GlobalScope.future { - mStorage.getTree(BookmarkRoot.Mobile.id, true)?.children?.toMutableList() + storage.getTree(BookmarkRoot.Mobile.id)?.children?.toMutableList() } fun addBookmark(aURL: String, aTitle: String) = GlobalScope.future { - mStorage.addItem(BookmarkRoot.Mobile.id, aURL, aTitle, null) + storage.addItem(BookmarkRoot.Mobile.id, aURL, aTitle, null) notifyListeners() } fun deleteBookmarkByURL(aURL: String) = GlobalScope.future { val bookmark = getBookmarkByUrl(aURL) if (bookmark != null) { - mStorage.deleteNode(bookmark.guid) + storage.deleteNode(bookmark.guid) } notifyListeners() } fun deleteBookmarkById(aId: String) = GlobalScope.future { - mStorage.deleteNode(aId) + storage.deleteNode(aId) notifyListeners() } @@ -73,7 +65,7 @@ class BookmarksStore constructor(aContext: Context) { private suspend fun getBookmarkByUrl(aURL: String): BookmarkNode? { - val bookmarks: List? = mStorage.getBookmarksWithUrl(aURL) + val bookmarks: List? = storage.getBookmarksWithUrl(aURL) if (bookmarks == null || bookmarks.isEmpty()) { return null } @@ -88,8 +80,8 @@ class BookmarksStore constructor(aContext: Context) { } private fun notifyListeners() { - if (mListeners.size > 0) { - val listenersCopy = ArrayList(mListeners) + if (listeners.size > 0) { + val listenersCopy = ArrayList(listeners) Handler(Looper.getMainLooper()).post { for (listener in listenersCopy) { listener.onBookmarksUpdated() @@ -98,4 +90,3 @@ class BookmarksStore constructor(aContext: Context) { } } } - diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/Places.kt b/app/src/common/shared/org/mozilla/vrbrowser/browser/Places.kt new file mode 100644 index 0000000000..44e15c761b --- /dev/null +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/Places.kt @@ -0,0 +1,16 @@ +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.vrbrowser.browser + +import android.content.Context +import mozilla.components.browser.storage.sync.PlacesBookmarksStorage + +/** + * Entry point for interacting with places-backed storage layers. + */ +class Places(context: Context) { + val bookmarks by lazy { PlacesBookmarksStorage(context) } +} \ No newline at end of file