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

Compose Multiplatform #89

Open
wants to merge 59 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
487261f
refactor core module to multiplatform
DatL4g Mar 29, 2024
58908fb
multiplatform core module
DatL4g Mar 29, 2024
1d38e02
fix old android targets
DatL4g Mar 29, 2024
2f65c32
prepare calendar multiplatform
DatL4g Mar 29, 2024
392641f
calendar multiplatform compatible
DatL4g Mar 29, 2024
afa6178
rating multiplatform compatible
DatL4g Mar 30, 2024
5393ed2
option multiplatform compatible
DatL4g Mar 30, 2024
50dcb48
list multiplatform compatible
DatL4g Mar 30, 2024
83dde05
multiplatform landscape check
DatL4g Mar 30, 2024
8e11b75
prepare snapshot release
DatL4g Mar 30, 2024
75d6893
fix snapshot release
DatL4g Mar 30, 2024
0cd7fa2
added release workflow
DatL4g Mar 30, 2024
da08075
api core library
DatL4g Mar 30, 2024
8ecc373
make states fully serializable
DatL4g Mar 30, 2024
14e7852
added missing java serialization
DatL4g Mar 30, 2024
981caa0
skip workflows
DatL4g Mar 30, 2024
3da43ed
fix calendar module
DatL4g Mar 30, 2024
4efdbde
fix calendar module
DatL4g Mar 30, 2024
879f669
fix calendar resources
DatL4g Mar 30, 2024
87bb4a0
fix calendar arrow size
DatL4g Mar 30, 2024
ba08232
added default selection for rating
DatL4g Apr 3, 2024
a8ba838
fix resources
DatL4g Apr 3, 2024
b795aa9
added possibility to deselect rating and rating of 0 is valid
DatL4g Apr 4, 2024
47a5cca
migrate state module
DatL4g Apr 4, 2024
8d3262e
migrate input module
DatL4g Apr 4, 2024
adeb1c0
migrate info module
DatL4g Apr 4, 2024
6f30cb1
migrate color module
DatL4g Apr 4, 2024
446bdd0
migrate emoji module
DatL4g Apr 11, 2024
37bc03c
migrate emoji module
DatL4g Apr 11, 2024
2a6a658
migrate date_time module
DatL4g Apr 11, 2024
dcc5d95
migrate date_time module
DatL4g Apr 11, 2024
7b2940a
migrate date_time module
DatL4g Apr 11, 2024
0d1f94e
migrate duration module
DatL4g Apr 12, 2024
38d3372
migrate clock module
DatL4g Apr 12, 2024
1d5bfa7
added missing configuration
DatL4g Apr 12, 2024
b29bc84
changed readme as core module is not required to add manually anymore
DatL4g Apr 12, 2024
02dbb78
added wasm target
DatL4g Apr 12, 2024
a6a90ff
added missing wasm implementations
DatL4g Apr 12, 2024
5f185b5
chore(deps): ⬆️ Upgraded Compose versions
maicol07 Apr 15, 2024
fb7d802
refactor: Moved dependency management from buildSrc to Version Catalog
maicol07 Apr 16, 2024
1c81161
Merge pull request #1 from maicol07/main
DatL4g Apr 16, 2024
da9f1f1
updated compose
DatL4g Apr 17, 2024
3886da0
ignore some fields from java serialization
DatL4g Apr 18, 2024
8c34770
roll back compose version
DatL4g Apr 18, 2024
e53bb5f
update to latest compose version
DatL4g Apr 29, 2024
cee7dbe
update to latest compose version
DatL4g Apr 30, 2024
90b23af
option invoke click listener
DatL4g Apr 30, 2024
01e57b0
option icon size
DatL4g May 12, 2024
b022d95
rating body max width
DatL4g May 12, 2024
dd2451a
revert sizing
DatL4g May 12, 2024
f16d2d3
revert LocalDate calculations and fix visual error
DatL4g May 13, 2024
dce4cd0
removed unused file
DatL4g May 13, 2024
f21cd3d
apply system locale on jvm
DatL4g May 14, 2024
24de041
added missing localization
DatL4g May 14, 2024
27692e6
added body for option
DatL4g May 14, 2024
ed2ba8c
added missing body dependency injection
DatL4g May 14, 2024
d2848f5
updated compose
DatL4g May 17, 2024
01f3361
updated dependencies
DatL4g May 22, 2024
3518cc7
fix crash on android
DatL4g May 31, 2024
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
Prev Previous commit
Next Next commit
migrate clock module
  • Loading branch information
DatL4g committed Apr 12, 2024
commit 38d33721d7192035f267c822e9885404d3845bae
59 changes: 53 additions & 6 deletions clock/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.vanniktech.maven.publish.SonatypeHost

/*
* Copyright (C) 2022-2024. Maximilian Keppeler (https://www.maxkeppeler.com)
*
Expand All @@ -14,22 +16,67 @@
* limitations under the License.
*/
plugins {
id(Plugins.CUSTOM_LIBRARY_MODULE.id)
alias(libs.plugins.android.library)
alias(libs.plugins.compose)
alias(libs.plugins.multiplatform)
alias(libs.plugins.serialization)
alias(libs.plugins.publish)
`maven-publish`
}

android {
namespace = Modules.CLOCK.namespace
compileSdk = 34

defaultConfig {
minSdk = 21
}
compileOptions {
// Flag to enable support for the new language APIs
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}

kotlin {
androidTarget {
publishAllLibraryVariants()
}
jvm()

iosX64()
iosArm64()
iosSimulatorArm64()

macosX64()
macosArm64()

js(IR) {
browser()
binaries.executable()
}

applyDefaultHierarchyTemplate()

sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material3)
implementation(compose.components.resources)

implementation(libs.datetime)
implementation(libs.serialization)

api(project(":core"))
}
}
}

dependencies {
coreLibraryDesugaring(Dependencies.DESUGAR)
coreLibraryDesugaring(libs.desugar)
}

mavenPublishing {
publishToMavenCentral()
signAllPublications()
}
publishToMavenCentral(SonatypeHost.S01, automaticRelease = true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.maxkeppeler.sheets.clock

import android.text.format.DateFormat
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

@Composable
internal actual fun is24HourFormat(): Boolean {
return DateFormat.is24HourFormat(LocalContext.current)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.maxkeppeler.sheets.clock.views

import com.maxkeppeler.sheets.clock.utils.FormatStyle
import kotlinx.datetime.LocalTime
import kotlinx.datetime.format.DateTimeFormat
import kotlinx.datetime.format.FormatStringsInDatetimeFormats
import kotlinx.datetime.format.byUnicodePattern
import java.time.chrono.Chronology
import java.time.format.DateTimeFormatterBuilder
import java.util.Locale

internal fun FormatStyle.toJava(): java.time.format.FormatStyle {
return when (this) {
FormatStyle.FULL -> java.time.format.FormatStyle.FULL
FormatStyle.LONG -> java.time.format.FormatStyle.LONG
FormatStyle.MEDIUM -> java.time.format.FormatStyle.MEDIUM
FormatStyle.SHORT -> java.time.format.FormatStyle.SHORT
}
}

@OptIn(FormatStringsInDatetimeFormats::class)
internal actual fun getTimeFormatter(style: FormatStyle): DateTimeFormat<LocalTime> {
val locale = Locale.getDefault()
val pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
null, style.toJava(), Chronology.ofLocale(locale), locale
).toString()

return LocalTime.Format {
byUnicodePattern(pattern)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.maxkeppeler.sheets.clock

import androidx.compose.runtime.Composable
import platform.Foundation.NSDateFormatter
import platform.Foundation.NSLocale
import platform.Foundation.currentLocale

@Composable
internal actual fun is24HourFormat(): Boolean {
val dateFormat = NSDateFormatter.dateFormatFromTemplate("j", 0u, NSLocale.currentLocale)
return dateFormat?.contains('a')?.not() ?: true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.maxkeppeler.sheets.clock.views

import com.maxkeppeler.sheets.clock.utils.FormatStyle
import kotlinx.datetime.LocalTime
import kotlinx.datetime.format.DateTimeFormat
import kotlinx.datetime.format.FormatStringsInDatetimeFormats
import kotlinx.datetime.format.byUnicodePattern
import platform.Foundation.*

internal fun FormatStyle.toSwift(): NSDateFormatterStyle {
return when (this) {
FormatStyle.FULL -> NSDateFormatterFullStyle
FormatStyle.LONG -> NSDateFormatterLongStyle
FormatStyle.MEDIUM -> NSDateFormatterMediumStyle
FormatStyle.SHORT -> NSDateFormatterShortStyle
}
}

@OptIn(FormatStringsInDatetimeFormats::class)
internal actual fun getTimeFormatter(style: FormatStyle): DateTimeFormat<LocalTime> {
val formatter = NSDateFormatter()
formatter.setDateStyle(NSDateFormatterNoStyle)
formatter.setTimeStyle(style.toSwift())
val pattern = formatter.dateFormat()

return LocalTime.Format {
byUnicodePattern(pattern)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.maxkeppeler.sheets.clock

import android.content.Context
import android.text.format.DateFormat
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -27,6 +25,7 @@ import com.maxkeppeker.sheets.core.models.base.Debouncer
import com.maxkeppeker.sheets.core.views.BaseTypeState
import com.maxkeppeler.sheets.clock.models.ClockConfig
import com.maxkeppeler.sheets.clock.models.ClockSelection
import com.maxkeppeler.sheets.clock.utils.*
import com.maxkeppeler.sheets.clock.utils.Constants
import com.maxkeppeler.sheets.clock.utils.convertTimeIntoTimeTextValues
import com.maxkeppeler.sheets.clock.utils.convertTimeTextValuesIntoTime
Expand All @@ -36,8 +35,9 @@ import com.maxkeppeler.sheets.clock.utils.inputValue
import com.maxkeppeler.sheets.clock.utils.isAm
import com.maxkeppeler.sheets.clock.utils.moveToNextIndex
import com.maxkeppeler.sheets.clock.utils.moveToPreviousIndex
import java.io.Serializable
import java.time.LocalTime
import kotlinx.datetime.LocalTime
import kotlinx.serialization.Serializable
import com.maxkeppeker.sheets.core.utils.JvmSerializable

/**
* Handles the clock state.
Expand All @@ -47,9 +47,9 @@ import java.time.LocalTime
* @param stateData The data of the state when the state is required to be restored.
*/
internal class ClockState(
private val context: Context,
val selection: ClockSelection,
val config: ClockConfig,
private val _is24HourFormat: Boolean,
stateData: ClockStateData? = null
) : BaseTypeState() {

Expand Down Expand Up @@ -79,7 +79,7 @@ internal class ClockState(
private fun isValid(): Boolean = config.boundary?.let { time in it } ?: true

private fun isInit24HourFormat(): Boolean {
return config.is24HourFormat ?: DateFormat.is24HourFormat(context)
return config.is24HourFormat ?: _is24HourFormat
}

private fun getInitTime(): LocalTime {
Expand Down Expand Up @@ -194,9 +194,9 @@ internal class ClockState(
* @param config The general configuration for the dialog view.
*/
fun Saver(
context: Context,
selection: ClockSelection,
config: ClockConfig
config: ClockConfig,
is24HourFormat: Boolean
): Saver<ClockState, *> = Saver(
save = { state ->
ClockStateData(
Expand All @@ -207,21 +207,22 @@ internal class ClockState(
isAm = state.isAm
)
},
restore = { data -> ClockState(context, selection, config, data) }
restore = { data -> ClockState(selection, config, is24HourFormat, data) }
)
}

/**
* Data class that stores the important information of the current state
* and can be used by the [Saver] to save and restore the state.
*/
@Serializable
data class ClockStateData(
val groupIndex: Int,
val valueIndex: Int,
val is24HourFormat: Boolean,
val time: LocalTime,
val isAm: Boolean
) : Serializable
) : JvmSerializable
}

/**
Expand All @@ -232,11 +233,17 @@ internal class ClockState(
*/
@Composable
internal fun rememberClockState(
context: Context,
selection: ClockSelection,
config: ClockConfig,
): ClockState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = ClockState.Saver(context, selection, config),
init = { ClockState(context, selection, config) }
)
): ClockState {
val is24Hour = is24HourFormat()

return rememberSaveable(
inputs = arrayOf(selection, config),
saver = ClockState.Saver(selection, config, is24Hour),
init = { ClockState(selection, config, is24Hour) }
)
}

@Composable
internal expect fun is24HourFormat(): Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.dp
import com.maxkeppeker.sheets.core.models.base.Header
import com.maxkeppeker.sheets.core.models.base.LibOrientation
Expand All @@ -38,7 +36,6 @@ import com.maxkeppeler.sheets.clock.views.KeyboardComponent
import com.maxkeppeler.sheets.clock.views.LandscapeTimeValueComponent
import com.maxkeppeler.sheets.clock.views.PortraitTimeValueComponent
import com.maxkeppeler.sheets.clock.views.TimeHintComponent
import com.maxkeppeler.sheets.core.R

/**
* Clock view for the use-case to to select a clock time.
Expand All @@ -55,8 +52,7 @@ fun ClockView(
config: ClockConfig = ClockConfig(),
header: Header? = null,
) {
val context = LocalContext.current
val clockState = rememberClockState(context, selection, config)
val clockState = rememberClockState(selection, config)
StateHandler(useCaseState, clockState)

FrameBase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.maxkeppeker.sheets.core.icons.LibIcons
import com.maxkeppeker.sheets.core.models.base.BaseConfigs
import com.maxkeppeker.sheets.core.models.base.LibOrientation
import com.maxkeppeker.sheets.core.utils.BaseConstants
import java.time.LocalTime
import kotlinx.datetime.LocalTime

/**
* The general configuration for the clock dialog.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.maxkeppeler.sheets.clock.utils

import kotlinx.datetime.Clock
import kotlinx.datetime.LocalTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

internal fun LocalTime.Companion.now(): LocalTime {
return Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).time
}

internal val Char.numericValue: Int
get() {
if (this.isDigit()) {
return this.digitToInt()
}
val codePoint = this.code

if (codePoint < 128) {
if (codePoint >= '0'.code && codePoint <= '9'.code) {
return codePoint - '0'.code
}
if (codePoint >= 'a'.code && codePoint <= 'z'.code) {
return codePoint - ('a'.code - 10)
}
if (codePoint >= 'A'.code && codePoint <= 'Z'.code) {
return codePoint - ('A'.code - 10)
}
return codePoint
}
// Full-width uppercase A-Z.
if (codePoint in 0xff21..0xff3a) {
return codePoint - 0xff17
}
// Full-width lowercase a-z.
if (codePoint in 0xff41..0xff5a) {
return codePoint - 0xff37
}
return codePoint
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.maxkeppeler.sheets.clock.utils

/**
* Enumeration of the style of a localized date, time or date-time formatter.
*
* These styles are used when obtaining a date-time style from configuration.
*/
enum class FormatStyle {
// ordered from large to small
/**
* Full text style, with the most detail.
* For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
*/
FULL,

/**
* Long text style, with lots of detail.
* For example, the format might be 'January 12, 1952'.
*/
LONG,

/**
* Medium text style, with some detail.
* For example, the format might be 'Jan 12, 1952'.
*/
MEDIUM,

/**
* Short text style, typically numeric.
* For example, the format might be '12.13.52' or '3:30pm'.
*/
SHORT
}
Loading