Skip to content

Compose workspace config #1450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ configurations {
}

dependencies {
ktlint "com.github.shyiko:ktlint:0.31.0"
ktlint ("com.pinterest:ktlint:0.48.2") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
}
}
}

task("ktlint", type: JavaExec, group: "verification") {
Expand All @@ -75,7 +79,7 @@ task("ktlint", type: JavaExec, group: "verification") {

description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
mainClass.set("com.pinterest.ktlint.Main")
args = [
"--format",
"--android",
Expand Down
42 changes: 37 additions & 5 deletions config/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,42 @@ android {

defaultConfig {
applicationId "com.google.samples.quickstart.config"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
minifyEnabled true
minifyEnabled = true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

buildFeatures {
viewBinding = true
compose = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}

Expand All @@ -37,6 +56,7 @@ dependencies {
implementation project(":internal:chooserx")

implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'

// Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom)
implementation platform('com.google.firebase:firebase-bom:31.1.0')
Expand All @@ -50,9 +70,21 @@ dependencies {
// For an optimal experience using Remote Config, add the Firebase SDK
// for Google Analytics. This is recommended, but not required.
implementation 'com.google.firebase:firebase-analytics'
implementation 'androidx.compose.material:material:1.3.1'

debugImplementation "androidx.fragment:fragment-testing:1.5.4"
// Jetpack Compose
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.activity:activity-compose:1.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
17 changes: 10 additions & 7 deletions config/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

android:theme="@style/AppTheme">
<activity
android:name=".kotlin.MainComposeActivity"
android:exported="false"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
<activity
android:name=".java.MainActivity"
android:label="@string/app_name" />

<activity
android:name=".kotlin.MainActivity"
android:label="@string/app_name" />

<activity
android:name=".EntryChoiceActivity"
android:label="@string/app_name"
android:exported="true">
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -27,4 +30,4 @@
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ class EntryChoiceActivity : BaseEntryChoiceActivity() {

override fun getChoices(): List<Choice> {
return listOf(
Choice(
"Java",
"Run the Firebase Remote Config quickstart written in Java.",
Intent(
this,
com.google.samples.quickstart.config.java.MainActivity::class.java)),
Choice(
"Kotlin",
"Run the Firebase Remote Config quickstart written in Kotlin.",
Intent(
this,
com.google.samples.quickstart.config.kotlin.MainActivity::class.java))
Choice(
"Java",
"Run the Firebase Remote Config quickstart written in Java.",
Intent(
this,
com.google.samples.quickstart.config.java.MainActivity::class.java
)
),
Choice(
"Kotlin",
"Run the Firebase Remote Config quickstart written in Kotlin.",
Intent(
this,
com.google.samples.quickstart.config.kotlin.MainActivity::class.java
)
),
Choice(
"Compose",
"Run the Firebase Remote Config quickstart written in Compose.",
Intent(
this,
com.google.samples.quickstart.config.kotlin.MainComposeActivity::class.java
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,21 @@ public void onClick(View v) {
});

// Get Remote Config instance.
// [START get_remote_config_instance]
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
// [END get_remote_config_instance]

// Create a Remote Config Setting to enable developer mode, which you can use to increase
// the number of fetches available per hour during development. Also use Remote Config
// Setting to set the minimum fetch interval.
// [START enable_dev_mode]
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
// [END enable_dev_mode]

// Set default Remote Config parameter values. An app uses the in-app default values, and
// when you need to adjust those defaults, you set an updated value for only the values you
// want to change in the Firebase console. See Best Practices in the README for more
// information.
// [START set_default_values]
mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
// [END set_default_values]

fetchWelcome();
}
Expand All @@ -96,7 +90,6 @@ public void onClick(View v) {
private void fetchWelcome() {
mWelcomeTextView.setText(mFirebaseRemoteConfig.getString(LOADING_PHRASE_CONFIG_KEY));

// [START fetch_config_with_callback]
mFirebaseRemoteConfig.fetchAndActivate()
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
@Override
Expand All @@ -114,24 +107,19 @@ public void onComplete(@NonNull Task<Boolean> task) {
displayWelcomeMessage();
}
});
// [END fetch_config_with_callback]
}

/**
* Display a welcome message in all caps if welcome_message_caps is set to true. Otherwise,
* display a welcome message as fetched from welcome_message.
*/
// [START display_welcome_message]
private void displayWelcomeMessage() {
// [START get_config_values]
String welcomeMessage = mFirebaseRemoteConfig.getString(WELCOME_MESSAGE_KEY);
// [END get_config_values]
if (mFirebaseRemoteConfig.getBoolean(WELCOME_MESSAGE_CAPS_KEY)) {
mWelcomeTextView.setAllCaps(true);
} else {
mWelcomeTextView.setAllCaps(false);
}
mWelcomeTextView.setText(welcomeMessage);
}
// [END display_welcome_message]
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,38 @@
package com.google.samples.quickstart.config.kotlin

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.get
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import androidx.lifecycle.lifecycleScope
import com.google.samples.quickstart.config.R
import com.google.samples.quickstart.config.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

private lateinit var remoteConfig: FirebaseRemoteConfig
private lateinit var binding: ActivityMainBinding
private val viewModel: RemoteConfigViewModel by viewModels { RemoteConfigViewModel.Factory }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.fetchButton.setOnClickListener { fetchWelcome() }

// Get Remote Config instance.
// [START get_remote_config_instance]
remoteConfig = Firebase.remoteConfig
// [END get_remote_config_instance]
binding.fetchButton.setOnClickListener { viewModel.fetchRemoteConfig() }

// Create a Remote Config Setting to enable developer mode, which you can use to increase
// the number of fetches available per hour during development. Also use Remote Config
// Setting to set the minimum fetch interval.
// [START enable_dev_mode]
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
// [END enable_dev_mode]
viewModel.enableDeveloperMode()

// Set default Remote Config parameter values. An app uses the in-app default values, and
// when you need to adjust those defaults, you set an updated value for only the values you
// want to change in the Firebase console. See Best Practices in the README for more
// information.
// [START set_default_values]
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
// [END set_default_values]
viewModel.setDefaultValues(R.xml.remote_config_defaults)

fetchWelcome()
}

/**
* Fetch a welcome message from the Remote Config service, and then activate it.
*/
private fun fetchWelcome() {
binding.welcomeTextView.text = remoteConfig[LOADING_PHRASE_CONFIG_KEY].asString()

// [START fetch_config_with_callback]
remoteConfig.fetchAndActivate()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val updated = task.result
Log.d(TAG, "Config params updated: $updated")
Toast.makeText(this, "Fetch and activate succeeded",
Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Fetch failed",
Toast.LENGTH_SHORT).show()
}
displayWelcomeMessage()
}
// [END fetch_config_with_callback]
}

/**
* Display a welcome message in all caps if welcome_message_caps is set to true. Otherwise,
* display a welcome message as fetched from welcome_message.
*/
// [START display_welcome_message]
private fun displayWelcomeMessage() {
// [START get_config_values]
val welcomeMessage = remoteConfig[WELCOME_MESSAGE_KEY].asString()
// [END get_config_values]
binding.welcomeTextView.isAllCaps = remoteConfig[WELCOME_MESSAGE_CAPS_KEY].asBoolean()
binding.welcomeTextView.text = welcomeMessage
}

companion object {

private const val TAG = "MainActivity"
lifecycleScope.launch {
viewModel.welcomeMessage.collect { welcomeMessage ->
binding.welcomeTextView.text = welcomeMessage
}
}

// Remote Config keys
private const val LOADING_PHRASE_CONFIG_KEY = "loading_phrase"
private const val WELCOME_MESSAGE_KEY = "welcome_message"
private const val WELCOME_MESSAGE_CAPS_KEY = "welcome_message_caps"
lifecycleScope.launch {
viewModel.allCaps.collect { isWelcomeAllCaps ->
binding.welcomeTextView.isAllCaps = isWelcomeAllCaps
}
}
}
// [END display_welcome_message]
}
Loading