Skip to content

Commit

Permalink
Capabilities along with layers and usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
davigonz committed Jul 15, 2019
1 parent 9e3e8c0 commit 0ff325c
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class OCCapabilityViewModelTest {
val ocCapabilityRepository = mockkClass(OCCapabilityRepository::class)

every {
ocCapabilityRepository.getCapabilityForAccount(
ocCapabilityRepository.refreshCapabilitiesForAccount(
"admin@server"
)
} returns MutableLiveData<DataResult<OCCapabilityEntity>>().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener {
override fun removePublicShare(share: OCShareEntity) {
}

override fun refreshCapabilities(shouldFetchFromNetwork: Boolean) {
override fun observeCapabilities(shouldFetchFromNetwork: Boolean) {
val publicShareDialogFragment: PublicShareDialogFragment =
supportFragmentManager.findFragmentByTag("TEST FRAGMENT") as PublicShareDialogFragment
publicShareDialogFragment.updateCapabilities(capabilities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,84 @@ package com.owncloud.android.presentation.capabilities
import android.accounts.Account
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import com.owncloud.android.data.capabilities.datasources.OCLocalCapabilitiesDataSource
import com.owncloud.android.data.capabilities.datasources.OCRemoteCapabilitiesDataSource
import androidx.lifecycle.viewModelScope
import com.owncloud.android.data.capabilities.db.OCCapabilityEntity
import com.owncloud.android.data.capabilities.CapabilityRepository
import com.owncloud.android.domain.capabilities.OCCapabilityRepository
import com.owncloud.android.lib.common.OwnCloudAccount
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
import com.owncloud.android.data.DataResult
import com.owncloud.android.domain.capabilities.usecases.CapabilitiesLiveDataUseCase
import com.owncloud.android.domain.capabilities.usecases.RefreshCapabilitiesUseCase
import com.owncloud.android.operations.common.OperationType
import com.owncloud.android.presentation.UIResult
import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* View Model to keep a reference to the capability repository and an up-to-date capability
*/
class OCCapabilityViewModel(
context: Context,
val context: Context,
val account: Account,
val capabilityRepository: CapabilityRepository = OCCapabilityRepository.create(
localCapabilitiesDataSource = OCLocalCapabilitiesDataSource(
context
),
remoteCapabilitiesDataSource = OCRemoteCapabilitiesDataSource(
OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(
OwnCloudAccount(account, context),
context
)
)
)
capabilitiesLiveDataUseCase: CapabilitiesLiveDataUseCase = CapabilitiesLiveDataUseCase(context, account),
private val refreshCapabilitiesUseCase: RefreshCapabilitiesUseCase = RefreshCapabilitiesUseCase(context, account)
) : ViewModel() {
fun getCapabilityForAccount(shouldFetchFromNetwork: Boolean = true): LiveData<DataResult<OCCapabilityEntity>> =
capabilityRepository.getCapabilityForAccount(account.name, shouldFetchFromNetwork)

private val _capabilities = MutableLiveData<UIResult<OCCapabilityEntity>>()
val capabilities: LiveData<UIResult<OCCapabilityEntity>> = _capabilities

private var capabilitiesLiveData: LiveData<OCCapabilityEntity>? = capabilitiesLiveDataUseCase.execute(
CapabilitiesLiveDataUseCase.Params(
accountName = account.name
)
).data

// to detect changes in capabilities
private val capabilitiesObserver: Observer<OCCapabilityEntity> = Observer { capabilities ->
if (capabilities != null) {
_capabilities.postValue(UIResult.success(capabilities))
}
}

init {
capabilitiesLiveData?.observeForever(capabilitiesObserver)

viewModelScope.launch {
withContext(Dispatchers.IO) {
refreshCapabilities()
}
}
}

private fun refreshCapabilities() {
_capabilities.postValue(
UIResult.loading(capabilitiesLiveData?.value)
)

refreshCapabilitiesUseCase.execute(
RefreshCapabilitiesUseCase.Params(
accountName = account.name
)
).also { useCaseResult ->
if (!useCaseResult.isSuccess()) {
_capabilities.postValue(
UIResult.error(
capabilitiesLiveData?.value,
errorMessage = useCaseResult.msg ?: ErrorMessageAdapter.getResultMessage(
useCaseResult.code,
useCaseResult.exception,
OperationType.GET_CAPABILITIES,
context.resources
)
)
)
}
}
}

override fun onCleared() {
super.onCleared()
capabilitiesLiveData?.removeObserver(capabilitiesObserver)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,10 @@ class OCShareViewModel(
val context: Context,
val account: Account,
sharesLiveDataUseCase: SharesLiveDataUseCase = SharesLiveDataUseCase(context, account),
private val refreshSharesUseCase: RefreshSharesUseCase = RefreshSharesUseCase(
context, account
),
private val createPublicShareUseCase: CreatePublicShareUseCase = CreatePublicShareUseCase(
context, account
),
private val editPublicShareUseCase: EditPublicShareUseCase = EditPublicShareUseCase(
context, account
),
private val deletePublicShareUseCase: DeletePublicShareUseCase = DeletePublicShareUseCase(
context, account
)
private val refreshSharesUseCase: RefreshSharesUseCase = RefreshSharesUseCase(context, account),
private val createPublicShareUseCase: CreatePublicShareUseCase = CreatePublicShareUseCase(context, account),
private val editPublicShareUseCase: EditPublicShareUseCase = EditPublicShareUseCase(context, account),
private val deletePublicShareUseCase: DeletePublicShareUseCase = DeletePublicShareUseCase(context, account)
) : ViewModel() {

private val _shares = MutableLiveData<UIResult<List<OCShareEntity>>>()
Expand All @@ -73,9 +65,7 @@ class OCShareViewModel(

// To detect changes in shares
private val sharesObserver: Observer<List<OCShareEntity>> = Observer { shares ->
if (shares.isNotEmpty()) {
_shares.postValue(UIResult.success(shares))
}
_shares.postValue(UIResult.success(shares))
}

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,55 +167,53 @@ class ShareActivity : FileActivity(), ShareFragmentListener {
}

override fun startShareObservers() {
refreshCapabilities()
observeCapabilities()
observeShares()
observePublicShareCreation()
observePublicShareEdition()
observePublicShareDeletion()
}

override fun refreshCapabilities(shouldFetchFromNetwork: Boolean) {
// ocCapabilityViewModel.getCapabilityForAccount(shouldFetchFromNetwork).observe(
// this,
// Observer { resource ->
// when (resource?.status) {
// SUCCESS -> {
// if (publicShareFragment != null) {
// publicShareFragment?.updateCapabilities(resource.data)
// } else {
// shareFileFragment?.updateCapabilities(resource.data)
// }
// dismissLoadingDialog()
// }
// ERROR -> {
// val errorMessage = ErrorMessageAdapter.getResultMessage(
// resource.code,
// resource.exception,
// OperationType.GET_CAPABILITIES,
// resources
// )
// if (publicShareFragment != null) {
// publicShareFragment?.showError(errorMessage)
// } else {
// Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT)
// .show()
// shareFileFragment?.updateCapabilities(resource.data)
// }
// dismissLoadingDialog()
// }
// LOADING -> {
// showLoadingDialog(R.string.common_loading)
// if (publicShareFragment != null) {
// publicShareFragment?.updateCapabilities(resource.data)
// } else {
// shareFileFragment?.updateCapabilities(resource.data)
// }
// }
// else -> {
// Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}")
// }
// }
// }
// )
override fun observeCapabilities(shouldFetchFromNetwork: Boolean) {
ocCapabilityViewModel.capabilities.observe(
this,
Observer { uiResult ->
when (uiResult?.status) {
Status.SUCCESS -> {
if (publicShareFragment != null) {
publicShareFragment?.updateCapabilities(uiResult.data)
} else {
shareFileFragment?.updateCapabilities(uiResult.data)
}
dismissLoadingDialog()
}
Status.ERROR -> {
if (publicShareFragment != null) {
publicShareFragment?.showError(uiResult.errorMessage!!)
} else {
Snackbar.make(
findViewById(android.R.id.content),
uiResult.errorMessage!!,
Snackbar.LENGTH_SHORT
).show()
shareFileFragment?.updateCapabilities(uiResult.data)
}
dismissLoadingDialog()
}
Status.LOADING -> {
showLoadingDialog(R.string.common_loading)
if (publicShareFragment != null) {
publicShareFragment?.updateCapabilities(uiResult.data)
} else {
shareFileFragment?.updateCapabilities(uiResult.data)
}
}
else -> {
Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}")
}
}
}
)
}

private fun observeShares() {
Expand Down Expand Up @@ -257,18 +255,14 @@ class ShareActivity : FileActivity(), ShareFragmentListener {
share.shareType == ShareType.GROUP.value ||
share.shareType == ShareType.FEDERATED.value
}.also { privateShares ->
if (privateShares?.isNotEmpty()!!) {
updatePrivateSharesInFileFragment(privateShares)
updatePrivateSharesInSearchShareesFragment(privateShares)
}
updatePrivateSharesInFileFragment(privateShares)
updatePrivateSharesInSearchShareesFragment(privateShares)
}

shares?.filter { share ->
share.shareType == ShareType.PUBLIC_LINK.value
}.also { publicShares ->
if (publicShares?.isNotEmpty()!!) {
shareFileFragment?.updatePublicShares(publicShares as ArrayList<OCShareEntity>)
}
shareFileFragment?.updatePublicShares(publicShares as ArrayList<OCShareEntity>)
}
}

Expand Down Expand Up @@ -384,6 +378,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener {
*********************************************** PUBLIC SHARES ************************************************
**************************************************************************************************************/

// Share creation
private fun observePublicShareCreation() {
ocShareViewModel.publicShareCreationStatus.observe(
this,
Expand Down Expand Up @@ -443,6 +438,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener {
)
}

// Share edition
private fun observePublicShareEdition() {
ocShareViewModel.publicShareEditionStatus.observe(
this,
Expand Down Expand Up @@ -495,6 +491,32 @@ class ShareActivity : FileActivity(), ShareFragmentListener {
)
}

// Share deletion
private fun observePublicShareDeletion() {
ocShareViewModel.publicShareDeletionStatus.observe(
this,
Observer { uiResult ->
when (uiResult?.status) {
Status.SUCCESS -> {
publicShareFragment?.dismiss()
}
Status.ERROR -> {
publicShareFragment?.showError(uiResult.errorMessage!!)
dismissLoadingDialog()
}
Status.LOADING -> {
showLoadingDialog(R.string.common_loading)
}
else -> {
Log.d(
TAG, "Unknown status when removing public share"
)
}
}
}
)
}

override fun showRemovePublicShare(share: OCShareEntity) {
val removePublicShareFragment = RemoveShareDialogFragment.newInstance(share)
showDialogFragment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class PublicShareDialogFragment : DialogFragment() {
super.onActivityCreated(savedInstanceState)
Log_OC.d(TAG, "onActivityCreated")

listener?.refreshCapabilities(false)
listener?.observeCapabilities(false)
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface ShareFragmentListener {

fun removePublicShare(remoteId: Long)

fun refreshCapabilities(shouldFetchFromNetwork: Boolean = true)
fun observeCapabilities(shouldFetchFromNetwork: Boolean = true)

fun copyOrSendPrivateLink(file: OCFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class OCLocalCapabilitiesDataSourceTest {
)

every {
ocCapabilityDao.getCapabilityForAccount(
ocCapabilityDao.getCapabilitiesForAccountAsLiveData(
"user@server1"
)
} returns capabilityAsLiveData
Expand All @@ -69,7 +69,7 @@ class OCLocalCapabilitiesDataSourceTest {
)

every {
ocCapabilityDao.getCapabilityForAccount(
ocCapabilityDao.getCapabilitiesForAccountAsLiveData(
"user@server2"
)
} returns newCapabilityAsLiveData
Expand All @@ -83,7 +83,7 @@ class OCLocalCapabilitiesDataSourceTest {
@Test
fun readLocalCapability() {
val capability = getValue(
ocLocalCapabilitiesDataSource.getCapabilityForAccountAsLiveData(
ocLocalCapabilitiesDataSource.getCapabilitiesForAccountAsLiveData(
"user@server1"
)
)
Expand All @@ -107,7 +107,7 @@ class OCLocalCapabilitiesDataSourceTest {
)

val capability = getValue(
ocLocalCapabilitiesDataSource.getCapabilityForAccountAsLiveData(
ocLocalCapabilitiesDataSource.getCapabilitiesForAccountAsLiveData(
"user@server2"
)
)
Expand Down
Loading

0 comments on commit 0ff325c

Please sign in to comment.