Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete file (sample app) #32

Merged
merged 8 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.omh.android.storage.api.drive.nongms.data
import com.omh.android.storage.api.drive.nongms.data.source.body.CreateFileRequestBody
import com.omh.android.storage.api.drive.nongms.data.source.response.FileListRemoteResponse
import com.omh.android.storage.api.drive.nongms.data.source.response.FileRemoteResponse
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.DELETE
Expand Down Expand Up @@ -44,5 +45,5 @@ internal interface GoogleStorageApiService {
@DELETE("$FILES_PARTICLE/{$FILE_ID}")
fun deleteFile(
@Path(FILE_ID) fileId: String
): Call<Nothing>
): Call<ResponseBody>
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class FileAdapter(
interface GridItemListener {

fun onFileClicked(file: OmhFile)

fun onDeleteClicked(file: OmhFile)
}

abstract class FileViewHolder(binding: View) : RecyclerView.ViewHolder(binding) {
Expand All @@ -99,6 +101,7 @@ class FileAdapter(
fileName.text = file.name
loadFileIcon(context, iconLink, fileIcon)
root.setOnClickListener { listener.onFileClicked(file) }
buttonDelete.setOnClickListener { listener.onDeleteClicked(file) }
}
}
}
Expand All @@ -115,6 +118,7 @@ class FileAdapter(
fileName.text = file.name
loadFileIcon(context, iconLink, fileIcon)
root.setOnClickListener { listener.onFileClicked(file) }
buttonDelete.setOnClickListener { listener.onDeleteClicked(file) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ class FileViewerActivity :
dispatchEvent(FileViewerViewEvent.FileClicked(file))
}

override fun onDeleteClicked(file: OmhFile) {
dispatchEvent(FileViewerViewEvent.DeleteFile(file))
}

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

private fun showCreateFileDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ sealed class FileViewerViewEvent : ViewEvent {

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

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

override fun getEventName() = "FileViewerViewEvent.CreateFile"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.omh.android.storage.sample.presentation.file_viewer

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.CreateFileUseCase
import com.omh.android.storage.api.domain.usecase.CreateFileUseCaseParams
import com.omh.android.storage.api.domain.usecase.DeleteFileUseCase
import com.omh.android.storage.api.domain.usecase.DeleteFileUseCaseParams
import com.omh.android.storage.api.domain.usecase.DeleteFileUseCaseResult
import com.omh.android.storage.api.domain.usecase.GetFilesListUseCase
import com.omh.android.storage.api.domain.usecase.GetFilesListUseCaseParams
import com.omh.android.storage.api.domain.usecase.OmhResult
Expand Down Expand Up @@ -42,7 +46,8 @@ class FileViewerViewModel @Inject constructor(
is FileViewerViewEvent.SwapLayoutManager -> swapLayoutManagerEvent()
is FileViewerViewEvent.FileClicked -> fileClickedEvent(event)
FileViewerViewEvent.BackPressed -> backPressedEvent()
is FileViewerViewEvent.CreateFile -> createFile(event)
is FileViewerViewEvent.CreateFile -> createFileEvent(event)
is FileViewerViewEvent.DeleteFile -> deleteFileEvent(event)
}
}

Expand Down Expand Up @@ -97,7 +102,7 @@ class FileViewerViewModel @Inject constructor(
}
}

private suspend fun createFile(event: FileViewerViewEvent.CreateFile) {
private suspend fun createFileEvent(event: FileViewerViewEvent.CreateFile) {
setState(FileViewerViewState.Loading)
val parentId = parentIdStack.peek()

Expand All @@ -117,4 +122,37 @@ class FileViewerViewModel @Inject constructor(
}
}
}

private suspend fun deleteFileEvent(event: FileViewerViewEvent.DeleteFile) {
setState(FileViewerViewState.Loading)

val file = event.file

val deleteFileUseCase: DeleteFileUseCase = omhStorageClient.deleteFile()

when (val result = deleteFileUseCase(DeleteFileUseCaseParams(file.id))) {
is OmhResult.OmhSuccess -> {
handleDeleteSuccess(result, file)
}

is OmhResult.OmhError -> {
toastMessage.postValue("ERROR: ${file.name} was NOT deleted")
}
}

refreshFileListEvent()
}

private fun handleDeleteSuccess(
result: OmhResult.OmhSuccess<DeleteFileUseCaseResult>,
file: OmhFile
) {
val toastText = if (result.data.isSuccess) {
"${file.name} was successfully deleted"
} else {
"${file.name} was NOT deleted"
}

toastMessage.postValue(toastText)
}
}
57 changes: 37 additions & 20 deletions storage-sample/src/main/res/layout/file_grid_adapter.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/rounded_for_grid"
android:orientation="vertical">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/rounded_for_grid">

<ImageView
android:id="@+id/fileIcon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
tools:ignore="ContentDescription"
tools:scaleType="centerCrop"
tools:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:src="@android:drawable/ic_menu_delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/black" />

</FrameLayout>
<ImageView
android:id="@+id/fileIcon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonDelete"
tools:ignore="ContentDescription"
tools:scaleType="centerCrop"
tools:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/fileName"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="17sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fileIcon"
tools:text="File Name" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
15 changes: 14 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 @@ -20,12 +20,25 @@
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="@+id/fileIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonDelete"
app:layout_constraintStart_toEndOf="@+id/fileIcon"
app:layout_constraintTop_toTopOf="@+id/fileIcon"
tools:text="File Name" />

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

<View
android:id="@+id/line"
android:layout_width="match_parent"
Expand Down