Skip to content

Commit

Permalink
Merge pull request #3461 from owncloud/new_arch/file_list_isolation_flow
Browse files Browse the repository at this point in the history
Retrieve data from DB as LiveData has been implemented.
  • Loading branch information
fesave authored Nov 25, 2021
2 parents 14c8d04 + db6c763 commit 553cb37
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 20 deletions.
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 @@ -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 @@ -21,27 +21,24 @@
package com.owncloud.android.presentation.ui.files.filelist

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
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.files.usecases.GetFolderContentAsLiveDataUseCase
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.UIResult
import com.owncloud.android.providers.ContextProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainFileListViewModel(
private val getFolderContentUseCase: GetFolderContentUseCase,
private val getFolderContentAsLiveDataUseCase: GetFolderContentAsLiveDataUseCase,
private val contextProvider: ContextProvider,
) : ViewModel() {

private var file: OCFile? = null

private val _getFilesListStatusLiveData = MutableLiveData<Event<UIResult<List<OCFile>>>>()
private val _getFilesListStatusLiveData = MediatorLiveData<Event<UIResult<List<OCFile>>>>()
val getFilesListStatusLiveData: LiveData<Event<UIResult<List<OCFile>>>>
get() = _getFilesListStatusLiveData

Expand All @@ -54,14 +51,11 @@ 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())))
}
}
val filesListLiveData: LiveData<List<OCFile>> =
getFolderContentAsLiveDataUseCase.execute(GetFolderContentAsLiveDataUseCase.Params(folderId = folderId))

_getFilesListStatusLiveData.addSource(filesListLiveData) {
_getFilesListStatusLiveData.postValue(Event(UIResult.Success(it)))
}
}

Expand Down Expand Up @@ -140,3 +134,5 @@ class MainFileListViewModel(
getFilesList(directory.id!!)
}
}


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)

}

0 comments on commit 553cb37

Please sign in to comment.