diff --git a/app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt b/app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt index 86b203d1d..0d8e15946 100644 --- a/app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt +++ b/app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt @@ -19,8 +19,10 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.viewinterop.AndroidView +import androidx.datastore.preferences.core.edit import androidx.navigation.NavController import com.zionhuang.innertube.YouTube +import com.zionhuang.innertube.utils.parseCookieString import com.zionhuang.music.LocalPlayerAwareWindowInsets import com.zionhuang.music.R import com.zionhuang.music.constants.AccountChannelHandleKey @@ -30,11 +32,13 @@ import com.zionhuang.music.constants.InnerTubeCookieKey import com.zionhuang.music.constants.VisitorDataKey import com.zionhuang.music.ui.component.IconButton import com.zionhuang.music.ui.utils.backToMain +import com.zionhuang.music.utils.dataStore import com.zionhuang.music.utils.rememberPreference import com.zionhuang.music.utils.reportException import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import kotlin.collections.contains @SuppressLint("SetJavaScriptEnabled") @OptIn(ExperimentalMaterial3Api::class, DelicateCoroutinesApi::class) @@ -59,8 +63,15 @@ fun LoginScreen( webViewClient = object : WebViewClient() { override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) { if (url.startsWith("https://music.youtube.com")) { - innerTubeCookie = CookieManager.getInstance().getCookie(url) + val youtubeCookieString = CookieManager.getInstance().getCookie(url) GlobalScope.launch { + if ("SAPISID" in parseCookieString(youtubeCookieString)) { // if logged in + innerTubeCookie = youtubeCookieString + } else { // if logged out + context.dataStore.edit { settings -> + settings.remove(InnerTubeCookieKey) + } + } YouTube.accountInfo().onSuccess { accountName = it.name accountEmail = it.email.orEmpty() @@ -84,6 +95,15 @@ fun LoginScreen( addJavascriptInterface(object { @JavascriptInterface fun onRetrieveVisitorData(newVisitorData: String?) { + if (innerTubeCookie == "") { // clear visitorData after logout (this will be regenerated in App.kt) + GlobalScope.launch { + context.dataStore.edit { settings -> + settings.remove(VisitorDataKey) + } + } + return + } + if (newVisitorData != null) { visitorData = newVisitorData } diff --git a/settings.gradle.kts b/settings.gradle.kts index 06a0c2243..5d58be3cf 100755 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -10,6 +10,10 @@ dependencyResolutionManagement { } } +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version("0.4.0") +} + rootProject.name = "InnerTune" include(":app") include(":innertube")