Database dependency reconfigured & web-app bug troubleshooting

This commit is contained in:
shabinder 2021-03-08 22:11:08 +05:30
parent 149f2dc0b1
commit 0d42ea545c
20 changed files with 46 additions and 183 deletions

View File

@ -55,7 +55,6 @@ const val disableDozeCode = 1223
@ExperimentalAnimationApi
class MainActivity : ComponentActivity(), PaymentResultListener {
private val database: Database by inject()
private val fetcher: FetchPlatformQueryResult by inject()
private val dir: Dir by inject()
private lateinit var root: SpotiFlyerRoot
@ -115,7 +114,7 @@ class MainActivity : ComponentActivity(), PaymentResultListener {
componentContext,
dependencies = object : SpotiFlyerRoot.Dependencies{
override val storeFactory = LoggingStoreFactory(DefaultStoreFactory)
override val database = this@MainActivity.database
override val database = this@MainActivity.dir.db
override val fetchPlatformQueryResult = this@MainActivity.fetcher
override val directories: Dir = this@MainActivity.dir
override val showPopUpMessage: (String) -> Unit = ::uikitShowPopUpMessage

View File

@ -21,7 +21,7 @@ kotlin {
sourceSets {
named("commonMain") {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
}
}

View File

@ -17,7 +17,6 @@ kotlin {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.1.1")
implementation(Ktor.clientCore)
//implementation(Ktor.clientCio)
implementation(Ktor.clientSerialization)
implementation(Ktor.clientLogging)
implementation(Ktor.clientJson)

View File

@ -11,6 +11,7 @@ import co.touchlab.kermit.Kermit
import com.mpatric.mp3agic.Mp3File
import com.shabinder.common.database.appContext
import com.shabinder.common.models.TrackDetails
import com.shabinder.database.Database
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@ -23,7 +24,8 @@ import java.net.HttpURLConnection
import java.net.URL
actual class Dir actual constructor(
private val logger: Kermit
private val logger: Kermit,
private val database: Database?
) {
private val scope = CoroutineScope(Dispatchers.IO)
@ -170,4 +172,6 @@ actual class Dir actual constructor(
null
}
}
actual val db: Database? = database
}

View File

@ -18,19 +18,16 @@ package com.shabinder.common.di
import co.touchlab.kermit.Kermit
import com.github.kiulian.downloader.YoutubeDownloader
import com.shabinder.common.database.DownloadRecordDatabaseQueries
import com.shabinder.common.models.DownloadStatus
import com.shabinder.common.models.PlatformQueryResult
import com.shabinder.common.models.TrackDetails
import com.shabinder.common.models.spotify.Source
import com.shabinder.database.Database
import io.ktor.client.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
actual class YoutubeProvider actual constructor(
private val httpClient: HttpClient,
private val database: Database,
private val logger: Kermit,
private val dir: Dir,
){
@ -44,9 +41,6 @@ actual class YoutubeProvider actual constructor(
private val sampleDomain2 = "youtube.com"
private val sampleDomain3 = "youtu.be"
private val db: DownloadRecordDatabaseQueries
get() = database.downloadRecordDatabaseQueries
actual suspend fun query(fullLink: String): PlatformQueryResult?{
val link = fullLink.removePrefix("https://").removePrefix("http://")
if(link.contains("playlist",true) || link.contains("list",true)){
@ -133,22 +127,6 @@ actual class YoutubeProvider actual constructor(
videoID = it.videoId()
)
}
withContext(Dispatchers.IO) {
db.add(
type = "PlayList",
name = if (name.length > 17) {
"${name.subSequence(0, 16)}..."
} else {
name
},
link = "https://www.youtube.com/playlist?list=$searchId",
coverUrl = "https://i.ytimg.com/vi/${
videos.firstOrNull()?.videoId()
}/hqdefault.jpg",
totalFiles = videos.size.toLong(),
)
}
} catch (e: Exception) {
e.printStackTrace()
logger.d{"An Error Occurred While Processing!"}
@ -204,20 +182,6 @@ actual class YoutubeProvider actual constructor(
)
)
title = name
withContext(Dispatchers.IO) {
db.add(
type = "Track",
name = if (name.length > 17) {
"${name.subSequence(0, 16)}..."
} else {
name
},
link = "https://www.youtube.com/watch?v=$searchId",
coverUrl = "https://i.ytimg.com/vi/$searchId/hqdefault.jpg",
totalFiles = 1,
)
}
} catch (e: Exception) {
e.printStackTrace()
logger.e{"An Error Occurred While Processing!,$searchId"}

View File

@ -23,17 +23,16 @@ fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclarat
}
fun commonModule(enableNetworkLogs: Boolean) = module {
single { Dir(get()) }
single { createDatabase() }
single { createHttpClient(enableNetworkLogs = enableNetworkLogs) }
single { Dir(get(),createDatabase()) }
single { Kermit(getLogger()) }
single { TokenStore(get(),get()) }
single { YoutubeMusic(get(),get()) }
single { SpotifyProvider(get(),get(),get(),get()) }
single { GaanaProvider(get(),get(),get(),get()) }
single { YoutubeProvider(get(),get(),get(),get()) }
single { YoutubeMp3(get(),get(),get(),get()) }
single { SpotifyProvider(get(),get(),get()) }
single { GaanaProvider(get(),get(),get()) }
single { YoutubeProvider(get(),get(),get()) }
single { YoutubeMp3(get(),get(),get()) }
single { FetchPlatformQueryResult(get(),get(),get(),get(),get(),get()) }
single { createHttpClient(enableNetworkLogs = enableNetworkLogs) }
}
val kotlinxSerializer = KotlinxSerializer( Json {

View File

@ -1,8 +1,10 @@
package com.shabinder.common.di
import co.touchlab.kermit.Kermit
import com.shabinder.common.database.createDatabase
import com.shabinder.common.models.DownloadResult
import com.shabinder.common.models.TrackDetails
import com.shabinder.database.Database
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
@ -11,8 +13,10 @@ import kotlinx.coroutines.flow.flow
import kotlin.math.roundToInt
expect class Dir(
logger: Kermit
logger: Kermit,
database: Database? = createDatabase()
) {
val db :Database?
fun isPresent(path:String):Boolean
fun fileSeparator(): String
fun defaultDir(): String

View File

@ -16,10 +16,10 @@ class FetchPlatformQueryResult(
val youtubeProvider: YoutubeProvider,
val youtubeMusic: YoutubeMusic,
val youtubeMp3: YoutubeMp3,
private val database: Database
private val dir: Dir
) {
private val db:DownloadRecordDatabaseQueries
get() = database.downloadRecordDatabaseQueries
private val db:DownloadRecordDatabaseQueries?
get() = dir.db?.downloadRecordDatabaseQueries
suspend fun query(link:String): PlatformQueryResult?{
val result = when{
@ -41,7 +41,7 @@ class FetchPlatformQueryResult(
}
result?.run {
withContext(Dispatchers.Default){
db.add(
db?.add(
folderType, title, link, coverUrl, trackList.size.toLong()
)
}

View File

@ -4,25 +4,24 @@ import co.touchlab.kermit.Kermit
import com.shabinder.common.database.TokenDBQueries
import com.shabinder.common.di.spotify.authenticateSpotify
import com.shabinder.common.models.spotify.TokenData
import com.shabinder.database.Database
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
class TokenStore(
private val tokenDB: Database,
private val dir: Dir,
private val logger: Kermit,
) {
private val db: TokenDBQueries
get() = tokenDB.tokenDBQueries
private val db: TokenDBQueries?
get() = dir.db?.tokenDBQueries
private fun save(token: TokenData){
if(!token.access_token.isNullOrBlank() && token.expiry != null)
db.add(token.access_token!!, token.expiry!! + Clock.System.now().epochSeconds)
db?.add(token.access_token!!, token.expiry!! + Clock.System.now().epochSeconds)
}
suspend fun getToken(): TokenData? {
var token: TokenData? = db.select().executeAsOneOrNull()?.let {
var token: TokenData? = db?.select()?.executeAsOneOrNull()?.let {
TokenData(it.accessToken,null,it.expiry)
}
logger.d{"System Time:${Clock.System.now().epochSeconds} , Token Expiry:${token?.expiry}"}

View File

@ -7,7 +7,6 @@ import io.ktor.client.*
expect class YoutubeProvider(
httpClient: HttpClient,
database: Database,
logger: Kermit,
dir: Dir
) {

View File

@ -17,7 +17,6 @@
package com.shabinder.common.di.providers
import co.touchlab.kermit.Kermit
import com.shabinder.common.database.DownloadRecordDatabaseQueries
import com.shabinder.common.di.Dir
import com.shabinder.common.di.finalOutputDir
import com.shabinder.common.di.gaana.GaanaRequests
@ -26,21 +25,15 @@ import com.shabinder.common.models.PlatformQueryResult
import com.shabinder.common.models.TrackDetails
import com.shabinder.common.models.gaana.GaanaTrack
import com.shabinder.common.models.spotify.Source
import com.shabinder.database.Database
import io.ktor.client.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class GaanaProvider(
override val httpClient: HttpClient,
private val database: Database,
private val logger: Kermit,
private val dir: Dir,
): GaanaRequests {
private val gaanaPlaceholderImageUrl = "https://a10.gaanacdn.com/images/social/gaana_social.jpg"
private val db: DownloadRecordDatabaseQueries
get() = database.downloadRecordDatabaseQueries
suspend fun query(fullLink: String): PlatformQueryResult?{
//Link Schema: https://gaana.com/type/link
@ -90,15 +83,6 @@ class GaanaProvider(
trackList = listOf(it).toTrackDetailsList(folderType, subFolder)
title = it.track_title
coverUrl = it.artworkLink
withContext(Dispatchers.Default) {
db.add(
type = "Track",
name = title,
link = "https://gaana.com/$type/$link",
coverUrl = coverUrl,
totalFiles = 1,
)
}
}
}
"album" -> {
@ -121,15 +105,6 @@ class GaanaProvider(
trackList = it.tracks.toTrackDetailsList(folderType, subFolder)
title = link
coverUrl = it.custom_artworks.size_480p
withContext(Dispatchers.Default) {
db.add(
type = "Album",
name = title,
link = "https://gaana.com/$type/$link",
coverUrl = coverUrl,
totalFiles = trackList.size.toLong(),
)
}
}
}
"playlist" -> {
@ -153,15 +128,6 @@ class GaanaProvider(
title = link
//coverUrl.value = "TODO"
coverUrl = gaanaPlaceholderImageUrl
withContext(Dispatchers.Default) {
db.add(
type = "Playlist",
name = title,
link = "https://gaana.com/$type/$link",
coverUrl = coverUrl,
totalFiles = it.tracks.size.toLong(),
)
}
}
}
"artist" -> {
@ -189,15 +155,6 @@ class GaanaProvider(
}
}
trackList = it.tracks?.toTrackDetailsList(folderType, subFolder) ?: emptyList()
withContext(Dispatchers.Default) {
db.add(
type = "Artist",
name = artistDetails?.name ?: link,
link = "https://gaana.com/$type/$link",
coverUrl = coverUrl,
totalFiles = trackList.size.toLong(),
)
}
}
}
else -> {//TODO Handle Error}

View File

@ -17,7 +17,6 @@
package com.shabinder.common.di.providers
import co.touchlab.kermit.Kermit
import com.shabinder.common.database.DownloadRecordDatabaseQueries
import com.shabinder.common.di.*
import com.shabinder.common.di.spotify.SpotifyRequests
import com.shabinder.common.models.PlatformQueryResult
@ -26,7 +25,6 @@ import com.shabinder.common.models.spotify.Album
import com.shabinder.common.models.spotify.Image
import com.shabinder.common.models.spotify.Source
import com.shabinder.common.models.spotify.Track
import com.shabinder.database.Database
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
@ -34,18 +32,16 @@ import io.ktor.client.request.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class SpotifyProvider(
private val tokenStore: TokenStore,
private val database: Database,
private val logger: Kermit,
private val dir: Dir,
) : SpotifyRequests {
init {
logger.d { "Creating Spotify Provider" }
GlobalScope.launch(Dispatchers.Default) {authenticateSpotify()}
//GlobalScope.launch(Dispatchers.Default) {authenticateSpotify()}
}
override suspend fun authenticateSpotify(): HttpClient?{
@ -70,9 +66,6 @@ class SpotifyProvider(
override lateinit var httpClient: HttpClient
private val db:DownloadRecordDatabaseQueries
get() = database.downloadRecordDatabaseQueries
suspend fun query(fullLink: String): PlatformQueryResult?{
if(!this::httpClient.isInitialized){
@ -139,15 +132,6 @@ class SpotifyProvider(
title = it.name.toString()
coverUrl = (it.album?.images?.elementAtOrNull(1)?.url
?: it.album?.images?.elementAtOrNull(0)?.url).toString()
withContext(Dispatchers.Default) {
db.add(
type = "Track",
name = title,
link = "https://open.spotify.com/$type/$link",
coverUrl = coverUrl,
totalFiles = 1,
)
}
}
}
@ -184,15 +168,6 @@ class SpotifyProvider(
title = albumObject.name.toString()
coverUrl = (albumObject.images?.elementAtOrNull(1)?.url
?: albumObject.images?.elementAtOrNull(0)?.url).toString()
withContext(Dispatchers.Default) {
db.add(
type = "Album",
name = title,
link = "https://open.spotify.com/$type/$link",
coverUrl = coverUrl,
totalFiles = trackList.size.toLong(),
)
}
}
}
}
@ -235,15 +210,6 @@ class SpotifyProvider(
title = playlistObject.name.toString()
coverUrl = playlistObject.images?.elementAtOrNull(1)?.url
?: playlistObject.images?.firstOrNull()?.url.toString()
withContext(Dispatchers.Default) {
db.add(
type = "Playlist",
name = title,
link = "https://open.spotify.com/$type/$link",
coverUrl = coverUrl,
totalFiles = tempTrackList.size.toLong(),
)
}
}
"episode" -> {//TODO
}

View File

@ -8,7 +8,6 @@ import io.ktor.client.*
class YoutubeMp3(
override val httpClient: HttpClient,
private val database: Database,
private val logger: Kermit,
private val dir: Dir,
):Yt1sMp3 {

View File

@ -5,6 +5,7 @@ import androidx.compose.ui.graphics.asImageBitmap
import co.touchlab.kermit.Kermit
import com.mpatric.mp3agic.Mp3File
import com.shabinder.common.models.TrackDetails
import com.shabinder.database.Database
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@ -21,7 +22,10 @@ import javax.imageio.ImageIO
actual class Dir actual constructor(private val logger: Kermit) {
actual class Dir actual constructor(
private val logger: Kermit,
private val database: Database?,
) {
init {
createDirectories()
@ -120,6 +124,9 @@ actual class Dir actual constructor(private val logger: Kermit) {
null
}
}
actual val db: Database?
get() = database
}
fun BufferedImage.toImageBitmap() = Image.makeFromEncoded(
toByteArray(this)

View File

@ -18,19 +18,14 @@ package com.shabinder.common.di
import co.touchlab.kermit.Kermit
import com.github.kiulian.downloader.YoutubeDownloader
import com.shabinder.common.database.DownloadRecordDatabaseQueries
import com.shabinder.common.models.DownloadStatus
import com.shabinder.common.models.PlatformQueryResult
import com.shabinder.common.models.TrackDetails
import com.shabinder.common.models.spotify.Source
import com.shabinder.database.Database
import io.ktor.client.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
actual class YoutubeProvider actual constructor(
private val httpClient: HttpClient,
private val database: Database,
private val logger: Kermit,
private val dir: Dir,
){
@ -45,9 +40,6 @@ actual class YoutubeProvider actual constructor(
private val sampleDomain2 = "youtube.com"
private val sampleDomain3 = "youtu.be"
private val db: DownloadRecordDatabaseQueries
get() = database.downloadRecordDatabaseQueries
actual suspend fun query(fullLink: String): PlatformQueryResult?{
val link = fullLink.removePrefix("https://").removePrefix("http://")
if(link.contains("playlist",true) || link.contains("list",true)){
@ -130,22 +122,6 @@ actual class YoutubeProvider actual constructor(
videoID = it.videoId()
)
}
withContext(Dispatchers.IO) {
db.add(
type = "PlayList",
name = if (name.length > 17) {
"${name.subSequence(0, 16)}..."
} else {
name
},
link = "https://www.youtube.com/playlist?list=$searchId",
coverUrl = "https://i.ytimg.com/vi/${
videos.firstOrNull()?.videoId()
}/hqdefault.jpg",
totalFiles = videos.size.toLong(),
)
}
} catch (e: Exception) {
e.printStackTrace()
logger.d{"An Error Occurred While Processing!"}
@ -201,20 +177,6 @@ actual class YoutubeProvider actual constructor(
)
)
title = name
withContext(Dispatchers.IO) {
db.add(
type = "Track",
name = if (name.length > 17) {
"${name.subSequence(0, 16)}..."
} else {
name
},
link = "https://www.youtube.com/watch?v=$searchId",
coverUrl = "https://i.ytimg.com/vi/$searchId/hqdefault.jpg",
totalFiles = 1,
)
}
} catch (e: Exception) {
e.printStackTrace()
logger.e{"An Error Occurred While Processing!,$searchId"}

View File

@ -2,9 +2,13 @@ package com.shabinder.common.di
import co.touchlab.kermit.Kermit
import com.shabinder.common.models.TrackDetails
import com.shabinder.database.Database
import org.w3c.dom.ImageBitmap
actual class Dir actual constructor(private val logger: Kermit) {
actual class Dir actual constructor(
private val logger: Kermit,
private val database: Database?,
) {
/*init {
createDirectories()
@ -51,4 +55,7 @@ actual class Dir actual constructor(private val logger: Kermit) {
private suspend fun freshImage(url:String): ImageBitmap?{
return null
}
actual val db: Database?
get() = database
}

View File

@ -7,7 +7,6 @@ import io.ktor.client.*
actual class YoutubeProvider actual constructor(
httpClient: HttpClient,
database: Database,
logger: Kermit,
dir: Dir
) {

View File

@ -48,9 +48,9 @@ private fun spotiFlyerRoot(componentContext: ComponentContext): SpotiFlyerRoot =
componentContext = componentContext,
dependencies = object : SpotiFlyerRoot.Dependencies {
override val storeFactory = DefaultStoreFactory
override val database: Database? = koin.get()
override val fetchPlatformQueryResult: FetchPlatformQueryResult = koin.get()
override val directories: Dir = koin.get()
override val database: Database? = directories.db
override val showPopUpMessage: (String) -> Unit = ::uikitShowPopUpMessage
override val downloadProgressReport = DownloadProgressFlow
}

View File

@ -34,9 +34,9 @@ class App(props: AppProps): RComponent<AppProps, RState>(props) {
private val root = SpotiFlyerRoot(ctx,
object : SpotiFlyerRoot.Dependencies{
override val storeFactory: StoreFactory = DefaultStoreFactory
override val database: Database? = null
override val fetchPlatformQueryResult = dependencies.fetchPlatformQueryResult
override val directories = dependencies.directories
override val database: Database? = directories.db
override val showPopUpMessage: (String) -> Unit = {}//TODO
override val downloadProgressReport: MutableSharedFlow<HashMap<String, DownloadStatus>>
= MutableSharedFlow(1)

View File

@ -20,7 +20,6 @@ fun main() {
}
}
object AppDependencies : KoinComponent {
val logger: Kermit
val directories: Dir