Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve data from DB as LiveData has been implemented. #3461

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.owncloud.android.domain.files.usecases.CreateFolderAsyncUseCase
import com.owncloud.android.domain.files.usecases.GetFileByIdUseCase
import com.owncloud.android.domain.files.usecases.GetFileByRemotePathUseCase
import com.owncloud.android.domain.files.usecases.GetFilesSharedByLinkUseCase
import com.owncloud.android.domain.files.usecases.GetFolderContentAsLiveDataUseCase
import com.owncloud.android.domain.files.usecases.GetFolderContentUseCase
import com.owncloud.android.domain.files.usecases.GetFolderImagesUseCase
import com.owncloud.android.domain.files.usecases.MoveFileUseCase
Expand Down Expand Up @@ -92,6 +93,7 @@ val useCaseModule = module {
factory { GetFileByIdUseCase(get()) }
factory { GetFileByRemotePathUseCase(get()) }
factory { GetFolderContentUseCase(get()) }
factory { GetFolderContentAsLiveDataUseCase(get()) }
factory { GetFolderImagesUseCase(get()) }
factory { MoveFileUseCase(get()) }
factory { RefreshFolderFromServerAsyncUseCase(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ val viewModelModule = module {
viewModel { PreviewImageViewModel(get(), get(), get()) }
viewModel { FileDetailsViewModel(get(), get(), get(), get(), get()) }
viewModel { FileOperationViewModel(get(), get(), get(), get(), get(), get()) }
viewModel { MainFileListViewModel(get(), get()) }
viewModel { MainFileListViewModel(get(), get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.owncloud.android.presentation

import com.owncloud.android.domain.UseCaseResult

sealed class UIResult<out T> {
data class Loading<out T>(val data: T? = null) : UIResult<T>()
data class Success<out T>(val data: T? = null) : UIResult<T>()
Expand All @@ -41,6 +43,15 @@ sealed class UIResult<out T> {
is Error -> error
else -> null
}

companion object {
fun <T> fromResult(result: UseCaseResult<T>): UIResult<T> =
if (result.isSuccess) {
Success(result.getDataOrNull())
abelgardep marked this conversation as resolved.
Show resolved Hide resolved
} else {
Error(result.getThrowableOrNull())
}
}
abelgardep marked this conversation as resolved.
Show resolved Hide resolved
}

fun <T> UIResult<T>.onLoading(action: (data: T?) -> Unit): UIResult<T> {
Expand Down Expand Up @@ -69,3 +80,5 @@ fun <T> UIResult<T>.fold(
is UIResult.Error -> onFailure(error)
}
}

fun <T> UseCaseResult<T>.toUIResult() = UIResult.fromResult(this)
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import com.owncloud.android.databinding.MainFileListFragmentBinding
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.adapters.filelist.FileListAdapter
import com.owncloud.android.presentation.onError
import com.owncloud.android.presentation.onLoading
import com.owncloud.android.presentation.onSuccess
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down Expand Up @@ -69,15 +67,13 @@ class MainFileListFragment : Fragment() {
}

private fun subscribeToViewModels() {
//Observe the action of retrieving the list of files.
//Observe the action of retrieving the list of files from BBDD.
mainFileListViewModel.getFilesListStatusLiveData.observe(viewLifecycleOwner, Event.EventObserver {
it.onLoading { /*TODO Manage Loading*/ }
it.onSuccess { data ->
val files = data ?: emptyList()
fileListAdapter.updateFileList(filesToAdd = files)
mainFileListViewModel.manageListOfFiles(files)
}
it.onError { /*TODO Manage Error*/ }
})

mainFileListViewModel.numberOfFilesPerType.observe(viewLifecycleOwner, Event.EventObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.owncloud.android.R
import com.owncloud.android.domain.UseCaseResult
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.files.usecases.GetFolderContentUseCase
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.UIResult
import com.owncloud.android.presentation.toUIResult
import com.owncloud.android.providers.ContextProvider
import kotlinx.coroutines.Dispatchers
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import kotlinx.coroutines.launch

class MainFileListViewModel(
private val getFolderContentUseCase: GetFolderContentUseCase,
private val getFolderContentAsFlowUseCase: GetFolderContentUseCase,
abelgardep marked this conversation as resolved.
Show resolved Hide resolved
private val contextProvider: ContextProvider,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
abelgardep marked this conversation as resolved.
Show resolved Hide resolved
) : ViewModel() {

private var file: OCFile? = null
Expand All @@ -54,13 +55,9 @@ class MainFileListViewModel(
get() = _footerText

private fun getFilesList(folderId: Long) {
viewModelScope.launch(Dispatchers.IO) {
_getFilesListStatusLiveData.postValue(Event(UIResult.Loading()))
getFolderContentUseCase.execute(GetFolderContentUseCase.Params(folderId = folderId)).let {
when (it) {
is UseCaseResult.Error -> _getFilesListStatusLiveData.postValue(Event(UIResult.Error(it.getThrowableOrNull())))
is UseCaseResult.Success -> _getFilesListStatusLiveData.postValue(Event(UIResult.Success(it.getDataOrNull())))
}
viewModelScope.launch(coroutinesDispatcherProvider.io) {
with(getFolderContentAsFlowUseCase.execute(GetFolderContentUseCase.Params(folderId = folderId)).toUIResult()) {
_getFilesListStatusLiveData.postValue(Event(this))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

package com.owncloud.android.data.files.datasources

import androidx.lifecycle.LiveData
import com.owncloud.android.domain.files.model.OCFile

interface LocalFileDataSource {
fun copyFile(sourceFile: OCFile, targetFile: OCFile, finalRemotePath: String, remoteId: String)
fun getFileById(fileId: Long): OCFile?
fun getFileByRemotePath(remotePath: String, owner: String): OCFile?
fun getFolderContent(folderId: Long): List<OCFile>
fun getFolderContentAsLiveData(folderId: Long): LiveData<List<OCFile>>
fun getFolderImages(folderId: Long): List<OCFile>
fun getFilesSharedByLink(owner: String): List<OCFile>
fun moveFile(sourceFile: OCFile, targetFile: OCFile, finalRemotePath: String, finalStoragePath: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.owncloud.android.data.files.datasources.implementation

import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import com.owncloud.android.data.files.datasources.LocalFileDataSource
import com.owncloud.android.data.files.db.FileDao
import com.owncloud.android.data.files.db.OCFileEntity
Expand Down Expand Up @@ -67,6 +69,13 @@ class OCLocalFileDataSource(
it.toModel()
}

override fun getFolderContentAsLiveData(folderId: Long): LiveData<List<OCFile>> =
Transformations.map(fileDao.getFolderContentAsLiveData(folderId = folderId)) { list ->
list.map {
it.toModel()
}
}

override fun getFolderImages(folderId: Long): List<OCFile> =
fileDao.getFolderByMimeType(folderId = folderId, mimeType = MIME_PREFIX_IMAGE).map {
it.toModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.owncloud.android.data.files.db

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
Expand Down Expand Up @@ -45,6 +46,11 @@ abstract class FileDao {
folderId: Long
): List<OCFileEntity>

@Query(SELECT_FOLDER_CONTENT)
abstract fun getFolderContentAsLiveData(
folderId: Long
): LiveData<List<OCFileEntity>>

@Query(SELECT_FOLDER_BY_MIMETYPE)
abstract fun getFolderByMimeType(
folderId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.owncloud.android.data.files.repository

import androidx.lifecycle.LiveData
import com.owncloud.android.data.files.datasources.LocalFileDataSource
import com.owncloud.android.data.files.datasources.RemoteFileDataSource
import com.owncloud.android.data.storage.LocalStorageProvider
Expand Down Expand Up @@ -113,6 +114,9 @@ class OCFileRepository(
override fun getFolderContent(folderId: Long): List<OCFile> =
localFileDataSource.getFolderContent(folderId)

override fun getFolderContentAsLiveData(folderId: Long): LiveData<List<OCFile>> =
localFileDataSource.getFolderContentAsLiveData(folderId)

override fun getFolderImages(folderId: Long): List<OCFile> =
localFileDataSource.getFolderImages(folderId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.owncloud.android.domain.files

import androidx.lifecycle.LiveData
import com.owncloud.android.domain.files.model.OCFile

interface FileRepository {
Expand All @@ -28,6 +29,7 @@ interface FileRepository {
fun getFileById(fileId: Long): OCFile?
fun getFileByRemotePath(remotePath: String, owner: String): OCFile?
fun getFolderContent(folderId: Long): List<OCFile>
fun getFolderContentAsLiveData(folderId: Long): LiveData<List<OCFile>>
fun getFolderImages(folderId: Long): List<OCFile>
fun getFilesSharedByLink(owner: String): List<OCFile>
fun moveFile(listOfFilesToMove: List<OCFile>, targetFile: OCFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.domain.files.usecases

import androidx.lifecycle.LiveData
import com.owncloud.android.domain.BaseUseCase
import com.owncloud.android.domain.files.FileRepository
import com.owncloud.android.domain.files.model.OCFile

class GetFolderContentAsLiveDataUseCase(
private val repository: FileRepository
) : BaseUseCase<LiveData<List<OCFile>>, GetFolderContentAsLiveDataUseCase.Params>() {

override fun run(params: Params) = repository.getFolderContentAsLiveData(params.folderId)

data class Params(val folderId: Long)

}