Skip to content
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

ASAA- 122 - Migrate Launchpad compose #1

Merged
merged 10 commits into from
Dec 11, 2023
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl
androidLibrary = { id = "com.android.library", version.ref = "agp" }
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kt-lint-gradle = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "kt-lint-gradle" }
3 changes: 3 additions & 0 deletions kmp-launchpad-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.jetbrains.compose)
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.kt.lint.gradle)
`maven-publish`
}
Expand Down Expand Up @@ -44,6 +45,8 @@ kotlin {
implementation(compose.ui)
}
commonTest.dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(libs.kotlin.test)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bottlerocketstudios.launchpad.compose.util

import java.text.NumberFormat
import java.util.Currency
import java.util.Locale

actual fun Double?.toCurrency(): String {
val numberFormat = NumberFormat.getCurrencyInstance()
numberFormat.currency = Currency.getInstance(Locale.getDefault())
return numberFormat.format(this ?: 0.0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bottlerocketstudios.launchpad.compose

import com.bottlerocketstudios.launchpad.compose.util.toCurrency
import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertEquals


class CurrencyUtilsTest {

@Test
fun `test double toCurrency`() {
Locale.setDefault(Locale.US)

val input = 1234.56
val expectedOutput = "$1,234.56"
val actualOutput = input.toCurrency()

assertEquals(expectedOutput, actualOutput)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
@file:Suppress("ConstructorParameterNaming", "MatchingDeclarationName", "LongParameterList")

@file:Suppress(
"ConstructorParameterNaming",
"MatchingDeclarationName",
"LongParameterList",
"unused"
)
package com.bottlerocketstudios.launchpad.compose.resources

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

Expand Down Expand Up @@ -74,6 +82,8 @@ val smallDimensions = Dimensions(
plane_5 = 12.dp
)

const val SMALL_SCREEN_WIDTH_DP = 360

val sw360Dimensions = Dimensions(
grid_0_25 = 2.dp,
grid_0_5 = 4.dp,
Expand Down Expand Up @@ -101,3 +111,16 @@ val sw360Dimensions = Dimensions(
plane_4 = 8.dp,
plane_5 = 16.dp
)

@Composable
fun ProvideDimens(
dimensions: Dimensions,
content: @Composable () -> Unit
) {
val dimensionSet = remember { dimensions }
CompositionLocalProvider(LocalAppDimens provides dimensionSet, content = content)
}

val LocalAppDimens = staticCompositionLocalOf {
sw360Dimensions
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bottlerocketstudios.launchpad.compose.util


/**
* Kotlin Multiplatform function - convertToCurrencyString
*
* This function takes a Double value representing a raw amount of currency,
* and returns a String representing that amount formatted appropriately as
* currency for the current locale of the device running the code.
*
* @param [amount] The raw amount of currency as a Double
* @return A string representing that amount in the appropriate currency
* format for the device's current locale.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicely done!

expect fun Double?.toCurrency(): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:Suppress("unused")

package com.bottlerocketstudios.launchpad.compose.util

import androidx.compose.runtime.mutableStateOf

fun <T> T.asMutableState() = mutableStateOf(this)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bottlerocketstudios.launchpad.compose.util

import platform.Foundation.NSLocale
import platform.Foundation.NSNumber
import platform.Foundation.NSNumberFormatter
import platform.Foundation.NSNumberFormatterCurrencyStyle
import platform.Foundation.currentLocale
import platform.Foundation.numberWithDouble

actual fun Double?.toCurrency(): String {
val formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterCurrencyStyle
formatter.locale = NSLocale.currentLocale()

val number = NSNumber.numberWithDouble(this ?: 0.0)
return formatter.stringFromNumber(number) ?: "NaN"
}