From f4ea041a8ba69ce5e632110b6e662307d4e74bd5 Mon Sep 17 00:00:00 2001
From: tibbi <tibor@kaputa.sk>
Date: Wed, 27 Feb 2019 16:44:33 +0100
Subject: [PATCH] take selected file loading priority into consideration at
 loading files

---
 .../gallery/pro/activities/MainActivity.kt    |  5 +++--
 .../pro/asynctasks/GetMediaAsynctask.kt       |  6 ++++--
 .../gallery/pro/helpers/MediaFetcher.kt       | 19 ++++++++++++-------
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt
index 52b27337d..7ccbbc379 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt
@@ -816,6 +816,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
         val tempFolderPath = config.tempFolderPath
         val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0
         val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0
+        val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 || config.fileLoadingPriority == PRIORITY_COMPROMISE
         val favoritePaths = getFavoritePaths()
         val dirPathsToRemove = ArrayList<String>()
 
@@ -825,7 +826,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
                     return
                 }
 
-                val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths, false)
+                val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false)
                 val newDir = if (curMedia.isEmpty()) {
                     if (directory.path != tempFolderPath) {
                         dirPathsToRemove.add(directory.path)
@@ -898,7 +899,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
                 return
             }
 
-            val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths, false)
+            val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperFileSize, favoritePaths, false)
             if (newMedia.isEmpty()) {
                 continue
             }
diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt
index ed3035ac0..76537076d 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt
@@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.pro.asynctasks
 import android.content.Context
 import android.os.AsyncTask
 import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN
+import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
 import com.simplemobiletools.gallery.pro.extensions.config
 import com.simplemobiletools.gallery.pro.extensions.getFavoritePaths
 import com.simplemobiletools.gallery.pro.helpers.*
@@ -18,20 +19,21 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
     override fun doInBackground(vararg params: Void): ArrayList<ThumbnailItem> {
         val pathToUse = if (showAll) SHOW_ALL else mPath
         val getProperDateTaken = context.config.getFileSorting(pathToUse) and SORT_BY_DATE_TAKEN != 0 || context.config.getFolderGrouping(pathToUse) and GROUP_BY_DATE_TAKEN != 0
+        val getProperFileSize = context.config.getFileSorting(pathToUse) and SORT_BY_SIZE != 0 || context.config.fileLoadingPriority == PRIORITY_COMPROMISE
         val favoritePaths = context.getFavoritePaths()
         val getVideoDurations = context.config.showThumbnailVideoDuration
         val media = if (showAll) {
             val foldersToScan = mediaFetcher.getFoldersToScan().filter { it != RECYCLE_BIN && it != FAVORITES }
             val media = ArrayList<Medium>()
             foldersToScan.forEach {
-                val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, favoritePaths, getVideoDurations, false)
+                val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperFileSize, favoritePaths, getVideoDurations, false)
                 media.addAll(newMedia)
             }
 
             mediaFetcher.sortMedia(media, context.config.getFileSorting(SHOW_ALL))
             media
         } else {
-            mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths, getVideoDurations)
+            mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, getProperFileSize, favoritePaths, getVideoDurations)
         }
         return mediaFetcher.groupMedia(media, pathToUse)
     }
diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt
index 4e631fc21..b725d7327 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt
@@ -17,15 +17,15 @@ import java.util.*
 class MediaFetcher(val context: Context) {
     var shouldStop = false
 
-    fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, favoritePaths: ArrayList<String>,
-                     getVideoDurations: Boolean, sortMedia: Boolean = true): ArrayList<Medium> {
+    fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperFileSize: Boolean,
+                     favoritePaths: ArrayList<String>, getVideoDurations: Boolean, sortMedia: Boolean = true): ArrayList<Medium> {
         val filterMedia = context.config.filterMedia
         if (filterMedia == 0) {
             return ArrayList()
         }
 
         val curMedia = ArrayList<Medium>()
-        val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, favoritePaths, getVideoDurations)
+        val newMedia = getMediaInFolder(curPath, isPickImage, isPickVideo, filterMedia, getProperDateTaken, getProperFileSize, favoritePaths, getVideoDurations)
         curMedia.addAll(newMedia)
 
         if (sortMedia) {
@@ -155,7 +155,7 @@ class MediaFetcher(val context: Context) {
     }
 
     private fun getMediaInFolder(folder: String, isPickImage: Boolean, isPickVideo: Boolean, filterMedia: Int, getProperDateTaken: Boolean,
-                                 favoritePaths: ArrayList<String>, getVideoDurations: Boolean): ArrayList<Medium> {
+                                 getProperFileSize: Boolean, favoritePaths: ArrayList<String>, getVideoDurations: Boolean): ArrayList<Medium> {
         val media = ArrayList<Medium>()
 
         val isRecycleBin = folder == RECYCLE_BIN
@@ -209,9 +209,14 @@ class MediaFetcher(val context: Context) {
             if (!showHidden && filename.startsWith('.'))
                 continue
 
-            val size = file.length()
-            if (size <= 0L || (checkFileExistence && !file.exists()))
+            val size = if (getProperFileSize || checkFileExistence) file.length() else 1L
+            if ((getProperFileSize || checkFileExistence) && size <= 0L) {
                 continue
+            }
+
+            if (checkFileExistence && !file.exists()) {
+                continue
+            }
 
             if (isRecycleBin) {
                 deletedMedia.firstOrNull { it.path == path }?.apply {
@@ -356,7 +361,7 @@ class MediaFetcher(val context: Context) {
         return if (timestamp.areDigitsOnly()) {
             val cal = Calendar.getInstance(Locale.ENGLISH)
             cal.timeInMillis = timestamp.toLong()
-            return DateFormat.format("dd MMM yyyy", cal).toString()
+            DateFormat.format("dd MMM yyyy", cal).toString()
         } else {
             ""
         }