Minor cleanup, added comments
This commit is contained in:
parent
d51544f927
commit
107146c8d9
|
@ -69,7 +69,13 @@ class DownloadFile(
|
||||||
|
|
||||||
val progress: MutableLiveData<Int> = MutableLiveData(0)
|
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 {
|
when {
|
||||||
Storage.isPathExists(saveFile) -> {
|
Storage.isPathExists(saveFile) -> {
|
||||||
DownloadStatus.PINNED
|
DownloadStatus.PINNED
|
||||||
|
@ -84,7 +90,7 @@ class DownloadFile(
|
||||||
}
|
}
|
||||||
|
|
||||||
val status: MutableLiveData<DownloadStatus> by lazy {
|
val status: MutableLiveData<DownloadStatus> by lazy {
|
||||||
MutableLiveData(lazyStatus.value)
|
MutableLiveData(lazyInitialStatus.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|
|
@ -267,7 +267,7 @@ class Downloader(
|
||||||
temp.addAll(downloadQueue)
|
temp.addAll(downloadQueue)
|
||||||
temp.addAll(
|
temp.addAll(
|
||||||
playlist.filter {
|
playlist.filter {
|
||||||
if (!it.lazyStatus.isInitialized()) false
|
if (!it.isStatusInitialized) false
|
||||||
else when (it.status.value) {
|
else when (it.status.value) {
|
||||||
DownloadStatus.DOWNLOADING -> true
|
DownloadStatus.DOWNLOADING -> true
|
||||||
else -> false
|
else -> false
|
||||||
|
|
|
@ -12,6 +12,9 @@ import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains the abstract file operations which Ultrasonic uses during the media store access
|
||||||
|
*/
|
||||||
abstract class AbstractFile : Comparable<AbstractFile> {
|
abstract class AbstractFile : Comparable<AbstractFile> {
|
||||||
abstract val name: String
|
abstract val name: String
|
||||||
abstract val isDirectory: Boolean
|
abstract val isDirectory: Boolean
|
||||||
|
|
|
@ -16,6 +16,10 @@ import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import org.moire.ultrasonic.app.UApp
|
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() {
|
class JavaFile(override val parent: AbstractFile?, val file: File) : AbstractFile() {
|
||||||
override val name: String = file.name
|
override val name: String = file.name
|
||||||
override val isDirectory: Boolean = file.isDirectory
|
override val isDirectory: Boolean = file.isDirectory
|
||||||
|
|
|
@ -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
|
package org.moire.ultrasonic.util
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicReference
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
import kotlin.reflect.KProperty
|
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) {
|
class ResettableLazy<T>(private val initializer: () -> T) {
|
||||||
private val lazyRef: AtomicReference<Lazy<T>> = AtomicReference(
|
private val lazyRef: AtomicReference<Lazy<T>> = AtomicReference(
|
||||||
lazy(
|
lazy(
|
||||||
|
|
|
@ -24,21 +24,6 @@ object Storage {
|
||||||
getRoot()!!
|
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() {
|
fun reset() {
|
||||||
StorageFile.storageFilePathDictionary.clear()
|
StorageFile.storageFilePathDictionary.clear()
|
||||||
StorageFile.notExistingPathDictionary.clear()
|
StorageFile.notExistingPathDictionary.clear()
|
||||||
|
@ -74,6 +59,21 @@ object Storage {
|
||||||
fun rename(pathFrom: AbstractFile, pathTo: String) {
|
fun rename(pathFrom: AbstractFile, pathTo: String) {
|
||||||
mediaRoot.value.rename(pathFrom, pathTo)
|
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 {
|
fun String.isUri(): Boolean {
|
||||||
|
|
|
@ -18,6 +18,10 @@ import java.util.concurrent.ConcurrentHashMap
|
||||||
import org.moire.ultrasonic.app.UApp
|
import org.moire.ultrasonic.app.UApp
|
||||||
import timber.log.Timber
|
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(
|
class StorageFile(
|
||||||
override val parent: StorageFile?,
|
override val parent: StorageFile?,
|
||||||
var uri: Uri,
|
var uri: Uri,
|
||||||
|
|
Loading…
Reference in New Issue