-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from BobbyESP/downloader_impl
Implementation of song searching.
- Loading branch information
Showing
30 changed files
with
1,147 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/bobbyesp/spowlo/data/auth/AuthModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.bobbyesp.spowlo.data.auth | ||
|
||
import com.adamratzman.spotify.auth.SpotifyDefaultCredentialStore | ||
import com.bobbyesp.spowlo.BuildConfig | ||
import com.bobbyesp.spowlo.Spowlo | ||
|
||
object AuthModel { | ||
val credentialStore by lazy { | ||
SpotifyDefaultCredentialStore( | ||
clientId = BuildConfig.SPOTIFY_CLIENT_ID, | ||
redirectUri = BuildConfig.SPOTIFY_REDIRECT_URI_PKCE, | ||
applicationContext = Spowlo.context | ||
) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/bobbyesp/spowlo/database/AppDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.bobbyesp.spowlo.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
|
||
@Database( | ||
entities = [DownloadedSongInfo::class, CommandTemplate::class], version = 3, | ||
exportSchema = false | ||
) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun songInfoDao(): SongInfoDao | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/bobbyesp/spowlo/database/CommandTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.bobbyesp.spowlo.database | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity | ||
@kotlinx.serialization.Serializable | ||
data class CommandTemplate( | ||
@PrimaryKey(autoGenerate = true) val id: Int, | ||
val name: String, | ||
val template: String | ||
) |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/bobbyesp/spowlo/database/DownloadedSongInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.bobbyesp.spowlo.database | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "DownloadedSongInfo") | ||
data class DownloadedSongInfo( | ||
@PrimaryKey(autoGenerate = true) val id: Int, | ||
val songTitle: String, | ||
val songArtist: String, | ||
val songUrl: String, | ||
val thumbnailUrl: String, | ||
val songPath: String, | ||
@ColumnInfo(defaultValue = "Unknown") | ||
val extractor: String = "Unknown" | ||
) |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/bobbyesp/spowlo/database/SongInfoDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.bobbyesp.spowlo.database | ||
|
||
import androidx.room.* | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface SongInfoDao { | ||
@Insert | ||
suspend fun insertAll(vararg info: DownloadedSongInfo) | ||
|
||
@Query("select * from DownloadedSongInfo") | ||
fun getAllMedia(): Flow<List<DownloadedSongInfo>> | ||
|
||
@Query("select * from DownloadedSongInfo where id=:id") | ||
suspend fun getInfoById(id: Int): DownloadedSongInfo | ||
|
||
@Delete | ||
suspend fun delete(info: DownloadedSongInfo) | ||
|
||
@Query("DELETE FROM DownloadedSongInfo WHERE id = :id") | ||
suspend fun deleteInfoById(id: Int) | ||
|
||
@Query("DELETE FROM DownloadedSongInfo WHERE songPath = :path") | ||
suspend fun deleteInfoByPath(path: String) | ||
|
||
@Query("SELECT * FROM CommandTemplate") | ||
fun getTemplateFlow(): Flow<List<CommandTemplate>> | ||
|
||
@Query("SELECT * FROM CommandTemplate") | ||
suspend fun getTemplateList(): List<CommandTemplate> | ||
|
||
@Insert | ||
suspend fun insertTemplate(template: CommandTemplate) | ||
|
||
@Update | ||
suspend fun updateTemplate(template: CommandTemplate) | ||
|
||
@Delete | ||
suspend fun deleteTemplate(template: CommandTemplate) | ||
|
||
@Query("SELECT * FROM CommandTemplate where id = :id") | ||
suspend fun getTemplateById(id: Int): CommandTemplate | ||
} |
31 changes: 31 additions & 0 deletions
31
...main/java/com/bobbyesp/spowlo/domain/spotify/web_api/auth/SpotifyPkceLoginActivityImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.bobbyesp.spowlo.domain.spotify.web_api.auth | ||
|
||
import android.app.Activity | ||
import com.adamratzman.spotify.SpotifyClientApi | ||
import com.adamratzman.spotify.SpotifyScope | ||
import com.adamratzman.spotify.auth.pkce.AbstractSpotifyPkceLoginActivity | ||
import com.bobbyesp.spowlo.BuildConfig | ||
import com.bobbyesp.spowlo.Spowlo | ||
import com.bobbyesp.spowlo.presentation.MainActivity | ||
import com.bobbyesp.spowlo.util.Utils.makeToast | ||
|
||
internal var pkceClassBackTo: Class<out Activity>? = null | ||
|
||
class SpotifyPkceLoginActivityImpl: AbstractSpotifyPkceLoginActivity() { | ||
override val clientId = BuildConfig.SPOTIFY_CLIENT_ID | ||
override val redirectUri = BuildConfig.SPOTIFY_REDIRECT_URI_PKCE | ||
override val scopes = SpotifyScope.values().toList() | ||
|
||
override fun onFailure(exception: Exception) { | ||
exception.printStackTrace() | ||
pkceClassBackTo = null | ||
makeToast("Auth failed: ${exception.message}") | ||
} | ||
|
||
override fun onSuccess(api: SpotifyClientApi) { | ||
val model = (application as Spowlo).model | ||
model.credentialStore.setSpotifyApi(api) | ||
val classBackTo = pkceClassBackTo ?: MainActivity::class.java | ||
pkceClassBackTo = null | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...src/main/java/com/bobbyesp/spowlo/domain/spotify/web_api/utilities/VerifyLoggedInUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.bobbyesp.spowlo.domain.spotify.web_api.utilities | ||
|
||
import android.app.Activity | ||
import com.adamratzman.spotify.SpotifyClientApi | ||
import com.adamratzman.spotify.SpotifyException | ||
import com.adamratzman.spotify.auth.pkce.startSpotifyClientPkceLoginActivity | ||
import com.bobbyesp.spowlo.data.auth.AuthModel | ||
import com.bobbyesp.spowlo.domain.spotify.web_api.auth.SpotifyPkceLoginActivityImpl | ||
import com.bobbyesp.spowlo.domain.spotify.web_api.auth.pkceClassBackTo | ||
import kotlinx.coroutines.runBlocking | ||
|
||
fun <T> Activity.guardValidSpotifyApi( | ||
classBackTo: Class<out Activity>? = null, | ||
alreadyTriedToReauthenticate: Boolean = false, | ||
block: suspend (api: SpotifyClientApi) -> T | ||
): T? { | ||
return runBlocking { | ||
try { | ||
val api = AuthModel.credentialStore.getSpotifyClientPkceApi() ?: throw SpotifyException.ReAuthenticationNeededException() | ||
block(api) | ||
} catch (e: SpotifyException) { | ||
e.printStackTrace() | ||
val api = AuthModel.credentialStore.getSpotifyClientPkceApi()!! | ||
if (!alreadyTriedToReauthenticate) { | ||
try { | ||
api.refreshToken() | ||
AuthModel.credentialStore.spotifyToken = api.token | ||
block(api) | ||
} catch (e: SpotifyException.ReAuthenticationNeededException) { | ||
e.printStackTrace() | ||
return@runBlocking guardValidSpotifyApi( | ||
classBackTo = classBackTo, | ||
alreadyTriedToReauthenticate = true, | ||
block = block | ||
) | ||
} catch (e: IllegalArgumentException) { | ||
e.printStackTrace() | ||
return@runBlocking guardValidSpotifyApi( | ||
classBackTo = classBackTo, | ||
alreadyTriedToReauthenticate = true, | ||
block = block | ||
) | ||
} | ||
} else { | ||
pkceClassBackTo = classBackTo | ||
startSpotifyClientPkceLoginActivity(SpotifyPkceLoginActivityImpl::class.java) | ||
null | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.