Skip to content

Commit

Permalink
UI update a file (#63)
Browse files Browse the repository at this point in the history
* Storage sdk: Implement update functionality in non gms.

* Storage sdk: Remove parentId as paramater.

* Storage sdk: Implement the ui to update a file.

* Storage sdk: Resolve merge conflicts.

* Storage sdk: Remove omhFile from viewmodel and use other variable.

* Storage sdk: Use the local file name instead of the remote name.

* Storage sdk: Refactor to select a file to update.

* Storage sdk: Refactor to use one single dialog and better use of ViewModel.

* Solve comments

---------

Co-authored-by: Hector A. Narvaez <hector.narvaez@encora.com>
  • Loading branch information
hans-hamel and HectorNarvaez committed Jul 5, 2023
1 parent e5cce5a commit 40b643f
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class FileAdapter(
fun onFileClicked(file: OmhFile)

fun onDeleteClicked(file: OmhFile)

fun onUpdateClicked(file: OmhFile)
}

abstract class FileViewHolder(binding: View) : RecyclerView.ViewHolder(binding) {
Expand All @@ -114,6 +116,7 @@ class FileAdapter(
loadFileIcon(context, iconLink, fileIcon)
root.setOnClickListener { listener.onFileClicked(file) }
buttonDelete.setOnClickListener { listener.onDeleteClicked(file) }
buttonUpdate.setOnClickListener { listener.onUpdateClicked(file) }
}
}
}
Expand All @@ -131,6 +134,7 @@ class FileAdapter(
loadFileIcon(context, iconLink, fileIcon)
root.setOnClickListener { listener.onFileClicked(file) }
buttonDelete.setOnClickListener { listener.onDeleteClicked(file) }
buttonUpdate.setOnClickListener { listener.onUpdateClicked(file) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class FileViewerActivity :
private lateinit var binding: ActivityFileViewerBinding
private var filesAdapter: FileAdapter? = null
private lateinit var onBackPressedCallback: OnBackPressedCallback
private lateinit var filePicker: ActivityResultLauncher<String>
private lateinit var filePickerUpload: ActivityResultLauncher<String>
private lateinit var filePickerUpdate: ActivityResultLauncher<String>
private lateinit var requestPermissionLauncher: ActivityResultLauncher<Array<String>>

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -61,11 +62,24 @@ class FileViewerActivity :
}
}

filePicker = registerForActivityResult(
filePickerUpload = registerForActivityResult(ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
val titleText = getString(R.string.text_upload_file_title)
val positiveText = getString(R.string.text_upload)

showBeforeSubmitFileDialog(uri, titleText, positiveText, ::configureUploadFilePositiveButtonEvent)
}
}

filePickerUpdate = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let { validUri ->
showUploadFileDialog(validUri)
uri?.let {
val titleText = getString(R.string.text_update_file_title)
val positiveText = getString(R.string.text_update)

showBeforeSubmitFileDialog(uri, titleText, positiveText, ::configureUpdateFilePositiveButtonEvent)
}
}

Expand Down Expand Up @@ -110,9 +124,7 @@ class FileViewerActivity :
}

R.id.uploadFile -> {
filePicker.launch(
FileViewerViewModel.ANY_MIME_TYPE
)
filePickerUpload.launch(FileViewerViewModel.ANY_MIME_TYPE)
}

R.id.signOut -> {
Expand All @@ -130,6 +142,7 @@ class FileViewerActivity :
FileViewerViewState.Finish -> buildFinishState()
FileViewerViewState.CheckPermissions -> requestPermissions()
FileViewerViewState.SignOut -> buildSignOutState()
is FileViewerViewState.ShowUpdateFilePicker -> launchUpdateFilePicker()
}

private fun buildInitialState() {
Expand Down Expand Up @@ -195,6 +208,14 @@ class FileViewerActivity :
dispatchEvent(FileViewerViewEvent.DeleteFile(file))
}

override fun onUpdateClicked(file: OmhFile) {
dispatchEvent(FileViewerViewEvent.UpdateFileClicked(file))
}

private fun launchUpdateFilePicker() {
filePickerUpdate.launch(FileViewerViewModel.ANY_MIME_TYPE)
}

private fun buildFinishState() = finish().also { finishAffinity() }

private fun showCreateFileDialog() {
Expand Down Expand Up @@ -266,18 +287,22 @@ class FileViewerActivity :
}
}

private fun showUploadFileDialog(uri: Uri) {
val fileName: String = DocumentFile
.fromSingleUri(this, uri)?.name
?: FileViewerViewModel.DEFAULT_FILE_NAME

private fun showBeforeSubmitFileDialog(
uri: Uri,
titleText: String,
positiveTextButton: String,
positiveAction: (DialogInterface, String, Uri) -> Unit
) {
val dialogUploadFileView = DialogUploadFileBinding.inflate(layoutInflater)
val documentFileName = DocumentFile.fromSingleUri(this, uri)?.name
val fileName = viewModel.getFileName(documentFileName)

dialogUploadFileView.fileName.text = fileName

val uploadFileDialogBuilder = AlertDialog.Builder(this)
.setTitle(getString(R.string.text_upload_file_title))
.setPositiveButton(getString(R.string.text_upload)) { dialog, _ ->
configureUploadFilePositiveButtonEvent(dialog, fileName, uri)
.setTitle(titleText)
.setPositiveButton(positiveTextButton) { dialog, _ ->
positiveAction(dialog, fileName, uri)
}
.setNegativeButton(getString(R.string.text_cancel)) { dialog, _ ->
dialog.cancel()
Expand All @@ -303,6 +328,15 @@ class FileViewerActivity :
dialog.dismiss()
}

private fun configureUpdateFilePositiveButtonEvent(
dialog: DialogInterface,
fileName: String,
uri: Uri
) {
dispatchEvent(FileViewerViewEvent.UpdateFile(this, uri, fileName))
dialog.dismiss()
}

private fun requestPermissions() {
val permissionsToRequest: Array<String> = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ sealed class FileViewerViewEvent : ViewEvent {

override fun getEventName() = "FileViewerViewEvent.SignOut"
}

class UpdateFileClicked(val file: OmhFile) : FileViewerViewEvent() {

override fun getEventName() = "FileViewerViewEvent.UpdateFileClicked"
}

class UpdateFile(val context: Context, val uri: Uri, val fileName: String) : FileViewerViewEvent() {

override fun getEventName() = "FileViewerViewEvent.UpdateFile"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.omh.android.storage.sample.presentation.file_viewer

import android.content.Context
import android.net.Uri
import com.omh.android.auth.api.OmhAuthClient
import android.os.Environment
import com.omh.android.auth.api.OmhAuthClient
import com.omh.android.storage.api.OmhStorageClient
import com.omh.android.storage.api.domain.model.OmhFile
import com.omh.android.storage.api.domain.model.OmhFileType
import com.omh.android.storage.api.domain.usecase.DownloadFileUseCaseResult
import com.omh.android.storage.sample.domain.model.FileType
import com.omh.android.storage.sample.presentation.BaseViewModel
import com.omh.android.storage.sample.util.getNameWithExtension
import com.omh.android.storage.sample.util.normalizedMimeType
import com.omh.android.storage.sample.util.isDownloadable
import com.omh.android.storage.sample.util.normalizedMimeType
import dagger.hilt.android.lifecycle.HiltViewModel
import java.io.File
import java.io.FileOutputStream
Expand All @@ -39,6 +39,7 @@ class FileViewerViewModel @Inject constructor(
const val DEFAULT_FILE_NAME = "Untitled"
}

var isUpload = false
var isGridLayoutManager = true
var createFileSelectedType: OmhFileType? = null
private val parentIdStack = Stack<String>().apply { push(ID_ROOT) }
Expand All @@ -56,8 +57,10 @@ class FileViewerViewModel @Inject constructor(
is FileViewerViewEvent.CreateFile -> createFileEvent(event)
is FileViewerViewEvent.DeleteFile -> deleteFileEvent(event)
is FileViewerViewEvent.UploadFile -> uploadFile(event)
is FileViewerViewEvent.UpdateFile -> updateFileEvent(event)
FileViewerViewEvent.SignOut -> signOut()
FileViewerViewEvent.DownloadFile -> downloadFileEvent()
is FileViewerViewEvent.UpdateFileClicked -> updateFileClickEvent(event)
}
}

Expand Down Expand Up @@ -116,6 +119,7 @@ class FileViewerViewModel @Inject constructor(
lastFileClicked?.let { file ->
if (!file.isDownloadable()) {
toastMessage.postValue("${file.name} is not downloadable")
refreshFileListEvent()
return
}

Expand All @@ -138,6 +142,38 @@ class FileViewerViewModel @Inject constructor(
}
}

private fun updateFileEvent(event: FileViewerViewEvent.UpdateFile) {
val fileId = lastFileClicked?.id

if (fileId.isNullOrBlank()) {
refreshFileListEvent()
return
}

val filePath = getFile(event.context, event.uri, event.fileName)
val cancellable = omhStorageClient.updateFile(filePath, fileId)
.addOnSuccess { result ->
val resultMessage = if (result.file == null) {
"${event.fileName} was not updated"
} else {
"${event.fileName} was successfully updated"
}

toastMessage.postValue(resultMessage)

refreshFileListEvent()
}
.addOnFailure { e ->
toastMessage.postValue("ERROR: ${event.fileName} was not updated")
e.printStackTrace()

refreshFileListEvent()
}
.execute()

cancellableCollector.addCancellable(cancellable)
}

private fun createFileEvent(event: FileViewerViewEvent.CreateFile) {
setState(FileViewerViewState.Loading)
val parentId = parentIdStack.peek()
Expand Down Expand Up @@ -234,6 +270,11 @@ class FileViewerViewModel @Inject constructor(
cancellableCollector.addCancellable(cancellable)
}

private fun updateFileClickEvent(event: FileViewerViewEvent.UpdateFileClicked) {
lastFileClicked = event.file
setState(FileViewerViewState.ShowUpdateFilePicker)
}

private fun handleDownloadSuccess(
result: DownloadFileUseCaseResult,
file: OmhFile
Expand All @@ -253,4 +294,6 @@ class FileViewerViewModel @Inject constructor(
super.onCleared()
cancellableCollector.clear()
}

fun getFileName(documentFileName: String?): String = documentFileName ?: DEFAULT_FILE_NAME
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ sealed class FileViewerViewState : ViewState {

override fun getName() = "FileViewerViewState.SignOut"
}

object ShowUpdateFilePicker : FileViewerViewState() {

override fun getName() = "FileViewerViewState.FilePicker"
}
}
11 changes: 11 additions & 0 deletions storage-sample/src/main/res/layout/file_grid_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
android:background="@drawable/rounded_for_grid"
android:orientation="vertical">

<ImageView
android:id="@+id/buttonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:src="@android:drawable/ic_menu_edit"
app:layout_constraintEnd_toStartOf="@id/buttonDelete"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/black" />

<ImageView
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
Expand Down
13 changes: 12 additions & 1 deletion storage-sample/src/main/res/layout/file_linear_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,22 @@
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="@+id/fileIcon"
app:layout_constraintEnd_toStartOf="@+id/buttonDelete"
app:layout_constraintEnd_toStartOf="@+id/buttonUpdate"
app:layout_constraintStart_toEndOf="@+id/fileIcon"
app:layout_constraintTop_toTopOf="@+id/fileIcon"
tools:text="File Name" />

<ImageView
android:id="@+id/buttonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:src="@android:drawable/ic_menu_edit"
app:layout_constraintBottom_toBottomOf="@+id/fileName"
app:layout_constraintEnd_toStartOf="@+id/buttonDelete"
app:layout_constraintTop_toTopOf="@+id/fileName"
app:tint="@color/black" />

<ImageView
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions storage-sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<string name="text_file_type_label">File Type:</string>
<string name="text_upload_file_title">Upload File</string>
<string name="text_upload">Upload</string>
<string name="text_update_file_title">Update File</string>
<string name="text_update">Update</string>
<string name="text_cancel">Cancel</string>
<string name="text_sign_out">Sign out</string>
</resources>

0 comments on commit 40b643f

Please sign in to comment.