Skip to content

Commit

Permalink
Merge pull request #538 from 07jasjeet/improve-login-flow
Browse files Browse the repository at this point in the history
Improve login flow
  • Loading branch information
07jasjeet authored Jan 26, 2025
2 parents 2d22071 + 852dd49 commit 1dbd65c
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.net.Uri
import android.webkit.WebView
import android.webkit.WebViewClient
import com.limurse.logger.Logger
import org.listenbrainz.android.util.Resource

class ListenBrainzWebClient(private val setLBAuthToken: (String) -> Unit) : WebViewClient() {
class ListenBrainzWebClient(
private val onLoad: (Resource<String>) -> Unit,
) : WebViewClient() {

private var attemptedSettingsNavigation = false

Expand All @@ -30,22 +33,28 @@ class ListenBrainzWebClient(private val setLBAuthToken: (String) -> Unit) : WebV
view?.loadUrl("https://listenbrainz.org/settings")
}
uri.path?.contains("/settings") == true -> {
onLoad(Resource.loading())
Logger.d("ListenBrainzWebClient", "On settings page, waiting to extract token...")
view?.postDelayed({
view.evaluateJavascript(
"(function() { return document.getElementById('auth-token') ? document.getElementById('auth-token').value : 'not found'; })();"
) { value ->
val token = value.removePrefix("\"").removeSuffix("\"")
when {
token.isNotEmpty() && token != "not found" -> {
setLBAuthToken(token)
}
else -> {
Logger.d("ListenBrainzWebClient", "Token not found or empty")

view?.postDelayed(
/*action = */{
view.evaluateJavascript(
"(function() { return document.getElementById('auth-token') ? document.getElementById('auth-token').value : 'not found'; })();"
) { value ->
val token = value.removePrefix("\"").removeSuffix("\"")
when {
token.isNotEmpty() && token != "not found" -> {
onLoad(Resource.success(token))
}
else -> {
Logger.d("ListenBrainzWebClient", "Token not found or empty")
onLoad(Resource.failure())
}
}
}
}
}, 2000)
},
/*delayMillis =*/2000
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,217 @@ import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.ViewModelProvider
import androidx.hilt.navigation.compose.hiltViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlinx.coroutines.delay
import org.listenbrainz.android.ui.components.LoadingAnimation
import org.listenbrainz.android.ui.theme.ListenBrainzTheme
import org.listenbrainz.android.util.Utils.getActivity
import org.listenbrainz.android.util.Resource
import org.listenbrainz.android.util.Utils.LaunchedEffectUnit
import org.listenbrainz.android.util.Utils.SetSystemBarsForegroundAppearance
import org.listenbrainz.android.viewmodel.ListensViewModel

import kotlin.time.Duration.Companion.seconds

@AndroidEntryPoint
class LoginActivity : ComponentActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

val viewModel = ViewModelProvider(this)[ListensViewModel::class.java]

setContent {
ListenBrainzTheme {
Box(
ListenBrainzLogin(
modifier = Modifier
.fillMaxSize()
.background(ListenBrainzTheme.colorScheme.background)
.safeDrawingPadding(),
contentAlignment = Alignment.Center
) {
// FIXME: Security certificate warning in API 24 and below.
ListenBrainzLogin(viewModel) {
finish()
}
}
onLoginFinished = ::finish
)
}
}
}
}

@SuppressLint("SetJavaScriptEnabled")
@Composable
fun ListenBrainzLogin(viewModel: ListensViewModel, onLoginSuccess: (String) -> Unit) {
val url = "https://listenbrainz.org/login"
val coroutineScope = rememberCoroutineScope()
Row(modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
fun ListenBrainzLogin(
modifier: Modifier = Modifier,
onLoginFinished: () -> Unit
) {
val viewModel = hiltViewModel<ListensViewModel>()
var loadState by remember {
mutableStateOf<Resource<String>?>(null)
}

Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
AndroidView(factory = {
WebView(it).apply {

fun clearCookies() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
} else {
val cookieSyncManager = CookieSyncManager.createInstance(context)
cookieSyncManager.startSync()
val cookieManager: CookieManager = CookieManager.getInstance()
cookieManager.removeAllCookie()
cookieManager.removeSessionCookie()
cookieSyncManager.stopSync()
cookieSyncManager.sync()
// FIXME: Security certificate warning in API 24 and below.
ListenBrainzClient {
loadState = it
}

AnimatedContent(
modifier = Modifier
.fillMaxSize()
.align(Alignment.Center),
targetState = loadState,
transitionSpec = {
fadeIn() togetherWith fadeOut()
}
) { state ->
when (state?.status) {
Resource.Status.LOADING, Resource.Status.SUCCESS -> {
var isTokenValidRes by remember {
mutableStateOf<Resource<Unit>?>(null)
}

if (state.isSuccess) {
LaunchedEffectUnit {
isTokenValidRes = Resource.loading()
val isTokenValid = viewModel.saveUserDetails(state.data!!)

if (!isTokenValid) {
loadState = Resource.failure()
} else {
isTokenValidRes = Resource.success(Unit)
}
}
}

Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(0.4f)),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
when (isTokenValidRes?.status) {
null -> {
LoadingAnimation()
}
Resource.Status.LOADING -> {
LoadingAnimation()
Text(
text = "Verifying token...",
color = Color.White,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
}
Resource.Status.SUCCESS -> {
LaunchedEffectUnit {
delay(1.seconds)
onLoginFinished()
}

Text(
text = "Login successful!",
color = Color.White,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
}
Resource.Status.FAILED -> {
LaunchedEffectUnit {
delay(1.seconds)
onLoginFinished()
}

Text(
text = "Login failed.",
color = Color.White,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
}
}
}
}
}

layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
webViewClient = ListenBrainzWebClient { token ->
// Token is not null or empty.
coroutineScope.launch {
viewModel.saveUserDetails(token)
onLoginSuccess(token)
Resource.Status.FAILED -> {
LaunchedEffectUnit {
delay(1.seconds)
onLoginFinished()
}

Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(0.4f)),
contentAlignment = Alignment.Center
) {
Text(
text = "Something went wrong, please try again later.",
color = Color.White,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
}
}
clearCookies()
settings.javaScriptEnabled = true
setLayerType(View.LAYER_TYPE_HARDWARE, null)
loadUrl(url)
else -> {}
}
}, update = {
it.loadUrl(url)
})
}
}
}

@SuppressLint("SetJavaScriptEnabled")
@Composable
private fun ListenBrainzClient(
onLoad: (Resource<String>) -> Unit,
) {
val url = "https://listenbrainz.org/login"
AndroidView(factory = {
WebView(it).apply {

fun clearCookies() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
} else {
val cookieSyncManager = CookieSyncManager.createInstance(context)
cookieSyncManager.startSync()
val cookieManager: CookieManager = CookieManager.getInstance()
cookieManager.removeAllCookie()
cookieManager.removeSessionCookie()
cookieSyncManager.stopSync()
cookieSyncManager.sync()
}
}

layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
webViewClient = ListenBrainzWebClient(onLoad = onLoad)
clearCookies()
settings.javaScriptEnabled = true
setLayerType(View.LAYER_TYPE_HARDWARE, null)
loadUrl(url)
}
}, update = {
it.loadUrl(url)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -37,6 +38,7 @@ import com.airbnb.lottie.compose.rememberLottieComposition
import kotlinx.coroutines.launch
import org.listenbrainz.android.R
import org.listenbrainz.android.ui.theme.ListenBrainzTheme
import org.listenbrainz.android.viewmodel.ListensViewModel

@Composable
fun LoginScreen(
Expand Down Expand Up @@ -90,15 +92,12 @@ fun LoginScreen(
}

if (startLogin) {
ListenBrainzLogin(
viewModel = hiltViewModel(),
onLoginSuccess = {
scope.launch {
navigateToUserProfile()
startLogin = false
}
ListenBrainzLogin {
scope.launch {
navigateToUserProfile()
startLogin = false
}
)
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/org/listenbrainz/android/util/Resource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import org.listenbrainz.android.model.ResponseError
/**Use this class to pass [data] and [status] to view-model.
* @param error Whenever an error is occurred, the error must be passed to view-model through this parameter. *null* means no error.*/
class Resource<T>(val status: Status, val data: T?, val error: ResponseError? = null) {


inline val isSuccess get() = status.isSuccessful()
inline val isFailed get() = status.isFailed()
inline val isLoading get() = status.isLoading()

enum class Status {
LOADING, FAILED, SUCCESS;

fun isSuccessful(): Boolean {
return this == SUCCESS
}

fun isFailed(): Boolean {
return this == FAILED
}
fun isSuccessful() = this == SUCCESS

fun isFailed() = this == FAILED

fun isLoading() = this == LOADING

}

companion object {
Expand Down
Loading

0 comments on commit 1dbd65c

Please sign in to comment.