Skip to content

Commit fce06fa

Browse files
Fix rename
1 parent a8b60a7 commit fce06fa

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt

+50-11
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@ import android.widget.EditText
2727
import android.widget.ProgressBar
2828
import android.widget.TextView
2929
import androidx.core.net.toFile
30+
import androidx.core.net.toUri
3031
import com.dp.logcatapp.R
3132
import com.dp.logcatapp.activities.BaseActivityWithToolbar
3233
import com.dp.logcatapp.activities.CabToolbarCallback
3334
import com.dp.logcatapp.activities.SavedLogsActivity
3435
import com.dp.logcatapp.activities.SavedLogsViewerActivity
3536
import com.dp.logcatapp.db.MyDB
37+
import com.dp.logcatapp.db.SavedLogInfo
3638
import com.dp.logcatapp.fragments.base.BaseDialogFragment
3739
import com.dp.logcatapp.fragments.base.BaseFragment
3840
import com.dp.logcatapp.fragments.logcatlive.LogcatLiveFragment
39-
import com.dp.logcatapp.util.ShareUtils
40-
import com.dp.logcatapp.util.closeQuietly
41-
import com.dp.logcatapp.util.inflateLayout
42-
import com.dp.logcatapp.util.showToast
41+
import com.dp.logcatapp.util.*
4342
import io.reactivex.Flowable
4443
import io.reactivex.android.schedulers.AndroidSchedulers
4544
import io.reactivex.disposables.Disposable
@@ -62,6 +61,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
6261
private lateinit var progressBar: ProgressBar
6362

6463
private var deleteSubscriptionHandler: Disposable? = null
64+
private var renameSubscriptionHandler: Disposable? = null
6565

6666
override fun onCreate(savedInstanceState: Bundle?) {
6767
super.onCreate(savedInstanceState)
@@ -350,10 +350,26 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
350350
recyclerViewAdapter.notifyDataSetChanged()
351351
}
352352

353+
private fun onRename(newName: String, newPath: Uri) {
354+
val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0])
355+
renameSubscriptionHandler = Flowable.just(MyDB.getInstance(context!!))
356+
.subscribeOn(Schedulers.io())
357+
.map { db ->
358+
db.savedLogsDao().delete(fileInfo.info)
359+
db.savedLogsDao().insert(SavedLogInfo(newName, newPath.toString(), fileInfo.info.isCustom))
360+
}
361+
.observeOn(AndroidSchedulers.mainThread())
362+
.subscribe {
363+
viewModel.fileNames.reload()
364+
}
365+
}
366+
353367
override fun onDestroy() {
354368
super.onDestroy()
355369
deleteSubscriptionHandler?.dispose()
356370
deleteSubscriptionHandler = null
371+
renameSubscriptionHandler?.dispose()
372+
renameSubscriptionHandler = null
357373
}
358374

359375
private class MyRecyclerViewAdapter(
@@ -483,10 +499,8 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
483499
val view = inflateLayout(R.layout.rename_dialog)
484500
val editText = view.findViewById<EditText>(R.id.editText)
485501

486-
val path = arguments!!.getString(KEY_PATH)
487-
val file = File(path)
488-
489-
editText.setText(file.name)
502+
val path = arguments!!.getString(KEY_PATH)!!
503+
editText.setText(getName(path))
490504
editText.selectAll()
491505

492506
val dialog = AlertDialog.Builder(activity!!)
@@ -495,9 +509,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
495509
.setPositiveButton(android.R.string.ok) { _, _ ->
496510
val newName = editText.text.toString()
497511
if (newName.isNotEmpty()) {
498-
if (file.renameTo(File(file.parent, newName))) {
499-
(targetFragment as SavedLogsFragment).viewModel.fileNames.reload()
500-
} else {
512+
if (!doRename(path, newName)) {
501513
activity!!.showToast(getString(R.string.error))
502514
}
503515
(activity as SavedLogsActivity).closeCabToolbar()
@@ -518,5 +530,32 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
518530

519531
return dialog
520532
}
533+
534+
private fun getName(path: String): String {
535+
if (Utils.isUsingCustomSaveLocation(context!!) && Build.VERSION.SDK_INT >= 21) {
536+
return DocumentFile.fromSingleUri(context!!, path.toUri())!!.name!!
537+
} else {
538+
return File(path).name
539+
}
540+
}
541+
542+
private fun doRename(path: String, newName: String): Boolean {
543+
if (Utils.isUsingCustomSaveLocation(context!!) && Build.VERSION.SDK_INT >= 21) {
544+
val file = DocumentFile.fromSingleUri(context!!, path.toUri())!!
545+
if (file.renameTo(newName)) {
546+
(targetFragment as SavedLogsFragment).onRename(newName, file.uri)
547+
return true
548+
}
549+
} else {
550+
val file = File(path)
551+
val newFile = File(file.parent, newName)
552+
if (file.renameTo(newFile)) {
553+
(targetFragment as SavedLogsFragment).onRename(newName, newFile.toUri())
554+
return true
555+
}
556+
}
557+
558+
return false
559+
}
521560
}
522561
}

0 commit comments

Comments
 (0)