Minor cleanup, added comments

This commit is contained in:
Nite 2021-12-16 14:18:54 +01:00
parent d51544f927
commit 107146c8d9
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
7 changed files with 45 additions and 18 deletions

View File

@ -69,7 +69,13 @@ class DownloadFile(
val progress: MutableLiveData<Int> = MutableLiveData(0)
val lazyStatus: Lazy<DownloadStatus> = lazy {
// We must be able to query if the status is initialized.
// The status is lazy because DownloadFiles are usually created in bulk, and
// checking their status possibly means a slow SAF operation.
val isStatusInitialized: Boolean
get() = lazyInitialStatus.isInitialized()
private val lazyInitialStatus: Lazy<DownloadStatus> = lazy {
when {
Storage.isPathExists(saveFile) -> {
DownloadStatus.PINNED
@ -84,7 +90,7 @@ class DownloadFile(
}
val status: MutableLiveData<DownloadStatus> by lazy {
MutableLiveData(lazyStatus.value)
MutableLiveData(lazyInitialStatus.value)
}
init {

View File

@ -267,7 +267,7 @@ class Downloader(
temp.addAll(downloadQueue)
temp.addAll(
playlist.filter {
if (!it.lazyStatus.isInitialized()) false
if (!it.isStatusInitialized) false
else when (it.status.value) {
DownloadStatus.DOWNLOADING -> true
else -> false

View File

@ -12,6 +12,9 @@ import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
/**
* Contains the abstract file operations which Ultrasonic uses during the media store access
*/
abstract class AbstractFile : Comparable<AbstractFile> {
abstract val name: String
abstract val isDirectory: Boolean

View File

@ -16,6 +16,10 @@ import java.io.InputStream
import java.io.OutputStream
import org.moire.ultrasonic.app.UApp
/**
* The java.io.File based implementation of AbstractFile
* This class is used when the Ultrasonic directory is set as media storage
*/
class JavaFile(override val parent: AbstractFile?, val file: File) : AbstractFile() {
override val name: String = file.name
override val isDirectory: Boolean = file.isDirectory

View File

@ -1,8 +1,18 @@
/*
* ResettableLazy.kt
* Copyright (C) 2009-2021 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
*/
package org.moire.ultrasonic.util
import java.util.concurrent.atomic.AtomicReference
import kotlin.reflect.KProperty
/**
* This class is similar to Lazy, but its value can be reset and then reinitialized
*/
class ResettableLazy<T>(private val initializer: () -> T) {
private val lazyRef: AtomicReference<Lazy<T>> = AtomicReference(
lazy(

View File

@ -24,21 +24,6 @@ object Storage {
getRoot()!!
}
private fun getRoot(): AbstractFile? {
return if (Settings.cacheLocation.isUri()) {
val documentFile = DocumentFile.fromTreeUri(
UApp.applicationContext(),
Uri.parse(Settings.cacheLocation)
) ?: return null
if (!documentFile.exists()) return null
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory)
} else {
val file = File(Settings.cacheLocation)
if (!file.exists()) return null
JavaFile(null, file)
}
}
fun reset() {
StorageFile.storageFilePathDictionary.clear()
StorageFile.notExistingPathDictionary.clear()
@ -74,6 +59,21 @@ object Storage {
fun rename(pathFrom: AbstractFile, pathTo: String) {
mediaRoot.value.rename(pathFrom, pathTo)
}
private fun getRoot(): AbstractFile? {
return if (Settings.cacheLocation.isUri()) {
val documentFile = DocumentFile.fromTreeUri(
UApp.applicationContext(),
Uri.parse(Settings.cacheLocation)
) ?: return null
if (!documentFile.exists()) return null
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory)
} else {
val file = File(Settings.cacheLocation)
if (!file.exists()) return null
JavaFile(null, file)
}
}
}
fun String.isUri(): Boolean {

View File

@ -18,6 +18,10 @@ import java.util.concurrent.ConcurrentHashMap
import org.moire.ultrasonic.app.UApp
import timber.log.Timber
/**
* The DocumentsContract based implementation of AbstractFile
* This class is used when a user selected directory is set as media storage
*/
class StorageFile(
override val parent: StorageFile?,
var uri: Uri,