Skip to content

Commit

Permalink
Added the different buttons of the transfer item view
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Jul 21, 2022
1 parent b52342b commit d9d073b
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import com.owncloud.android.domain.sharing.shares.usecases.EditPublicShareAsyncU
import com.owncloud.android.domain.sharing.shares.usecases.GetShareAsLiveDataUseCase
import com.owncloud.android.domain.sharing.shares.usecases.GetSharesAsLiveDataUseCase
import com.owncloud.android.domain.sharing.shares.usecases.RefreshSharesFromServerAsyncUseCase
import com.owncloud.android.domain.transfers.usecases.DeleteTransferWithIdUseCase
import com.owncloud.android.domain.transfers.usecases.GetAllTransfersUseCase
import com.owncloud.android.domain.user.usecases.GetStoredQuotaUseCase
import com.owncloud.android.domain.user.usecases.GetUserAvatarAsyncUseCase
Expand All @@ -80,6 +81,8 @@ import com.owncloud.android.usecases.transfers.downloads.DownloadFileUseCase
import com.owncloud.android.usecases.transfers.downloads.GetLiveDataForDownloadingFileUseCase
import com.owncloud.android.usecases.transfers.downloads.GetLiveDataForFinishedDownloadsFromAccountUseCase
import com.owncloud.android.usecases.transfers.uploads.CancelUploadForFileUseCase
import com.owncloud.android.usecases.transfers.uploads.CancelUploadWithIdUseCase
import com.owncloud.android.usecases.transfers.uploads.RetryUploadFromContentUriUseCase
import com.owncloud.android.usecases.transfers.uploads.RetryUploadFromSystemUseCase
import com.owncloud.android.usecases.transfers.uploads.UploadFileInConflictUseCase
import com.owncloud.android.usecases.transfers.uploads.UploadFilesFromSAFUseCase
Expand Down Expand Up @@ -149,7 +152,10 @@ val useCaseModule = module {
factory { UploadFileInConflictUseCase(get(), get()) }
factory { CancelUploadForFileUseCase(get(), get()) }
factory { RetryUploadFromSystemUseCase(get(), get()) }
factory { RetryUploadFromContentUriUseCase(get(), get()) }
factory { GetAllTransfersUseCase(get()) }
factory { CancelUploadWithIdUseCase(get(), get()) }
factory { DeleteTransferWithIdUseCase(get()) }

// User
factory { GetStoredQuotaUseCase(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ val viewModelModule = module {
viewModel { FileDetailsViewModel(get(), get(), get(), get(), get()) }
viewModel { FileOperationsViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { MainFileListViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { TransfersViewModel(get(), get(), get(), get()) }
viewModel { TransfersViewModel(get(), get(), get(), get(), get(), get(), get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ import com.owncloud.android.utils.MimetypeIconUtil
import timber.log.Timber
import java.io.File

class TransfersAdapter : RecyclerView.Adapter<TransfersAdapter.TransferItemViewHolder>() {
class TransfersAdapter(
val cancel: (Long) -> Unit,
val delete: (Long) -> Unit,
val retry: (OCTransfer) -> Unit,
) : RecyclerView.Adapter<TransfersAdapter.TransferItemViewHolder>() {

private val transfersList = mutableListOf<OCTransfer>()

Expand Down Expand Up @@ -165,7 +169,30 @@ class TransfersAdapter : RecyclerView.Adapter<TransfersAdapter.TransferItemViewH
MimetypeIconUtil.getFileTypeIconId(MimetypeIconUtil.getBestMimeTypeByFilename(transfer.localPath), fileName)
)
}
// TODO: progress bar, upload right button

uploadRightButton.isVisible = transfer.status != TransferStatus.TRANSFER_SUCCEEDED
holder.itemView.setOnClickListener(null)
if (transfer.status == TransferStatus.TRANSFER_IN_PROGRESS) {
uploadRightButton.apply {
setImageResource(R.drawable.ic_action_cancel_grey)
setOnClickListener {
cancel(transfer.id!!)
}
}
} else if (transfer.status == TransferStatus.TRANSFER_FAILED) {
uploadRightButton.apply {
setImageResource(R.drawable.ic_action_delete_grey)
setOnClickListener{
delete(transfer.id!!)
}
}
holder.itemView.setOnClickListener {
retry(transfer)
}
holder.binding.ListItemLayout.isClickable = true
holder.binding.ListItemLayout.isFocusable = true
}
// TODO: progress bar
}
}

Expand All @@ -183,11 +210,5 @@ class TransfersAdapter : RecyclerView.Adapter<TransfersAdapter.TransferItemViewH
class TransferItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val binding = UploadListItemBinding.bind(itemView)
}

interface Listener {
fun delete(transfer: OCTransfer)
fun retry(transfer: OCTransfer)
fun cancel(transfer: OCTransfer)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@

package com.owncloud.android.presentation.ui.transfers

import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.owncloud.android.R
import com.owncloud.android.authentication.AccountUtils
import com.owncloud.android.databinding.FragmentTransferListBinding
import com.owncloud.android.domain.transfers.model.OCTransfer
import com.owncloud.android.domain.transfers.model.TransferResult
import com.owncloud.android.presentation.adapters.transfers.TransfersAdapter
import com.owncloud.android.presentation.viewmodels.transfers.TransfersViewModel
import com.owncloud.android.ui.activity.FileActivity
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.io.File

class TransferListFragment : Fragment() {

Expand All @@ -42,7 +50,7 @@ class TransferListFragment : Fragment() {
private var _binding: FragmentTransferListBinding? = null
val binding get() = _binding!!

private val transfersAdapter = TransfersAdapter()
private lateinit var transfersAdapter: TransfersAdapter

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentTransferListBinding.inflate(inflater, container, false)
Expand All @@ -51,6 +59,34 @@ class TransferListFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
transfersAdapter = TransfersAdapter(
cancel = { id ->
transfersViewModel.cancelTransferWithId(id)
},
delete = { id ->
transfersViewModel.deleteTransferWithId(id)
},
retry = { transfer: OCTransfer ->
if (transfer.lastResult == TransferResult.CREDENTIAL_ERROR) {
val parentActivity = requireActivity() as FileActivity
val account = AccountUtils.getOwnCloudAccountByName(requireContext(), transfer.accountName)
parentActivity.fileOperationsHelper.checkCurrentCredentials(account)
} else {
val file = File(transfer.localPath)
if (file.exists()) {
transfersViewModel.retryUploadFromSystem(transfer.id!!)
} else if (DocumentFile.isDocumentUri(requireContext(), Uri.parse(transfer.localPath))) {
transfersViewModel.retryUploadFromContentUri(transfer.id!!)
} else {
Snackbar.make(
view,
getString(R.string.local_file_not_found_toast),
Snackbar.LENGTH_LONG
).show()
}
}
}
)
binding.transfersRecyclerView.apply {
layoutManager = LinearLayoutManager(context)
itemAnimator = DefaultItemAnimator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ package com.owncloud.android.presentation.viewmodels.transfers
import android.net.Uri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.owncloud.android.domain.transfers.model.OCTransfer
import com.owncloud.android.domain.transfers.usecases.DeleteTransferWithIdUseCase
import com.owncloud.android.domain.transfers.usecases.GetAllTransfersUseCase
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.UIResult
import com.owncloud.android.providers.CoroutinesDispatcherProvider
import com.owncloud.android.usecases.transfers.uploads.CancelUploadWithIdUseCase
import com.owncloud.android.usecases.transfers.uploads.RetryUploadFromContentUriUseCase
import com.owncloud.android.usecases.transfers.uploads.RetryUploadFromSystemUseCase
import com.owncloud.android.usecases.transfers.uploads.UploadFilesFromSAFUseCase
import com.owncloud.android.usecases.transfers.uploads.UploadFilesFromSystemUseCase
import kotlinx.coroutines.launch

class TransfersViewModel(
private val uploadFilesFromSAFUseCase: UploadFilesFromSAFUseCase,
private val uploadFilesFromSystemUseCase: UploadFilesFromSystemUseCase,
private val getAllTransfersUseCase: GetAllTransfersUseCase,
private val cancelUploadWithIdUseCase: CancelUploadWithIdUseCase,
private val deleteTransferWithIdUseCase: DeleteTransferWithIdUseCase,
private val retryUploadFromSystemUseCase: RetryUploadFromSystemUseCase,
private val retryUploadFromContentUriUseCase: RetryUploadFromContentUriUseCase,
getAllTransfersUseCase: GetAllTransfersUseCase,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
) : ViewModel() {

Expand Down Expand Up @@ -85,4 +90,36 @@ class TransfersViewModel(
)
}
}

fun cancelTransferWithId(id: Long) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
cancelUploadWithIdUseCase.execute(
CancelUploadWithIdUseCase.Params(uploadId = id)
)
}
}

fun deleteTransferWithId(id: Long) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
deleteTransferWithIdUseCase.execute(
DeleteTransferWithIdUseCase.Params(id = id)
)
}
}

fun retryUploadFromSystem(id: Long) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
retryUploadFromSystemUseCase.execute(
RetryUploadFromSystemUseCase.Params(uploadIdInStorageManager = id)
)
}
}

fun retryUploadFromContentUri(id: Long) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
retryUploadFromContentUriUseCase.execute(
RetryUploadFromContentUriUseCase.Params(uploadIdInStorageManager = id)
)
}
}
}
2 changes: 1 addition & 1 deletion owncloudApp/src/main/res/layout/upload_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="center_vertical"
android:paddingStart="8dp"
android:paddingHorizontal="8dp"
>

<ImageButton
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* ownCloud Android client application
*
* @author Juan Carlos Garrote Gascón
*
* Copyright (C) 2022 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.transfers.usecases

import com.owncloud.android.domain.BaseUseCase
import com.owncloud.android.domain.transfers.TransferRepository

class DeleteTransferWithIdUseCase(
private val transferRepository: TransferRepository,
) : BaseUseCase<Unit, DeleteTransferWithIdUseCase.Params>() {
override fun run(params: Params) =
transferRepository.removeTransferById(params.id)

data class Params(val id: Long)
}

0 comments on commit d9d073b

Please sign in to comment.