Skip to content

Commit

Permalink
Add runtime support for Compose UI
Browse files Browse the repository at this point in the history
  • Loading branch information
theisenp committed Apr 20, 2023
1 parent 9a32049 commit acd7c11
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
29 changes: 29 additions & 0 deletions runtime-compose-ui/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.mavenPublish)
alias(libs.plugins.dokka)
}

android {
namespace = "app.cash.paraphrase.compose"
compileSdk = 33

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}

defaultConfig {
minSdk = 24
targetSdk = 33
}
}

dependencies {
api(libs.composeUi)
api(projects.runtime)
}
3 changes: 3 additions & 0 deletions runtime-compose-ui/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Maven
POM_ARTIFACT_ID=paraphrase-runtime-compose-ui
POM_NAME=Paraphrase runtime for Compose UI
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 Cash App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paraphrase.compose

import android.icu.text.MessageFormat
import android.icu.util.ULocale
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.res.stringResource
import app.cash.paraphrase.FormattedResource
import java.util.Locale

/**
* Resolves and returns the final formatted version of the given resource in the default locale.
*/
@Composable
@ReadOnlyComposable
fun formattedResource(formattedResource: FormattedResource): String =
MessageFormat(stringResource(formattedResource.id)).format(formattedResource.arguments)

/**
* Resolves and returns the final formatted version of the given resource in the given locale.
*/
@Composable
@ReadOnlyComposable
fun formattedResource(formattedResource: FormattedResource, locale: Locale): String =
MessageFormat(stringResource(formattedResource.id), locale).format(formattedResource.arguments)

/**
* Resolves and returns the final formatted version of the given resource in the given locale.
*/
@Composable
@ReadOnlyComposable
fun formattedResource(formattedResource: FormattedResource, locale: ULocale): String =
MessageFormat(stringResource(formattedResource.id), locale).format(formattedResource.arguments)
1 change: 1 addition & 0 deletions sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ androidComponents {
}

dependencies {
implementation(projects.runtimeComposeUi)
implementation(projects.sample.library)
implementation(libs.androidActivityCompose)
implementation(libs.googleMaterial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.cash.paraphrase.FormattedResource
import app.cash.paraphrase.getString
import app.cash.paraphrase.compose.formattedResource
import app.cash.paraphrase.sample.app.FormattedResources as AppFormattedResources
import app.cash.paraphrase.sample.library.FormattedResources as LibraryFormattedResources
import java.time.LocalDate
Expand Down Expand Up @@ -74,7 +74,7 @@ class MainActivity : ComponentActivity() {
)

Text(
text = resources.getString(sample.resource),
text = formattedResource(sample.resource),
fontSize = 16.sp,
)
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rootProject.name = "paraphrase"
include(
":plugin",
":runtime",
":runtime-compose-ui",
":sample:app",
":sample:library",
":tests",
Expand Down

0 comments on commit acd7c11

Please sign in to comment.