Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86c9e63a authored by Hasib Prince's avatar Hasib Prince
Browse files

App Lounge: update user data on Locale change

Update locale of authdata and clear cache of Httpclient on Locale change
parent e4e123fa
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -82,6 +82,12 @@
            </intent-filter>
        </receiver>

        <receiver android:name=".setup.signin.LocaleChangedBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED"/>
            </intent-filter>
        </receiver>

        <!-- If you want to disable android.startup completely. -->
        <provider
            android:name="androidx.startup.InitializationProvider"
+4 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

package foundation.e.apps.api.gplay

import android.content.Context
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.App
import com.aurora.gplayapi.data.models.AuthData
@@ -33,6 +34,7 @@ import com.aurora.gplayapi.helpers.PurchaseHelper
import com.aurora.gplayapi.helpers.SearchHelper
import com.aurora.gplayapi.helpers.StreamHelper
import com.aurora.gplayapi.helpers.TopChartsHelper
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.api.gplay.token.TokenRepository
import foundation.e.apps.api.gplay.utils.GPlayHttpClient
import foundation.e.apps.utils.modules.DataStoreModule
@@ -40,10 +42,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.withContext
import java.util.Locale
import javax.inject.Inject

class GPlayAPIImpl @Inject constructor(
    @ApplicationContext private val context: Context,
    private val tokenRepository: TokenRepository,
    private val dataStoreModule: DataStoreModule,
    private val gPlayHttpClient: GPlayHttpClient
@@ -53,7 +55,7 @@ class GPlayAPIImpl @Inject constructor(
    suspend fun fetchAuthData() = withContext(Dispatchers.IO) {
        val data = async { tokenRepository.getAuthData() }
        data.await()?.let {
            it.locale = Locale.getDefault() // update locale with the default locale from settings
            it.locale = context.resources.configuration.locales[0] // update locale with the default locale from settings
            dataStoreModule.saveCredentials(it)
        }
    }
+58 −0
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 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 <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.setup.signin

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.aurora.gplayapi.data.models.AuthData
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.utils.modules.DataStoreModule
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.Cache
import javax.inject.Inject

@AndroidEntryPoint
@DelicateCoroutinesApi
class LocaleChangedBroadcastReceiver : BroadcastReceiver() {

    @Inject
    lateinit var dataStoreModule: DataStoreModule
    @Inject
    lateinit var gson: Gson
    @Inject
    lateinit var cache: Cache

    override fun onReceive(context: Context, intent: Intent) {
        GlobalScope.launch {
            val authDataJson = dataStoreModule.getAuthDataSync()
            val authData = gson.fromJson(authDataJson, AuthData::class.java)
            authData.locale = context.resources.configuration.locales[0]
            dataStoreModule.saveCredentials(authData)
            withContext(Dispatchers.IO) {
                cache.evictAll()
            }
        }
    }
}