Skip to content

Commit

Permalink
Merge pull request #175 from 07jasjeet/remove-auto-disable-spotify
Browse files Browse the repository at this point in the history
Fix: Remove automatic disabling of Spotify
  • Loading branch information
akshaaatt authored Jun 7, 2023
2 parents 08989e1 + 4503d40 commit f9cae76
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 100 deletions.
27 changes: 3 additions & 24 deletions app/src/main/java/org/listenbrainz/android/application/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ import android.content.Intent
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import org.listenbrainz.android.repository.AppPreferences
import org.listenbrainz.android.service.ListenScrobbleService
import org.listenbrainz.android.service.ListensService
import org.listenbrainz.android.util.Constants
import org.listenbrainz.android.util.Log
import javax.inject.Inject

@HiltAndroidApp
Expand All @@ -25,7 +18,7 @@ class App : Application() {
@Inject
lateinit var listensService: ListensService

private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
//private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

override fun onCreate() {
super.onCreate()
Expand All @@ -34,26 +27,12 @@ class App : Application() {
if(appPreferences.isNotificationServiceAllowed && !appPreferences.lbAccessToken.isNullOrEmpty()) {
startListenService()
}

if (!appPreferences.username.isNullOrEmpty() && !appPreferences.lbAccessToken.isNullOrEmpty()) {
applicationScope.launch {
if(listensService.getServicesLinkedToAccount("Bearer: ${appPreferences.lbAccessToken}",
appPreferences.username!!
).services.contains("spotify")) {
Log.d("Spotify is already linked with web.")
if(!appPreferences.listeningBlacklist.contains(Constants.SPOTIFY_PACKAGE_NAME)) {
Log.d("Adding Spotify to blacklist.")
appPreferences.listeningBlacklist = appPreferences.listeningBlacklist.plus(Constants.SPOTIFY_PACKAGE_NAME)
}
}
}
}
}

override fun onTerminate() {
/*override fun onTerminate() {
super.onTerminate()
applicationScope.cancel()
}
}*/

companion object {
lateinit var context: App
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.listenbrainz.android.repository
import org.listenbrainz.android.model.AccessToken
import org.listenbrainz.android.model.Playable
import org.listenbrainz.android.model.UserInfo
import org.listenbrainz.android.util.LinkedService

interface AppPreferences {
val themePreference : String?
Expand Down Expand Up @@ -43,6 +44,7 @@ interface AppPreferences {
var lbAccessToken: String?
var username: String?
val refreshToken: String?
var linkedServices: List<LinkedService>

val isNotificationServiceAllowed: Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.listenbrainz.android.model.UserInfo
import org.listenbrainz.android.util.Constants.ONBOARDING
import org.listenbrainz.android.util.Constants.Strings.CURRENT_PLAYABLE
import org.listenbrainz.android.util.Constants.Strings.LB_ACCESS_TOKEN
import org.listenbrainz.android.util.Constants.Strings.LINKED_SERVICES
import org.listenbrainz.android.util.Constants.Strings.MB_ACCESS_TOKEN
import org.listenbrainz.android.util.Constants.Strings.PREFERENCE_ALBUMS_ON_DEVICE
import org.listenbrainz.android.util.Constants.Strings.PREFERENCE_LISTENING_APPS
Expand All @@ -24,6 +25,7 @@ import org.listenbrainz.android.util.Constants.Strings.REFRESH_TOKEN
import org.listenbrainz.android.util.Constants.Strings.STATUS_LOGGED_IN
import org.listenbrainz.android.util.Constants.Strings.STATUS_LOGGED_OUT
import org.listenbrainz.android.util.Constants.Strings.USERNAME
import org.listenbrainz.android.util.LinkedService
import org.listenbrainz.android.util.TypeConverter

class AppPreferencesImpl(private val context : Context): AppPreferences {
Expand Down Expand Up @@ -154,6 +156,17 @@ class AppPreferencesImpl(private val context : Context): AppPreferences {
override var username: String?
get() = preferences.getString(USERNAME, "")
set(value) = setString(USERNAME, value)

override var linkedServices: List<LinkedService>
get() {
val jsonString = preferences.getString(LINKED_SERVICES, "")
val type = object : TypeToken<List<LinkedService>>() {}.type
return gson.fromJson(jsonString, type) ?: emptyList()
}
set(value) {
val jsonString = gson.toJson(value)
setString(LINKED_SERVICES, jsonString)
}

override val refreshToken: String?
get() = preferences.getString(REFRESH_TOKEN, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.listenbrainz.android.model.CoverArt
import org.listenbrainz.android.model.Listen
import org.listenbrainz.android.model.ListenSubmitBody
import org.listenbrainz.android.model.TokenValidation
import org.listenbrainz.android.util.LinkedService
import org.listenbrainz.android.util.Resource

interface ListensRepository {
Expand All @@ -20,4 +21,6 @@ interface ListensRepository {
fun getPackageLabel(packageName: String): String

fun submitListen(token: String, body: ListenSubmitBody)

suspend fun getLinkedServices(token: String, username: String) : List<LinkedService>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.listenbrainz.android.model.Listen
import org.listenbrainz.android.model.ListenSubmitBody
import org.listenbrainz.android.model.TokenValidation
import org.listenbrainz.android.service.ListensService
import org.listenbrainz.android.util.LinkedService
import org.listenbrainz.android.util.Log.d
import org.listenbrainz.android.util.Resource
import org.listenbrainz.android.util.Resource.Status.FAILED
Expand Down Expand Up @@ -98,4 +99,17 @@ class ListensRepositoryImpl @Inject constructor(val service: ListensService) : L
}
})
}

override suspend fun getLinkedServices(token: String, username: String): List<LinkedService> {
val services = service.getServicesLinkedToAccount(
authHeader = "Bearer $token", // TODO: Refactor this after feed section phase 1 is completed.
user_name = username
)
val result = mutableListOf<LinkedService>()
services.services.forEach {
result.add(LinkedService.parseService(it))
}
return result
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ListensService {

@GET("1/user/{user_name}/services")
suspend fun getServicesLinkedToAccount(
@Header("Authorization") token: String?,
@Header("Authorization") authHeader: String?,
@Path("user_name") user_name: String,
): ListenBrainzExternalServices
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package org.listenbrainz.android.ui.screens.listens

import androidx.appcompat.content.res.AppCompatResources
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.AlertDialog
import androidx.compose.material.Icon
import androidx.compose.material.Switch
import androidx.compose.material.SwitchDefaults
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -29,9 +40,11 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import org.listenbrainz.android.R
import org.listenbrainz.android.util.Constants.SPOTIFY_PACKAGE_NAME
import org.listenbrainz.android.viewmodel.ListensViewModel

@Composable
Expand All @@ -40,8 +53,13 @@ fun ListeningAppsList(
onDismiss: () -> Unit
){
var blacklist by remember { mutableStateOf(viewModel.appPreferences.listeningBlacklist) }
val isSpotifyLinked by viewModel.isSpotifyLinked.collectAsState()
val context = LocalContext.current

LaunchedEffect(Unit){
viewModel.fetchLinkedServices()
}

AlertDialog(
onDismissRequest = onDismiss,
backgroundColor = MaterialTheme.colorScheme.background,
Expand Down Expand Up @@ -75,53 +93,86 @@ fun ListeningAppsList(
LazyColumn {
items(items = viewModel.appPreferences.listeningApps){packageName ->

Box(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.Start) {

Row(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxWidth(0.85f),
verticalAlignment = Alignment.CenterVertically
) {

Image(
// Content
Box(modifier = Modifier.fillMaxWidth()) {
Row(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxWidth(0.85f),
verticalAlignment = Alignment.CenterVertically
) {

Image(
modifier = Modifier
.fillMaxWidth(0.15f)
.padding(end = 5.dp),
painter = rememberDrawablePainter(
drawable = viewModel.repository.getPackageIcon(packageName)
?: AppCompatResources.getDrawable(
context,
R.drawable.music_solid
)
),
contentDescription = null
)

Text(
modifier = Modifier.fillMaxWidth(0.85f),
text = viewModel.repository.getPackageLabel(packageName),
color = MaterialTheme.colorScheme.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
Switch(
modifier = Modifier
.fillMaxWidth(0.15f)
.padding(end = 5.dp),
painter = rememberDrawablePainter(
drawable = viewModel.repository.getPackageIcon(packageName) ?: AppCompatResources.getDrawable(context, R.drawable.music_solid)
),
contentDescription = null
.align(Alignment.CenterEnd),
checked = packageName !in blacklist,
onCheckedChange = { isChecked ->
if (!isChecked) {
viewModel.appPreferences.listeningBlacklist =
blacklist + packageName
blacklist = blacklist + packageName
} else {
viewModel.appPreferences.listeningBlacklist =
blacklist - packageName
blacklist = blacklist - packageName
}

},
colors = SwitchDefaults.colors(
checkedTrackColor = MaterialTheme.colorScheme.inverseOnSurface,
checkedThumbColor = MaterialTheme.colorScheme.inverseOnSurface
)
)
}

// Warning for spotify.
AnimatedVisibility (visible = packageName == SPOTIFY_PACKAGE_NAME && isSpotifyLinked){

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
Icon(
modifier = Modifier.size(16.dp),
imageVector = Icons.Rounded.Info,
contentDescription = "Attention",
tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f)
)
Spacer(modifier = Modifier.width(4.dp))
Text(
text = "Spotify is already linked to ListenBrainz.",
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f),
fontSize = 10.sp
)
}

Text(
modifier = Modifier.fillMaxWidth(0.85f),
text = viewModel.repository.getPackageLabel(packageName),
color = MaterialTheme.colorScheme.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
Switch(
modifier = Modifier
.fillMaxWidth(0.15f)
.align(Alignment.CenterEnd),
checked = packageName !in blacklist,
onCheckedChange = { isChecked ->
if (!isChecked){
viewModel.appPreferences.listeningBlacklist = blacklist + packageName
blacklist = blacklist + packageName
} else{
viewModel.appPreferences.listeningBlacklist = blacklist - packageName
blacklist = blacklist - packageName
}

},
colors = SwitchDefaults.colors(
checkedTrackColor = MaterialTheme.colorScheme.inverseOnSurface,
checkedThumbColor = MaterialTheme.colorScheme.inverseOnSurface
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.FloatingActionButton
Expand Down Expand Up @@ -47,6 +47,7 @@ import org.listenbrainz.android.ui.components.ListenCard
import org.listenbrainz.android.ui.components.LoadingAnimation
import org.listenbrainz.android.ui.screens.profile.UserData
import org.listenbrainz.android.util.Constants
import org.listenbrainz.android.util.Utils.getCoverArtUrl
import org.listenbrainz.android.viewmodel.ListensViewModel

@Composable
Expand Down Expand Up @@ -86,9 +87,9 @@ fun ListensScreen(
}

val youtubeApiKey = stringResource(id = R.string.youtubeApiKey)

// Listens list
val listens = viewModel.listensFlow.collectAsState().value
val coverArtUrls = viewModel.coverArtFlow.collectAsState().value

Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {

Expand All @@ -110,10 +111,13 @@ fun ListensScreen(
}
}

itemsIndexed(listens) { index, listen ->
items(listens) { listen ->
ListenCard(
listen,
coverArtUrls[index]
getCoverArtUrl(
caaReleaseMbid = listen.track_metadata.mbid_mapping?.caa_release_mbid,
caaId = listen.track_metadata.mbid_mapping?.caa_id
)
)
{
if (it.track_metadata.additional_info?.spotify_id != null) {
Expand Down
Loading

0 comments on commit f9cae76

Please sign in to comment.