implementing recording renaming

This commit is contained in:
tibbi 2020-04-02 11:05:50 +02:00
parent 491d330fdf
commit 35ea5868c3
3 changed files with 93 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.dialogs.RenameRecordingDialog
import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.item_recording.view.*
import java.util.*
@ -60,17 +61,22 @@ class RecordingsAdapter(activity: SimpleActivity, var recordings: ArrayList<Reco
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_recording, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val group = recordings[position]
holder.bindView(group, true, true) { itemView, layoutPosition ->
setupView(itemView, group)
val recording = recordings[position]
holder.bindView(recording, true, true) { itemView, layoutPosition ->
setupView(itemView, recording)
}
bindViewHolder(holder)
}
override fun getItemCount() = recordings.size
private fun renameRecording() {
private fun getItemWithKey(key: Int): Recording? = recordings.firstOrNull { it.id == key }
private fun renameRecording() {
val recording = getItemWithKey(selectedKeys.first()) ?: return
RenameRecordingDialog(activity, recording) {
finishActMode()
}
}
private fun askConfirmDelete() {

View File

@ -0,0 +1,66 @@
package com.simplemobiletools.voicerecorder.dialogs
import android.content.ContentUris
import android.content.ContentValues
import android.provider.MediaStore
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.dialog_rename_recording.view.*
class RenameRecordingDialog(val activity: BaseSimpleActivity, val recording: Recording, val callback: () -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_rename_recording, null).apply {
rename_recording_title.setText(recording.title.substringBeforeLast('.'))
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.rename) {
showKeyboard(view.rename_recording_title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newTitle = view.rename_recording_title.value
if (newTitle.isEmpty()) {
activity.toast(R.string.empty_name)
return@setOnClickListener
}
if (!newTitle.isAValidFilename()) {
activity.toast(R.string.invalid_name)
return@setOnClickListener
}
ensureBackgroundThread {
if (isQPlus()) {
updateMediaStoreTitle(recording, newTitle)
}
activity.runOnUiThread {
callback()
dismiss()
}
}
}
}
}
}
private fun updateMediaStoreTitle(recording: Recording, newTitle: String) {
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, recording.id.toLong())
val oldExtension = recording.title.getFilenameExtension()
val newDisplayName = "${newTitle.removeSuffix(".$oldExtension")}.$oldExtension"
val values = ContentValues().apply {
put(MediaStore.Audio.Media.TITLE, newTitle.substringAfterLast('.'))
put(MediaStore.Audio.Media.DISPLAY_NAME, newDisplayName)
}
activity.contentResolver.update(uri, values, null, null)
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rename_recording_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/rename_recording_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size" />
</LinearLayout>