Skip to content

Allow user to delete note files #237

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

Merged
merged 2 commits into from
Nov 19, 2020
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
10 changes: 7 additions & 3 deletions app/src/main/java/ro/code4/monitorizarevot/helper/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ object FileUtils {
contentResolver.query(uri, null, null, null, null)
?.let { cursor ->
cursor.run {
if (moveToFirst())
getString(getColumnIndex(OpenableColumns.DISPLAY_NAME))
else null
if (moveToFirst()) {
getString(getColumnIndex(OpenableColumns.DISPLAY_NAME))?.let { name ->
"${System.currentTimeMillis()}_${name}"
}
} else {
null
}
}.also { cursor.close() }
}
}.getOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.provider.Settings
import android.text.TextWatcher
import android.view.MotionEvent
import android.view.View
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -79,12 +80,16 @@ class NoteFragment : ViewModelFragment<NoteViewModel>(), PermissionManager.Permi
viewModel.filesNames().observe(viewLifecycleOwner, Observer {
noteFileContainer.visibility = View.VISIBLE
noteFileContainer.removeAllViews()
it.forEach { filename ->
val newTextView = requireActivity().layoutInflater.inflate(
it.forEachIndexed { index, filename ->
val attachmentView = requireActivity().layoutInflater.inflate(
R.layout.include_note_filename, noteFileContainer, false
) as TextView
newTextView.text = filename
noteFileContainer.addView(newTextView)
).also { view ->
view.findViewById<TextView>(R.id.filenameText).text = filename
view.findViewById<ImageButton>(R.id.deleteFile).setOnClickListener {
viewModel.deleteFile(filename, index)
}
}
noteFileContainer.addView(attachmentView)
}
})
viewModel.submitCompleted().observe(this, Observer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NoteViewModel : BaseFormViewModel() {
private val notesLiveData = MutableLiveData<ArrayList<ListItem>>()
private val filesNamesLiveData = MutableLiveData<List<String>>()
private val submitCompletedLiveData = SingleLiveEvent<Void>()
private val noteFiles = mutableListOf<File>()
private var noteFiles = mutableListOf<File>()
private val listObserver =
Observer<List<Note>> { list ->
processList(list)
Expand Down Expand Up @@ -131,6 +131,21 @@ class NoteViewModel : BaseFormViewModel() {
}
}

fun deleteFile(filename: String, position: Int) {
val targetFiles = noteFiles.filter { it.absolutePath.endsWith("/$filename") }
if (targetFiles.isNotEmpty() && targetFiles.size == 1) {
try {
targetFiles[0].delete()
} catch (ex: Exception) {
// ignored
// this try-catch block will prevent the app from crashing if the file can't be deleted(which
// is ok because we dereference the file below and it is safe to remain on disk)
}
}
noteFiles = noteFiles.filterIndexed { index, _ -> index != position }.toMutableList()
filesNamesLiveData.postValue(noteFiles.map { file -> file.name }.toList())
}

fun addUserGeneratedFile(file: File?) {
file?.let { noteFiles.add(it) }
}
Expand All @@ -144,4 +159,4 @@ class NoteViewModel : BaseFormViewModel() {
app.sendBroadcast(mediaScanIntent)
}
}
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_note_file_delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="64"
android:viewportHeight="64">
<path
android:strokeColor="#ff0000"
android:strokeWidth="3"
android:pathData="M32,8 A24,24 0 1 1 31.99,8 z" />

<path
android:strokeColor="#ff0000"
android:strokeWidth="3"
android:pathData="M24,24 L40,40 M40,24 L24,40" />
</vector>
32 changes: 25 additions & 7 deletions app/src/main/res/layout/include_note_filename.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout 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:id="@+id/filenameText"
style="@style/Text.Small"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin"
android:drawableStart="@drawable/ic_file"
android:drawablePadding="@dimen/small_margin"
tools:text="Frauda_0342.jpg" />
android:orientation="horizontal">

<TextView
android:id="@+id/filenameText"
style="@style/Text.Small"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:drawableStart="@drawable/ic_file"
android:drawablePadding="@dimen/small_margin"
tools:text="Frauda_0342.jpg" />

<ImageButton
android:id="@+id/deleteFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:contentDescription="@null"
app:srcCompat="@drawable/ic_note_file_delete" />
</LinearLayout>