Link Args Fixes.

This commit is contained in:
shabinder 2021-01-02 17:58:06 +05:30
parent dd0fd06036
commit d91313370a
6 changed files with 56 additions and 23 deletions

View File

@ -42,6 +42,8 @@ import com.tonyodev.fetch2.Status
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.accompanist.insets.ProvideWindowInsets import dev.chrisbanes.accompanist.insets.ProvideWindowInsets
import dev.chrisbanes.accompanist.insets.statusBarsHeight import dev.chrisbanes.accompanist.insets.statusBarsHeight
import kotlinx.coroutines.*
import okhttp3.Dispatcher
import javax.inject.Inject import javax.inject.Inject
/* /*
@ -196,7 +198,20 @@ class MainActivity : AppCompatActivity() {
if ("text/plain" == intent.type) { if ("text/plain" == intent.type) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let { intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
log("Intent Received", it) log("Intent Received", it)
navController.navigateToTrackList(it) GlobalScope.launch {
while(!this@MainActivity::navController.isInitialized){
//Wait for Navigation Controller to initialize
delay(200)
}
val filterLinkRegex = """http.+\w""".toRegex()
withContext(Dispatchers.Main) {
val string = it.replace("\n".toRegex(), " ")
val link = filterLinkRegex.find(string)?.value.toString()
log("Intent Link",link)
sharedViewModel.updateLink(link)
navController.navigateToTrackList(link)
}
}
} }
} }
} }

View File

@ -47,6 +47,16 @@ class SharedViewModel @ViewModelInject constructor(
isAuthenticated = s isAuthenticated = s
} }
/*
* Nav Gives Error on YT links with ? sign
* */
var link by mutableStateOf("")
private set
fun updateLink(s:String) {
link = s
}
val trackList = mutableStateListOf<TrackDetails>() val trackList = mutableStateListOf<TrackDetails>()

View File

@ -47,6 +47,7 @@ suspend fun queryYoutube(fullLink: String): PlatformQueryResult?{
val link = fullLink.removePrefix("https://").removePrefix("http://") val link = fullLink.removePrefix("https://").removePrefix("http://")
if(link.contains("playlist",true) || link.contains("list",true)){ if(link.contains("playlist",true) || link.contains("list",true)){
// Given Link is of a Playlist // Given Link is of a Playlist
log("YT Play",link)
val playlistId = link.substringAfter("?list=").substringAfter("&list=").substringBefore("&") val playlistId = link.substringAfter("?list=").substringAfter("&list=").substringBefore("&")
return getYTPlaylist( return getYTPlaylist(
playlistId, playlistId,
@ -78,7 +79,7 @@ suspend fun getYTPlaylist(
searchId: String, searchId: String,
ytDownloader: YoutubeDownloader, ytDownloader: YoutubeDownloader,
databaseDAO: DatabaseDAO, databaseDAO: DatabaseDAO,
):PlatformQueryResult{ ):PlatformQueryResult?{
val result = PlatformQueryResult( val result = PlatformQueryResult(
folderType = "", folderType = "",
subFolder = "", subFolder = "",
@ -147,10 +148,12 @@ suspend fun getYTPlaylist(
} }
queryActiveTracks() queryActiveTracks()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace()
showDialog("An Error Occurred While Processing!") showDialog("An Error Occurred While Processing!")
} }
} }
return result return if(result.title.isNotBlank()) result
else null
} }
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
@ -158,7 +161,7 @@ suspend fun getYTTrack(
searchId:String, searchId:String,
ytDownloader: YoutubeDownloader, ytDownloader: YoutubeDownloader,
databaseDAO: DatabaseDAO databaseDAO: DatabaseDAO
):PlatformQueryResult { ):PlatformQueryResult? {
val result = PlatformQueryResult( val result = PlatformQueryResult(
folderType = "", folderType = "",
subFolder = "", subFolder = "",
@ -219,8 +222,10 @@ suspend fun getYTTrack(
} }
queryActiveTracks() queryActiveTracks()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace()
showDialog("An Error Occurred While Processing!") showDialog("An Error Occurred While Processing!")
} }
} }
return result return if(result.title.isNotBlank()) result
else null
} }

View File

@ -49,8 +49,8 @@ fun Home(navController: NavController, modifier: Modifier = Modifier) {
AuthenticationBanner(sharedViewModel.isAuthenticated,modifier) AuthenticationBanner(sharedViewModel.isAuthenticated,modifier)
SearchPanel( SearchPanel(
viewModel.link, sharedViewModel.link,
viewModel::updateLink, sharedViewModel::updateLink,
navController, navController,
modifier modifier
) )

View File

@ -13,13 +13,6 @@ import androidx.compose.runtime.setValue
class HomeViewModel : ViewModel() { class HomeViewModel : ViewModel() {
var link by mutableStateOf("")
private set
fun updateLink(s:String) {
link = s
}
var selectedCategory by mutableStateOf(HomeCategory.About) var selectedCategory by mutableStateOf(HomeCategory.About)
private set private set

View File

@ -15,6 +15,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.vectorResource import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
@ -31,11 +32,11 @@ import com.shabinder.spotiflyer.providers.querySpotify
import com.shabinder.spotiflyer.providers.queryYoutube import com.shabinder.spotiflyer.providers.queryYoutube
import com.shabinder.spotiflyer.ui.utils.calculateDominantColor import com.shabinder.spotiflyer.ui.utils.calculateDominantColor
import com.shabinder.spotiflyer.utils.downloadTracks import com.shabinder.spotiflyer.utils.downloadTracks
import com.shabinder.spotiflyer.utils.log
import com.shabinder.spotiflyer.utils.sharedViewModel import com.shabinder.spotiflyer.utils.sharedViewModel
import com.shabinder.spotiflyer.utils.showDialog import com.shabinder.spotiflyer.utils.showDialog
import dev.chrisbanes.accompanist.coil.CoilImage import dev.chrisbanes.accompanist.coil.CoilImage
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.launch
/* /*
* UI for List of Tracks to be universally used. * UI for List of Tracks to be universally used.
@ -50,17 +51,22 @@ fun TrackList(
var result by remember(fullLink) { mutableStateOf<PlatformQueryResult?>(null) } var result by remember(fullLink) { mutableStateOf<PlatformQueryResult?>(null) }
coroutineScope.launch { coroutineScope.launch(Dispatchers.Default) {
@Suppress("UnusedEquals")//Add Delay if result is not Initialized yet.
try{result == null}catch(e:java.lang.IllegalStateException){delay(100)}
if(result == null){ if(result == null){
result = when{ result = when{
/*
* Using SharedViewModel's Link as NAVIGATION's Arg is buggy for links.
* */
//SPOTIFY //SPOTIFY
fullLink.contains("spotify",true) -> querySpotify(fullLink) sharedViewModel.link.contains("spotify",true) -> querySpotify(sharedViewModel.link)
//YOUTUBE //YOUTUBE
fullLink.contains("youtube.com",true) || fullLink.contains("youtu.be",true) -> queryYoutube(fullLink) sharedViewModel.link.contains("youtube.com",true) || sharedViewModel.link.contains("youtu.be",true) -> queryYoutube(sharedViewModel.link)
//GAANA //GAANA
fullLink.contains("gaana",true) -> queryGaana(fullLink) sharedViewModel.link.contains("gaana",true) -> queryGaana(sharedViewModel.link)
else -> { else -> {
showDialog("Link is Not Valid") showDialog("Link is Not Valid")
@ -68,9 +74,11 @@ fun TrackList(
} }
} }
} }
withContext(Dispatchers.Main){
//Error Occurred And Has Been Shown to User //Error Occurred And Has Been Shown to User
if(result == null) navController.popBackStack() if(result == null) navController.popBackStack()
} }
}
sharedViewModel.updateTrackList(result?.trackList ?: listOf()) sharedViewModel.updateTrackList(result?.trackList ?: listOf())
@ -104,7 +112,6 @@ fun TrackList(
track.downloaded = DownloadStatus.Queued track.downloaded = DownloadStatus.Queued
} }
} }
showDialog("Downloading All Tracks")
}, },
modifier = Modifier.padding(bottom = 24.dp).align(Alignment.BottomCenter) modifier = Modifier.padding(bottom = 24.dp).align(Alignment.BottomCenter)
) )
@ -135,6 +142,9 @@ fun CoverImage(
Text( Text(
text = title, text = title,
style = SpotiFlyerTypography.h5, style = SpotiFlyerTypography.h5,
maxLines = 2,
textAlign = TextAlign.Center,
overflow = TextOverflow.Ellipsis,
//color = colorAccent, //color = colorAccent,
) )
} }