forked from organicmaps/organicmaps
android: sync language
This commit is contained in:
parent
661807cde8
commit
451d628bfd
3 changed files with 38 additions and 3 deletions
|
@ -38,6 +38,10 @@ class UserPreferences(context: Context) {
|
|||
|
||||
fun getUserId() = sharedPref.getString("user_id", "")
|
||||
fun setUserId(value: String?) = sharedPref.edit { putString("user_id", value) }
|
||||
|
||||
fun getShouldSyncLanguage() = sharedPref.getBoolean("should_sync_language", false)
|
||||
fun setShouldSyncLanguage(value: Boolean) =
|
||||
sharedPref.edit { putBoolean("should_sync_language", value) }
|
||||
}
|
||||
|
||||
data class Language(val code: String, val name: String)
|
||||
|
|
|
@ -5,7 +5,9 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.net.NetworkInfo
|
||||
import android.net.wifi.WifiManager
|
||||
import app.tourism.data.prefs.UserPreferences
|
||||
import app.tourism.data.repositories.PlacesRepository
|
||||
import app.tourism.data.repositories.ProfileRepository
|
||||
import app.tourism.data.repositories.ReviewsRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
@ -16,8 +18,17 @@ import javax.inject.Inject
|
|||
|
||||
@AndroidEntryPoint
|
||||
class WifiReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var reviewsRepository: ReviewsRepository
|
||||
@Inject lateinit var placesRepository: PlacesRepository
|
||||
@Inject
|
||||
lateinit var reviewsRepository: ReviewsRepository
|
||||
|
||||
@Inject
|
||||
lateinit var placesRepository: PlacesRepository
|
||||
|
||||
@Inject
|
||||
lateinit var profileRepository: ProfileRepository
|
||||
|
||||
@Inject
|
||||
lateinit var userPreferences: UserPreferences
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val info: NetworkInfo? = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)
|
||||
|
@ -27,7 +38,9 @@ class WifiReceiver : BroadcastReceiver() {
|
|||
delay(2000L) // to avoid errors
|
||||
CoroutineScope(Dispatchers.IO).launch { reviewsRepository.syncReviews() }
|
||||
CoroutineScope(Dispatchers.IO).launch { placesRepository.syncFavorites() }
|
||||
CoroutineScope(Dispatchers.IO).launch { profileRepository.syncLanguageIfNecessary() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import app.tourism.data.dto.profile.ThemeDto
|
|||
import app.tourism.data.prefs.UserPreferences
|
||||
import app.tourism.data.remote.TourismApi
|
||||
import app.tourism.data.remote.handleGenericCall
|
||||
import app.tourism.data.remote.handleResponse
|
||||
import app.tourism.data.remote.toFormDataRequestBody
|
||||
import app.tourism.domain.models.profile.PersonalData
|
||||
import app.tourism.domain.models.resource.Resource
|
||||
|
@ -67,12 +68,29 @@ class ProfileRepository(
|
|||
|
||||
suspend fun updateLanguage(code: String) {
|
||||
try {
|
||||
api.updateLanguage(language = LanguageDto(code))
|
||||
val resource = handleResponse(
|
||||
call = { api.updateLanguage(language = LanguageDto(code)) },
|
||||
context
|
||||
)
|
||||
if (resource is Resource.Success) {
|
||||
userPreferences.setShouldSyncLanguage(false)
|
||||
} else if (resource is Resource.Error) {
|
||||
userPreferences.setShouldSyncLanguage(true)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
userPreferences.setShouldSyncLanguage(true)
|
||||
println(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun syncLanguageIfNecessary() {
|
||||
userPreferences.getLanguage()?.code?.let {
|
||||
if (userPreferences.getShouldSyncLanguage()) {
|
||||
updateLanguage(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateTheme(code: String) {
|
||||
try {
|
||||
api.updateTheme(theme = ThemeDto(code))
|
||||
|
|
Loading…
Add table
Reference in a new issue