2019-04-04 22:40:39 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* 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/. */
|
2019-04-04 22:40:39 +02:00
|
|
|
|
|
|
|
package org.mozilla.fenix.library.bookmarks
|
|
|
|
|
2019-09-27 17:57:39 +02:00
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
import androidx.lifecycle.LiveData
|
2019-07-26 04:55:51 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData
|
2019-04-04 22:40:39 +02:00
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import mozilla.components.concept.storage.BookmarkNode
|
2019-09-27 17:57:39 +02:00
|
|
|
import mozilla.components.concept.sync.AccountObserver
|
|
|
|
import mozilla.components.concept.sync.AuthType
|
|
|
|
import mozilla.components.concept.sync.OAuthAccount
|
|
|
|
import mozilla.components.service.fxa.manager.FxaAccountManager
|
2019-04-04 22:40:39 +02:00
|
|
|
|
2019-09-27 17:57:39 +02:00
|
|
|
/**
|
|
|
|
* [ViewModel] that shares data between various bookmarks fragments.
|
|
|
|
*/
|
|
|
|
class BookmarksSharedViewModel : ViewModel(), AccountObserver {
|
|
|
|
|
|
|
|
private val signedInMutable = MutableLiveData(true)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the user is signed in.
|
|
|
|
*/
|
|
|
|
val signedIn: LiveData<Boolean> get() = signedInMutable
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently selected bookmark root.
|
|
|
|
*/
|
2019-04-04 22:40:39 +02:00
|
|
|
var selectedFolder: BookmarkNode? = null
|
2019-09-27 17:57:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the [signedIn] boolean once the account observer sees that the user logged in.
|
|
|
|
*/
|
|
|
|
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
|
|
|
|
signedInMutable.postValue(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the [signedIn] boolean once the account observer sees that the user logged out.
|
|
|
|
*/
|
|
|
|
override fun onLoggedOut() {
|
|
|
|
signedInMutable.postValue(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun observeAccountManager(accountManager: FxaAccountManager, owner: LifecycleOwner) {
|
|
|
|
accountManager.register(this, owner = owner)
|
|
|
|
if (accountManager.authenticatedAccount() != null) {
|
|
|
|
signedInMutable.postValue(true)
|
|
|
|
} else {
|
|
|
|
signedInMutable.postValue(false)
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:40:39 +02:00
|
|
|
}
|