@@ -27,19 +27,18 @@ import android.widget.EditText
27
27
import android.widget.ProgressBar
28
28
import android.widget.TextView
29
29
import androidx.core.net.toFile
30
+ import androidx.core.net.toUri
30
31
import com.dp.logcatapp.R
31
32
import com.dp.logcatapp.activities.BaseActivityWithToolbar
32
33
import com.dp.logcatapp.activities.CabToolbarCallback
33
34
import com.dp.logcatapp.activities.SavedLogsActivity
34
35
import com.dp.logcatapp.activities.SavedLogsViewerActivity
35
36
import com.dp.logcatapp.db.MyDB
37
+ import com.dp.logcatapp.db.SavedLogInfo
36
38
import com.dp.logcatapp.fragments.base.BaseDialogFragment
37
39
import com.dp.logcatapp.fragments.base.BaseFragment
38
40
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.*
43
42
import io.reactivex.Flowable
44
43
import io.reactivex.android.schedulers.AndroidSchedulers
45
44
import io.reactivex.disposables.Disposable
@@ -62,6 +61,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
62
61
private lateinit var progressBar: ProgressBar
63
62
64
63
private var deleteSubscriptionHandler: Disposable ? = null
64
+ private var renameSubscriptionHandler: Disposable ? = null
65
65
66
66
override fun onCreate (savedInstanceState : Bundle ? ) {
67
67
super .onCreate(savedInstanceState)
@@ -350,10 +350,26 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
350
350
recyclerViewAdapter.notifyDataSetChanged()
351
351
}
352
352
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
+
353
367
override fun onDestroy () {
354
368
super .onDestroy()
355
369
deleteSubscriptionHandler?.dispose()
356
370
deleteSubscriptionHandler = null
371
+ renameSubscriptionHandler?.dispose()
372
+ renameSubscriptionHandler = null
357
373
}
358
374
359
375
private class MyRecyclerViewAdapter (
@@ -483,10 +499,8 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
483
499
val view = inflateLayout(R .layout.rename_dialog)
484
500
val editText = view.findViewById<EditText >(R .id.editText)
485
501
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))
490
504
editText.selectAll()
491
505
492
506
val dialog = AlertDialog .Builder (activity!! )
@@ -495,9 +509,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
495
509
.setPositiveButton(android.R .string.ok) { _, _ ->
496
510
val newName = editText.text.toString()
497
511
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)) {
501
513
activity!! .showToast(getString(R .string.error))
502
514
}
503
515
(activity as SavedLogsActivity ).closeCabToolbar()
@@ -518,5 +530,32 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
518
530
519
531
return dialog
520
532
}
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
+ }
521
560
}
522
561
}
0 commit comments