From 433d31385ef488627b08ebb00ebe6300e2733a96 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 30 Aug 2017 20:53:51 +0200 Subject: [PATCH] misc code style updates --- app/build.gradle | 2 +- .../filemanager/activities/MainActivity.kt | 40 +++++++++---------- .../filemanager/adapters/ItemsAdapter.kt | 24 +++++------ .../dialogs/ChangeSortingDialog.kt | 24 ++++------- .../dialogs/CreateNewItemDialog.kt | 4 +- .../filemanager/fragments/ItemsFragment.kt | 5 ++- 6 files changed, 43 insertions(+), 56 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ed554da5..0751821b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,7 +37,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.26.9' + compile 'com.simplemobiletools:commons:2.27.1' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index 89795f6e..06b9cc92 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -29,17 +29,14 @@ import kotlinx.android.synthetic.main.activity_main.* import java.util.* class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { - var latestFragment: ItemsFragment? = null - var mScrollStates = HashMap() - var mStoredTextColor = 0 - var currentPath = "" + private val STORAGE_PERMISSION = 1 + private val BACK_PRESS_TIMEOUT = 5000 - companion object { - private val STORAGE_PERMISSION = 1 - private val BACK_PRESS_TIMEOUT = 5000 - - private var mWasBackJustPressed: Boolean = false - } + private var latestFragment: ItemsFragment? = null + private var scrollStates = HashMap() + private var storedTextColor = 0 + private var currentPath = "" + private var wasBackJustPressed = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -53,9 +50,9 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br override fun onResume() { super.onResume() updateTextColors(main_screen) - if (mStoredTextColor != config.textColor) { - mStoredTextColor = config.textColor - breadcrumbs.setTextColor(mStoredTextColor) + if (storedTextColor != config.textColor) { + storedTextColor = config.textColor + breadcrumbs.setTextColor(storedTextColor) openPath(currentPath) } invalidateOptionsMenu() @@ -63,7 +60,7 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br override fun onPause() { super.onPause() - mStoredTextColor = config.textColor + storedTextColor = config.textColor } override fun onDestroy() { @@ -89,12 +86,12 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br val bundle = Bundle() bundle.putString(PATH, realPath) - if (mScrollStates.containsKey(realPath)) { - bundle.putParcelable(SCROLL_STATE, mScrollStates[realPath]) + if (scrollStates.containsKey(realPath)) { + bundle.putParcelable(SCROLL_STATE, scrollStates[realPath]) } if (latestFragment != null) { - mScrollStates.put(latestFragment!!.mPath.trimEnd('/'), latestFragment!!.getScrollState()) + scrollStates.put(latestFragment!!.mPath.trimEnd('/'), latestFragment!!.getScrollState()) } latestFragment = ItemsFragment().apply { @@ -208,17 +205,16 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br override fun onBackPressed() { if (breadcrumbs.childCount <= 1) { - if (!mWasBackJustPressed) { - mWasBackJustPressed = true + if (!wasBackJustPressed) { + wasBackJustPressed = true toast(R.string.press_back_again) - Handler().postDelayed({ mWasBackJustPressed = false }, BACK_PRESS_TIMEOUT.toLong()) + Handler().postDelayed({ wasBackJustPressed = false }, BACK_PRESS_TIMEOUT.toLong()) } else { finish() } } else { breadcrumbs.removeBreadcrumb() - val item = breadcrumbs.lastItem - openPath(item.path) + openPath(breadcrumbs.lastItem.path) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt index b73bacb9..cc63e13a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt @@ -43,8 +43,8 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList = selectedPositions + override fun getSelectedPositions() = selectedPositions } - val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { + private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.cab_rename -> displayRenameDialog() @@ -201,7 +199,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList() var curIndex = 0 - for (i in 0..itemViews.size() - 1) { + for (i in 0 until itemViews.size()) { if (itemViews[i] != null) { newItems.put(curIndex, itemViews[i]) curIndex++ @@ -293,7 +291,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList -1 && min < to) { - (min..to - 1).filter { it != from } + (min until to).filter { it != from } .forEach { toggleItemSelection(false, it) } } if (max > -1) { @@ -310,7 +308,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList -1) { - for (i in min..from - 1) + for (i in min until from) toggleItemSelection(false, i) } } @@ -350,7 +348,7 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList Unit) : - DialogInterface.OnClickListener { - companion object { - private var currSorting = 0 - - lateinit var config: Config - lateinit var view: View - } +class ChangeSortingDialog(val activity: SimpleActivity, val path: String = "", val callback: () -> Unit) { + private var currSorting = 0 + private var config = activity.config + private var view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null) init { - config = activity.config - view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null).apply { - sorting_dialog_use_for_this_folder.isChecked = config.hasCustomSorting(path) - } + view.sorting_dialog_use_for_this_folder.isChecked = config.hasCustomSorting(path) AlertDialog.Builder(activity) - .setPositiveButton(R.string.ok, this) + .setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() }) .setNegativeButton(R.string.cancel, null) .create().apply { activity.setupDialogStuff(view, this, R.string.sort_by) @@ -62,7 +52,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val path: String = "", v orderBtn.isChecked = true } - override fun onClick(dialog: DialogInterface, which: Int) { + private fun dialogConfirmed() { val sortingRadio = view.sorting_dialog_radio_sorting var sorting = when (sortingRadio.checkedRadioButtonId) { R.id.sorting_dialog_radio_name -> SORT_BY_NAME diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt index 19535e2d..636dca17 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CreateNewItemDialog.kt @@ -89,8 +89,8 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca success(alertDialog) callback(true) } - } catch (ignored: IOException) { - + } catch (exception: IOException) { + activity.showErrorToast(exception.toString()) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt index fde767fb..38289f1f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt @@ -61,7 +61,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener { } context.updateTextColors(items_holder) if (mStoredTextColor != config.textColor) { - mItems = ArrayList() + mItems = ArrayList() fillItems() mStoredTextColor = config.textColor } @@ -78,6 +78,9 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener { mPath = arguments.getString(PATH) getItems(mPath) { + if (!isAdded) + return@getItems + val newItems = it FileDirItem.sorting = context.config.getFolderSorting(mPath) newItems.sort()