From d91313370ad8b45cf709095d3a10ecbd28487cd9 Mon Sep 17 00:00:00 2001 From: shabinder Date: Sat, 2 Jan 2021 17:58:06 +0530 Subject: [PATCH] Link Args Fixes. --- .../com/shabinder/spotiflyer/MainActivity.kt | 17 ++++++++++- .../shabinder/spotiflyer/SharedViewModel.kt | 10 +++++++ .../spotiflyer/providers/YoutubeProvider.kt | 13 ++++++--- .../com/shabinder/spotiflyer/ui/home/Home.kt | 4 +-- .../spotiflyer/ui/home/HomeViewModel.kt | 7 ----- .../spotiflyer/ui/tracklist/TrackList.kt | 28 +++++++++++++------ 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt b/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt index f4c782ee..2c999064 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/MainActivity.kt @@ -42,6 +42,8 @@ import com.tonyodev.fetch2.Status import dagger.hilt.android.AndroidEntryPoint import dev.chrisbanes.accompanist.insets.ProvideWindowInsets import dev.chrisbanes.accompanist.insets.statusBarsHeight +import kotlinx.coroutines.* +import okhttp3.Dispatcher import javax.inject.Inject /* @@ -196,7 +198,20 @@ class MainActivity : AppCompatActivity() { if ("text/plain" == intent.type) { intent.getStringExtra(Intent.EXTRA_TEXT)?.let { 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) + } + } } } } diff --git a/app/src/main/java/com/shabinder/spotiflyer/SharedViewModel.kt b/app/src/main/java/com/shabinder/spotiflyer/SharedViewModel.kt index be5428a9..5df02bb3 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/SharedViewModel.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/SharedViewModel.kt @@ -47,6 +47,16 @@ class SharedViewModel @ViewModelInject constructor( 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() diff --git a/app/src/main/java/com/shabinder/spotiflyer/providers/YoutubeProvider.kt b/app/src/main/java/com/shabinder/spotiflyer/providers/YoutubeProvider.kt index a95383fe..09045ffe 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/providers/YoutubeProvider.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/providers/YoutubeProvider.kt @@ -47,6 +47,7 @@ suspend fun queryYoutube(fullLink: String): PlatformQueryResult?{ val link = fullLink.removePrefix("https://").removePrefix("http://") if(link.contains("playlist",true) || link.contains("list",true)){ // Given Link is of a Playlist + log("YT Play",link) val playlistId = link.substringAfter("?list=").substringAfter("&list=").substringBefore("&") return getYTPlaylist( playlistId, @@ -78,7 +79,7 @@ suspend fun getYTPlaylist( searchId: String, ytDownloader: YoutubeDownloader, databaseDAO: DatabaseDAO, -):PlatformQueryResult{ +):PlatformQueryResult?{ val result = PlatformQueryResult( folderType = "", subFolder = "", @@ -147,10 +148,12 @@ suspend fun getYTPlaylist( } queryActiveTracks() } catch (e: Exception) { + e.printStackTrace() showDialog("An Error Occurred While Processing!") } } - return result + return if(result.title.isNotBlank()) result + else null } @SuppressLint("DefaultLocale") @@ -158,7 +161,7 @@ suspend fun getYTTrack( searchId:String, ytDownloader: YoutubeDownloader, databaseDAO: DatabaseDAO -):PlatformQueryResult { +):PlatformQueryResult? { val result = PlatformQueryResult( folderType = "", subFolder = "", @@ -219,8 +222,10 @@ suspend fun getYTTrack( } queryActiveTracks() } catch (e: Exception) { + e.printStackTrace() showDialog("An Error Occurred While Processing!") } } - return result + return if(result.title.isNotBlank()) result + else null } diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/home/Home.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/home/Home.kt index eb72b9e0..424e8e1d 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/home/Home.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/home/Home.kt @@ -49,8 +49,8 @@ fun Home(navController: NavController, modifier: Modifier = Modifier) { AuthenticationBanner(sharedViewModel.isAuthenticated,modifier) SearchPanel( - viewModel.link, - viewModel::updateLink, + sharedViewModel.link, + sharedViewModel::updateLink, navController, modifier ) diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/home/HomeViewModel.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/home/HomeViewModel.kt index 9d59d1c9..0864f716 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/home/HomeViewModel.kt @@ -13,13 +13,6 @@ import androidx.compose.runtime.setValue class HomeViewModel : ViewModel() { - var link by mutableStateOf("") - private set - - fun updateLink(s:String) { - link = s - } - var selectedCategory by mutableStateOf(HomeCategory.About) private set diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/tracklist/TrackList.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/tracklist/TrackList.kt index 021a920f..c6cd76ab 100644 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/tracklist/TrackList.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/tracklist/TrackList.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.vectorResource +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp 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.ui.utils.calculateDominantColor import com.shabinder.spotiflyer.utils.downloadTracks +import com.shabinder.spotiflyer.utils.log import com.shabinder.spotiflyer.utils.sharedViewModel import com.shabinder.spotiflyer.utils.showDialog import dev.chrisbanes.accompanist.coil.CoilImage -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.* /* * UI for List of Tracks to be universally used. @@ -50,17 +51,22 @@ fun TrackList( var result by remember(fullLink) { mutableStateOf(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){ result = when{ + /* + * Using SharedViewModel's Link as NAVIGATION's Arg is buggy for links. + * */ //SPOTIFY - fullLink.contains("spotify",true) -> querySpotify(fullLink) + sharedViewModel.link.contains("spotify",true) -> querySpotify(sharedViewModel.link) //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 - fullLink.contains("gaana",true) -> queryGaana(fullLink) + sharedViewModel.link.contains("gaana",true) -> queryGaana(sharedViewModel.link) else -> { showDialog("Link is Not Valid") @@ -68,8 +74,10 @@ fun TrackList( } } } - //Error Occurred And Has Been Shown to User - if(result == null) navController.popBackStack() + withContext(Dispatchers.Main){ + //Error Occurred And Has Been Shown to User + if(result == null) navController.popBackStack() + } } sharedViewModel.updateTrackList(result?.trackList ?: listOf()) @@ -104,7 +112,6 @@ fun TrackList( track.downloaded = DownloadStatus.Queued } } - showDialog("Downloading All Tracks") }, modifier = Modifier.padding(bottom = 24.dp).align(Alignment.BottomCenter) ) @@ -135,6 +142,9 @@ fun CoverImage( Text( text = title, style = SpotiFlyerTypography.h5, + maxLines = 2, + textAlign = TextAlign.Center, + overflow = TextOverflow.Ellipsis, //color = colorAccent, ) }