Skip to content

Commit

Permalink
some real spaghetti code 2
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Sep 17, 2022
1 parent 98b67e1 commit 2f53281
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ class MainActivity : AppCompatActivity() {
downloadButton.setDefaultClickListener {
val id = downloadButton.persistentId ?: return@setDefaultClickListener listOf()
listOf(
//newUriRequest(id,"magnet:?xt=urn:btih:f7a543c036b06ba973beea56e98543c9e315314d&dn=%5BSubsPlease%5D%20Yofukashi%20no%20Uta%20-%2010%20%281080p%29%20%5B52314177%5D.mkv&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce")
newUriRequest(
id, "a", "Hello World",
id, "https://speed.hetzner.de/1010MB.bin", "Hello World",
),
newUriRequest(
id, "https://speed.hetzner.de/100MB.bin", "Hello World",
id, "https://speed.hetzner.de/100MB.bin", "Hello World2",
),
//newUriRequest(
// id, "https://speed.hetzner.de/100MB.bin", "Hello World2",
//),
//newUriRequest(
// id, "https://speed.hetzner.de/100MB.bin", "Hello World",
//)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import com.lagradost.fetchbutton.Aria2Save.setKey
import com.lagradost.fetchbutton.aria2c.AbstractClient.DownloadListener.getInfo
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand Down Expand Up @@ -278,11 +279,12 @@ abstract class AbstractClient(
updateMutex.withLock {
batchRequestStatus().forEach { resultList ->
resultList.getOrNull()?.results?.forEach { json ->
// if less then 10% completed and error
if (json.status == "error" && json.completedLength * 100L / (json.totalLength + 1) < 10L) {
// if less then 5% completed and error
if (json.status == "error" && json.completedLength * 100L / (json.totalLength + 1) < 5L) {
DownloadListener.failQueueMapMutex.withLock {
DownloadListener.failQueueMap[json.gid]?.let { queue ->
DownloadListener.failQueueMap.remove(json.gid)
remove(json.gid, true)
downloadFailQueue(queue.slice(1 until queue.size)) { _, _ -> }
}
}
Expand All @@ -292,7 +294,7 @@ abstract class AbstractClient(
DownloadListener.sessionIdToLastRequest[id]?.let { lastRequest ->
Aria2Starter.saveActivity.get()?.setKey(
id,
SavedData(lastRequest, json.files)
SavedData(lastRequest, getInfo(json.gid).map { it.files }.flatten())
)
}
}
Expand Down Expand Up @@ -413,7 +415,6 @@ abstract class AbstractClient(
private fun createUnPauseRequest(gid: String): AriaRequest = createRequest(Method.UNPAUSE, gid)
private fun createRemoveRequest(gid: String): AriaRequest = createRequest(Method.REMOVE, gid)


private fun createRequest(method: Method, vararg args: Any): AriaRequest =
AriaRequest(id = ++requestId, method = method, params = arrayOf(*args))

Expand All @@ -423,7 +424,7 @@ abstract class AbstractClient(

suspend fun pauseAsync(gid: String, all: Boolean) {
if (all) {
DownloadListener.getInfo(gid)
getInfo(gid)
.forEach { item -> sendRaw(createPauseRequest(item.gid)) }
} else {
sendRaw(createPauseRequest(gid))
Expand All @@ -436,7 +437,7 @@ abstract class AbstractClient(

suspend fun unpauseAsync(gid: String, all: Boolean = true) {
if (all) {
DownloadListener.getInfo(gid)
getInfo(gid)
.forEach { item -> sendRaw(createUnPauseRequest(item.gid)) }
} else {
sendRaw(createUnPauseRequest(gid))
Expand All @@ -449,7 +450,7 @@ abstract class AbstractClient(

suspend fun removeAsync(gid: String, all: Boolean = true) {
if (all) {
DownloadListener.getInfo(gid)
getInfo(gid)
.forEach { item -> sendRaw(createRemoveRequest(item.gid)) }
} else {
sendRaw(createRemoveRequest(gid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
}
setProgress(downloadedBytes, totalBytes)
// some extra padding for just in case
setStatus(if (downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.Complete else DownloadStatusTell.Paused)
setStatus(if (downloadedBytes > 1024L && downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.Complete else DownloadStatusTell.Paused)
} ?: run {
resetView()
}
Expand Down Expand Up @@ -149,7 +149,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
abstract fun resetView()

open fun performDownload(request: UriRequest) {
Aria2Starter.client?.download(request) { localGid ->
Aria2Starter.client?.download(request) { _ ->
lastRequest = null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) :
open fun setDefaultClickListener(requestGetter: suspend BaseFetchButton.() -> List<UriRequest>) {
this.setOnClickListener {
when (this.currentStatus) {
null -> ioThread {
val request = requestGetter.invoke(this)
if (request.size == 1) {
performDownload(request.first())
} else if (request.isNotEmpty()) {
performFailQueueDownload(request)
null -> {
setStatus(DownloadStatusTell.Waiting)
ioThread {
val request = requestGetter.invoke(this)
if (request.size == 1) {
performDownload(request.first())
} else if (request.isNotEmpty()) {
performFailQueueDownload(request)
}
}
}
DownloadStatusTell.Paused -> {
Expand Down

0 comments on commit 2f53281

Please sign in to comment.