Skip to content

Commit

Permalink
Cleaned some code and added "Open in Spotify" in the metadata card
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyESP committed Mar 3, 2023
1 parent 9953517 commit ea932b8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 166 deletions.
136 changes: 0 additions & 136 deletions app/src/main/java/com/bobbyesp/spowlo/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package com.bobbyesp.spowlo

import android.util.Log
import androidx.annotation.CheckResult
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.text.AnnotatedString
import com.bobbyesp.library.SpotDL
import com.bobbyesp.library.dto.Song
import com.bobbyesp.spowlo.App.Companion.applicationScope
import com.bobbyesp.spowlo.App.Companion.context
import com.bobbyesp.spowlo.database.CommandTemplate
import com.bobbyesp.spowlo.utils.DownloaderUtil
import com.bobbyesp.spowlo.utils.FilesUtil
import com.bobbyesp.spowlo.utils.ToastUtil
Expand All @@ -36,65 +32,6 @@ object Downloader {

fun makeKey(url: String, templateName: String): String = "${templateName}_$url"

data class CustomCommandTask(
val template: CommandTemplate,
val url: String,
val output: String,
val state: State,
val currentLine: String
) {
fun toKey() = makeKey(url, template.name)
sealed class State {
data class Error(val errorReport: String) : State()
object Completed : State()
object Canceled : State()
data class Running(val progress: Float) : State()
}

override fun hashCode(): Int {
return (this.url + this.template.name + this.template.template).hashCode()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as CustomCommandTask

if (template != other.template) return false
if (url != other.url) return false
if (output != other.output) return false
if (state != other.state) return false
if (currentLine != other.currentLine) return false

return true
}


fun onCopyLog(clipboardManager: ClipboardManager) {
clipboardManager.setText(AnnotatedString(output))
}


fun onRestart() {
applicationScope.launch(Dispatchers.IO) {
DownloaderUtil.executeCommandInBackground(url, template)
}
}


fun onCopyError(clipboardManager: ClipboardManager) {
clipboardManager.setText(AnnotatedString(currentLine))
ToastUtil.makeToast(R.string.error_copied)
}

fun onCancel() {
toKey().run {
SpotDL.getInstance().destroyProcessById(this)
onProcessCanceled(this)
}
}
}

data class ErrorState(
val errorReport: String = "",
Expand Down Expand Up @@ -157,23 +94,10 @@ object Downloader {
private val mutableProcessCount = MutableStateFlow(0)
private val processCount = mutableProcessCount.asStateFlow()

val mutableTaskList = mutableStateMapOf<String, CustomCommandTask>()


fun onProcessEnded() =
mutableProcessCount.update { it - 1 }


fun onProcessCanceled(taskId: String) =
mutableTaskList.run {
get(taskId)?.let {
this.put(
taskId,
it.copy(state = CustomCommandTask.State.Canceled)
)
}
}

fun isDownloaderAvailable(): Boolean {
if (downloaderState.value !is State.Idle) {
ToastUtil.makeToastSuspend(context.getString(R.string.task_running))
Expand Down Expand Up @@ -292,66 +216,6 @@ object Downloader {
}
}

fun onTaskStarted(template: CommandTemplate, url: String) =
CustomCommandTask(
template = template,
url = url,
output = "",
state = CustomCommandTask.State.Running(0f),
currentLine = ""
).run {
mutableTaskList.put(this.toKey(), this)
}

fun updateTaskOutput(template: CommandTemplate, url: String, line: String, progress: Float) {
val key = makeKey(url, template.name)
val oldValue = mutableTaskList[key] ?: return
val newValue = oldValue.run {
copy(
output = output + line + "\n",
currentLine = line,
state = CustomCommandTask.State.Running(progress)
)
}
mutableTaskList[key] = newValue
}

fun onTaskEnded(
template: CommandTemplate,
url: String,
response: String? = null
) {
val key = makeKey(url, template.name)
/*NotificationUtil.finishNotification(
notificationId = key.toNotificationId(),
title = key,
text = context.getString(R.string.status_completed),
)*/
mutableTaskList.run {
val oldValue = get(key) ?: return
val newValue = oldValue.copy(state = CustomCommandTask.State.Completed).run {
response?.let { copy(output = response) } ?: this
}
this[key] = newValue
}
FilesUtil.scanDownloadDirectoryToMediaLibrary(App.audioDownloadDir)
}

fun onTaskError(errorReport: String, template: CommandTemplate, url: String) =
mutableTaskList.run {
val key = makeKey(url, template.name)
/*NotificationUtil.makeErrorReportNotification(
notificationId = key.toNotificationId(),
error = errorReport
)*/
val oldValue = mutableTaskList[key] ?: return
mutableTaskList[key] = oldValue.copy(
state = CustomCommandTask.State.Error(
errorReport
), currentLine = errorReport, output = oldValue.output + "\n" + errorReport
)
}

fun updateState(state: State) = mutableDownloaderState.update { state }

fun clearErrorState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bobbyesp.spowlo.ui.components.about

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -21,14 +20,12 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bobbyesp.spowlo.R
import com.bobbyesp.spowlo.ui.common.AsyncImageImpl
import com.bobbyesp.spowlo.ui.theme.Shapes
import com.bobbyesp.spowlo.utils.ChromeCustomTabsUtil

@Composable
Expand Down Expand Up @@ -65,7 +62,7 @@ fun ContributorComponent(
Text(
text = description,
modifier = Modifier
.padding(bottom = 6.dp)
.padding(bottom = 6.dp, end = 16.dp)
.alpha(0.7f),
fontSize = 12.sp,
maxLines = 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bobbyesp.spowlo.ui.components.songs

import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand All @@ -10,20 +9,23 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -33,6 +35,8 @@ import com.bobbyesp.library.dto.Song
import com.bobbyesp.spowlo.R
import com.bobbyesp.spowlo.ui.common.AsyncImageImpl
import com.bobbyesp.spowlo.ui.components.MarqueeText
import com.bobbyesp.spowlo.ui.theme.harmonizeWithPrimary
import com.bobbyesp.spowlo.utils.ChromeCustomTabsUtil
import com.bobbyesp.spowlo.utils.GeneralTextUtils

@Composable
Expand Down Expand Up @@ -116,6 +120,21 @@ fun SongMetadataCard(
)

}
FilledTonalButton(
modifier = Modifier,
onClick = { ChromeCustomTabsUtil.openUrl(song.url) },
colors = ButtonDefaults.filledTonalButtonColors(
containerColor = Color(red = 30, green = 215, blue = 96).harmonizeWithPrimary(),
),
)
{
Icon(
modifier = Modifier.size(24.dp),
imageVector = ImageVector.vectorResource(id = R.drawable.spotify_logo),
contentDescription = null
)
}

}
Row(Modifier.fillMaxWidth()) {
Column(Modifier.fillMaxWidth(0.5f)) {
Expand Down Expand Up @@ -209,7 +228,9 @@ fun MetadataTag(
textAlign = TextAlign.Start
)
Text(
modifier = Modifier.clickable { GeneralTextUtils.copyToClipboardAndNotify(metadata) }.fillMaxWidth(),
modifier = Modifier
.clickable { GeneralTextUtils.copyToClipboardAndNotify(metadata) }
.fillMaxWidth(),
text = metadata,
style = MaterialTheme.typography.titleLarge.copy(fontSize = 16.sp),
textAlign = TextAlign.Start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ import com.google.accompanist.permissions.rememberPermissionState

@Composable
@OptIn(
ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class,
ExperimentalPermissionsApi::class, ExperimentalComposeUiApi::class, ExperimentalMaterialApi::class
)
fun DownloaderPage(
Expand Down Expand Up @@ -252,14 +251,6 @@ fun DownloaderPageImplementation(
)
}
}, actions = {

/*IconButton(onClick = { onNavigateToTaskList() }) {
Icon(
imageVector = Icons.Outlined.Terminal,
contentDescription = stringResource(id = R.string.running_tasks)
)
}*/

IconButton(onClick = { navigateToMods() }) {
Icon(
imageVector = LocalAsset(id = R.drawable.spotify_logo),
Expand Down Expand Up @@ -430,7 +421,7 @@ fun FABs(

}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun InputUrl(
url: String,
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/com/bobbyesp/spowlo/utils/DownloaderUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.bobbyesp.library.dto.Song
import com.bobbyesp.spowlo.App.Companion.audioDownloadDir
import com.bobbyesp.spowlo.App.Companion.context
import com.bobbyesp.spowlo.R
import com.bobbyesp.spowlo.database.CommandTemplate
import com.bobbyesp.spowlo.database.DownloadedSongInfo
import com.bobbyesp.spowlo.ui.pages.settings.cookies.Cookie
import com.bobbyesp.spowlo.utils.FilesUtil.getCookiesFile
Expand Down Expand Up @@ -332,7 +331,6 @@ object DownloaderUtil {
): List<String> = FilesUtil.scanFileToMediaLibraryPostDownload(
title = songInfo.name, downloadDir = downloadPath
).apply {
//TODO: FIX HERE THE DIRECTORY BEING REMOVED
Log.d(TAG, "scanVideoIntoDownloadHistory: $downloadPath")
insertInfoIntoDownloadHistory(songInfo, this)
}
Expand All @@ -356,11 +354,4 @@ object DownloaderUtil {
)
}
}

suspend fun executeCommandInBackground(
url: String,
template: CommandTemplate = PreferencesUtil.getTemplate()
) {
TODO("Not yet implemented")
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ kotlinxSerializationJson = "1.4.1"
okhttp = "5.0.0-alpha.10"
ktor = "1.6.3"

exoplayer = "2.16.1"
exoplayer = "2.18.4"

room = "2.5.0"

mmkv = "1.2.14"
mmkv = "1.2.15"

#chrome custom tabs
customTabs = "1.5.0"
Expand Down

0 comments on commit ea932b8

Please sign in to comment.