Sorted image swipe & ExplorerViewModel

This commit is contained in:
Hardcore Sushi 2020-08-04 11:44:29 +02:00
parent 400aa831b2
commit 03d5b468f2
4 changed files with 79 additions and 39 deletions

View File

@ -10,7 +10,8 @@ import android.widget.AdapterView.OnItemClickListener
import android.widget.AdapterView.OnItemLongClickListener import android.widget.AdapterView.OnItemLongClickListener
import android.widget.EditText import android.widget.EditText
import android.widget.Toast 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.activity_explorer_base.*
import kotlinx.android.synthetic.main.explorer_info_bar.* import kotlinx.android.synthetic.main.explorer_info_bar.*
import kotlinx.android.synthetic.main.toolbar.* 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.PathUtils
import sushi.hardcore.droidfs.util.GocryptfsVolume import sushi.hardcore.droidfs.util.GocryptfsVolume
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
import java.util.*
open class BaseExplorerActivity : BaseActivity() { open class BaseExplorerActivity : BaseActivity() {
private lateinit var sortModesEntries: Array<String> private lateinit var sortOrderEntries: Array<String>
private lateinit var sortModesValues: Array<String> private lateinit var sortOrderValues: Array<String>
private var currentSortModeIndex = 0 private var currentSortOrderIndex = 0
protected lateinit var gocryptfsVolume: GocryptfsVolume protected lateinit var gocryptfsVolume: GocryptfsVolume
private lateinit var volumeName: String 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<ExplorerElement> protected lateinit var explorerElements: MutableList<ExplorerElement>
protected lateinit var explorerAdapter: ExplorerElementAdapter protected lateinit var explorerAdapter: ExplorerElementAdapter
private var usf_open = false private var usf_open = false
@ -52,14 +57,16 @@ open class BaseExplorerActivity : BaseActivity() {
volumeName = intent.getStringExtra("volume_name") ?: "" volumeName = intent.getStringExtra("volume_name") ?: ""
val sessionID = intent.getIntExtra("sessionID", -1) val sessionID = intent.getIntExtra("sessionID", -1)
gocryptfsVolume = GocryptfsVolume(sessionID) gocryptfsVolume = GocryptfsVolume(sessionID)
sortModesEntries = resources.getStringArray(R.array.sort_orders_entries) sortOrderEntries = resources.getStringArray(R.array.sort_orders_entries)
sortModesValues = resources.getStringArray(R.array.sort_orders_values) sortOrderValues = resources.getStringArray(R.array.sort_orders_values)
currentSortModeIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name")) currentSortOrderIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name"))
init() init()
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
title = "" title = ""
title_text.text = getString(R.string.volume, volumeName) title_text.text = getString(R.string.volume, volumeName)
explorerAdapter = ExplorerElementAdapter(this) explorerAdapter = ExplorerElementAdapter(this)
explorerViewModel= ViewModelProvider(this).get(ExplorerViewModel::class.java)
currentDirectoryPath = explorerViewModel.currentDirectoryPath
setCurrentPath(currentDirectoryPath) setCurrentPath(currentDirectoryPath)
list_explorer.adapter = explorerAdapter list_explorer.adapter = explorerAdapter
list_explorer.onItemClickListener = OnItemClickListener { _, _, position, _ -> onExplorerItemClick(position) } list_explorer.onItemClickListener = OnItemClickListener { _, _, position, _ -> onExplorerItemClick(position) }
@ -70,14 +77,21 @@ open class BaseExplorerActivity : BaseActivity() {
} }
} }
class ExplorerViewModel: ViewModel() {
var currentDirectoryPath = ""
}
protected open fun init() { protected open fun init() {
setContentView(R.layout.activity_explorer_base) 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) val intent = Intent(this, cls)
intent.putExtra("path", filePath) intent.putExtra("path", filePath)
intent.putExtra("sessionID", gocryptfsVolume.sessionID) intent.putExtra("sessionID", gocryptfsVolume.sessionID)
if (sortOrder.isNotEmpty()){
intent.putExtra("sortOrder", sortOrder)
}
startActivity(intent) startActivity(intent)
} }
@ -95,7 +109,7 @@ open class BaseExplorerActivity : BaseActivity() {
setCurrentPath(PathUtils.getParentPath(currentDirectoryPath)) setCurrentPath(PathUtils.getParentPath(currentDirectoryPath))
} }
isImage(fullPath) -> { isImage(fullPath) -> {
startFileViewer(ImageViewer::class.java, fullPath) startFileViewer(ImageViewer::class.java, fullPath, sortOrderValues[currentSortOrderIndex])
} }
isVideo(fullPath) -> { isVideo(fullPath) -> {
startFileViewer(VideoPlayer::class.java, fullPath) startFileViewer(VideoPlayer::class.java, fullPath)
@ -141,28 +155,9 @@ open class BaseExplorerActivity : BaseActivity() {
} }
private fun sortExplorerElements() { private fun sortExplorerElements() {
when (sortModesValues[currentSortModeIndex]) { ExplorerElement.sortBy(sortOrderValues[currentSortOrderIndex], explorerElements)
"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) })
}
}
val sharedPrefsEditor = sharedPrefs.edit() val sharedPrefsEditor = sharedPrefs.edit()
sharedPrefsEditor.putString(ConstValues.sort_order_key, sortModesValues[currentSortModeIndex]) sharedPrefsEditor.putString(ConstValues.sort_order_key, sortOrderValues[currentSortOrderIndex])
sharedPrefsEditor.apply() sharedPrefsEditor.apply()
} }
@ -297,8 +292,8 @@ open class BaseExplorerActivity : BaseActivity() {
R.id.sort -> { R.id.sort -> {
ColoredAlertDialogBuilder(this) ColoredAlertDialogBuilder(this)
.setTitle(R.string.sort_order) .setTitle(R.string.sort_order)
.setSingleChoiceItems(DialogSingleChoiceAdapter(this, sortModesEntries), currentSortModeIndex) { dialog, which -> .setSingleChoiceItems(DialogSingleChoiceAdapter(this, sortOrderEntries), currentSortOrderIndex) { dialog, which ->
currentSortModeIndex = which currentSortOrderIndex = which
setCurrentPath(currentDirectoryPath) setCurrentPath(currentDirectoryPath)
dialog.dismiss() dialog.dismiss()
} }

View File

@ -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 { private fun copyFile(srcPath: String, dstPath: String): Boolean {
var success = true var success = true
val originalHandleId = gocryptfsVolume.openReadMode(srcPath) val originalHandleId = gocryptfsVolume.openReadMode(srcPath)

View File

@ -18,4 +18,29 @@ class ExplorerElement(val name: String, val elementType: Short, val size: Long,
fun getFullPath(): String { fun getFullPath(): String {
return PathUtils.path_join(parentPath, name) return PathUtils.path_join(parentPath, name)
} }
companion object {
fun sortBy(sortOrder: String, explorerElements: MutableList<ExplorerElement>) {
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) })
}
}
}
}
} }

View File

@ -13,6 +13,11 @@ import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import kotlinx.android.synthetic.main.activity_image_viewer.* import kotlinx.android.synthetic.main.activity_image_viewer.*
import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R 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 sushi.hardcore.droidfs.util.PathUtils
import java.security.MessageDigest import java.security.MessageDigest
import kotlin.math.abs import kotlin.math.abs
@ -25,7 +30,8 @@ class ImageViewer: FileViewerActivity() {
private lateinit var glideImage: RequestBuilder<Drawable> private lateinit var glideImage: RequestBuilder<Drawable>
private var x1 = 0F private var x1 = 0F
private var x2 = 0F private var x2 = 0F
private val mappedImages = mutableListOf<String>() private val mappedImages = mutableListOf<ExplorerElement>()
private lateinit var sortOrder: String
private var wasMapped = false private var wasMapped = false
private var currentMappedImageIndex = -1 private var currentMappedImageIndex = -1
private var rotationAngle: Float = 0F private var rotationAngle: Float = 0F
@ -54,11 +60,16 @@ class ImageViewer: FileViewerActivity() {
if (!wasMapped){ if (!wasMapped){
for (e in gocryptfsVolume.recursiveMapFiles(PathUtils.getParentPath(filePath))){ for (e in gocryptfsVolume.recursiveMapFiles(PathUtils.getParentPath(filePath))){
if (e.isRegularFile && ConstValues.isImage(e.name)){ 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 wasMapped = true
} }
if (deltaX < 0){ if (deltaX < 0){
@ -74,7 +85,7 @@ class ImageViewer: FileViewerActivity() {
currentMappedImageIndex -= 1 currentMappedImageIndex -= 1
} }
} }
loadWholeFile(mappedImages[currentMappedImageIndex])?.let { loadWholeFile(mappedImages[currentMappedImageIndex].getFullPath())?.let {
glideImage = Glide.with(this).load(it) glideImage = Glide.with(this).load(it)
glideImage.into(image_viewer) glideImage.into(image_viewer)
} }