/
akrilly
/
Firebase-Remote-Config
Обзор
Документация
Войти
/
akrilly
/
Firebase-Remote-Config
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/src/main/java/note/internship/firebasepetproject/MainActivity.kt
121 строка
4 KB
Ясунов Владислав
Init commit
07 апр 2026, 16:29
07 апр 2026, 16:29
454e7eb
Код
Авторство
О чём код?
package note.internship.firebasepetproject import android.os.Bundle import android.text.Layout import android.util.Log import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Text import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment.Companion import androidx.compose.ui.Modifier import androidx.compose.ui.text.style.LineHeightStyle import com.google.firebase.Firebase import com.google.firebase.remoteconfig.ConfigUpdate import com.google.firebase.remoteconfig.ConfigUpdateListener import com.google.firebase.remoteconfig.FirebaseRemoteConfig import com.google.firebase.remoteconfig.FirebaseRemoteConfigException import com.google.firebase.remoteconfig.get import com.google.firebase.remoteconfig.remoteConfig import com.google.firebase.remoteconfig.remoteConfigSettings import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import note.internship.firebasepetproject.ui.theme.FirebasePetProjectTheme class MainActivity : ComponentActivity() { private var remoteConfig: FirebaseRemoteConfig? = null private val text: MutableStateFlow<String> = MutableStateFlow("no message") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setUpFirebase() fetchWelcome() setContent { val welcomeMessage by text.collectAsState() FirebasePetProjectTheme { Box( contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize(), ) { Text(text = welcomeMessage, modifier = Modifier.align(Alignment.Center)) } } } } private fun setUpFirebase() { remoteConfig = Firebase.remoteConfig val configSettings = remoteConfigSettings { minimumFetchIntervalInSeconds = FETCH_MINIMUM_INTERVAL_IN_SEC } remoteConfig ?.setDefaultsAsync(/* resourceId = */ R.xml.remote_config_defaults) ?.addOnCompleteListener { updateWelcomeText() } remoteConfig?.setConfigSettingsAsync(/* settings = */ configSettings) remoteConfig?.addOnConfigUpdateListener(object : ConfigUpdateListener { override fun onUpdate(configUpdate: ConfigUpdate) { Log.d(/* tag = */ TAG, /* msg = */ "Updated keys: " + configUpdate.updatedKeys) if (configUpdate.updatedKeys.contains(WELCOME_MESSAGE_KEY)) { remoteConfig?.activate()?.addOnCompleteListener { updateWelcomeText() } } } override fun onError(error: FirebaseRemoteConfigException) { Log.w(/* tag = */ TAG, /* msg = */ "Config update error with code: " + error.code, /* tr = */ error) } }) } private fun fetchWelcome() { remoteConfig?.fetchAndActivate() ?.addOnCompleteListener(this@MainActivity) { task -> if (task.isSuccessful) { val updated = task.result Log.d(/* tag = */ TAG, /* msg = */ "Config params updated: $updated") Toast.makeText( /* context = */ this.baseContext!!, /* text = */ "Fetch and activate succeeded", /* duration = */ Toast.LENGTH_SHORT, ).show() } else { Toast.makeText( /* context = */ this.baseContext!!, /* text = */ "Fetch failed", /* duration = */ Toast.LENGTH_SHORT, ).show() } } } private fun updateWelcomeText() { // Безопасно получаем строку из конфига (вернет дефолтную или серверную) val message = remoteConfig?.getString(WELCOME_MESSAGE_KEY) ?: "no message" text.value = message } companion object { private const val TAG = "MainActivity" // Remote Config keys private const val FETCH_MINIMUM_INTERVAL_IN_SEC = 3600L private const val WELCOME_MESSAGE_KEY = "welcome_message" } }