adding a couple improvements related to un/hiding folders

This commit is contained in:
tibbi 2018-04-09 14:04:42 +02:00
parent 71127d45d1
commit d91228eedf
7 changed files with 61 additions and 47 deletions

View File

@ -686,6 +686,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
gotDirectories(directories, true)
} else {
mDirs = directories
storeDirectories()
}
}

View File

@ -181,26 +181,20 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
private fun toggleFoldersVisibility(hide: Boolean) {
getSelectedPaths().forEach {
val path = it
if (hide) {
if (config.wasHideFolderTooltipShown) {
hideFolder(it)
hideFolder(path)
} else {
config.wasHideFolderTooltipShown = true
ConfirmationDialog(activity, activity.getString(R.string.hide_folder_description)) {
hideFolder(it)
hideFolder(path)
}
}
} else {
activity.removeNoMedia(it) {
activity.removeNoMedia(path) {
if (activity.config.shouldShowHidden) {
dirs.forEachIndexed { index, directory ->
if (directory.path.startsWith(it, true)) {
val hidden = activity.getString(R.string.hidden)
directory.name = directory.name.removeSuffix(hidden).trim()
}
}
updateDirs(dirs)
listener?.updateDirectories(dirs.toList() as ArrayList, true)
updateFolderNames()
} else {
activity.runOnUiThread {
listener?.refreshItems()
@ -212,17 +206,22 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
}
}
private fun updateFolderNames() {
val includedFolders = activity.config.includedFolders
val hidden = activity.getString(R.string.hidden)
dirs.forEach {
it.name = activity.checkAppendingHidden(it.path, hidden, includedFolders)
}
listener?.updateDirectories(dirs.toList() as ArrayList, false)
activity.runOnUiThread {
updateDirs(dirs)
}
}
private fun hideFolder(path: String) {
activity.addNoMedia(path) {
if (activity.config.shouldShowHidden) {
val hidden = activity.getString(R.string.hidden)
dirs.forEachIndexed { index, directory ->
if (directory.path.startsWith(path, true)) {
directory.name += " $hidden"
}
}
updateDirs(dirs)
listener?.updateDirectories(dirs.toList() as ArrayList, true)
updateFolderNames()
} else {
val affectedPositions = ArrayList<Int>()
val newDirs = dirs.filterIndexed { index, directory ->

View File

@ -7,8 +7,8 @@ import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.checkAppendingHidden
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.doesParentHaveNoMedia
import com.simplemobiletools.gallery.extensions.sumByLong
import com.simplemobiletools.gallery.helpers.MediaFetcher
import com.simplemobiletools.gallery.models.Directory
@ -50,23 +50,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
}
}
var dirName = when (parentDir) {
context.internalStoragePath -> context.getString(R.string.internal)
context.sdCardPath -> context.getString(R.string.sd_card)
OTG_PATH -> context.getString(R.string.otg)
else -> {
if (parentDir.startsWith(OTG_PATH)) {
parentDir.trimEnd('/').substringAfterLast('/')
} else {
parentDir.getFilenameFromPath()
}
}
}
if (File(parentDir).doesParentHaveNoMedia() && !includedFolders.contains(parentDir)) {
dirName += " $hidden"
}
val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders)
val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified)
val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken)
val size = curMedia.sumByLong { it.size }

View File

@ -120,25 +120,34 @@ fun BaseSimpleActivity.addNoMedia(path: String, callback: () -> Unit) {
val fileDocument = getDocumentFile(path)
if (fileDocument?.exists() == true && fileDocument.isDirectory) {
fileDocument.createFile("", NOMEDIA)
applicationContext.scanFile(file) {
callback()
}
} else {
toast(R.string.unknown_error_occurred)
callback()
}
}
} else {
try {
file.createNewFile()
applicationContext.scanFile(file) {
callback()
}
} catch (e: Exception) {
showErrorToast(e)
callback()
}
}
applicationContext.scanFile(file) {
callback()
}
}
fun BaseSimpleActivity.removeNoMedia(path: String, callback: (() -> Unit)? = null) {
val file = File(path, NOMEDIA)
if (!file.exists()) {
callback?.invoke()
return
}
deleteFile(file.toFileDirItem(applicationContext)) {
callback?.invoke()
}

View File

@ -10,9 +10,9 @@ import android.os.Build
import android.provider.MediaStore
import android.view.WindowManager
import com.google.gson.Gson
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.OTG_PATH
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
@ -163,3 +163,24 @@ fun Context.storeDirectoryItems(items: ArrayList<Directory>) {
val subList = items.subList(0, Math.min(SAVE_DIRS_CNT, items.size))
config.directories = Gson().toJson(subList)
}
fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>): String {
val dirName = when (path) {
internalStoragePath -> getString(R.string.internal)
sdCardPath -> getString(R.string.sd_card)
OTG_PATH -> getString(R.string.otg)
else -> {
if (path.startsWith(OTG_PATH)) {
path.trimEnd('/').substringAfterLast('/')
} else {
path.getFilenameFromPath()
}
}
}
return if (File(path).doesThisOrParentHaveNoMedia() && !includedFolders.contains(path)) {
"$dirName $hidden"
} else {
dirName
}
}

View File

@ -5,13 +5,13 @@ import java.io.File
fun File.containsNoMedia() = isDirectory && File(this, NOMEDIA).exists()
fun File.doesParentHaveNoMedia(): Boolean {
fun File.doesThisOrParentHaveNoMedia(): Boolean {
var curFile = this
while (true) {
if (curFile.containsNoMedia()) {
return true
}
curFile = curFile.parentFile
curFile = curFile.parentFile ?: break
if (curFile.absolutePath == "/") {
break
}

View File

@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.containsNoMedia
import com.simplemobiletools.gallery.extensions.doesParentHaveNoMedia
import com.simplemobiletools.gallery.extensions.doesThisOrParentHaveNoMedia
import com.simplemobiletools.gallery.extensions.isPathInMediaStore
import com.simplemobiletools.gallery.models.Medium
import java.io.File
@ -253,7 +253,7 @@ class MediaFetcher(val context: Context) {
} else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.")
if (!containsNoMediaOrDot) {
containsNoMediaOrDot = file.doesParentHaveNoMedia()
containsNoMediaOrDot = file.doesThisOrParentHaveNoMedia()
}
!containsNoMediaOrDot
} else {