Skip to content

Commit

Permalink
added variables to config, fixed infinite login screen on error, rem…
Browse files Browse the repository at this point in the history
…oved empty parameters from get requests
  • Loading branch information
icefields committed Jun 14, 2024
1 parent d3901ed commit 35c810c
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 93 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ android {
buildConfigField("String", "DEFAULT_SERVER_URL", "\"\"")
buildConfigField("boolean", "FORCE_LOGIN_DIALOG_ON_ALL_VERSIONS", "true")
buildConfigField("boolean", "DEMO_VERSION", "false")
buildConfigField("String", "REMOTE_CONFIG_FILE", "\"config.json\"")
}

buildTypes {
Expand Down Expand Up @@ -199,6 +200,7 @@ android {
buildConfigField("String", "DEFAULT_SERVER_URL", dogmazicUrl)
buildConfigField("String", "URL_ERROR_LOG", "\"https://pastebin.com/api/\"")
buildConfigField("String", "PASTEBIN_API_KEY", pastebinApiKey)
buildConfigField("String", "REMOTE_CONFIG_FILE", "\"config-dogmazic.json\"")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package luci.sixsixsix.powerampache2.common

import luci.sixsixsix.powerampache2.BuildConfig


object Constants {
// LOCAL DB
Expand Down Expand Up @@ -91,6 +93,6 @@ object Constants {
const val DOGMAZIC_FAKE_CITY = "Aoshima"

// fetch this from remote config or initialize locally
const val CONFIG_URL = "https://icefields.github.io/powerampache/config.json"
const val CONFIG_URL = "https://icefields.github.io/powerampache/${BuildConfig.REMOTE_CONFIG_FILE}"
var config = Pa2Config()
}
17 changes: 17 additions & 0 deletions app/src/main/java/luci/sixsixsix/powerampache2/common/Pa2Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const val PLAYLIST_FETCH_LIMIT = 0
const val PLAYLIST_ADD_NEW_ENABLE = true
const val RESET_QUEUE_ON_NEW_SESSION = BuildConfig.RESET_QUEUE_ON_NEW_SESSION

const val PLAYLISTS_USER_FETCH = true
const val SMARTLISTS_USER_FETCH = true
const val PLAYLISTS_ADMIN_FETCH = true
const val SMARTLISTS_ADMIN_FETCH = true
const val PLAYLISTS_ALL_SERVER_FETCH = true

data class Pa2Config(
// use new fast method for adding albums and playlists to playlist
val playlistAddNewEnable: Boolean = PLAYLIST_ADD_NEW_ENABLE,
Expand Down Expand Up @@ -61,4 +67,15 @@ data class Pa2Config(

val dogmazicDemoToken: String = BuildConfig.DOGMAZIC_TOKEN,
val dogmazicDemoUrl: String = BuildConfig.DOGMAZIC_URL,

// fetch user playlists before fetching the bulk of all server playlists
val playlistsUserFetch: Boolean = PLAYLISTS_USER_FETCH,
// fetch user smartlists before fetching the bulk of all server playlists
val smartlistsUserFetch: Boolean = SMARTLISTS_USER_FETCH,
// fetch admin playlists only before fetching the bulk of all server playlists
val playlistsAdminFetch: Boolean = PLAYLISTS_ADMIN_FETCH,
// fetch admin smartlists only before fetching the bulk of all server playlists
val smartlistsAdminFetch: Boolean = SMARTLISTS_ADMIN_FETCH,
// fetch all playlists from server
val playlistsServerAllFetch: Boolean = PLAYLISTS_ALL_SERVER_FETCH,
)
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import luci.sixsixsix.powerampache2.data.local.entities.toSessionEntity
import luci.sixsixsix.powerampache2.data.local.entities.toUser
import luci.sixsixsix.powerampache2.data.local.models.UserWithCredentials
import luci.sixsixsix.powerampache2.data.local.models.toUser
import luci.sixsixsix.powerampache2.data.local.multiuserDbKey
import luci.sixsixsix.powerampache2.data.remote.MainNetwork
import luci.sixsixsix.powerampache2.data.remote.dto.toError
import luci.sixsixsix.powerampache2.data.remote.dto.toGenre
Expand Down Expand Up @@ -129,6 +128,7 @@ class MusicRepositoryImpl @Inject constructor(
Constants.config = try {
api.getConfig().toPa2Config()
} catch (e: Exception) {
L.e(e)
Pa2Config()
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ class MusicRepositoryImpl @Inject constructor(
override suspend fun ping(): Resource<Pair<ServerInfo, Session?>> =
try {
val dbSession = getSession()
val pingResponse = api.ping(dbSession?.auth ?: "")
val pingResponse = api.ping(authKey = dbSession?.auth)

// Updated session only valid of previous session exists, authorize otherwise
dbSession?.let { cachedSession ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,61 +109,96 @@ class PlaylistsRepositoryImpl @Inject constructor(
}
}

val cred = getCurrentCredentials()
val user = cred.username

if (query.isNullOrBlank()) {
// fetch user and admin playlists to quick load user's playlists before everyone else's
getUserPlaylists()
getUserPlaylists(username = user, serverUrl = cred.serverUrl)
}

val cred = getCurrentCredentials()
val user = cred.username

val playlistNetwork = getPlaylistsNetwork(authToken(), user, offset, query, ALWAYS_FETCH_ALL_PLAYLISTS)
val playlistNetworkEntities = playlistNetwork.map { it.toPlaylistEntity(username = user, serverUrl = cred.serverUrl) }
if (Constants.config.playlistsServerAllFetch) {
val playlistNetwork =
getPlaylistsNetwork(authToken(), user, offset, query, ALWAYS_FETCH_ALL_PLAYLISTS)
val playlistNetworkEntities = playlistNetwork.map {
it.toPlaylistEntity(
username = user,
serverUrl = cred.serverUrl
)
}

if (query.isNullOrBlank() && offset == 0 &&
(!AmpacheModel.listsHaveSameElements(playlistNetwork, dbList.map { it.toPlaylist() } ))) {
// avoid clearing if lists are equal, insert will already replace the old versions
dao.clearPlaylists()
if (query.isNullOrBlank() &&
offset == 0 &&
(!AmpacheModel.listsHaveSameElements(playlistNetwork, dbList.map { it.toPlaylist() }))
) {
L("aaaa", "lists don't have same elements")
// avoid clearing if lists are equal, insert will already replace the old versions
dao.clearPlaylists()
}
dao.insertPlaylists(playlistNetworkEntities)
// stick to the single source of truth pattern despite performance deterioration
emit(Resource.Success(
data = dao.searchPlaylists(query).map { it.toPlaylist() },
networkData = playlistNetwork)
)
}
dao.insertPlaylists(playlistNetworkEntities)
// stick to the single source of truth pattern despite performance deterioration
emit(Resource.Success(data = dao.searchPlaylists(query).map { it.toPlaylist() }, networkData = playlistNetwork))
emit(Resource.Loading(false))
}.catch { e -> errorHandler("getPlaylists()", e, this) }

private suspend fun getUserPlaylists() {
val cred = getCurrentCredentials()

private suspend fun getUserPlaylists(username: String, serverUrl: String) {
// first get user playlists and save to database, saving to db will trigger the flow
try {
api.getUserPlaylists(authToken()).playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(cred.username, cred.serverUrl) }?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) { L.e(e) }
try {
api.getUserSmartlists(authToken()).playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(cred.username, cred.serverUrl) }?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) { L.e(e) }
// fetch admin playlists
try {
api.getUserPlaylists(authToken(), user = "admin").playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(cred.username, cred.serverUrl) }?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) { L.e(e) }
try {
api.getUserSmartlists(authToken(), user = "admin").playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(cred.username, cred.serverUrl) }?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) { L.e(e) }
if (Constants.config.playlistsUserFetch) {
try {
api.getUserPlaylists(authToken()).playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(username, serverUrl) }
?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) {
L.e(e)
}
}

if (Constants.config.smartlistsUserFetch) {
try {
api.getUserSmartlists(authToken()).playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(username, serverUrl) }
?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) {
L.e(e)
}
}

if (Constants.config.playlistsAdminFetch) {
// fetch admin playlists
try {
api.getUserPlaylists(authToken(), user = "admin").playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(username, serverUrl) }
?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) {
L.e(e)
}
}

if (Constants.config.smartlistsAdminFetch) {
try {
api.getUserSmartlists(authToken(), user = "admin").playlist
?.map { it.toPlaylist() }
?.map { it.toPlaylistEntity(username, serverUrl) }
?.let { userPlaylists ->
dao.insertPlaylists(userPlaylists)
}
} catch (e: Exception) {
L.e(e)
}
}
}

private suspend fun getPlaylistsNetwork(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ interface MusicDao {

@Query("""SELECT * FROM playlistentity WHERE (LOWER(name) LIKE '%' || LOWER(:query) || '%' OR LOWER(:query) == name)
AND $multiUserCondition
order by flag DESC, rating DESC, (LOWER(owner) == LOWER( (SELECT username FROM credentialsentity WHERE primaryKey == '$CREDENTIALS_PRIMARY_KEY')) ) DESC, id DESC""")
GROUP BY id
ORDER BY flag DESC, rating DESC, (LOWER(owner) == LOWER( (SELECT username FROM credentialsentity WHERE primaryKey == '$CREDENTIALS_PRIMARY_KEY')) ) DESC, id DESC""")
suspend fun searchPlaylists(query: String): List<PlaylistEntity>

@Query("""SELECT * FROM playlistentity WHERE $multiUserCondition order by flag DESC, rating DESC, (LOWER(owner) == LOWER((SELECT username FROM credentialsentity WHERE primaryKey == '$CREDENTIALS_PRIMARY_KEY')) ) DESC, id DESC""")
Expand Down
Loading

0 comments on commit 35c810c

Please sign in to comment.