Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
VishalNehra committed Oct 30, 2023
1 parent 7003711 commit a5e68ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ kotlin.code.style=official
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
#org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home
org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home
32 changes: 17 additions & 15 deletions trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.FileReader
Expand All @@ -39,42 +40,43 @@ typealias ListTrashBinFilesCallback = (parentTrashBinPath: String) -> List<Trash
* Pass null if you want to invoke cleanup manually through triggerCleanup. This will be executed in the same thread where your bin functions are executed.
*/
class TrashBin constructor(
context: Context,
doTriggerCleanup: Boolean,
var trashConfig: TrashBinConfig,
var deletePermanentlySuperCallback: DeletePermanentlyCallback?,
var listTrashBinFilesSuperCallback:
ListTrashBinFilesCallback? = null,
context: Context
ListTrashBinFilesCallback? = null
) {

private var metadata: TrashBinMetadata? = null

init {
trashConfig.getTrashBinFilesDirectory()
metadata = getTrashBinMetadata()

val sharedPreferences = context.getSharedPreferences(
"${context.packageName}.com.amaze.trashbin",
Context.MODE_PRIVATE
)

val lastCleanup = sharedPreferences.getLong("com.amaze.trashbin.lastCleanup", -1)

val lastCleanup = sharedPreferences.getLong(
"com.amaze.trashbin.lastCleanup",
-1
)
val currentTime = System.currentTimeMillis()

val hours = ((lastCleanup - currentTime) / (1000 * 60 * 60))

if (hours >= trashConfig.getCleanupIntervalHours()) {

GlobalScope.launch {
if (trashConfig.getCleanupIntervalHours() != -1 &&
hours >= trashConfig.getCleanupIntervalHours() &&
doTriggerCleanup && deletePermanentlySuperCallback != null
) {
GlobalScope.launch(Dispatchers.IO) {
triggerCleanup {

sharedPreferences.edit().putLong("com.amaze.trashbin.lastCleanup", currentTime).apply()

sharedPreferences.edit().putLong(
"com.amaze.trashbin.lastCleanup",
currentTime
).apply()
false
}
}
}

}

fun deletePermanently(
Expand Down
8 changes: 4 additions & 4 deletions trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ data class TrashBinConfig(
val retentionDays: Int,
val retentionBytes: Long,
val retentionNumOfFiles: Int,
val cleanupHours: Int = -1,
val deleteRogueFiles: Boolean,
val triggerCleanupAutomatically: Boolean,
private val cleanupDays: Int,
private val cleanupHours: Int
val triggerCleanupAutomatically: Boolean
) {

companion object {
const val RETENTION_DAYS_INFINITE = -1
const val RETENTION_BYTES_INFINITE = -1L
const val RETENTION_NUM_OF_FILES = -1
const val INTERVAL_CLEANUP_HOURS = 1
const val TRASH_BIN_CAPACITY_INVALID = -1
const val TRASH_BIN_DIR = "TrashBinFiles"
const val TRASH_BIN_META_FILE = "metadata.json"
Expand Down Expand Up @@ -69,6 +69,6 @@ data class TrashBinConfig(
}

fun getCleanupIntervalHours(): Int {
return cleanupDays * 24 + cleanupHours;
return cleanupHours
}
}

0 comments on commit a5e68ee

Please sign in to comment.