Skip to content

Commit

Permalink
Merge pull request #3457 from owncloud/new_arch/footer_and_other_changes
Browse files Browse the repository at this point in the history
New arch/footer and other changes
  • Loading branch information
fesave authored Nov 23, 2021
2 parents 06de306 + 3f8d605 commit 14c8d04
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 9 deletions.
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()) }
viewModel { MainFileListViewModel(get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sealed class UIResult<out T> {
val isSuccess get() = this is Success
val isError get() = this is Error

@Deprecated(message = "Start to use new extensions")
fun getStoredData(): T? =
when (this) {
is Loading -> data
Expand All @@ -41,3 +42,30 @@ sealed class UIResult<out T> {
else -> null
}
}

fun <T> UIResult<T>.onLoading(action: (data: T?) -> Unit): UIResult<T> {
if (this is UIResult.Loading) action(data)
return this
}

fun <T> UIResult<T>.onSuccess(action: (data: T?) -> Unit): UIResult<T> {
if (this is UIResult.Success) action(data)
return this
}

fun <T> UIResult<T>.onError(action: (error: Throwable?) -> Unit): UIResult<T> {
if (this is UIResult.Error) action(error)
return this
}

fun <T> UIResult<T>.fold(
onLoading: (data: T?) -> Unit,
onSuccess: (data: T?) -> Unit,
onFailure: (error: Throwable?) -> Unit
) {
when (this) {
is UIResult.Loading -> onLoading(data)
is UIResult.Success -> onSuccess(data)
is UIResult.Error -> onFailure(error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class FileListAdapter(

override fun getItemId(position: Int): Long = position.toLong()

fun getItem(position: Int): Any? {
return if (files.size <= position) {
null
} else files[position]
}

override fun getItemViewType(position: Int): Int = position

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import androidx.recyclerview.widget.LinearLayoutManager
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.UIResult
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

class MainFileListFragment : Fragment() {
Expand Down Expand Up @@ -68,11 +70,27 @@ class MainFileListFragment : Fragment() {

private fun subscribeToViewModels() {
//Observe the action of retrieving the list of files.
mainFileListViewModel.getFilesListStatusLiveData.observe(viewLifecycleOwner, Event.EventObserver { result ->
when (result) {
is UIResult.Error -> {} //TODO Manage Error
is UIResult.Loading -> {} //TODO Manage Loading
is UIResult.Success -> fileListAdapter.updateFileList(filesToAdd = result.data ?: emptyList())
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 {
it.onSuccess { data ->
if (!isShowingJustFolders()) {
mainFileListViewModel.generateFooterText(data!!.first, data.second)
}
}
})

mainFileListViewModel.footerText.observe(viewLifecycleOwner, Event.EventObserver {
it.onSuccess { data ->
setFooterText(data)
}
})
}
Expand All @@ -81,12 +99,29 @@ class MainFileListFragment : Fragment() {
mainFileListViewModel.listDirectory(directory = directory)
}

private fun setFooterText(text: String?) {
if (text?.isNotEmpty() == true) {
binding.footerMainFileList.footerText.text = text
// TODO Manage footer enable/disable options
//setFooterEnabled(true)
} else {
//setFooterEnabled(false)
}
}

private fun isShowingJustFolders(): Boolean {
val args = arguments
return args != null && args.getBoolean(ARG_JUST_FOLDERS, false)
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}

companion object {
val ARG_JUST_FOLDERS = MainFileListFragment::class.java.canonicalName + ".JUST_FOLDERS"

fun newInstance(): MainFileListFragment {
val args = Bundle()
return MainFileListFragment().apply { arguments = args }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ import androidx.lifecycle.LiveData
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.providers.ContextProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

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

private var file: OCFile? = null
Expand All @@ -42,6 +45,14 @@ class MainFileListViewModel(
val getFilesListStatusLiveData: LiveData<Event<UIResult<List<OCFile>>>>
get() = _getFilesListStatusLiveData

private val _numberOfFilesPerType = MutableLiveData<Event<UIResult<Pair<Int, Int>>>>()
val numberOfFilesPerType: LiveData<Event<UIResult<Pair<Int, Int>>>>
get() = _numberOfFilesPerType

private val _footerText = MutableLiveData<Event<UIResult<String>>>()
val footerText: LiveData<Event<UIResult<String>>>
get() = _footerText

private fun getFilesList(folderId: Long) {
viewModelScope.launch(Dispatchers.IO) {
_getFilesListStatusLiveData.postValue(Event(UIResult.Loading()))
Expand All @@ -54,6 +65,77 @@ class MainFileListViewModel(
}
}

fun manageListOfFiles(list: List<OCFile>) {
var filesCount = 0
var foldersCount = 0
val count: Int = list.size
var file: OCFile
for (i in 0 until count) {
file = list[i]
if (file.isFolder) {
foldersCount++
} else {
if (!file.isHidden) {
filesCount++
}
}
}

_numberOfFilesPerType.postValue(Event(UIResult.Success(Pair(foldersCount, filesCount))))
}

fun generateFooterText(filesCount: Int, foldersCount: Int) {
_footerText.postValue(
Event(
UIResult.Success(
when {
filesCount <= 0 -> {
when {
foldersCount <= 0 -> {
""
}
foldersCount == 1 -> {
contextProvider.getContext().getString(R.string.file_list__footer__folder)
}
else -> { // foldersCount > 1
contextProvider.getContext().getString(R.string.file_list__footer__folders, foldersCount)
}
}
}
filesCount == 1 -> {
when {
foldersCount <= 0 -> {
contextProvider.getContext().getString(R.string.file_list__footer__file)
}
foldersCount == 1 -> {
contextProvider.getContext().getString(R.string.file_list__footer__file_and_folder)
}
else -> { // foldersCount > 1
contextProvider.getContext().getString(R.string.file_list__footer__file_and_folders, foldersCount)
}
}
}
else -> { // filesCount > 1
when {
foldersCount <= 0 -> {
contextProvider.getContext().getString(R.string.file_list__footer__files, filesCount)
}
foldersCount == 1 -> {
contextProvider.getContext().getString(R.string.file_list__footer__files_and_folder, filesCount)
}
else -> { // foldersCount > 1
contextProvider.getContext().getString(
R.string.file_list__footer__files_and_folders, filesCount, foldersCount
)
}
}
}
}
)
)
)
}

fun listDirectory(directory: OCFile) {
getFilesList(directory.id!!)
}
Expand Down
12 changes: 11 additions & 1 deletion owncloudApp/src/main/res/layout/main_file_list_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
android:id="@+id/recyclerView_main_file_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/footer_main_file_list"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_file_list" />

<include
android:id="@+id/footer_main_file_list"
layout="@layout/list_footer"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/recyclerView_main_file_list" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 14c8d04

Please sign in to comment.