From caf373102ea55d34732b627d2e3e2190a74d54fc Mon Sep 17 00:00:00 2001 From: Syer10 Date: Mon, 1 Aug 2022 22:11:17 -0400 Subject: [PATCH] Add iOS support to the underlying modules --- core/build.gradle.kts | 17 ++++++ .../ca/gosyer/jui/core/io/IosFileSystems.kt | 13 +++++ .../ca/gosyer/jui/core/io/IosSerializable.kt | 9 +++ .../ca/gosyer/jui/core/lang/IosDispatchers.kt | 18 ++++++ .../ca/gosyer/jui/core/lang/IosLocale.kt | 30 ++++++++++ .../jui/core/prefs/PreferenceStoreFactory.kt | 21 +++++++ data/build.gradle.kts | 17 ++++++ .../ca/gosyer/jui/data/base/IosDateHandler.kt | 58 +++++++++++++++++++ domain/build.gradle.kts | 20 +++++++ .../ca/gosyer/jui/domain/DomainComponent.kt | 12 ++++ .../ca/gosyer/jui/domain/server/Engine.kt | 29 ++++++++++ i18n/build.gradle.kts | 2 + 12 files changed, 246 insertions(+) create mode 100644 core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosFileSystems.kt create mode 100644 core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosSerializable.kt create mode 100644 core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosDispatchers.kt create mode 100644 core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosLocale.kt create mode 100644 core/src/iosMain/kotlin/ca/gosyer/jui/core/prefs/PreferenceStoreFactory.kt create mode 100644 data/src/iosMain/kotlin/ca/gosyer/jui/data/base/IosDateHandler.kt create mode 100644 domain/src/iosMain/kotlin/ca/gosyer/jui/domain/DomainComponent.kt create mode 100644 domain/src/iosMain/kotlin/ca/gosyer/jui/domain/server/Engine.kt diff --git a/core/build.gradle.kts b/core/build.gradle.kts index c66e8737..a788fd54 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -24,6 +24,9 @@ kotlin { } } } + iosX64() + iosArm64() + //iosSimulatorArm64() sourceSets { all { @@ -80,6 +83,20 @@ kotlin { } } val androidTest by getting + + val iosMain by creating { + dependsOn(commonMain) + } + val iosTest by creating { + dependsOn(commonTest) + } + + listOf("iosX64Main", "iosArm64Main"/*, "iosSimulatorArm64Main"*/).forEach { + getByName(it).dependsOn(iosMain) + } + listOf("iosX64Test", "iosArm64Test"/*, "iosSimulatorArm64Test"*/).forEach { + getByName(it).dependsOn(iosTest) + } } } diff --git a/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosFileSystems.kt b/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosFileSystems.kt new file mode 100644 index 00000000..c43b8201 --- /dev/null +++ b/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosFileSystems.kt @@ -0,0 +1,13 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.core.io + +import okio.FileSystem + +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +actual val FileSystem.Companion.SYSTEM: FileSystem + get() = SYSTEM diff --git a/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosSerializable.kt b/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosSerializable.kt new file mode 100644 index 00000000..8ef0bc69 --- /dev/null +++ b/core/src/iosMain/kotlin/ca/gosyer/jui/core/io/IosSerializable.kt @@ -0,0 +1,9 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.core.io + +actual interface Serializable diff --git a/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosDispatchers.kt b/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosDispatchers.kt new file mode 100644 index 00000000..94e430a9 --- /dev/null +++ b/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosDispatchers.kt @@ -0,0 +1,18 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.core.lang + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi + +@OptIn(ExperimentalCoroutinesApi::class) +private val ioDispatcher = Dispatchers.Default.limitedParallelism(64) + +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +actual val Dispatchers.IO: CoroutineDispatcher + get() = ioDispatcher diff --git a/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosLocale.kt b/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosLocale.kt new file mode 100644 index 00000000..d6b2a52b --- /dev/null +++ b/core/src/iosMain/kotlin/ca/gosyer/jui/core/lang/IosLocale.kt @@ -0,0 +1,30 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.core.lang + +import androidx.compose.ui.text.intl.Locale +import platform.Foundation.localizedStringForLanguageCode +import platform.Foundation.localizedStringForLocaleIdentifier +import platform.Foundation.NSLocale as PlatformLocale + +fun Locale.toPlatform(): PlatformLocale = PlatformLocale(toLanguageTag()) + +/** + * First Locale: en_IN + * Language: English + */ +actual fun Locale.getDisplayLanguage(displayLocale: Locale): String = toPlatform() + .localizedStringForLanguageCode(displayLocale.toLanguageTag())!! + +/** + * First Locale: en_US + * Language: English (United States) + */ +actual fun Locale.getDisplayName(displayLocale: Locale): String = toPlatform() + .localizedStringForLocaleIdentifier(displayLocale.toLanguageTag()) + +actual val Locale.displayName: String get() = getDisplayLanguage(this) diff --git a/core/src/iosMain/kotlin/ca/gosyer/jui/core/prefs/PreferenceStoreFactory.kt b/core/src/iosMain/kotlin/ca/gosyer/jui/core/prefs/PreferenceStoreFactory.kt new file mode 100644 index 00000000..1b76c830 --- /dev/null +++ b/core/src/iosMain/kotlin/ca/gosyer/jui/core/prefs/PreferenceStoreFactory.kt @@ -0,0 +1,21 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.core.prefs + +import com.russhwolf.settings.NSUserDefaultsSettings +import me.tatarka.inject.annotations.Inject +import platform.Foundation.NSUserDefaults + +actual class PreferenceStoreFactory @Inject constructor() { + actual fun create(vararg names: String): PreferenceStore { + return StandardPreferenceStore( + NSUserDefaultsSettings( + NSUserDefaults.standardUserDefaults + ) + ) + } +} diff --git a/data/build.gradle.kts b/data/build.gradle.kts index 034e40a1..0d9324ba 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -23,6 +23,9 @@ kotlin { } } } + iosX64() + iosArm64() + //iosSimulatorArm64() sourceSets { all { @@ -72,6 +75,20 @@ kotlin { } } val androidTest by getting + + val iosMain by creating { + dependsOn(commonMain) + } + val iosTest by creating { + dependsOn(commonTest) + } + + listOf("iosX64Main", "iosArm64Main"/*, "iosSimulatorArm64Main"*/).forEach { + getByName(it).dependsOn(iosMain) + } + listOf("iosX64Test", "iosArm64Test"/*, "iosSimulatorArm64Test"*/).forEach { + getByName(it).dependsOn(iosTest) + } } } diff --git a/data/src/iosMain/kotlin/ca/gosyer/jui/data/base/IosDateHandler.kt b/data/src/iosMain/kotlin/ca/gosyer/jui/data/base/IosDateHandler.kt new file mode 100644 index 00000000..e7f4109c --- /dev/null +++ b/data/src/iosMain/kotlin/ca/gosyer/jui/data/base/IosDateHandler.kt @@ -0,0 +1,58 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.data.base + +import androidx.compose.ui.text.intl.Locale +import ca.gosyer.jui.core.lang.toPlatform +import kotlinx.datetime.Instant +import kotlinx.datetime.toNSDate +import me.tatarka.inject.annotations.Inject +import platform.Foundation.NSDateFormatter +import platform.Foundation.NSDateFormatterNoStyle +import platform.Foundation.NSDateFormatterShortStyle + +actual class DateHandler @Inject constructor() { + actual val formatOptions by lazy { + listOf( + "", + "MM/dd/yy", + "dd/MM/yy", + "yyyy-MM-dd" + ) + } + + actual fun getDateFormat(format: String): (Instant) -> String = when (format) { + "" -> NSDateFormatter() + .apply { + setDateStyle(NSDateFormatterShortStyle) + setTimeStyle(NSDateFormatterNoStyle) + setLocale(Locale.current.toPlatform()) + } + else -> NSDateFormatter() + .apply { + setDateFormat(format) + } + }.let { formatter -> + { + formatter.stringFromDate(it.toNSDate()) + } + } + + actual val dateTimeFormat: (Instant) -> String by lazy { + NSDateFormatter() + .apply { + setDateStyle(NSDateFormatterShortStyle) + setTimeStyle(NSDateFormatterShortStyle) + setLocale(Locale.current.toPlatform()) + } + .let { formatter -> + { + formatter.stringFromDate(it.toNSDate()) + } + } + } +} diff --git a/domain/build.gradle.kts b/domain/build.gradle.kts index d5090dd4..ff9ae0b4 100644 --- a/domain/build.gradle.kts +++ b/domain/build.gradle.kts @@ -23,6 +23,9 @@ kotlin { } } } + iosX64() + iosArm64() + //iosSimulatorArm64() sourceSets { all { @@ -79,6 +82,23 @@ kotlin { } } val androidTest by getting + + val iosMain by creating { + dependsOn(commonMain) + dependencies { + api(libs.ktor.darwin) + } + } + val iosTest by creating { + dependsOn(commonTest) + } + + listOf("iosX64Main", "iosArm64Main"/*, "iosSimulatorArm64Main"*/).forEach { + getByName(it).dependsOn(iosMain) + } + listOf("iosX64Test", "iosArm64Test"/*, "iosSimulatorArm64Test"*/).forEach { + getByName(it).dependsOn(iosTest) + } } } diff --git a/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/DomainComponent.kt b/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/DomainComponent.kt new file mode 100644 index 00000000..9203173a --- /dev/null +++ b/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/DomainComponent.kt @@ -0,0 +1,12 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain + +actual interface DomainComponent : SharedDomainComponent { + + companion object +} \ No newline at end of file diff --git a/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/server/Engine.kt b/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/server/Engine.kt new file mode 100644 index 00000000..4e237bfc --- /dev/null +++ b/domain/src/iosMain/kotlin/ca/gosyer/jui/domain/server/Engine.kt @@ -0,0 +1,29 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package ca.gosyer.jui.domain.server + +import io.ktor.client.HttpClientConfig +import io.ktor.client.engine.HttpClientEngineConfig +import io.ktor.client.engine.HttpClientEngineFactory +import io.ktor.client.engine.darwin.Darwin +import io.ktor.client.engine.darwin.DarwinClientEngineConfig + +actual val Engine: HttpClientEngineFactory + get() = Darwin + +actual fun HttpClientConfig.configurePlatform() { + @Suppress("UNCHECKED_CAST") + (this as HttpClientConfig).realConfigurePlatform() +} + +private fun HttpClientConfig.realConfigurePlatform() { + engine { + configureRequest { + setAllowsCellularAccess(true) + } + } +} \ No newline at end of file diff --git a/i18n/build.gradle.kts b/i18n/build.gradle.kts index 8226d26a..2b9b9181 100644 --- a/i18n/build.gradle.kts +++ b/i18n/build.gradle.kts @@ -20,6 +20,8 @@ kotlin { } } } + iosX64() + iosArm64() sourceSets { val commonMain by getting {