Skip to content

Commit

Permalink
Storage sdk: Refactor to select a file to update.
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-hamel authored and HectorNarvaez committed Jul 5, 2023
1 parent a23250a commit 53c92a6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
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,17 +62,18 @@ class FileViewerActivity :
}
}

filePicker = registerForActivityResult(
filePickerUpload = registerForActivityResult(ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
showUploadFileDialog(uri)
}
}

filePickerUpdate = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
val fileName = DocumentFile.fromSingleUri(this, uri)?.name
?: FileViewerViewModel.DEFAULT_FILE_NAME
if (viewModel.isUpload) {
showUploadFileDialog(uri, fileName)
} else {
showUpdateFileDialog(uri, fileName)
}
showUpdateFileDialog(uri)
}
}

Expand All @@ -95,6 +97,9 @@ class FileViewerActivity :
}
}

private fun getFileName(uri: Uri) = (DocumentFile.fromSingleUri(this, uri)?.name
?: FileViewerViewModel.DEFAULT_FILE_NAME)

override fun onResume() {
super.onResume()
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
Expand All @@ -116,8 +121,7 @@ class FileViewerActivity :
}

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

R.id.signOut -> {
Expand All @@ -135,6 +139,7 @@ class FileViewerActivity :
FileViewerViewState.Finish -> buildFinishState()
FileViewerViewState.CheckPermissions -> requestPermissions()
FileViewerViewState.SignOut -> buildSignOutState()
is FileViewerViewState.FilePicker -> buildFilePicker()
}

private fun buildInitialState() {
Expand Down Expand Up @@ -201,9 +206,11 @@ class FileViewerActivity :
}

override fun onUpdateClicked(file: OmhFile) {
viewModel.lastFileClicked = file
viewModel.isUpload = false
filePicker.launch(FileViewerViewModel.ANY_MIME_TYPE)
dispatchEvent(FileViewerViewEvent.UpdateFileClicked(file))
}

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

private fun buildFinishState() = finish().also { finishAffinity() }
Expand Down Expand Up @@ -277,8 +284,10 @@ class FileViewerActivity :
}
}

private fun showUploadFileDialog(uri: Uri, fileName: String) {
private fun showUploadFileDialog(uri: Uri) {
val dialogUploadFileView = DialogUploadFileBinding.inflate(layoutInflater)
val fileName = getFileName(uri)

dialogUploadFileView.fileName.text = fileName

val uploadFileDialogBuilder = AlertDialog.Builder(this)
Expand All @@ -298,8 +307,10 @@ class FileViewerActivity :
createFileAlertDialog.show()
}

private fun showUpdateFileDialog(uri: Uri, fileName: String) {
private fun showUpdateFileDialog(uri: Uri) {
val dialogUploadFileView = DialogUploadFileBinding.inflate(layoutInflater)
val fileName = getFileName(uri)

dialogUploadFileView.fileName.text = fileName

val uploadFileDialogBuilder = AlertDialog.Builder(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ sealed class FileViewerViewEvent : ViewEvent {

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

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

override fun getEventName() = "FileViewerViewEvent.UpdateFileClicked"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FileViewerViewModel @Inject constructor(
is FileViewerViewEvent.UpdateFile -> updateFile(event)
FileViewerViewEvent.SignOut -> signOut()
FileViewerViewEvent.DownloadFile -> downloadFileEvent()
is FileViewerViewEvent.UpdateFileClicked -> updateFileClicked(event)
}
}

Expand Down Expand Up @@ -112,14 +113,14 @@ class FileViewerViewModel @Inject constructor(
}

private fun downloadFileEvent() {
setState(FileViewerViewState.Loading)

lastFileClicked?.let { file ->
if (!file.isDownloadable()) {
toastMessage.postValue("${file.name} is not downloadable")
return
}

setState(FileViewerViewState.Loading)

val mimeType = file.normalizedMimeType()
val cancellable = omhStorageClient.downloadFile(file.id, mimeType)
.addOnSuccess { data ->
Expand Down Expand Up @@ -264,6 +265,11 @@ class FileViewerViewModel @Inject constructor(
cancellableCollector.addCancellable(cancellable)
}

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

private fun handleDownloadSuccess(
result: DownloadFileUseCaseResult,
file: OmhFile
Expand Down
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 FilePicker : FileViewerViewState() {

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

0 comments on commit 53c92a6

Please sign in to comment.