Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Introduce a centralized places.kt which owns storage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Grisha Kruglov committed Jul 16, 2019
1 parent ee5aba1 commit 8fbbcbf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -30,4 +33,8 @@ public AppDatabase getDatabase() {
public DataRepository getRepository() {
return DataRepository.getInstance(getDatabase(), mAppExecutors);
}

public Places getPlaces() {
return mPlaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookmarkListener>()
class BookmarksStore constructor(val context: Context) {
private val listeners = ArrayList<BookmarkListener>()
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<List<BookmarkNode>?> = 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()
}

Expand All @@ -73,7 +65,7 @@ class BookmarksStore constructor(aContext: Context) {


private suspend fun getBookmarkByUrl(aURL: String): BookmarkNode? {
val bookmarks: List<BookmarkNode>? = mStorage.getBookmarksWithUrl(aURL)
val bookmarks: List<BookmarkNode>? = storage.getBookmarksWithUrl(aURL)
if (bookmarks == null || bookmarks.isEmpty()) {
return null
}
Expand All @@ -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()
Expand All @@ -98,4 +90,3 @@ class BookmarksStore constructor(aContext: Context) {
}
}
}

16 changes: 16 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/browser/Places.kt
Original file line number Diff line number Diff line change
@@ -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) }
}

0 comments on commit 8fbbcbf

Please sign in to comment.