mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-16 01:10:36 +03:00
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 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 {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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 {
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user