Skip to content

Commit

Permalink
Storage sdk: Implement the ui to update a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-hamel committed Jul 3, 2023
1 parent a20c178 commit 679d96a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 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 @@ -64,8 +64,13 @@ class FileViewerActivity :
filePicker = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let { validUri ->
showUploadFileDialog(validUri)
uri?.let {
val fileName = if (viewModel.isUpload) {
DocumentFile.fromSingleUri(this, uri)?.name
} else {
viewModel.selectedFile?.name
} ?: FileViewerViewModel.DEFAULT_FILE_NAME
showUploadFileDialog(uri, fileName)
}
}

Expand Down Expand Up @@ -110,9 +115,8 @@ class FileViewerActivity :
}

R.id.uploadFile -> {
filePicker.launch(
FileViewerViewModel.ANY_MIME_TYPE
)
viewModel.isUpload = true
filePicker.launch(FileViewerViewModel.ANY_MIME_TYPE)
}

R.id.signOut -> {
Expand Down Expand Up @@ -195,6 +199,12 @@ class FileViewerActivity :
dispatchEvent(FileViewerViewEvent.DeleteFile(file))
}

override fun onUpdateClicked(file: OmhFile) {
viewModel.selectedFile = file
viewModel.isUpload = false
filePicker.launch(FileViewerViewModel.ANY_MIME_TYPE)
}

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

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

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

private fun showUploadFileDialog(uri: Uri, fileName: String) {
val dialogUploadFileView = DialogUploadFileBinding.inflate(layoutInflater)
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)
if (viewModel.isUpload) {
configureUploadFilePositiveButtonEvent(dialog, fileName, uri)
} else {
configureUpdateFilePositiveButtonEvent(dialog, uri)
}
}
.setNegativeButton(getString(R.string.text_cancel)) { dialog, _ ->
dialog.cancel()
Expand All @@ -303,6 +313,14 @@ class FileViewerActivity :
dialog.dismiss()
}

private fun configureUpdateFilePositiveButtonEvent(
dialog: DialogInterface,
uri: Uri
) {
dispatchEvent(FileViewerViewEvent.UpdateFile(this, uri))
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 @@ -52,6 +52,11 @@ sealed class FileViewerViewEvent : ViewEvent {
override fun getEventName() = "FileViewerViewEvent.UploadFile"
}

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

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

object SignOut : FileViewerViewEvent() {

override fun getEventName() = "FileViewerViewEvent.SignOut"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class FileViewerViewModel @Inject constructor(
const val DEFAULT_FILE_NAME = "Untitled"
}

var selectedFile: OmhFile? = null
var isUpload = false
var isGridLayoutManager = true
var createFileSelectedType: OmhFileType? = null
private val parentIdStack = Stack<String>().apply { push(ID_ROOT) }
Expand All @@ -55,6 +57,7 @@ class FileViewerViewModel @Inject constructor(
is FileViewerViewEvent.CreateFile -> createFileEvent(event)
is FileViewerViewEvent.DeleteFile -> deleteFileEvent(event)
is FileViewerViewEvent.UploadFile -> uploadFile(event)
is FileViewerViewEvent.UpdateFile -> updateFile(event)
FileViewerViewEvent.SignOut -> signOut()
FileViewerViewEvent.DownloadFile -> downloadFileEvent()
}
Expand Down Expand Up @@ -137,6 +140,37 @@ class FileViewerViewModel @Inject constructor(
}
}

private fun updateFile(event: FileViewerViewEvent.UpdateFile) {
val file = selectedFile ?: return

setState(FileViewerViewState.Loading)

val parentId = parentIdStack.peek()
val filePath = getFile(event.context, event.uri, file.name)

val cancellable = omhStorageClient.updateFile(filePath, file.id, parentId)
.addOnSuccess { result ->
val resultMessage = if (result.file == null) {
"${file.name} was not updated"
} else {
"${file.name} was successfully updated"
}

toastMessage.postValue(resultMessage)

refreshFileListEvent()
}
.addOnFailure { e ->
toastMessage.postValue("ERROR: ${file.name} 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
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

0 comments on commit 679d96a

Please sign in to comment.