Skip to content

Commit

Permalink
Fix login function
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Dec 22, 2024
1 parent af28991 commit 7b81e2c
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ interface DatabaseDao {
suspend fun updateGoogleAccountUsed(
isUsed: Boolean,
email: String,
)
): Int

@Query("DELETE FROM googleaccountentity WHERE email = :email")
suspend fun deleteGoogleAccount(email: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class MainRepository(
suspend fun updateGoogleAccountUsed(
email: String,
isUsed: Boolean,
) = withContext(Dispatchers.IO) { localDataSource.updateGoogleAccountUsed(email, isUsed) }
): Flow<Int> = flow { emit(localDataSource.updateGoogleAccountUsed(email, isUsed)) }.flowOn(Dispatchers.IO)

suspend fun insertFollowedArtistSingleAndAlbum(followedArtistSingleAndAlbum: FollowedArtistSingleAndAlbum) =
withContext(Dispatchers.IO) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,9 @@ class MainActivity : AppCompatActivity() {
private fun checkForUpdate() {
viewModel.checkForUpdate()
viewModel.githubResponse.observe(this) { response ->
if (response != null && !this.isInPictureInPictureMode) {
if (response != null && !this.isInPictureInPictureMode && !viewModel.showedUpdateDialog) {
if (response.tagName != getString(R.string.version_name)) {
viewModel.showedUpdateDialog = true
val inputFormat =
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault())
val outputFormat = SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.getDefault())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LogInFragment : Fragment() {
) {
if (url == Config.YOUTUBE_MUSIC_MAIN_URL) {
CookieManager.getInstance().getCookie(url)?.let {
viewModel.saveCookie(it)
settingsViewModel.addAccount(it)
}
WebStorage.getInstance().deleteAllData()

Expand All @@ -84,18 +84,13 @@ class LogInFragment : Fragment() {
binding.webView.clearFormData()
binding.webView.clearHistory()
binding.webView.clearSslPreferences()
viewModel.status.observe(this@LogInFragment) {
if (it) {
settingsViewModel.addAccount()
Toast
.makeText(
requireContext(),
R.string.login_success,
Toast.LENGTH_SHORT,
).show()
findNavController().navigateUp()
}
}
Toast
.makeText(
requireContext(),
R.string.login_success,
Toast.LENGTH_SHORT,
).show()
findNavController().navigateUp()
}
}
}
Expand Down
351 changes: 201 additions & 150 deletions app/src/main/java/com/maxrave/simpmusic/ui/screen/home/SettingScreen.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.maxrave.simpmusic.viewModel

import android.app.Application
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.maxrave.simpmusic.viewModel.base.BaseViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LogInViewModel(
Expand All @@ -15,22 +13,9 @@ class LogInViewModel(
override val tag: String
get() = "LogInViewModel"

private val _status: MutableLiveData<Boolean> = MutableLiveData(false)
var status: LiveData<Boolean> = _status

private val _spotifyStatus: MutableLiveData<Boolean> = MutableLiveData(false)
var spotifyStatus: LiveData<Boolean> = _spotifyStatus

fun saveCookie(cookie: String) {
viewModelScope.launch {
Log.d("LogInViewModel", "saveCookie: $cookie")
dataStoreManager.setCookie(cookie)
dataStoreManager.setLoggedIn(true)
delay(1000)
_status.postValue(true)
}
}

fun saveSpotifySpdc(cookie: String) {
viewModelScope.launch {
cookie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.maxrave.simpmusic.extension.zipInputStream
import com.maxrave.simpmusic.extension.zipOutputStream
import com.maxrave.simpmusic.service.SimpleMediaService
import com.maxrave.simpmusic.service.test.download.DownloadUtils
import com.maxrave.simpmusic.utils.LocalResource
import com.maxrave.simpmusic.viewModel.base.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -779,22 +780,18 @@ class SettingsViewModel(
}
}

private var _googleAccounts: MutableStateFlow<ArrayList<GoogleAccountEntity>?> =
MutableStateFlow(null)
val googleAccounts: MutableStateFlow<ArrayList<GoogleAccountEntity>?> = _googleAccounts

private var _loading: MutableStateFlow<Boolean> = MutableStateFlow(false)
val loading: MutableStateFlow<Boolean> = _loading
private var _googleAccounts: MutableStateFlow<LocalResource<List<GoogleAccountEntity>>> =
MutableStateFlow(LocalResource.Loading())
val googleAccounts: StateFlow<LocalResource<List<GoogleAccountEntity>>> = _googleAccounts

fun getAllGoogleAccount() {
Log.w("getAllGoogleAccount", "getAllGoogleAccount: Go to function")
viewModelScope.launch {
_loading.value = true
mainRepository.getGoogleAccounts().collect { accounts ->
_googleAccounts.emit(LocalResource.Loading())
mainRepository.getGoogleAccounts().collectLatest { accounts ->
Log.w("getAllGoogleAccount", "getAllGoogleAccount: $accounts")
if (!accounts.isNullOrEmpty()) {
_googleAccounts.value = accounts as ArrayList<GoogleAccountEntity>
_loading.value = false
_googleAccounts.emit(LocalResource.Success(accounts))
} else {
if (loggedIn.value == DataStoreManager.TRUE) {
mainRepository.getAccountInfo().collect {
Expand All @@ -817,25 +814,34 @@ class SettingsViewModel(
delay(500)
getAllGoogleAccount()
} else {
_googleAccounts.value = null
_loading.value = false
_googleAccounts.emit(LocalResource.Success(emptyList()))
}
}
} else {
_googleAccounts.value = null
_loading.value = false
_googleAccounts.emit(LocalResource.Success(emptyList()))
}
}
}
}
}

fun addAccount() {
fun addAccount(cookie: String) {
viewModelScope.launch {
dataStoreManager.setCookie(cookie)
dataStoreManager.setLoggedIn(true)
mainRepository.getAccountInfo().collect { accountInfo ->
Log.d("getAllGoogleAccount", "addAccount: $accountInfo")
if (accountInfo != null) {
googleAccounts.value?.forEach {
mainRepository.updateGoogleAccountUsed(it.email, false)
runBlocking {
mainRepository.getGoogleAccounts().singleOrNull()?.forEach {
Log.d("getAllGoogleAccount", "set used: $it start")
mainRepository
.updateGoogleAccountUsed(it.email, false)
.singleOrNull()
?.let {
Log.w("getAllGoogleAccount", "set used: $it")
}
}
}
dataStoreManager.putString("AccountName", accountInfo.name)
dataStoreManager.putString(
Expand Down Expand Up @@ -864,20 +870,35 @@ class SettingsViewModel(
fun setUsedAccount(acc: GoogleAccountEntity?) {
viewModelScope.launch {
if (acc != null) {
googleAccounts.value?.forEach {
mainRepository.updateGoogleAccountUsed(it.email, false)
googleAccounts.value.data?.forEach {
mainRepository
.updateGoogleAccountUsed(it.email, false)
.singleOrNull()
?.let {
Log.w("getAllGoogleAccount", "set used: $it")
}
}
dataStoreManager.putString("AccountName", acc.name)
dataStoreManager.putString("AccountThumbUrl", acc.thumbnailUrl)
mainRepository.updateGoogleAccountUsed(acc.email, true)
mainRepository
.updateGoogleAccountUsed(acc.email, true)
.singleOrNull()
?.let {
Log.w("getAllGoogleAccount", "set used: $it")
}
dataStoreManager.setCookie(acc.cache ?: "")
dataStoreManager.setLoggedIn(true)
delay(500)
getAllGoogleAccount()
getLoggedIn()
} else {
googleAccounts.value?.forEach {
mainRepository.updateGoogleAccountUsed(it.email, false)
googleAccounts.value.data?.forEach {
mainRepository
.updateGoogleAccountUsed(it.email, false)
.singleOrNull()
?.let {
Log.w("getAllGoogleAccount", "set used: $it")
}
}
dataStoreManager.putString("AccountName", "")
dataStoreManager.putString("AccountThumbUrl", "")
Expand All @@ -892,7 +913,7 @@ class SettingsViewModel(

fun logOutAllYouTube() {
viewModelScope.launch {
googleAccounts.value?.forEach { account ->
googleAccounts.value.data?.forEach { account ->
mainRepository.deleteGoogleAccount(account.email)
}
dataStoreManager.putString("AccountName", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class SharedViewModel(
var isFirstLiked: Boolean = false
var isFirstMiniplayer: Boolean = false
var isFirstSuggestions: Boolean = false
var showedUpdateDialog: Boolean = false
var showOrHideMiniplayer: MutableSharedFlow<Boolean> = MutableSharedFlow()

override val tag = "SharedViewModel"
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="demo_description">You can also find us with: electronic dance music 2023 edm top electronic dance songs 2023 edm songs edm music 2023 electronic dance music best electro dance music popular edm songs top edm songs edm songs 2023 edm music top edm songs 2023 top electronic dance songs edm 2023 popular edm songs 2023 best electro dance music 2023 No matter how your day is, you can always listen to good music: 1. EDM Hits Playlist - Popular EDM Hits in 2023 - We would like to keep you with us many years from now on! So, in the next year, you could find our playlist like this: EDM 2024 ♫ Top EDM Songs 2024</string>
<string name="Song_and_artist_name">Bài hát • %1$s</string>
<string name="playlist_and_author">Danh sách phát • %1$s</string>
<string name="album_and_artist_name">Album • %1$s</string>
Expand Down Expand Up @@ -357,4 +356,18 @@
<string name="updating">Đang cập nhật</string>
<string name="go_to_log_in_page">Đi tới trang đăng nhập</string>
<string name="log_in_warning">YouTube Music hiện tại đang yêu cầu đăng nhập để nghe nhạc trực tuyến, cũng như các thực thể Piped đôi khi không hoạt động. Bạn nên đăng nhập vào YouTube để có trải nghiệm nghe nhạc tốt nhất với SimpMusic.</string>
<string name="spotify_canvas_cache">Cache của Spotify Canvas</string>
<string name="clear_canvas_cache">Xoá bộ nhớ cache của Spotify Canvas</string>
<string name="proxy">Proxy</string>
<string name="proxy_description">Sử dụng Proxy để vượt qua trình chặn nội dung</string>
<string name="proxy_type">Loại Proxy</string>
<string name="http">HTTP</string>
<string name="socks">Socks</string>
<string name="proxy_host">Máy chủ proxy</string>
<string name="invalid_host">Máy chủ không hợp lệ</string>
<string name="proxy_port">Cổng proxy</string>
<string name="invalid_port">Cổng không hợp lệ</string>
<string name="proxy_host_message">Hãy nhập máy chủ Proxy của bạn</string>
<string name="proxy_port_message">Hãy nhập cổng Proxy của bạn</string>
<string name="five_seconds">5 giây</string>
</resources>

0 comments on commit 7b81e2c

Please sign in to comment.