@@ -38,7 +38,10 @@ import com.dp.logcatapp.db.SavedLogInfo
38
38
import com.dp.logcatapp.fragments.base.BaseDialogFragment
39
39
import com.dp.logcatapp.fragments.base.BaseFragment
40
40
import com.dp.logcatapp.fragments.logcatlive.LogcatLiveFragment
41
- import com.dp.logcatapp.util.*
41
+ import com.dp.logcatapp.util.ShareUtils
42
+ import com.dp.logcatapp.util.closeQuietly
43
+ import com.dp.logcatapp.util.inflateLayout
44
+ import com.dp.logcatapp.util.showToast
42
45
import io.reactivex.Flowable
43
46
import io.reactivex.android.schedulers.AndroidSchedulers
44
47
import io.reactivex.disposables.Disposable
@@ -194,9 +197,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
194
197
return when (item.itemId) {
195
198
R .id.action_rename -> {
196
199
val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0 ])
197
- val folder = File (context!! .filesDir, LogcatLiveFragment .LOGCAT_DIR )
198
- val file = File (folder, fileInfo.info.fileName)
199
- val frag = RenameDialogFragment .newInstance(file.absolutePath)
200
+ val frag = RenameDialogFragment .newInstance(fileInfo.info.fileName, fileInfo.info.path)
200
201
frag.setTargetFragment(this , 0 )
201
202
frag.show(fragmentManager, RenameDialogFragment .TAG )
202
203
true
@@ -339,10 +340,16 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
339
340
val save = toolbar.menu.findItem(R .id.action_save)
340
341
val rename = toolbar.menu.findItem(R .id.action_rename)
341
342
342
- val visible = viewModel.selectedItems.size < = 1
343
+ val visible = viewModel.selectedItems.size = = 1
343
344
share.isVisible = visible
344
345
save.isVisible = visible
345
- rename.isVisible = visible
346
+
347
+ if (visible) {
348
+ val info = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0 ])
349
+ rename.isVisible = ! info.info.isCustom
350
+ } else {
351
+ rename.isVisible = false
352
+ }
346
353
}
347
354
348
355
override fun onCabToolbarClose (toolbar : Toolbar ) {
@@ -362,6 +369,8 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
362
369
.subscribe {
363
370
viewModel.fileNames.reload()
364
371
}
372
+
373
+ (activity as SavedLogsActivity ).closeCabToolbar()
365
374
}
366
375
367
376
override fun onDestroy () {
@@ -484,10 +493,12 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
484
493
companion object {
485
494
val TAG = RenameDialogFragment ::class .qualifiedName
486
495
496
+ private val KEY_FILENAME = TAG + " _key_filename"
487
497
private val KEY_PATH = TAG + " _key_path"
488
498
489
- fun newInstance (path : String ): RenameDialogFragment {
499
+ fun newInstance (fileName : String , path : String ): RenameDialogFragment {
490
500
val bundle = Bundle ()
501
+ bundle.putString(KEY_FILENAME , fileName)
491
502
bundle.putString(KEY_PATH , path)
492
503
val frag = RenameDialogFragment ()
493
504
frag.arguments = bundle
@@ -498,9 +509,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
498
509
override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
499
510
val view = inflateLayout(R .layout.rename_dialog)
500
511
val editText = view.findViewById<EditText >(R .id.editText)
501
-
502
- val path = arguments!! .getString(KEY_PATH )!!
503
- editText.setText(getName(path))
512
+ editText.setText(arguments!! .getString(KEY_FILENAME ))
504
513
editText.selectAll()
505
514
506
515
val dialog = AlertDialog .Builder (activity!! )
@@ -509,10 +518,13 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
509
518
.setPositiveButton(android.R .string.ok) { _, _ ->
510
519
val newName = editText.text.toString()
511
520
if (newName.isNotEmpty()) {
512
- if (! doRename(path, newName)) {
521
+ val file = arguments!! .getString(KEY_PATH )!! .toUri().toFile()
522
+ val newFile = File (file.parent, newName)
523
+ if (file.renameTo(newFile)) {
524
+ (targetFragment as SavedLogsFragment ).onRename(newName, newFile.toUri())
525
+ } else {
513
526
activity!! .showToast(getString(R .string.error))
514
527
}
515
- (activity as SavedLogsActivity ).closeCabToolbar()
516
528
}
517
529
dismiss()
518
530
}
@@ -530,32 +542,5 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick
530
542
531
543
return dialog
532
544
}
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
- }
560
545
}
561
546
}
0 commit comments