From 24ec094d6b49b0f0366dc5b920842e34eaeb372d Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 7 Dec 2023 15:05:57 +0530 Subject: [PATCH 1/8] move methods from LoginData to DataStoreModule and PreferenceManagerModule --- .../apps/data/login/CleanApkAuthenticator.kt | 9 ++-- .../e/apps/data/login/LoginCommon.kt | 29 ++++++----- .../apps/data/login/PlayStoreAuthenticator.kt | 25 +++++---- .../apps/data/login/api/GoogleLoginManager.kt | 8 +-- .../login/api/PlayStoreLoginManagerFactory.kt | 6 +-- .../e/apps/data/preference/DataStoreModule.kt | 52 ++++++++++++++++++- .../preference/PreferenceManagerModule.kt | 8 +++ 7 files changed, 102 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/login/CleanApkAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/CleanApkAuthenticator.kt index d3879f586..5a83b9a83 100644 --- a/app/src/main/java/foundation/e/apps/data/login/CleanApkAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/CleanApkAuthenticator.kt @@ -19,6 +19,8 @@ package foundation.e.apps.data.login import foundation.e.apps.data.ResultSupreme import foundation.e.apps.data.enums.User +import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.data.preference.PreferenceManagerModule import javax.inject.Inject import javax.inject.Singleton @@ -28,11 +30,12 @@ import javax.inject.Singleton */ @Singleton class CleanApkAuthenticator @Inject constructor( - val loginData: LoginData, + private val dataStoreModule: DataStoreModule, + private val preferenceManagerModule: PreferenceManagerModule, ) : StoreAuthenticator { private val user: User - get() = loginData.getUserType() + get() = dataStoreModule.getUserType() override fun isStoreActive(): Boolean { if (user == User.UNAVAILABLE) { @@ -41,7 +44,7 @@ class CleanApkAuthenticator @Inject constructor( */ return false } - return loginData.isOpenSourceSelected() || loginData.isPWASelected() + return preferenceManagerModule.isOpenSourceSelected() || preferenceManagerModule.isPWASelected() } override suspend fun login(): AuthObject.CleanApk { diff --git a/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt b/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt index b041a29db..4f2b0be9a 100644 --- a/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt +++ b/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt @@ -19,6 +19,8 @@ package foundation.e.apps.data.login import foundation.e.apps.data.Constants import foundation.e.apps.data.enums.User +import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.data.preference.PreferenceManagerModule import javax.inject.Inject import javax.inject.Singleton @@ -30,33 +32,34 @@ import javax.inject.Singleton */ @Singleton class LoginCommon @Inject constructor( - private val loginData: LoginData, + private val dataStoreModule: DataStoreModule, + private val preferenceManagerModule: PreferenceManagerModule, ) { suspend fun saveUserType(user: User) { - loginData.saveUserType(user) + dataStoreModule.saveUserType(user) } fun getUserType(): User { - return loginData.getUserType() + return dataStoreModule.getUserType() } suspend fun saveGoogleLogin(email: String, oauth: String) { - loginData.saveGoogleLogin(email, oauth) + dataStoreModule.saveGoogleLogin(email, oauth) } suspend fun setNoGoogleMode() { - loginData.setSource(Constants.PREFERENCE_SHOW_FOSS, true) - loginData.setSource(Constants.PREFERENCE_SHOW_PWA, true) - loginData.setSource(Constants.PREFERENCE_SHOW_GPLAY, false) - loginData.saveUserType(User.NO_GOOGLE) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_FOSS, true) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_PWA, true) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_GPLAY, false) + dataStoreModule.saveUserType(User.NO_GOOGLE) } suspend fun logout() { - loginData.destroyCredentials() - loginData.clearUserType() + dataStoreModule.destroyCredentials() + dataStoreModule.clearUserType() // reset app source preferences on logout. - loginData.setSource(Constants.PREFERENCE_SHOW_FOSS, true) - loginData.setSource(Constants.PREFERENCE_SHOW_PWA, true) - loginData.setSource(Constants.PREFERENCE_SHOW_GPLAY, true) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_FOSS, true) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_PWA, true) + preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_GPLAY, true) } } diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index b408ff7f9..fdf861354 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -28,6 +28,8 @@ import foundation.e.apps.data.login.api.PlayStoreLoginManagerFactory import foundation.e.apps.data.login.api.PlayStoreLoginManager import foundation.e.apps.data.login.api.GoogleLoginManager import foundation.e.apps.data.login.api.PlayStoreLoginWrapper +import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.data.preference.PreferenceManagerModule import foundation.e.apps.data.retryWithBackoff import timber.log.Timber import java.util.Locale @@ -44,14 +46,15 @@ import javax.inject.Singleton class PlayStoreAuthenticator @Inject constructor( @ApplicationContext private val context: Context, private val gson: Gson, - private val loginData: LoginData, + private val dataStoreModule: DataStoreModule, + private val preferenceManagerModule: PreferenceManagerModule, ) : StoreAuthenticator, AuthDataValidator { @Inject lateinit var loginManagerFactory: PlayStoreLoginManagerFactory private val user: User - get() = loginData.getUserType() + get() = dataStoreModule.getUserType() private val loginManager: PlayStoreLoginManager get() = loginManagerFactory.createLoginManager(user) @@ -69,7 +72,7 @@ class PlayStoreAuthenticator @Inject constructor( */ return false } - return loginData.isGplaySelected() + return preferenceManagerModule.isGplaySelected() } /** @@ -106,7 +109,7 @@ class PlayStoreAuthenticator @Inject constructor( } override suspend fun logout() { - loginData.clearAuthData() + dataStoreModule.clearAuthData() } /** @@ -114,7 +117,7 @@ class PlayStoreAuthenticator @Inject constructor( * Returns null if nothing is saved. */ private fun getSavedAuthData(): AuthData? { - val authJson = loginData.getAuthData() + val authJson = dataStoreModule.getAuthDataSync() return if (authJson.isBlank()) null else try { gson.fromJson(authJson, AuthData::class.java) @@ -125,14 +128,14 @@ class PlayStoreAuthenticator @Inject constructor( } private suspend fun saveAuthData(authData: AuthData) { - loginData.saveAuthData(authData) + dataStoreModule.saveCredentials(authData) } /** * Generate new AuthData based on the user type. */ private suspend fun generateAuthData(): ResultSupreme { - return when (loginData.getUserType()) { + return when (dataStoreModule.getUserType()) { User.ANONYMOUS -> getAuthDataAnonymously() User.GOOGLE -> getAuthDataWithGoogleAccount() else -> ResultSupreme.Error("User type not ANONYMOUS or GOOGLE") @@ -157,9 +160,9 @@ class PlayStoreAuthenticator @Inject constructor( private suspend fun getAuthDataWithGoogleAccount(): ResultSupreme { - val email = loginData.getEmail() - val oauthToken = loginData.getOAuthToken() - val aasToken = loginData.getAASToken() + val email = dataStoreModule.getEmail() + val oauthToken = dataStoreModule.getOAuthToken() + val aasToken = dataStoreModule.getAASToken() /* * If aasToken is not blank, means it was stored successfully from a previous Google login. * Use it to fetch auth data. @@ -195,7 +198,7 @@ class PlayStoreAuthenticator @Inject constructor( /* * Finally save the aasToken and create auth data. */ - loginData.saveAasToken(aasTokenFetched) + dataStoreModule.saveAasToken(aasTokenFetched) return loginWrapper.login(locale).run { if (isSuccess()) ResultSupreme.Success(formatAuthData(this.data!!)) else this diff --git a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt index c0d5eff3c..352bd253a 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt @@ -23,7 +23,7 @@ import com.aurora.gplayapi.helpers.AuthHelper import foundation.e.apps.data.playstore.utils.AC2DMTask import foundation.e.apps.data.playstore.utils.CustomAuthValidator import foundation.e.apps.data.playstore.utils.GPlayHttpClient -import foundation.e.apps.data.login.LoginData +import foundation.e.apps.data.preference.DataStoreModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.util.Properties @@ -32,7 +32,7 @@ class GoogleLoginManager( private val gPlayHttpClient: GPlayHttpClient, private val nativeDeviceProperty: Properties, private val aC2DMTask: AC2DMTask, - private val loginData: LoginData + private val dataStoreModule: DataStoreModule, ) : PlayStoreLoginManager { /** @@ -58,8 +58,8 @@ class GoogleLoginManager( * @return authData: authentication data */ override suspend fun login(): AuthData? { - val email = loginData.getEmail() - val aasToken = loginData.getAASToken() + val email = dataStoreModule.getEmail() + val aasToken = dataStoreModule.getAASToken() var authData: AuthData? withContext(Dispatchers.IO) { diff --git a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginManagerFactory.kt b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginManagerFactory.kt index 784532df9..fc1c55156 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginManagerFactory.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/PlayStoreLoginManagerFactory.kt @@ -21,7 +21,7 @@ import com.google.gson.Gson import foundation.e.apps.data.enums.User import foundation.e.apps.data.playstore.utils.AC2DMTask import foundation.e.apps.data.playstore.utils.GPlayHttpClient -import foundation.e.apps.data.login.LoginData +import foundation.e.apps.data.preference.DataStoreModule import java.util.Properties import javax.inject.Inject import javax.inject.Singleton @@ -32,12 +32,12 @@ class PlayStoreLoginManagerFactory @Inject constructor( private val nativeDeviceProperty: Properties, private val aC2DMTask: AC2DMTask, private val gson: Gson, - private val loginData: LoginData + private val dataStoreModule: DataStoreModule, ) { fun createLoginManager(user: User): PlayStoreLoginManager { return when (user) { - User.GOOGLE -> GoogleLoginManager(gPlayHttpClient, nativeDeviceProperty, aC2DMTask, loginData) + User.GOOGLE -> GoogleLoginManager(gPlayHttpClient, nativeDeviceProperty, aC2DMTask, dataStoreModule) else -> AnonymousLoginManager(gPlayHttpClient, nativeDeviceProperty, gson) } } diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index a8482aaeb..3f7078ebb 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -33,6 +33,22 @@ import kotlinx.coroutines.runBlocking import javax.inject.Inject import javax.inject.Singleton +/** + * Difference between [OAUTHTOKEN] and [AASTOKEN]: + * + * These two are used only for Google login, not for Anonymous login. + * OAuthToken is obtained from the Google Login web page, from the cookies. + * This OAuthToken is then used by AC2DMTask in GPlayAPIImpl class + * to generate AasToken. + * + * To get Google Play Store data, we need to create an AuthData instance. + * For Google user, this can only be done using AasToken, not OAuthToken. + * + * Very important: AasToken can be generated only ONCE from one OAuthToken. + * We cannot get AasToken again from the same OAuthToken. Thus it is + * important to safely store the AasToken to regenerate AuthData if needed. + * If AasToken is not stored, user has to logout and login again. + */ @Singleton class DataStoreModule @Inject constructor( @ApplicationContext @@ -48,13 +64,15 @@ class DataStoreModule @Inject constructor( private val AUTHDATA = stringPreferencesKey("authData") private val EMAIL = stringPreferencesKey("email") private val OAUTHTOKEN = stringPreferencesKey("oauthtoken") + private val AASTOKEN = stringPreferencesKey("aasToken") private val USERTYPE = stringPreferencesKey("userType") private val TOCSTATUS = booleanPreferencesKey("tocStatus") private val TOSVERSION = stringPreferencesKey("tosversion") val authData = context.dataStore.data.map { it[AUTHDATA] ?: "" } val emailData = context.dataStore.data.map { it[EMAIL] ?: "" } - val aasToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" } + val oauthToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" } + val aasToken = context.dataStore.data.map { it[AASTOKEN] ?: "" } val userType = context.dataStore.data.map { it[USERTYPE] ?: "" } val tocStatus = context.dataStore.data.map { it[TOCSTATUS] ?: false } val tosVersion = context.dataStore.data.map { it[TOSVERSION] ?: "" } @@ -80,6 +98,7 @@ class DataStoreModule @Inject constructor( it.remove(AUTHDATA) it.remove(EMAIL) it.remove(OAUTHTOKEN) + it.remove(AASTOKEN) } } @@ -142,4 +161,35 @@ class DataStoreModule @Inject constructor( } } } + + fun getOAuthToken(): String { + return runBlocking { + oauthToken.first() + } + } + + fun getAASToken(): String { + return runBlocking { + aasToken.first() + } + } + + suspend fun saveAasToken(aasToken: String) { + context.dataStore.edit { + it[AASTOKEN] = aasToken + } + } + + suspend fun saveGoogleLogin(email: String, token: String) { + context.dataStore.edit { + it[EMAIL] = email + it[OAUTHTOKEN] = token + } + } + + suspend fun clearAuthData() { + context.dataStore.edit { + it.remove(AUTHDATA) + } + } } diff --git a/app/src/main/java/foundation/e/apps/data/preference/PreferenceManagerModule.kt b/app/src/main/java/foundation/e/apps/data/preference/PreferenceManagerModule.kt index 0dba23101..813fa8584 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/PreferenceManagerModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/PreferenceManagerModule.kt @@ -67,4 +67,12 @@ class PreferenceManagerModule @Inject constructor( context.getString(R.string.update_apps_from_other_stores), true ) + + fun setSource(source: String, value: Boolean) { + val editor = preferenceManager.edit() + editor.run { + this.putBoolean(source, value) + } + editor.apply() + } } -- GitLab From 337ca035ef1e0a8865f59bfbb6e4dee758d633a6 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 7 Dec 2023 15:06:46 +0530 Subject: [PATCH 2/8] rename saveCredentials to saveAuthData --- .../java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt | 2 +- .../java/foundation/e/apps/data/preference/DataStoreModule.kt | 2 +- .../e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index fdf861354..772257af2 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -128,7 +128,7 @@ class PlayStoreAuthenticator @Inject constructor( } private suspend fun saveAuthData(authData: AuthData) { - dataStoreModule.saveCredentials(authData) + dataStoreModule.saveAuthData(authData) } /** diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index 3f7078ebb..208bc6f41 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -80,7 +80,7 @@ class DataStoreModule @Inject constructor( /** * Allows to save gplay API token data into datastore */ - suspend fun saveCredentials(authData: AuthData) { + suspend fun saveAuthData(authData: AuthData) { context.dataStore.edit { it[AUTHDATA] = gson.toJson(authData) } diff --git a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt index 5be760a1b..f3e0f2d5a 100644 --- a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt +++ b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt @@ -55,7 +55,7 @@ class LocaleChangedBroadcastReceiver : BroadcastReceiver() { val authDataJson = dataStoreModule.getAuthDataSync() val authData = gson.fromJson(authDataJson, AuthData::class.java) authData.locale = context.resources.configuration.locales[0] - dataStoreModule.saveCredentials(authData) + dataStoreModule.saveAuthData(authData) withContext(Dispatchers.IO) { cache.evictAll() } -- GitLab From 4da979d793c9fd44e8321166e448f9e9ab95cb2b Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 7 Dec 2023 15:08:05 +0530 Subject: [PATCH 3/8] Delete LoginData.kt --- .../foundation/e/apps/data/login/LoginData.kt | 188 ------------------ 1 file changed, 188 deletions(-) delete mode 100644 app/src/main/java/foundation/e/apps/data/login/LoginData.kt diff --git a/app/src/main/java/foundation/e/apps/data/login/LoginData.kt b/app/src/main/java/foundation/e/apps/data/login/LoginData.kt deleted file mode 100644 index c9da999b5..000000000 --- a/app/src/main/java/foundation/e/apps/data/login/LoginData.kt +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (C) 2019-2022 E FOUNDATION - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package foundation.e.apps.data.login - -import android.content.Context -import androidx.datastore.preferences.core.edit -import androidx.datastore.preferences.core.stringPreferencesKey -import androidx.preference.PreferenceManager -import com.aurora.gplayapi.data.models.AuthData -import com.google.gson.Gson -import dagger.hilt.android.qualifiers.ApplicationContext -import foundation.e.apps.data.Constants.PREFERENCE_SHOW_FOSS -import foundation.e.apps.data.Constants.PREFERENCE_SHOW_GPLAY -import foundation.e.apps.data.Constants.PREFERENCE_SHOW_PWA -import foundation.e.apps.data.enums.User -import foundation.e.apps.data.preference.DataStoreModule.Companion.dataStore -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking -import javax.inject.Inject -import javax.inject.Singleton - -@Singleton -class LoginData @Inject constructor( - @ApplicationContext - private val context: Context, - private val gson: Gson -) { - - private val preferenceManager = PreferenceManager.getDefaultSharedPreferences(context) - - private val AUTHDATA = stringPreferencesKey("authData") - private val EMAIL = stringPreferencesKey("email") - private val OAUTHTOKEN = stringPreferencesKey("oauthtoken") - private val AASTOKEN = stringPreferencesKey("aasToken") - private val USERTYPE = stringPreferencesKey("userType") - - /* - * Difference between OAUTHTOKEN and AASTOKEN: - * - * These two are used only for Google login, not for Anonymous login. - * OAuthToken is obtained from the Google Login web page, from the cookies. - * This OAuthToken is then used by AC2DMTask in GPlayAPIImpl class - * to generate AasToken. - * - * To get Google Play Store data, we need to create an AuthData instance. - * For Google user, this can only be done using AasToken, not OAuthToken. - * - * Very important: AasToken can be generated only ONCE from one OAuthToken. - * We cannot get AasToken again from the same OAuthToken. Thus it is - * important to safely store the AasToken to regenerate AuthData if needed. - * If AasToken is not stored, user has to logout and login again. - */ - - private val authData = context.dataStore.data.map { it[AUTHDATA] ?: "" } - private val emailData = context.dataStore.data.map { it[EMAIL] ?: "" } - private val aasToken = context.dataStore.data.map { it[AASTOKEN] ?: "" } - private val oauthToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" } - private val userType = context.dataStore.data.map { it[USERTYPE] ?: "" } - - // Setters - - suspend fun saveAuthData(authData: AuthData) { - context.dataStore.edit { - it[AUTHDATA] = gson.toJson(authData) - } - } - - suspend fun saveUserType(user: User) { - context.dataStore.edit { - it[USERTYPE] = user.name - } - } - - suspend fun saveGoogleLogin(email: String, token: String) { - context.dataStore.edit { - it[EMAIL] = email - it[OAUTHTOKEN] = token - } - } - - suspend fun saveAasToken(aasToken: String) { - context.dataStore.edit { - it[AASTOKEN] = aasToken - } - } - - // Getters - - fun getAuthData(): String { - return runBlocking { - authData.first() - } - } - - /** - * Get the [User] type stored in the data store. - * In case nothing is stored, returns [User.UNAVAILABLE]. - * - * No need to wrap this function in try-catch block. - * - * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5680 - */ - fun getUserType(): User { - return runBlocking { - userType.first().run { - val userStrings = User.values().map { it.name } - if (this !in userStrings) User.UNAVAILABLE - else User.valueOf(this) - } - } - } - - fun getEmail(): String { - return runBlocking { - emailData.first() - } - } - - fun getOAuthToken(): String { - return runBlocking { - oauthToken.first() - } - } - - fun getAASToken(): String { - return runBlocking { - aasToken.first() - } - } - - fun isOpenSourceSelected() = preferenceManager.getBoolean(PREFERENCE_SHOW_FOSS, true) - fun isPWASelected() = preferenceManager.getBoolean(PREFERENCE_SHOW_PWA, true) - fun isGplaySelected() = preferenceManager.getBoolean(PREFERENCE_SHOW_GPLAY, true) - - fun setSource(source: String, value: Boolean) { - val editor = preferenceManager.edit() - editor.run { - this.putBoolean(source, value) - } - editor.apply() - } - - // Clear data - - /** - * Destroy auth credentials if they are no longer valid. - * - * Modification for issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 - * Previously this method would also remove [USERTYPE]. - * To clear this value, call [clearUserType]. - */ - suspend fun destroyCredentials() { - context.dataStore.edit { - it.remove(AUTHDATA) - it.remove(EMAIL) - it.remove(OAUTHTOKEN) - it.remove(AASTOKEN) - } - } - - suspend fun clearAuthData() { - context.dataStore.edit { - it.remove(AUTHDATA) - } - } - - suspend fun clearUserType() { - context.dataStore.edit { - it.remove(USERTYPE) - } - } -} -- GitLab From c3e76a5427c2b9ba9d549fc4c1937775aaec84ef Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 7 Dec 2023 16:54:30 +0530 Subject: [PATCH 4/8] various changes to reduce number of methods in DataStoreModule to pass detekt --- .../foundation/e/apps/AppLoungeApplication.kt | 4 +- .../e/apps/data/login/LoginCommon.kt | 2 +- .../apps/data/login/PlayStoreAuthenticator.kt | 5 ++- .../e/apps/data/preference/DataStoreModule.kt | 43 +++---------------- .../e/apps/ui/setup/signin/SignInViewModel.kt | 14 ------ 5 files changed, 14 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt index 2a4ee0989..3fcf7e429 100644 --- a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt +++ b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt @@ -43,6 +43,8 @@ import timber.log.Timber import timber.log.Timber.Forest.plant import java.util.concurrent.Executors import javax.inject.Inject +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking @HiltAndroidApp @DelicateCoroutinesApi @@ -74,7 +76,7 @@ class AppLoungeApplication : Application(), Configuration.Provider { val pkgManagerBR = object : PkgManagerBR() {} registerReceiver(pkgManagerBR, pkgManagerModule.getFilter(), RECEIVER_EXPORTED) - val currentVersion = dataStoreModule.getTOSVersion() + val currentVersion = runBlocking { dataStoreModule.tosVersion.first() } if (!currentVersion.contentEquals(TOS_VERSION)) { MainScope().launch { dataStoreModule.saveTOCStatus(false, "") diff --git a/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt b/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt index 4f2b0be9a..8af0004e3 100644 --- a/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt +++ b/app/src/main/java/foundation/e/apps/data/login/LoginCommon.kt @@ -56,7 +56,7 @@ class LoginCommon @Inject constructor( suspend fun logout() { dataStoreModule.destroyCredentials() - dataStoreModule.clearUserType() + dataStoreModule.saveUserType(null) // reset app source preferences on logout. preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_FOSS, true) preferenceManagerModule.setSource(Constants.PREFERENCE_SHOW_PWA, true) diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index 772257af2..af6ef1993 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -35,6 +35,7 @@ import timber.log.Timber import java.util.Locale import javax.inject.Inject import javax.inject.Singleton +import kotlinx.coroutines.flow.first /** * Class to get GPlay auth data. Call [login] to get an already saved auth data @@ -109,7 +110,7 @@ class PlayStoreAuthenticator @Inject constructor( } override suspend fun logout() { - dataStoreModule.clearAuthData() + dataStoreModule.saveAuthData(null) } /** @@ -161,7 +162,7 @@ class PlayStoreAuthenticator @Inject constructor( private suspend fun getAuthDataWithGoogleAccount(): ResultSupreme { val email = dataStoreModule.getEmail() - val oauthToken = dataStoreModule.getOAuthToken() + val oauthToken = dataStoreModule.oauthToken.first() val aasToken = dataStoreModule.getAASToken() /* * If aasToken is not blank, means it was stored successfully from a previous Google login. diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index 208bc6f41..ffda93f88 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -80,9 +80,10 @@ class DataStoreModule @Inject constructor( /** * Allows to save gplay API token data into datastore */ - suspend fun saveAuthData(authData: AuthData) { + suspend fun saveAuthData(authData: AuthData?) { context.dataStore.edit { - it[AUTHDATA] = gson.toJson(authData) + if (authData == null) it.remove(AUTHDATA) + else it[AUTHDATA] = gson.toJson(authData) } } @@ -91,7 +92,7 @@ class DataStoreModule @Inject constructor( * * Modification for issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 * Previously this method would also remove [USERTYPE]. - * To clear this value, call [clearUserType]. + * To clear this value, call [saveUserType] with null. */ suspend fun destroyCredentials() { context.dataStore.edit { @@ -102,12 +103,6 @@ class DataStoreModule @Inject constructor( } } - suspend fun clearUserType() { - context.dataStore.edit { - it.remove(USERTYPE) - } - } - /** * TOC status */ @@ -118,18 +113,13 @@ class DataStoreModule @Inject constructor( } } - fun getTOSVersion(): String { - return runBlocking { - tosVersion.first() - } - } - /** * User auth type */ - suspend fun saveUserType(user: User) { + suspend fun saveUserType(user: User?) { context.dataStore.edit { - it[USERTYPE] = user.name + if (user == null) it.remove(USERTYPE) + else it[USERTYPE] = user.name } } @@ -139,13 +129,6 @@ class DataStoreModule @Inject constructor( } } - suspend fun saveEmail(email: String, token: String) { - context.dataStore.edit { - it[EMAIL] = email - it[OAUTHTOKEN] = token - } - } - fun getEmail(): String { return runBlocking { emailData.first() @@ -162,12 +145,6 @@ class DataStoreModule @Inject constructor( } } - fun getOAuthToken(): String { - return runBlocking { - oauthToken.first() - } - } - fun getAASToken(): String { return runBlocking { aasToken.first() @@ -186,10 +163,4 @@ class DataStoreModule @Inject constructor( it[OAUTHTOKEN] = token } } - - suspend fun clearAuthData() { - context.dataStore.edit { - it.remove(AUTHDATA) - } - } } diff --git a/app/src/main/java/foundation/e/apps/ui/setup/signin/SignInViewModel.kt b/app/src/main/java/foundation/e/apps/ui/setup/signin/SignInViewModel.kt index 61d7c5757..742cf51d9 100644 --- a/app/src/main/java/foundation/e/apps/ui/setup/signin/SignInViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/setup/signin/SignInViewModel.kt @@ -21,18 +21,4 @@ class SignInViewModel @Inject constructor( private val _authLiveData: MutableLiveData = MutableLiveData() val authLiveData: LiveData = _authLiveData - fun saveUserType(user: User) { - viewModelScope.launch { - dataStoreModule.saveUserType(user) - if (user == User.UNAVAILABLE) { - dataStoreModule.destroyCredentials() - } - } - } - - fun saveEmailToken(email: String, token: String) { - viewModelScope.launch { - dataStoreModule.saveEmail(email, token) - } - } } -- GitLab From f412da70aa6f921fc6b7bb13fa61963c9b7cda12 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Thu, 7 Dec 2023 13:57:59 +0000 Subject: [PATCH 5/8] Apply 1 suggestion(s) to 1 file(s) --- .../e/apps/data/preference/DataStoreModule.kt | 333 +++++++++--------- 1 file changed, 167 insertions(+), 166 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index ffda93f88..e6957f6be 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -1,166 +1,167 @@ -/* - * Apps Quickly and easily install Android apps onto your device! - * Copyright (C) 2021 E FOUNDATION - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package foundation.e.apps.data.preference - -import android.content.Context -import androidx.datastore.preferences.core.booleanPreferencesKey -import androidx.datastore.preferences.core.edit -import androidx.datastore.preferences.core.stringPreferencesKey -import androidx.datastore.preferences.preferencesDataStore -import com.aurora.gplayapi.data.models.AuthData -import com.google.gson.Gson -import dagger.hilt.android.qualifiers.ApplicationContext -import foundation.e.apps.data.enums.User -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking -import javax.inject.Inject -import javax.inject.Singleton - -/** - * Difference between [OAUTHTOKEN] and [AASTOKEN]: - * - * These two are used only for Google login, not for Anonymous login. - * OAuthToken is obtained from the Google Login web page, from the cookies. - * This OAuthToken is then used by AC2DMTask in GPlayAPIImpl class - * to generate AasToken. - * - * To get Google Play Store data, we need to create an AuthData instance. - * For Google user, this can only be done using AasToken, not OAuthToken. - * - * Very important: AasToken can be generated only ONCE from one OAuthToken. - * We cannot get AasToken again from the same OAuthToken. Thus it is - * important to safely store the AasToken to regenerate AuthData if needed. - * If AasToken is not stored, user has to logout and login again. - */ -@Singleton -class DataStoreModule @Inject constructor( - @ApplicationContext - private val context: Context, - private val gson: Gson -) { - - companion object { - private const val preferenceDataStoreName = "Settings" - val Context.dataStore by preferencesDataStore(preferenceDataStoreName) - } - - private val AUTHDATA = stringPreferencesKey("authData") - private val EMAIL = stringPreferencesKey("email") - private val OAUTHTOKEN = stringPreferencesKey("oauthtoken") - private val AASTOKEN = stringPreferencesKey("aasToken") - private val USERTYPE = stringPreferencesKey("userType") - private val TOCSTATUS = booleanPreferencesKey("tocStatus") - private val TOSVERSION = stringPreferencesKey("tosversion") - - val authData = context.dataStore.data.map { it[AUTHDATA] ?: "" } - val emailData = context.dataStore.data.map { it[EMAIL] ?: "" } - val oauthToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" } - val aasToken = context.dataStore.data.map { it[AASTOKEN] ?: "" } - val userType = context.dataStore.data.map { it[USERTYPE] ?: "" } - val tocStatus = context.dataStore.data.map { it[TOCSTATUS] ?: false } - val tosVersion = context.dataStore.data.map { it[TOSVERSION] ?: "" } - - /** - * Allows to save gplay API token data into datastore - */ - suspend fun saveAuthData(authData: AuthData?) { - context.dataStore.edit { - if (authData == null) it.remove(AUTHDATA) - else it[AUTHDATA] = gson.toJson(authData) - } - } - - /** - * Destroy auth credentials if they are no longer valid. - * - * Modification for issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 - * Previously this method would also remove [USERTYPE]. - * To clear this value, call [saveUserType] with null. - */ - suspend fun destroyCredentials() { - context.dataStore.edit { - it.remove(AUTHDATA) - it.remove(EMAIL) - it.remove(OAUTHTOKEN) - it.remove(AASTOKEN) - } - } - - /** - * TOC status - */ - suspend fun saveTOCStatus(status: Boolean, tosVersion: String) { - context.dataStore.edit { - it[TOCSTATUS] = status - it[TOSVERSION] = tosVersion - } - } - - /** - * User auth type - */ - suspend fun saveUserType(user: User?) { - context.dataStore.edit { - if (user == null) it.remove(USERTYPE) - else it[USERTYPE] = user.name - } - } - - fun getAuthDataSync(): String { - return runBlocking { - authData.first() - } - } - - fun getEmail(): String { - return runBlocking { - emailData.first() - } - } - - fun getUserType(): User { - return runBlocking { - userType.first().run { - val userStrings = User.values().map { it.name } - if (this !in userStrings) User.UNAVAILABLE - else User.valueOf(this) - } - } - } - - fun getAASToken(): String { - return runBlocking { - aasToken.first() - } - } - - suspend fun saveAasToken(aasToken: String) { - context.dataStore.edit { - it[AASTOKEN] = aasToken - } - } - - suspend fun saveGoogleLogin(email: String, token: String) { - context.dataStore.edit { - it[EMAIL] = email - it[OAUTHTOKEN] = token - } - } -} +/* + * Apps Quickly and easily install Android apps onto your device! + * Copyright (C) 2021 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.data.preference + +import android.content.Context +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import com.aurora.gplayapi.data.models.AuthData +import com.google.gson.Gson +import dagger.hilt.android.qualifiers.ApplicationContext +import foundation.e.apps.data.enums.User +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.runBlocking +import javax.inject.Inject +import javax.inject.Singleton + +/** + * Difference between [OAUTHTOKEN] and [AASTOKEN]: + * + * These two are used only for Google login, not for Anonymous login. + * OAuthToken is obtained from the Google Login web page, from the cookies. + * This OAuthToken is then used by AC2DMTask in GPlayAPIImpl class + * to generate AasToken. + * + * To get Google Play Store data, we need to create an AuthData instance. + * For Google user, this can only be done using AasToken, not OAuthToken. + * + * Very important: AasToken can be generated only ONCE from one OAuthToken. + * We cannot get AasToken again from the same OAuthToken. Thus it is + * important to safely store the AasToken to regenerate AuthData if needed. + * If AasToken is not stored, user has to logout and login again. + */ + +@Singleton +class DataStoreModule @Inject constructor( + @ApplicationContext + private val context: Context, + private val gson: Gson +) { + + companion object { + private const val preferenceDataStoreName = "Settings" + val Context.dataStore by preferencesDataStore(preferenceDataStoreName) + } + + private val AUTHDATA = stringPreferencesKey("authData") + private val EMAIL = stringPreferencesKey("email") + private val OAUTHTOKEN = stringPreferencesKey("oauthtoken") + private val AASTOKEN = stringPreferencesKey("aasToken") + private val USERTYPE = stringPreferencesKey("userType") + private val TOCSTATUS = booleanPreferencesKey("tocStatus") + private val TOSVERSION = stringPreferencesKey("tosversion") + + val authData = context.dataStore.data.map { it[AUTHDATA] ?: "" } + val emailData = context.dataStore.data.map { it[EMAIL] ?: "" } + val oauthToken = context.dataStore.data.map { it[OAUTHTOKEN] ?: "" } + val aasToken = context.dataStore.data.map { it[AASTOKEN] ?: "" } + val userType = context.dataStore.data.map { it[USERTYPE] ?: "" } + val tocStatus = context.dataStore.data.map { it[TOCSTATUS] ?: false } + val tosVersion = context.dataStore.data.map { it[TOSVERSION] ?: "" } + + /** + * Allows to save gplay API token data into datastore + */ + suspend fun saveAuthData(authData: AuthData?) { + context.dataStore.edit { + if (authData == null) it.remove(AUTHDATA) + else it[AUTHDATA] = gson.toJson(authData) + } + } + + /** + * Destroy auth credentials if they are no longer valid. + * + * Modification for issue: https://gitlab.e.foundation/e/backlog/-/issues/5168 + * Previously this method would also remove [USERTYPE]. + * To clear this value, call [saveUserType] with null. + */ + suspend fun destroyCredentials() { + context.dataStore.edit { + it.remove(AUTHDATA) + it.remove(EMAIL) + it.remove(OAUTHTOKEN) + it.remove(AASTOKEN) + } + } + + /** + * TOC status + */ + suspend fun saveTOCStatus(status: Boolean, tosVersion: String) { + context.dataStore.edit { + it[TOCSTATUS] = status + it[TOSVERSION] = tosVersion + } + } + + /** + * User auth type + */ + suspend fun saveUserType(user: User?) { + context.dataStore.edit { + if (user == null) it.remove(USERTYPE) + else it[USERTYPE] = user.name + } + } + + fun getAuthDataSync(): String { + return runBlocking { + authData.first() + } + } + + fun getEmail(): String { + return runBlocking { + emailData.first() + } + } + + fun getUserType(): User { + return runBlocking { + userType.first().run { + val userStrings = User.values().map { it.name } + if (this !in userStrings) User.UNAVAILABLE + else User.valueOf(this) + } + } + } + + fun getAASToken(): String { + return runBlocking { + aasToken.first() + } + } + + suspend fun saveAasToken(aasToken: String) { + context.dataStore.edit { + it[AASTOKEN] = aasToken + } + } + + suspend fun saveGoogleLogin(email: String, token: String) { + context.dataStore.edit { + it[EMAIL] = email + it[OAUTHTOKEN] = token + } + } +} -- GitLab From 74663988f3be38904db406c981aa6ce96f4c99fd Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Fri, 8 Dec 2023 18:14:32 +0530 Subject: [PATCH 6/8] use extension function getSync() --- .../apps/data/login/PlayStoreAuthenticator.kt | 9 +++++---- .../apps/data/login/api/GoogleLoginManager.kt | 5 +++-- .../e/apps/data/preference/DataStoreManager.kt | 7 ++----- .../e/apps/data/preference/DataStoreModule.kt | 18 ------------------ .../e/apps/receivers/DumpAuthData.kt | 3 ++- .../e/apps/ui/MainActivityViewModel.kt | 3 ++- .../signin/LocaleChangedBroadcastReceiver.kt | 3 ++- .../java/foundation/e/apps/utils/Extensions.kt | 9 +++++++++ 8 files changed, 25 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index af6ef1993..8d53afc64 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -31,6 +31,7 @@ import foundation.e.apps.data.login.api.PlayStoreLoginWrapper import foundation.e.apps.data.preference.DataStoreModule import foundation.e.apps.data.preference.PreferenceManagerModule import foundation.e.apps.data.retryWithBackoff +import foundation.e.apps.utils.getSync import timber.log.Timber import java.util.Locale import javax.inject.Inject @@ -118,7 +119,7 @@ class PlayStoreAuthenticator @Inject constructor( * Returns null if nothing is saved. */ private fun getSavedAuthData(): AuthData? { - val authJson = dataStoreModule.getAuthDataSync() + val authJson = dataStoreModule.authData.getSync() return if (authJson.isBlank()) null else try { gson.fromJson(authJson, AuthData::class.java) @@ -161,9 +162,9 @@ class PlayStoreAuthenticator @Inject constructor( private suspend fun getAuthDataWithGoogleAccount(): ResultSupreme { - val email = dataStoreModule.getEmail() - val oauthToken = dataStoreModule.oauthToken.first() - val aasToken = dataStoreModule.getAASToken() + val email = dataStoreModule.emailData.getSync() + val oauthToken = dataStoreModule.oauthToken.getSync() + val aasToken = dataStoreModule.aasToken.getSync() /* * If aasToken is not blank, means it was stored successfully from a previous Google login. * Use it to fetch auth data. diff --git a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt index 352bd253a..1b6a5e441 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt @@ -24,6 +24,7 @@ import foundation.e.apps.data.playstore.utils.AC2DMTask import foundation.e.apps.data.playstore.utils.CustomAuthValidator import foundation.e.apps.data.playstore.utils.GPlayHttpClient import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.utils.getSync import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.util.Properties @@ -58,8 +59,8 @@ class GoogleLoginManager( * @return authData: authentication data */ override suspend fun login(): AuthData? { - val email = dataStoreModule.getEmail() - val aasToken = dataStoreModule.getAASToken() + val email = dataStoreModule.emailData.getSync() + val aasToken = dataStoreModule.aasToken.getSync() var authData: AuthData? withContext(Dispatchers.IO) { diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt index 8bd36cbfe..2e108b711 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt @@ -20,6 +20,7 @@ package foundation.e.apps.data.preference import com.aurora.gplayapi.data.models.AuthData import com.google.gson.Gson import foundation.e.apps.data.enums.User +import foundation.e.apps.utils.getSync import javax.inject.Inject import javax.inject.Singleton @@ -32,15 +33,11 @@ class DataStoreManager @Inject constructor() { lateinit var gson: Gson fun getAuthData(): AuthData { - val authDataJson = dataStoreModule.getAuthDataSync() + val authDataJson = dataStoreModule.authData.getSync() return gson.fromJson(authDataJson, AuthData::class.java) ?: AuthData("", "") } fun getUserType(): User { return dataStoreModule.getUserType() } - - fun getAuthDataJson(): String { - return dataStoreModule.getAuthDataSync() - } } diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index e6957f6be..5a28d615b 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -124,18 +124,6 @@ class DataStoreModule @Inject constructor( } } - fun getAuthDataSync(): String { - return runBlocking { - authData.first() - } - } - - fun getEmail(): String { - return runBlocking { - emailData.first() - } - } - fun getUserType(): User { return runBlocking { userType.first().run { @@ -146,12 +134,6 @@ class DataStoreModule @Inject constructor( } } - fun getAASToken(): String { - return runBlocking { - aasToken.first() - } - } - suspend fun saveAasToken(aasToken: String) { context.dataStore.edit { it[AASTOKEN] = aasToken diff --git a/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt b/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt index 2e23ddc7b..ea7fc9de9 100644 --- a/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt +++ b/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt @@ -24,6 +24,7 @@ import com.google.gson.Gson import foundation.e.apps.data.Constants.ACTION_AUTHDATA_DUMP import foundation.e.apps.data.Constants.TAG_AUTHDATA_DUMP import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.utils.getSync import org.json.JSONObject import timber.log.Timber @@ -47,7 +48,7 @@ class DumpAuthData : BroadcastReceiver() { private fun getAuthDataDump(context: Context): String { val gson = Gson() // TODO: replace with context.configuration - val authData = DataStoreModule(context, gson).getAuthDataSync().let { + val authData = DataStoreModule(context, gson).authData.getSync().let { gson.fromJson(it, AuthData::class.java) } val filteredData = JSONObject().apply { diff --git a/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt index d110ba5c7..30134c8c4 100644 --- a/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt @@ -49,6 +49,7 @@ import foundation.e.apps.data.preference.DataStoreModule import foundation.e.apps.install.pkg.PWAManagerModule import foundation.e.apps.install.pkg.PkgManagerModule import foundation.e.apps.install.workmanager.AppInstallProcessor +import foundation.e.apps.utils.getSync import kotlinx.coroutines.channels.ProducerScope import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow @@ -91,7 +92,7 @@ class MainActivityViewModel @Inject constructor( } fun getUserEmail(): String { - return dataStoreModule.getEmail() + return dataStoreModule.emailData.getSync() } fun uploadFaultyTokenToEcloud(email: String, description: String = "") { diff --git a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt index f3e0f2d5a..0dd3b1e50 100644 --- a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt +++ b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt @@ -25,6 +25,7 @@ import com.aurora.gplayapi.data.models.AuthData import com.google.gson.Gson import dagger.hilt.android.AndroidEntryPoint import foundation.e.apps.data.preference.DataStoreModule +import foundation.e.apps.utils.getSync import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -52,7 +53,7 @@ class LocaleChangedBroadcastReceiver : BroadcastReceiver() { } GlobalScope.launch { try { - val authDataJson = dataStoreModule.getAuthDataSync() + val authDataJson = dataStoreModule.authData.getSync() val authData = gson.fromJson(authDataJson, AuthData::class.java) authData.locale = context.resources.configuration.locales[0] dataStoreModule.saveAuthData(authData) diff --git a/app/src/main/java/foundation/e/apps/utils/Extensions.kt b/app/src/main/java/foundation/e/apps/utils/Extensions.kt index aa763f6a8..63266b000 100644 --- a/app/src/main/java/foundation/e/apps/utils/Extensions.kt +++ b/app/src/main/java/foundation/e/apps/utils/Extensions.kt @@ -8,6 +8,9 @@ import foundation.e.apps.R import java.text.SimpleDateFormat import java.util.Date import java.util.Locale +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking fun Date.getFormattedString(format: String, locale: Locale = Locale.getDefault()): String { val dateFormat = SimpleDateFormat(format, locale) @@ -42,3 +45,9 @@ fun Context.isNetworkAvailable(): Boolean { return false } + +fun Flow.getSync(): String { + return runBlocking { + this@getSync.first() + } +} \ No newline at end of file -- GitLab From 42c6247738ad30f528e6c290d3c2f776f0225066 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Fri, 8 Dec 2023 18:32:27 +0530 Subject: [PATCH 7/8] add line break --- app/src/main/java/foundation/e/apps/utils/Extensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/apps/utils/Extensions.kt b/app/src/main/java/foundation/e/apps/utils/Extensions.kt index 63266b000..262647281 100644 --- a/app/src/main/java/foundation/e/apps/utils/Extensions.kt +++ b/app/src/main/java/foundation/e/apps/utils/Extensions.kt @@ -50,4 +50,4 @@ fun Flow.getSync(): String { return runBlocking { this@getSync.first() } -} \ No newline at end of file +} -- GitLab From a3263699b287d28e882c55107462bfa62af0de0b Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Fri, 8 Dec 2023 18:46:15 +0530 Subject: [PATCH 8/8] move getSync() to DataStoreModule --- .../e/apps/data/login/PlayStoreAuthenticator.kt | 3 +-- .../e/apps/data/login/api/GoogleLoginManager.kt | 2 +- .../e/apps/data/preference/DataStoreManager.kt | 1 - .../foundation/e/apps/data/preference/DataStoreModule.kt | 7 +++++++ .../java/foundation/e/apps/receivers/DumpAuthData.kt | 2 +- .../java/foundation/e/apps/ui/MainActivityViewModel.kt | 2 +- .../ui/setup/signin/LocaleChangedBroadcastReceiver.kt | 2 +- app/src/main/java/foundation/e/apps/utils/Extensions.kt | 9 --------- 8 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt index 8d53afc64..5f3038604 100644 --- a/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt +++ b/app/src/main/java/foundation/e/apps/data/login/PlayStoreAuthenticator.kt @@ -31,12 +31,11 @@ import foundation.e.apps.data.login.api.PlayStoreLoginWrapper import foundation.e.apps.data.preference.DataStoreModule import foundation.e.apps.data.preference.PreferenceManagerModule import foundation.e.apps.data.retryWithBackoff -import foundation.e.apps.utils.getSync +import foundation.e.apps.data.preference.getSync import timber.log.Timber import java.util.Locale import javax.inject.Inject import javax.inject.Singleton -import kotlinx.coroutines.flow.first /** * Class to get GPlay auth data. Call [login] to get an already saved auth data diff --git a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt index 1b6a5e441..d99fdb7f9 100644 --- a/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt +++ b/app/src/main/java/foundation/e/apps/data/login/api/GoogleLoginManager.kt @@ -24,7 +24,7 @@ import foundation.e.apps.data.playstore.utils.AC2DMTask import foundation.e.apps.data.playstore.utils.CustomAuthValidator import foundation.e.apps.data.playstore.utils.GPlayHttpClient import foundation.e.apps.data.preference.DataStoreModule -import foundation.e.apps.utils.getSync +import foundation.e.apps.data.preference.getSync import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.util.Properties diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt index 2e108b711..240d0716c 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreManager.kt @@ -20,7 +20,6 @@ package foundation.e.apps.data.preference import com.aurora.gplayapi.data.models.AuthData import com.google.gson.Gson import foundation.e.apps.data.enums.User -import foundation.e.apps.utils.getSync import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt index 5a28d615b..7f042d00d 100644 --- a/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt +++ b/app/src/main/java/foundation/e/apps/data/preference/DataStoreModule.kt @@ -32,6 +32,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking import javax.inject.Inject import javax.inject.Singleton +import kotlinx.coroutines.flow.Flow /** * Difference between [OAUTHTOKEN] and [AASTOKEN]: @@ -147,3 +148,9 @@ class DataStoreModule @Inject constructor( } } } + +fun Flow.getSync(): String { + return runBlocking { + this@getSync.first() + } +} diff --git a/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt b/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt index ea7fc9de9..02b8842ff 100644 --- a/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt +++ b/app/src/main/java/foundation/e/apps/receivers/DumpAuthData.kt @@ -24,7 +24,7 @@ import com.google.gson.Gson import foundation.e.apps.data.Constants.ACTION_AUTHDATA_DUMP import foundation.e.apps.data.Constants.TAG_AUTHDATA_DUMP import foundation.e.apps.data.preference.DataStoreModule -import foundation.e.apps.utils.getSync +import foundation.e.apps.data.preference.getSync import org.json.JSONObject import timber.log.Timber diff --git a/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt index 30134c8c4..51a52c965 100644 --- a/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/MainActivityViewModel.kt @@ -49,7 +49,7 @@ import foundation.e.apps.data.preference.DataStoreModule import foundation.e.apps.install.pkg.PWAManagerModule import foundation.e.apps.install.pkg.PkgManagerModule import foundation.e.apps.install.workmanager.AppInstallProcessor -import foundation.e.apps.utils.getSync +import foundation.e.apps.data.preference.getSync import kotlinx.coroutines.channels.ProducerScope import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow diff --git a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt index 0dd3b1e50..f659e9183 100644 --- a/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt +++ b/app/src/main/java/foundation/e/apps/ui/setup/signin/LocaleChangedBroadcastReceiver.kt @@ -25,7 +25,7 @@ import com.aurora.gplayapi.data.models.AuthData import com.google.gson.Gson import dagger.hilt.android.AndroidEntryPoint import foundation.e.apps.data.preference.DataStoreModule -import foundation.e.apps.utils.getSync +import foundation.e.apps.data.preference.getSync import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope diff --git a/app/src/main/java/foundation/e/apps/utils/Extensions.kt b/app/src/main/java/foundation/e/apps/utils/Extensions.kt index 262647281..aa763f6a8 100644 --- a/app/src/main/java/foundation/e/apps/utils/Extensions.kt +++ b/app/src/main/java/foundation/e/apps/utils/Extensions.kt @@ -8,9 +8,6 @@ import foundation.e.apps.R import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.runBlocking fun Date.getFormattedString(format: String, locale: Locale = Locale.getDefault()): String { val dateFormat = SimpleDateFormat(format, locale) @@ -45,9 +42,3 @@ fun Context.isNetworkAvailable(): Boolean { return false } - -fun Flow.getSync(): String { - return runBlocking { - this@getSync.first() - } -} -- GitLab