From 03d5b468f2f86b07f2cf607e208475763daac0a1 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 4 Aug 2020 11:44:29 +0200 Subject: [PATCH] Sorted image swipe & ExplorerViewModel --- .../droidfs/explorers/BaseExplorerActivity.kt | 63 +++++++++---------- .../droidfs/explorers/ExplorerActivity.kt | 9 +++ .../droidfs/explorers/ExplorerElement.kt | 25 ++++++++ .../droidfs/file_viewers/ImageViewer.kt | 21 +++++-- 4 files changed, 79 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index 03fea95..bcdb636 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -10,7 +10,8 @@ import android.widget.AdapterView.OnItemClickListener import android.widget.AdapterView.OnItemLongClickListener import android.widget.EditText import android.widget.Toast -import com.github.clans.fab.FloatingActionMenu +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import kotlinx.android.synthetic.main.activity_explorer_base.* import kotlinx.android.synthetic.main.explorer_info_bar.* import kotlinx.android.synthetic.main.toolbar.* @@ -33,15 +34,19 @@ import sushi.hardcore.droidfs.util.ExternalProvider import sushi.hardcore.droidfs.util.PathUtils import sushi.hardcore.droidfs.util.GocryptfsVolume import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder -import java.util.* open class BaseExplorerActivity : BaseActivity() { - private lateinit var sortModesEntries: Array - private lateinit var sortModesValues: Array - private var currentSortModeIndex = 0 + private lateinit var sortOrderEntries: Array + private lateinit var sortOrderValues: Array + private var currentSortOrderIndex = 0 protected lateinit var gocryptfsVolume: GocryptfsVolume private lateinit var volumeName: String - protected var currentDirectoryPath = "" + private lateinit var explorerViewModel: ExplorerViewModel + protected var currentDirectoryPath: String = "" + set(value) { + field = value + explorerViewModel.currentDirectoryPath = value + } protected lateinit var explorerElements: MutableList protected lateinit var explorerAdapter: ExplorerElementAdapter private var usf_open = false @@ -52,14 +57,16 @@ open class BaseExplorerActivity : BaseActivity() { volumeName = intent.getStringExtra("volume_name") ?: "" val sessionID = intent.getIntExtra("sessionID", -1) gocryptfsVolume = GocryptfsVolume(sessionID) - sortModesEntries = resources.getStringArray(R.array.sort_orders_entries) - sortModesValues = resources.getStringArray(R.array.sort_orders_values) - currentSortModeIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name")) + sortOrderEntries = resources.getStringArray(R.array.sort_orders_entries) + sortOrderValues = resources.getStringArray(R.array.sort_orders_values) + currentSortOrderIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name")) init() setSupportActionBar(toolbar) title = "" title_text.text = getString(R.string.volume, volumeName) explorerAdapter = ExplorerElementAdapter(this) + explorerViewModel= ViewModelProvider(this).get(ExplorerViewModel::class.java) + currentDirectoryPath = explorerViewModel.currentDirectoryPath setCurrentPath(currentDirectoryPath) list_explorer.adapter = explorerAdapter list_explorer.onItemClickListener = OnItemClickListener { _, _, position, _ -> onExplorerItemClick(position) } @@ -70,14 +77,21 @@ open class BaseExplorerActivity : BaseActivity() { } } + class ExplorerViewModel: ViewModel() { + var currentDirectoryPath = "" + } + protected open fun init() { setContentView(R.layout.activity_explorer_base) } - private fun startFileViewer(cls: Class<*>, filePath: String){ + private fun startFileViewer(cls: Class<*>, filePath: String, sortOrder: String = ""){ val intent = Intent(this, cls) intent.putExtra("path", filePath) intent.putExtra("sessionID", gocryptfsVolume.sessionID) + if (sortOrder.isNotEmpty()){ + intent.putExtra("sortOrder", sortOrder) + } startActivity(intent) } @@ -95,7 +109,7 @@ open class BaseExplorerActivity : BaseActivity() { setCurrentPath(PathUtils.getParentPath(currentDirectoryPath)) } isImage(fullPath) -> { - startFileViewer(ImageViewer::class.java, fullPath) + startFileViewer(ImageViewer::class.java, fullPath, sortOrderValues[currentSortOrderIndex]) } isVideo(fullPath) -> { startFileViewer(VideoPlayer::class.java, fullPath) @@ -141,28 +155,9 @@ open class BaseExplorerActivity : BaseActivity() { } private fun sortExplorerElements() { - when (sortModesValues[currentSortModeIndex]) { - "name" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) }) - } - "size" -> { - explorerElements.sortWith(Comparator { o1, o2 -> (o1.size - o2.size).toInt() }) - } - "date" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o1.mTime.compareTo(o2.mTime) }) - } - "name_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o2.name.compareTo(o1.name) }) - } - "size_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> (o2.size - o1.size).toInt() }) - } - "date_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o2.mTime.compareTo(o1.mTime) }) - } - } + ExplorerElement.sortBy(sortOrderValues[currentSortOrderIndex], explorerElements) val sharedPrefsEditor = sharedPrefs.edit() - sharedPrefsEditor.putString(ConstValues.sort_order_key, sortModesValues[currentSortModeIndex]) + sharedPrefsEditor.putString(ConstValues.sort_order_key, sortOrderValues[currentSortOrderIndex]) sharedPrefsEditor.apply() } @@ -297,8 +292,8 @@ open class BaseExplorerActivity : BaseActivity() { R.id.sort -> { ColoredAlertDialogBuilder(this) .setTitle(R.string.sort_order) - .setSingleChoiceItems(DialogSingleChoiceAdapter(this, sortModesEntries), currentSortModeIndex) { dialog, which -> - currentSortModeIndex = which + .setSingleChoiceItems(DialogSingleChoiceAdapter(this, sortOrderEntries), currentSortOrderIndex) { dialog, which -> + currentSortOrderIndex = which setCurrentPath(currentDirectoryPath) dialog.dismiss() } diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt index d12e352..ecc8437 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -410,6 +410,15 @@ class ExplorerActivity : BaseExplorerActivity() { } } + override fun onBackPressed() { + if (modeSelectLocation) { + cancelCopy() + invalidateOptionsMenu() + } else { + super.onBackPressed() + } + } + private fun copyFile(srcPath: String, dstPath: String): Boolean { var success = true val originalHandleId = gocryptfsVolume.openReadMode(srcPath) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt index 5e81c2d..27e45d8 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt @@ -18,4 +18,29 @@ class ExplorerElement(val name: String, val elementType: Short, val size: Long, fun getFullPath(): String { return PathUtils.path_join(parentPath, name) } + + companion object { + fun sortBy(sortOrder: String, explorerElements: MutableList) { + when (sortOrder) { + "name" -> { + explorerElements.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) }) + } + "size" -> { + explorerElements.sortWith(Comparator { o1, o2 -> (o1.size - o2.size).toInt() }) + } + "date" -> { + explorerElements.sortWith(Comparator { o1, o2 -> o1.mTime.compareTo(o2.mTime) }) + } + "name_desc" -> { + explorerElements.sortWith(Comparator { o1, o2 -> o2.name.compareTo(o1.name) }) + } + "size_desc" -> { + explorerElements.sortWith(Comparator { o1, o2 -> (o2.size - o1.size).toInt() }) + } + "date_desc" -> { + explorerElements.sortWith(Comparator { o1, o2 -> o2.mTime.compareTo(o1.mTime) }) + } + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt index 8116180..bb39bbd 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt @@ -13,6 +13,11 @@ import com.bumptech.glide.load.resource.bitmap.BitmapTransformation import kotlinx.android.synthetic.main.activity_image_viewer.* import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.R +<<<<<<< HEAD +======= +import sushi.hardcore.droidfs.util.MiscUtils +import sushi.hardcore.droidfs.explorers.ExplorerElement +>>>>>>> ccc453a... Sorted image swipe & ExplorerViewModel import sushi.hardcore.droidfs.util.PathUtils import java.security.MessageDigest import kotlin.math.abs @@ -25,7 +30,8 @@ class ImageViewer: FileViewerActivity() { private lateinit var glideImage: RequestBuilder private var x1 = 0F private var x2 = 0F - private val mappedImages = mutableListOf() + private val mappedImages = mutableListOf() + private lateinit var sortOrder: String private var wasMapped = false private var currentMappedImageIndex = -1 private var rotationAngle: Float = 0F @@ -54,11 +60,16 @@ class ImageViewer: FileViewerActivity() { if (!wasMapped){ for (e in gocryptfsVolume.recursiveMapFiles(PathUtils.getParentPath(filePath))){ if (e.isRegularFile && ConstValues.isImage(e.name)){ - mappedImages.add(e.getFullPath()) + mappedImages.add(e) + } + } + sortOrder = intent.getStringExtra("sortOrder") ?: "name" + ExplorerElement.sortBy(sortOrder, mappedImages) + for ((i, e) in mappedImages.withIndex()){ + if (filePath == e.getFullPath()){ + currentMappedImageIndex = i } } - mappedImages.sortWith(Comparator { p1, p2 -> p1.compareTo(p2) }) - currentMappedImageIndex = mappedImages.indexOf(filePath) wasMapped = true } if (deltaX < 0){ @@ -74,7 +85,7 @@ class ImageViewer: FileViewerActivity() { currentMappedImageIndex -= 1 } } - loadWholeFile(mappedImages[currentMappedImageIndex])?.let { + loadWholeFile(mappedImages[currentMappedImageIndex].getFullPath())?.let { glideImage = Glide.with(this).load(it) glideImage.into(image_viewer) }