├── lib
├── .gitignore
├── consumer-rules.pro
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ └── com
│ │ │ └── hshamkhani
│ │ │ └── persiandtpicker
│ │ │ ├── utils
│ │ │ ├── ClockPeriod.kt
│ │ │ ├── PersianNumberUtil.kt
│ │ │ ├── SimpleDateTime.kt
│ │ │ └── DatePickerUtils.kt
│ │ │ ├── components
│ │ │ ├── IconButton.kt
│ │ │ └── WheelPicker.kt
│ │ │ ├── picker
│ │ │ ├── RailPicker.kt
│ │ │ ├── PersianDatePicker.kt
│ │ │ └── TimePicker.kt
│ │ │ └── calendar
│ │ │ └── PersianCalendar.kt
│ │ └── res
│ │ └── drawable
│ │ ├── keyboard_arrow_left.xml
│ │ └── keyboard_arrow_right.xml
├── proguard-rules.pro
└── build.gradle.kts
├── sample
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── themes.xml
│ │ │ │ └── colors.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── xml
│ │ │ │ ├── backup_rules.xml
│ │ │ │ └── data_extraction_rules.xml
│ │ │ └── drawable
│ │ │ │ ├── ic_launcher_foreground.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── hshamkhani
│ │ │ │ └── persian_dtpicker
│ │ │ │ ├── ui
│ │ │ │ └── theme
│ │ │ │ │ ├── Color.kt
│ │ │ │ │ ├── Type.kt
│ │ │ │ │ └── Theme.kt
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── hshamkhani
│ │ │ └── persian_dtpicker
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── hshamkhani
│ │ └── persian_dtpicker
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle.kts
├── .idea
├── .gitignore
└── vcs.xml
├── images
└── banner.png
├── jitpack.yml
├── gradle
├── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── libs.versions.toml
├── .gitignore
├── settings.gradle.kts
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/lib/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/images/banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/images/banner.png
--------------------------------------------------------------------------------
/jitpack.yml:
--------------------------------------------------------------------------------
1 | jdk:
2 | - openjdk21
3 | before_install:
4 | - sdk install java 21.0.6-open
5 | - sdk use java 21.0.6-open
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | persian-dtpicker
3 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sham-h93/persian-dtpicker/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/utils/ClockPeriod.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.utils
2 |
3 |
4 | /**
5 | * Clock period (Am, Pm) for a clock in 12-hour time format.
6 | * */
7 | enum class ClockPeriod {
8 | Am, Pm
9 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jul 14 18:56:38 IRST 2025
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .kotlin
3 | .gradle
4 | .idea
5 | /local.properties
6 | /.idea/caches
7 | /.idea/libraries
8 | /.idea/modules.xml
9 | /.idea/workspace.xml
10 | /.idea/navEditor.xml
11 | /.idea/assetWizardSettings.xml
12 | .DS_Store
13 | /build
14 | /captures
15 | .externalNativeBuild
16 | .cxx
17 | local.properties
18 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/hshamkhani/persian_dtpicker/ui/theme/Color.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker.ui.theme
2 |
3 | import androidx.compose.ui.graphics.Color
4 |
5 | val Purple80 = Color(0xFFD0BCFF)
6 | val PurpleGrey80 = Color(0xFFCCC2DC)
7 | val Pink80 = Color(0xFFEFB8C8)
8 |
9 | val Purple40 = Color(0xFF6650a4)
10 | val PurpleGrey40 = Color(0xFF625b71)
11 | val Pink40 = Color(0xFF7D5260)
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/lib/src/main/res/drawable/keyboard_arrow_left.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/lib/src/main/res/drawable/keyboard_arrow_right.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/sample/src/test/java/com/hshamkhani/persian_dtpicker/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/sample/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/sample/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/components/IconButton.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.components
2 |
3 | import androidx.compose.material3.Icon
4 | import androidx.compose.material3.IconButton
5 | import androidx.compose.runtime.Composable
6 | import androidx.compose.ui.Modifier
7 | import androidx.compose.ui.graphics.vector.ImageVector
8 |
9 | @Composable
10 | fun FlatIconButton(
11 | modifier: Modifier = Modifier,
12 | icon: ImageVector,
13 | onClick: () -> Unit
14 | ) {
15 | IconButton(
16 | modifier = modifier,
17 | onClick = onClick
18 | ) {
19 | Icon(
20 | imageVector = icon,
21 | contentDescription = null
22 | )
23 | }
24 | }
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | maven("https://maven.myket.ir")
4 | google {
5 | content {
6 | includeGroupByRegex("com\\.android.*")
7 | includeGroupByRegex("com\\.google.*")
8 | includeGroupByRegex("androidx.*")
9 | }
10 | }
11 | mavenCentral()
12 | gradlePluginPortal()
13 | }
14 | }
15 | dependencyResolutionManagement {
16 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
17 | repositories {
18 | maven("https://maven.myket.ir")
19 | google()
20 | mavenCentral()
21 | }
22 | }
23 |
24 | rootProject.name = "persian-dtpicker"
25 | include(":sample")
26 | include(":lib")
27 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/sample/src/androidTest/java/com/hshamkhani/persian_dtpicker/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.hshamkhani.persian_dtpicker", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
14 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/hshamkhani/persian_dtpicker/ui/theme/Type.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker.ui.theme
2 |
3 | import androidx.compose.material3.Typography
4 | import androidx.compose.ui.text.TextStyle
5 | import androidx.compose.ui.text.font.FontFamily
6 | import androidx.compose.ui.text.font.FontWeight
7 | import androidx.compose.ui.unit.sp
8 |
9 | // Set of Material typography styles to start with
10 | val Typography = Typography(
11 | bodyLarge = TextStyle(
12 | fontFamily = FontFamily.Default,
13 | fontWeight = FontWeight.Normal,
14 | fontSize = 16.sp,
15 | lineHeight = 24.sp,
16 | letterSpacing = 0.5.sp
17 | )
18 | /* Other default text styles to override
19 | titleLarge = TextStyle(
20 | fontFamily = FontFamily.Default,
21 | fontWeight = FontWeight.Normal,
22 | fontSize = 22.sp,
23 | lineHeight = 28.sp,
24 | letterSpacing = 0.sp
25 | ),
26 | labelSmall = TextStyle(
27 | fontFamily = FontFamily.Default,
28 | fontWeight = FontWeight.Medium,
29 | fontSize = 11.sp,
30 | lineHeight = 16.sp,
31 | letterSpacing = 0.5.sp
32 | )
33 | */
34 | )
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. For more details, visit
12 | # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your sample's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/lib/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libs.plugins.android.library)
3 | alias(libs.plugins.kotlin.android)
4 | alias(libs.plugins.kotlin.compose)
5 | `maven-publish`
6 | }
7 |
8 | val libGroupId = "com.github.sham-h93"
9 | val libArtifactId = "persian-dtpicker"
10 | val libVersion = "1.0.0"
11 |
12 | android {
13 | namespace = "com.hshamkhani.persiandtpicker"
14 | compileSdk = 36
15 |
16 | defaultConfig {
17 | minSdk = 26
18 |
19 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
20 | consumerProguardFiles("consumer-rules.pro")
21 | }
22 |
23 | buildTypes {
24 | release {
25 | isMinifyEnabled = false
26 | }
27 | }
28 | compileOptions {
29 | sourceCompatibility = JavaVersion.VERSION_17
30 | targetCompatibility = JavaVersion.VERSION_17
31 | }
32 | kotlinOptions {
33 | jvmTarget = "17"
34 | }
35 | buildFeatures {
36 | compose = true
37 | }
38 | }
39 |
40 | dependencies {
41 | implementation(platform(libs.androidx.compose.bom))
42 | implementation(libs.androidx.material3)
43 | implementation(libs.androidx.ui)
44 | implementation(libs.jalali.calendar)
45 | implementation(libs.kotlinx.datetime)
46 | implementation(libs.kotlinx.collections.immutable)
47 | }
48 |
49 | publishing {
50 | publications {
51 | register("release") {
52 | groupId = libGroupId
53 | artifactId = libArtifactId
54 | version = libVersion
55 |
56 | afterEvaluate {
57 | from(components["release"])
58 | }
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/hshamkhani/persian_dtpicker/ui/theme/Theme.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker.ui.theme
2 |
3 | import android.app.Activity
4 | import android.os.Build
5 | import androidx.compose.foundation.isSystemInDarkTheme
6 | import androidx.compose.material3.MaterialTheme
7 | import androidx.compose.material3.darkColorScheme
8 | import androidx.compose.material3.dynamicDarkColorScheme
9 | import androidx.compose.material3.dynamicLightColorScheme
10 | import androidx.compose.material3.lightColorScheme
11 | import androidx.compose.runtime.Composable
12 | import androidx.compose.ui.platform.LocalContext
13 |
14 | private val DarkColorScheme = darkColorScheme(
15 | primary = Purple80,
16 | secondary = PurpleGrey80,
17 | tertiary = Pink80
18 | )
19 |
20 | private val LightColorScheme = lightColorScheme(
21 | primary = Purple40,
22 | secondary = PurpleGrey40,
23 | tertiary = Pink40
24 |
25 | /* Other default colors to override
26 | background = Color(0xFFFFFBFE),
27 | surface = Color(0xFFFFFBFE),
28 | onPrimary = Color.White,
29 | onSecondary = Color.White,
30 | onTertiary = Color.White,
31 | onBackground = Color(0xFF1C1B1F),
32 | onSurface = Color(0xFF1C1B1F),
33 | */
34 | )
35 |
36 | @Composable
37 | fun PersiandtpickerTheme(
38 | darkTheme: Boolean = isSystemInDarkTheme(),
39 | // Dynamic color is available on Android 12+
40 | dynamicColor: Boolean = true,
41 | content: @Composable () -> Unit
42 | ) {
43 | val colorScheme = when {
44 | dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
45 | val context = LocalContext.current
46 | if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
47 | }
48 |
49 | darkTheme -> DarkColorScheme
50 | else -> LightColorScheme
51 | }
52 |
53 | MaterialTheme(
54 | colorScheme = colorScheme,
55 | typography = Typography,
56 | content = content
57 | )
58 | }
--------------------------------------------------------------------------------
/sample/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libs.plugins.android.application)
3 | alias(libs.plugins.kotlin.android)
4 | alias(libs.plugins.kotlin.compose)
5 | }
6 |
7 | android {
8 | namespace = "com.hshamkhani.persian_dtpicker"
9 | compileSdk = 36
10 |
11 | defaultConfig {
12 | applicationId = "com.hshamkhani.persian_dtpicker"
13 | minSdk = 26
14 | targetSdk = 36
15 | versionCode = 1
16 | versionName = "1.0"
17 |
18 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 |
21 | buildTypes {
22 | release {
23 | isMinifyEnabled = false
24 | proguardFiles(
25 | getDefaultProguardFile("proguard-android-optimize.txt"),
26 | "proguard-rules.pro"
27 | )
28 | }
29 | }
30 | compileOptions {
31 | sourceCompatibility = JavaVersion.VERSION_17
32 | targetCompatibility = JavaVersion.VERSION_17
33 | }
34 | kotlinOptions {
35 | jvmTarget = "17"
36 | }
37 | buildFeatures {
38 | compose = true
39 | }
40 | }
41 |
42 | dependencies {
43 |
44 | implementation(libs.androidx.core.ktx)
45 | implementation(libs.androidx.lifecycle.runtime.ktx)
46 | implementation(libs.androidx.activity.compose)
47 | implementation(platform(libs.androidx.compose.bom))
48 | implementation(libs.androidx.ui)
49 | implementation(libs.androidx.ui.graphics)
50 | implementation(libs.androidx.ui.tooling.preview)
51 | implementation(libs.androidx.material3)
52 | testImplementation(libs.junit)
53 | androidTestImplementation(libs.androidx.junit)
54 | androidTestImplementation(libs.androidx.espresso.core)
55 | androidTestImplementation(platform(libs.androidx.compose.bom))
56 | androidTestImplementation(libs.androidx.ui.test.junit4)
57 | debugImplementation(libs.androidx.ui.tooling)
58 | debugImplementation(libs.androidx.ui.test.manifest)
59 | implementation(project(":lib"))
60 | }
--------------------------------------------------------------------------------
/gradle/libs.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | agp = "8.11.1"
3 | kotlin = "2.2.0"
4 | coreKtx = "1.16.0"
5 | junit = "4.13.2"
6 | junitVersion = "1.2.1"
7 | espressoCore = "3.6.1"
8 | kotlinxDatetime = "0.7.1"
9 | lifecycleRuntimeKtx = "2.9.1"
10 | activityCompose = "1.10.1"
11 | composeBom = "2025.10.00"
12 | appcompat = "1.7.1"
13 | material = "1.12.0"
14 | jalaliCalendar = "1.3.3"
15 | kotlinxCollectionsImmutable = "0.4.0"
16 |
17 | [libraries]
18 | androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
19 | junit = { group = "junit", name = "junit", version.ref = "junit" }
20 | androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
21 | androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
22 | androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
23 | androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
24 | androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
25 | androidx-ui = { group = "androidx.compose.ui", name = "ui" }
26 | androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
27 | androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
28 | androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
29 | androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
30 | androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
31 | androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
32 | androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
33 | kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinxDatetime" }
34 | material = { group = "com.google.android.material", name = "material", version.ref = "material" }
35 |
36 | kotlinx-collections-immutable = { group = "org.jetbrains.kotlinx", name = "kotlinx-collections-immutable", version.ref = "kotlinxCollectionsImmutable" }
37 |
38 | jalali-calendar = { group = "ir.huri", name = "JalaliCalendar", version.ref = "jalaliCalendar" }
39 |
40 | [plugins]
41 | android-application = { id = "com.android.application", version.ref = "agp" }
42 | kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
43 | kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
44 | android-library = { id = "com.android.library", version.ref = "agp" }
45 |
46 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/utils/PersianNumberUtil.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.utils
2 |
3 | import java.util.Locale
4 |
5 |
6 | internal object PersianNumberUtils {
7 |
8 | fun Int.padZeroToStartWithPersianDigits(): String {
9 | val number = if (this < 10) {
10 | this.toString().padStart(2, '0')
11 | } else {
12 | this.toString()
13 | }
14 | return number.formatToHindiIfLanguageIsFa()
15 | }
16 |
17 | fun String.formatToHindiIfLanguageIsFa(): String {
18 | return if (Locale.getDefault().language == "fa") {
19 | this.toHindiDigits()
20 | } else {
21 | this
22 | }
23 | }
24 |
25 | fun String.toHindiDigits(): String {
26 | val chars = this.toCharArray().map { char ->
27 | when (char) {
28 | '0' -> '۰'
29 | '1' -> '۱'
30 | '2' -> '۲'
31 | '3' -> '۳'
32 | '4' -> '۴'
33 | '5' -> '۵'
34 | '6' -> '۶'
35 | '7' -> '۷'
36 | '8' -> '۸'
37 | '9' -> '۹'
38 | else -> '۰'
39 | }
40 | }.joinToString(separator = "")
41 | return chars
42 | }
43 |
44 | fun String.toArabicDigits(): Int {
45 | val chars = this.toCharArray().map { char ->
46 | when (char) {
47 | '۱' -> 1
48 | '۲' -> 2
49 | '۳' -> 3
50 | '۴' -> 4
51 | '۵' -> 5
52 | '۶' -> 6
53 | '۷' -> 7
54 | '۸' -> 8
55 | '۹' -> 9
56 | '۰' -> 0
57 | else -> char
58 | }
59 | }.joinToString(separator = "")
60 | return chars.toInt()
61 | }
62 |
63 | fun Int.asStringMonthName(): String {
64 | val isFa = Locale.getDefault().language == "fa"
65 | return when (this) {
66 | 1 -> if (isFa) "فروردین" else "Farvardin"
67 | 2 -> if (isFa) "اردیبهشت" else "Ordibehesht"
68 | 3 -> if (isFa) "خرداد" else "Khordad"
69 | 4 -> if (isFa) "تیر" else "Tir"
70 | 5 -> if (isFa) "مرداد" else "Mordad"
71 | 6 -> if (isFa) "شهریور" else "Shahrivar"
72 | 7 -> if (isFa) "مهر" else "Mehr"
73 | 8 -> if (isFa) "آبان" else "Aban"
74 | 9 -> if (isFa) "آذر" else "Azar"
75 | 10 -> if (isFa) "دی" else "Day"
76 | 11 -> if (isFa) "بهمن" else "Bahman"
77 | 12 -> if (isFa) "اسفند" else "Esfand"
78 | else -> throw IllegalArgumentException()
79 | }
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Persian Date Picker
2 | 
3 |
4 | [](https://jitpack.io/#sham-h93/persian-dtpicker)  
5 |
6 | A simple, minimal, and fully customizable Persian Date Picker for Jetpack Compose.
7 |
8 | ## Installation
9 |
10 | > The library available via [JitPack](https://jitpack.io)
11 | >
12 | **Step 1** Add JitPack to your **root** `build.gradle.kts` or `settings.gradle`:
13 |
14 |
15 | dependencyResolutionManagement {
16 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
17 | repositories {
18 | maven { url = uri("https://jitpack.io") }
19 | }
20 | }
21 |
22 | **Step 2** Add the dependency to the module `build.gradle.kts`
23 |
24 | ```css
25 | dependencies {
26 | implementation("com.github.sham-h93:persian-dtpicker:")
27 | }
28 | ```
29 | ---
30 | ### Usage
31 |
32 | Minimal usage of `PersianDatePicker` with a time picker:
33 | >You can also use it without time picker by setting property `withTimePicker` to ` false`.
34 |
35 |
36 | ```css
37 | @Composable
38 | fun PersianDtPickerExample(
39 | modifier: Modifier = Modifier,
40 | context: Context
41 | ) {
42 | var selectedDate by remember { mutableStateOf(SimpleDate.now(context = context)) }
43 |
44 | PersianDatePicker(
45 | modifier = modifier,
46 | withTimePicker = true,
47 | onDateSelected = { date: SimpleDate ->
48 | selectedDate = date
49 | },
50 | )
51 | }
52 | ```
53 |
54 |
55 |
56 | Additionally, you can use the `TimePicker` independently:
57 |
58 |
59 | ```css
60 | @Composable
61 | fun TimePickerExample(
62 | modifier: Modifier = Modifier,
63 | context: Context
64 | ) {
65 | var selectedDate by remember { mutableStateOf(SimpleTime.now(context = context)) }
66 |
67 | TimePicker(
68 | modifier = modifier,
69 | onTimeChanged = { time: SimpleTime ->
70 | selectedDate = time
71 | },
72 | )
73 | }
74 | ```
75 | ## More Details
76 | |*Class*| *Description* |
77 | |---|---|
78 | |`data class SimpleDate`|Represents a simple date (either gregorian or jalali) with year, month, day, and an optional `SimpleTime` object.|
79 | |`data class SimpleTime`|Represents a time with hour, minute, and an `ClockPeriod` (`Am`/`Pm`), if the clock in 12-hour time format.|
80 | |`enum class ClockPeriod`|Clock period (`Am`, `Pm`) for a clock in 12-hour time format.|
81 |
82 | ---
83 |
84 | |*Function*| *Description* |
85 | |---|---|
86 | |`SimpleDate.now(context: Context): SimpleDate`|A function that represents current jalali date and time as a `SimpleDate` object.|
87 | |`SimpleDate.from(timestamp: Long, context: Context): SimpleDate`| A function that converts a timestamp (in milliseconds) to a jalali `SimpleDate` object.|
88 | |`SimpleTime.now(context: Context): SimpleTime`|A function that returns the current time as a `SimpleTime` object.|
89 | |||
90 | | `fun SimpleDate.toCalendar(): Calendar` | Converts a Gregorian `SimpleDate` to a Calendar object. |
91 | |`fun SimpleDate.totGregorianDate(): SimpleDate`|Converts a Jalali `SimpleDate` to a Gregorian `SimpleDate`.|
92 |
93 | ## 👥 Contributing
94 | I sincerely welcome your contributions to the Persian Date Picker project! I would love to have your help in making *PersianDatePicker* better.
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/picker/RailPicker.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.picker
2 |
3 | import androidx.compose.foundation.layout.Arrangement
4 | import androidx.compose.foundation.layout.Column
5 | import androidx.compose.foundation.layout.fillMaxWidth
6 | import androidx.compose.foundation.lazy.LazyRow
7 | import androidx.compose.material3.Card
8 | import androidx.compose.material3.MaterialTheme
9 | import androidx.compose.material3.Text
10 | import androidx.compose.runtime.Composable
11 | import androidx.compose.runtime.getValue
12 | import androidx.compose.runtime.mutableStateOf
13 | import androidx.compose.runtime.remember
14 | import androidx.compose.runtime.setValue
15 | import androidx.compose.ui.Alignment
16 | import androidx.compose.ui.Modifier
17 | import androidx.compose.ui.graphics.Color
18 | import androidx.compose.ui.platform.LocalContext
19 | import androidx.compose.ui.text.TextStyle
20 | import androidx.compose.ui.text.font.FontFamily
21 | import androidx.compose.ui.unit.dp
22 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.asStringMonthName
23 | import com.hshamkhani.persiandtpicker.utils.SimpleDate
24 | import com.hshamkhani.persiandtpicker.utils.initMonthDays
25 | import com.hshamkhani.persiandtpicker.utils.initYearList
26 |
27 | @Composable
28 | fun RailPicker(
29 | modifier: Modifier = Modifier,
30 | textStyle: TextStyle = MaterialTheme.typography.headlineMedium,
31 | fontFamily: FontFamily = FontFamily.Default,
32 | textColor: Color = MaterialTheme.colorScheme.onSurface,
33 | selectedTextColor: Color = MaterialTheme.colorScheme.primary,
34 | backGroundColor: Color = MaterialTheme.colorScheme.background,
35 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
36 | selectedDate: SimpleDate? = null,
37 | onDateSelected: (date: SimpleDate) -> Unit,
38 | ) {
39 | RailPickerImpl(
40 | modifier = modifier,
41 | textStyle = textStyle,
42 | fontFamily = fontFamily,
43 | textColor = textColor,
44 | selectedTextColor = selectedTextColor,
45 | backGroundColor = backGroundColor,
46 | selectedItemBackgroundColor = selectedItemBackgroundColor,
47 | selectedDate = selectedDate,
48 | onDateSelected = onDateSelected
49 | )
50 | }
51 |
52 | @Composable
53 | private fun RailPickerImpl(
54 | modifier: Modifier = Modifier,
55 | textStyle: TextStyle = MaterialTheme.typography.headlineMedium,
56 | fontFamily: FontFamily = FontFamily.Default,
57 | textColor: Color = MaterialTheme.colorScheme.onSurface,
58 | selectedTextColor: Color = MaterialTheme.colorScheme.primary,
59 | backGroundColor: Color = MaterialTheme.colorScheme.background,
60 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
61 | selectedDate: SimpleDate? = null,
62 | onDateSelected: (date: SimpleDate) -> Unit
63 | ) {
64 | val context = LocalContext.current
65 |
66 | val initialDate by remember { mutableStateOf(SimpleDate.now(context = context)) }
67 | var simpleDate by remember { mutableStateOf(selectedDate ?: initialDate) }
68 |
69 | val years by remember {
70 | mutableStateOf(
71 | initYearList(initialDate.year).map { year ->
72 | (1..12).map { month ->
73 | initMonthDays(
74 | monthNumber = month,
75 | year = year
76 | ).map { it.asStringMonthName() }.toList()
77 | }.flatten()
78 | }
79 | )
80 | }
81 |
82 | LazyRow(
83 | modifier = Modifier.fillMaxWidth(),
84 | horizontalArrangement = Arrangement.spacedBy(8.dp)
85 | ) {
86 | years.forEach {
87 | items(
88 | count = it.size
89 | ) { year ->
90 |
91 | }
92 | }
93 | }
94 | }
95 |
96 | @Composable
97 | fun DayCard(
98 | modifier: Modifier = Modifier,
99 | month: String,
100 | day: String,
101 | ) {
102 | Card {
103 | Column(
104 | modifier = modifier,
105 | horizontalAlignment = Alignment.CenterHorizontally
106 | ) {
107 | Text(text = month)
108 | Text(text = day)
109 | }
110 | }
111 | }
112 |
113 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/components/WheelPicker.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.components
2 |
3 | import androidx.compose.animation.animateContentSize
4 | import androidx.compose.foundation.Canvas
5 | import androidx.compose.foundation.background
6 | import androidx.compose.foundation.clickable
7 | import androidx.compose.foundation.layout.Arrangement
8 | import androidx.compose.foundation.layout.Box
9 | import androidx.compose.foundation.layout.defaultMinSize
10 | import androidx.compose.foundation.layout.fillMaxHeight
11 | import androidx.compose.foundation.layout.height
12 | import androidx.compose.foundation.layout.padding
13 | import androidx.compose.foundation.layout.wrapContentSize
14 | import androidx.compose.foundation.lazy.LazyColumn
15 | import androidx.compose.foundation.lazy.rememberLazyListState
16 | import androidx.compose.foundation.shape.RoundedCornerShape
17 | import androidx.compose.material3.MaterialTheme
18 | import androidx.compose.material3.Text
19 | import androidx.compose.runtime.Composable
20 | import androidx.compose.runtime.LaunchedEffect
21 | import androidx.compose.runtime.getValue
22 | import androidx.compose.runtime.mutableIntStateOf
23 | import androidx.compose.runtime.remember
24 | import androidx.compose.runtime.setValue
25 | import androidx.compose.runtime.snapshotFlow
26 | import androidx.compose.ui.Alignment
27 | import androidx.compose.ui.Modifier
28 | import androidx.compose.ui.draw.clip
29 | import androidx.compose.ui.graphics.Brush
30 | import androidx.compose.ui.graphics.Color
31 | import androidx.compose.ui.platform.LocalDensity
32 | import androidx.compose.ui.text.TextStyle
33 | import androidx.compose.ui.text.font.FontFamily
34 | import androidx.compose.ui.text.style.TextAlign
35 | import androidx.compose.ui.unit.Dp
36 | import androidx.compose.ui.unit.dp
37 |
38 | @Composable
39 | internal fun WheelPicker(
40 | modifier: Modifier = Modifier,
41 | options: List,
42 | initialValueIndex: Int = 0,
43 | textStyle: TextStyle = MaterialTheme.typography.headlineMedium,
44 | textColor: Color = MaterialTheme.colorScheme.onSurface,
45 | selectedTextColor: Color = MaterialTheme.colorScheme.primary,
46 | backGroundColor: Color = MaterialTheme.colorScheme.background,
47 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
48 | fontFamily: FontFamily = FontFamily.Default,
49 | onValueSelected: (index: Int, value: String) -> Unit,
50 | ) {
51 |
52 |
53 | var selectedIndex by remember { mutableIntStateOf(initialValueIndex) }
54 | val denisty = LocalDensity.current
55 | val itemSize = with(denisty) { (textStyle.fontSize * 2).toDp() }
56 | val lazyListState = rememberLazyListState(
57 | initialFirstVisibleItemIndex = initialValueIndex
58 | )
59 |
60 | LaunchedEffect(lazyListState) {
61 | snapshotFlow { lazyListState.isScrollInProgress }
62 | .collect { isScrolling ->
63 | if (!isScrolling) {
64 | val index = lazyListState.firstVisibleItemIndex
65 | val middleIndex = index
66 | lazyListState.animateScrollToItem(middleIndex)
67 | selectedIndex = middleIndex
68 | onValueSelected(selectedIndex, options[index])
69 | }
70 | }
71 | }
72 |
73 | Box(
74 | modifier = modifier.height((itemSize * 3)),
75 | ) {
76 | LazyColumn(
77 | modifier = Modifier
78 | .fillMaxHeight()
79 | .animateContentSize(),
80 | state = lazyListState,
81 | verticalArrangement = Arrangement.Center,
82 | ) {
83 | item { FillerItem(modifier = Modifier.height(itemSize)) }
84 | items(count = options.size, key = { index -> index }) { item ->
85 | Text(
86 | modifier = Modifier
87 | .height(itemSize)
88 | .wrapContentSize(align = Alignment.Center)
89 | .clip(RoundedCornerShape(itemSize / 4))
90 | .background(
91 | if (item == selectedIndex) {
92 | selectedItemBackgroundColor
93 | } else {
94 | backGroundColor
95 | }
96 | )
97 | .padding(itemSize / 10),
98 | text = options[item],
99 | style = textStyle,
100 | textAlign = TextAlign.Center,
101 | fontFamily = fontFamily,
102 | color = if (item == selectedIndex) {
103 | selectedTextColor
104 | } else {
105 | textColor
106 | }
107 | )
108 | }
109 | item { FillerItem(modifier = Modifier.height(itemSize)) }
110 | }
111 | Canvas(
112 | modifier = Modifier
113 | .matchParentSize()
114 | .height(itemSize)
115 | .align(Alignment.TopStart)
116 | ) {
117 | drawRect(
118 | brush = Brush.verticalGradient(
119 | .1f to backGroundColor,
120 | .3f to Color.Unspecified,
121 | .7f to Color.Unspecified,
122 | .9f to backGroundColor,
123 | )
124 | )
125 | }
126 | }
127 |
128 | }
129 |
130 | @Composable
131 | private fun FillerItem(modifier: Modifier = Modifier) {
132 | Canvas(modifier = modifier) {}
133 | }
--------------------------------------------------------------------------------
/sample/src/main/java/com/hshamkhani/persian_dtpicker/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persian_dtpicker
2 |
3 | import android.content.Context
4 | import android.os.Bundle
5 | import androidx.activity.ComponentActivity
6 | import androidx.activity.compose.setContent
7 | import androidx.activity.enableEdgeToEdge
8 | import androidx.compose.foundation.layout.Arrangement
9 | import androidx.compose.foundation.layout.Box
10 | import androidx.compose.foundation.layout.Column
11 | import androidx.compose.foundation.layout.fillMaxSize
12 | import androidx.compose.foundation.layout.fillMaxWidth
13 | import androidx.compose.foundation.layout.padding
14 | import androidx.compose.material3.MaterialTheme
15 | import androidx.compose.material3.Scaffold
16 | import androidx.compose.material3.Text
17 | import androidx.compose.runtime.Composable
18 | import androidx.compose.runtime.getValue
19 | import androidx.compose.runtime.mutableStateOf
20 | import androidx.compose.runtime.remember
21 | import androidx.compose.runtime.setValue
22 | import androidx.compose.ui.Alignment
23 | import androidx.compose.ui.Modifier
24 | import androidx.compose.ui.platform.LocalContext
25 | import androidx.compose.ui.unit.dp
26 | import com.hshamkhani.persian_dtpicker.ui.theme.PersiandtpickerTheme
27 | import com.hshamkhani.persiandtpicker.calendar.PersianCalendar
28 | import com.hshamkhani.persiandtpicker.picker.PersianDatePicker
29 | import com.hshamkhani.persiandtpicker.picker.TimePicker
30 | import com.hshamkhani.persiandtpicker.utils.SimpleDate
31 | import com.hshamkhani.persiandtpicker.utils.SimpleTime
32 | import com.hshamkhani.persiandtpicker.utils.toCalendar
33 | import com.hshamkhani.persiandtpicker.utils.totGregorianDate
34 |
35 | class MainActivity : ComponentActivity() {
36 | override fun onCreate(savedInstanceState: Bundle?) {
37 | super.onCreate(savedInstanceState)
38 | enableEdgeToEdge()
39 | setContent {
40 | PersiandtpickerTheme {
41 | Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
42 | SamplePersianCalendar(modifier = Modifier.padding(innerPadding))
43 | // SamplePersianDatePicker()
44 | }
45 | }
46 | }
47 | }
48 | }
49 |
50 | @Composable
51 | fun SamplePersianCalendar(
52 | modifier: Modifier = Modifier,
53 | ) {
54 | Column(
55 | modifier = modifier,
56 | ) {
57 | PersianCalendar(
58 | onDateSelected = { simpleDate ->
59 |
60 | }
61 | )
62 |
63 | /* PersianCalendar(
64 | modifier = Modifier.fillMaxWidth().weight(1f),
65 | dayAlign = Alignment.TopStart,
66 | textStyle = MaterialTheme.typography.titleMedium,
67 | textColor = MaterialTheme.colorScheme.onBackground,
68 | backGroundColor = MaterialTheme.colorScheme.background,
69 | selectedItemBorderColor = MaterialTheme.colorScheme.primaryContainer,
70 | dayContent = { day, isSelected ->
71 | if (isSelected) Text(
72 | modifier = Modifier.align(Alignment.Center),
73 | text = "+"
74 | )
75 | },
76 | onDateSelected = { simpleDate ->
77 | }
78 | )*/
79 | }
80 | }
81 |
82 | @Composable
83 | fun SamplePersianDatePicker(
84 | modifier: Modifier = Modifier,
85 | ) {
86 | val context = LocalContext.current
87 | var selectedDate by remember { mutableStateOf(SimpleDate.now(context)) }
88 | Column(
89 | modifier = modifier.fillMaxSize(),
90 | horizontalAlignment = Alignment.CenterHorizontally,
91 | verticalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterVertically)
92 | ) {
93 | Text(
94 | modifier = Modifier
95 | .padding(16.dp),
96 | style = MaterialTheme.typography.titleLarge,
97 | text = selectedDate.toString()
98 | )
99 |
100 | Text(
101 | modifier = Modifier
102 | .padding(16.dp),
103 | style = MaterialTheme.typography.titleLarge,
104 | text = selectedDate.totGregorianDate().toString()
105 | )
106 |
107 | Text(
108 | modifier = Modifier
109 | .padding(16.dp),
110 | style = MaterialTheme.typography.titleLarge,
111 | text = selectedDate.totGregorianDate().toCalendar().time.toString()
112 | )
113 |
114 | PersianDatePicker(
115 | withTimePicker = true,
116 | onDateSelected = { date ->
117 | selectedDate = date
118 | },
119 | textStyle = MaterialTheme.typography.titleLarge,
120 | textColor = MaterialTheme.colorScheme.onBackground,
121 | selectedTextColor = MaterialTheme.colorScheme.onBackground,
122 | backGroundColor = MaterialTheme.colorScheme.background,
123 | selectedItemBackgroundColor = MaterialTheme.colorScheme.surfaceContainerHighest,
124 | )
125 | }
126 | }
127 |
128 | @Composable
129 | fun PersianDtPickerExample(
130 | modifier: Modifier = Modifier,
131 | context: Context
132 | ) {
133 | var selectedDate by remember { mutableStateOf(SimpleDate.now(context = context)) }
134 |
135 | PersianDatePicker(
136 | modifier = modifier,
137 | withTimePicker = true,
138 | onDateSelected = { date: SimpleDate ->
139 | selectedDate = date
140 | },
141 | )
142 | }
143 |
144 | @Composable
145 | fun TimePickerExample(
146 | modifier: Modifier = Modifier,
147 | context: Context
148 | ) {
149 | var selectedDate by remember { mutableStateOf(SimpleTime.now(context = context)) }
150 |
151 | TimePicker(
152 | modifier = modifier,
153 | onTimeChanged = { time: SimpleTime ->
154 | selectedDate = time
155 | },
156 | )
157 | }
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/utils/SimpleDateTime.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.utils
2 |
3 | import android.content.Context
4 | import android.text.format.DateFormat
5 | import com.hshamkhani.persiandtpicker.utils.gregorianToJalali
6 | import java.time.Instant
7 | import java.time.LocalDateTime
8 | import java.time.LocalTime
9 | import java.time.ZoneId
10 | import java.util.Calendar
11 | import java.util.Date
12 |
13 | /**
14 | * Represents a simple date (either gregorian or jalali)
15 | * with year, month, day, and an optional time.
16 | *
17 | * * `year`: The year of the date (e.g., 1402 for Jalali, 2023 for Gregorian).
18 | * * `month`: The month of the date (1-12 for both Jalali and Gregorian).
19 | * * `day`: The day of the month (1-31 depending on the month).
20 | * * `time`: An `SimpleTime` object representing the time of the day.
21 | * */
22 | data class SimpleDate(
23 | val year: Int,
24 | val month: Int,
25 | val day: Int,
26 | val time: SimpleTime = SimpleTime()
27 | ) {
28 |
29 | companion object {
30 | /**
31 | * Represents current jalali date and time as a `SimpleDate` object.
32 | * */
33 | fun now(context: Context): SimpleDate {
34 | val date = currentJalaliDate()
35 | val dateTime = LocalDateTime.now(ZoneId.systemDefault())
36 | val is24Hour = DateFormat.is24HourFormat(context)
37 |
38 | return SimpleDate(
39 | year = date.component1(),
40 | month = date.component2(),
41 | day = date.component3(),
42 | time = SimpleTime(
43 | hour = dateTime.hour,
44 | minute = dateTime.minute,
45 | clockPeriod = if (is24Hour) null else {
46 | if (dateTime.hour < 12) ClockPeriod.Am else ClockPeriod.Pm
47 | }
48 | ),
49 | )
50 |
51 | }
52 |
53 | /**
54 | * This function converts a timestamp (in milliseconds) to a jalali `SimpleDate` object.
55 | *
56 | * * `timestamp`: The timestamp in milliseconds.
57 | * */
58 | fun from(timestamp: Long, context: Context): SimpleDate {
59 | val localDateTime = Date(timestamp).let { date ->
60 | LocalDateTime.ofInstant(
61 | Instant.ofEpochMilli(date.time),
62 | ZoneId.systemDefault()
63 | )
64 | }
65 |
66 | val jalaliDt = gregorianToJalali(
67 | gy = localDateTime.year,
68 | gm = localDateTime.monthValue,
69 | gd = localDateTime.dayOfMonth
70 | )
71 |
72 | val dateTime = LocalDateTime.now(ZoneId.systemDefault())
73 | val is24Hour = DateFormat.is24HourFormat(context)
74 |
75 | return SimpleDate(
76 | year = jalaliDt.component1(),
77 | month = jalaliDt.component2(),
78 | day = jalaliDt.component3(),
79 | time = SimpleTime(
80 | hour = dateTime.hour,
81 | minute = dateTime.minute,
82 | clockPeriod = if (is24Hour) null else {
83 | if (dateTime.hour < 12) ClockPeriod.Am else ClockPeriod.Pm
84 | }
85 | )
86 | )
87 | }
88 | }
89 |
90 | override fun toString(): String {
91 | return "${year}-${month}-${day}" +
92 | " ${time.hour}:${time.minute} ${time.clockPeriod ?: ""}"
93 | }
94 | }
95 |
96 | /**
97 | * Represents a time with hour, minute, second, and an clock period (AM/PM)
98 | * if the clock in 12-hour time format.
99 | *
100 | * * `hour`: hour The hour of the time (0-23 for 24-hour format,
101 | * 1-12 for 12-hour format).
102 | * * `minute`: minute The minute of the time (0-59).
103 | * * `clockPeriod`: An optional `ClockPeriod` enum value that indicates that the time is in 12-hour format.
104 | * */
105 | data class SimpleTime(
106 | val hour: Int = 0,
107 | val minute: Int = 0,
108 | val clockPeriod: ClockPeriod? = null
109 | ) {
110 | companion object {
111 | /**
112 | * This function returns the current time as a `SimpleTime` object.
113 | * */
114 | fun now(context: Context): SimpleTime {
115 | val time = LocalTime.now(ZoneId.systemDefault())
116 | val is24Hour = DateFormat.is24HourFormat(context)
117 |
118 | return SimpleTime(
119 | hour = time.hour,
120 | minute = time.minute,
121 | clockPeriod = if (is24Hour) null else {
122 | if (time.hour < 12) ClockPeriod.Am else ClockPeriod.Pm
123 | }
124 | )
125 | }
126 | }
127 | }
128 |
129 |
130 | /**
131 | * This function converts a Jalali `SimpleDate` to a Gregorian `SimpleDate`.
132 | *
133 | * @param year The Jalali year.
134 | * @param month The Jalali month.
135 | * @param day The Jalali day.
136 | *
137 | * @return A SimpleDate object representing the Gregorian date.
138 | * */
139 | fun SimpleDate.totGregorianDate(): SimpleDate {
140 | val (year, month, day) = jalaliToGregorian(
141 | jy = year,
142 | jm = month,
143 | jd = day
144 | )
145 | return SimpleDate(
146 | year = year,
147 | month = month,
148 | day = day,
149 | time = time
150 | )
151 | }
152 |
153 | /**
154 | * This function converts a Gregorian `SimpleDate` to a Calendar.
155 | *
156 | * @param this The SimpleDate object representing the Gregorian date.
157 | * @return A Calendar object.
158 | * */
159 |
160 | fun SimpleDate.toCalendar(): Calendar {
161 | val calendar = Calendar.Builder().setCalendarType("gregorian").build()
162 | calendar.apply {
163 | set(Calendar.YEAR, year)
164 | set(Calendar.MONTH, month - 1) // Calendar months are 0-based
165 | set(Calendar.DAY_OF_MONTH, day)
166 | set(Calendar.MINUTE, this@toCalendar.time.minute)
167 | if (this@toCalendar.time.clockPeriod != null) {
168 | set(
169 | Calendar.AM_PM,
170 | if (this@toCalendar.time.clockPeriod == ClockPeriod.Am) Calendar.AM else Calendar.PM
171 | )
172 | set(Calendar.HOUR, this@toCalendar.time.hour)
173 | } else {
174 | set(Calendar.HOUR_OF_DAY, this@toCalendar.time.hour)
175 | }
176 | }
177 | return calendar
178 | }
179 |
180 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/picker/PersianDatePicker.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.picker
2 |
3 | import androidx.compose.foundation.layout.Arrangement
4 | import androidx.compose.foundation.layout.Row
5 | import androidx.compose.foundation.layout.Spacer
6 | import androidx.compose.foundation.layout.width
7 | import androidx.compose.material3.MaterialTheme
8 | import androidx.compose.runtime.Composable
9 | import androidx.compose.runtime.CompositionLocalProvider
10 | import androidx.compose.runtime.LaunchedEffect
11 | import androidx.compose.runtime.getValue
12 | import androidx.compose.runtime.mutableStateOf
13 | import androidx.compose.runtime.remember
14 | import androidx.compose.runtime.setValue
15 | import androidx.compose.ui.Modifier
16 | import androidx.compose.ui.graphics.Color
17 | import androidx.compose.ui.platform.LocalContext
18 | import androidx.compose.ui.platform.LocalLayoutDirection
19 | import androidx.compose.ui.text.TextStyle
20 | import androidx.compose.ui.text.font.FontFamily
21 | import androidx.compose.ui.text.intl.Locale
22 | import androidx.compose.ui.unit.LayoutDirection
23 | import androidx.compose.ui.unit.dp
24 | import com.hshamkhani.persiandtpicker.components.WheelPicker
25 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.formatToHindiIfLanguageIsFa
26 | import com.hshamkhani.persiandtpicker.utils.SimpleDate
27 | import com.hshamkhani.persiandtpicker.utils.initDaysList
28 | import com.hshamkhani.persiandtpicker.utils.initMonthDays
29 | import com.hshamkhani.persiandtpicker.utils.initMonthList
30 | import com.hshamkhani.persiandtpicker.utils.initYearList
31 |
32 | /**
33 | * A composable function that displays a Persian date picker with options
34 | * for selecting the day, month, and year.
35 | *
36 | * @param modifier The modifier to be applied to the time picker.
37 | * @param textStyle The text style for the time picker options.
38 | * @param textColor The color of the text for the time picker options.
39 | * @param selectedTextColor The color of the text for the selected time picker option.
40 | * @param backGroundColor The background color of the time picker.
41 | * @param selectedItemBackgroundColor The background color of the selected item in the time picker.
42 | * @param fontFamily The font family to be used for the text in the time picker.
43 | * @param onDateSelected A callback function that is invoked when the selected time changes,
44 | * it receives a `SimpleDate` object representing the selected time.
45 | * */
46 |
47 | @Composable
48 | fun PersianDatePicker(
49 | modifier: Modifier = Modifier,
50 | withTimePicker: Boolean = false,
51 | textStyle: TextStyle = MaterialTheme.typography.headlineMedium,
52 | fontFamily: FontFamily = FontFamily.Default,
53 | textColor: Color = MaterialTheme.colorScheme.onSurface,
54 | selectedTextColor: Color = MaterialTheme.colorScheme.primary,
55 | backGroundColor: Color = MaterialTheme.colorScheme.background,
56 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
57 | onDateSelected: (date: SimpleDate) -> Unit,
58 | ) {
59 |
60 | val context = LocalContext.current
61 |
62 | val initialDate by remember { mutableStateOf(SimpleDate.now(context = context)) }
63 | var simpleDate by remember { mutableStateOf(initialDate) }
64 |
65 | LaunchedEffect(simpleDate) {
66 | onDateSelected(simpleDate)
67 | }
68 |
69 | val years by remember {
70 | mutableStateOf(
71 | initYearList(initialDate.year)
72 | .map { it.toString().formatToHindiIfLanguageIsFa() })
73 | }
74 |
75 | val months by remember {
76 | mutableStateOf(
77 | initMonthList()
78 | )
79 | }
80 |
81 | val days by remember(simpleDate.month) {
82 | val monthLength =
83 | initMonthDays(simpleDate.month, simpleDate.year).size
84 | mutableStateOf(
85 | initDaysList(monthLength).map { it }
86 | )
87 | }
88 |
89 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
90 | Row(
91 | modifier = modifier,
92 | horizontalArrangement = Arrangement.spacedBy(4.dp)
93 | ) {
94 | WheelPicker(
95 | options = years,
96 | fontFamily = fontFamily,
97 | textStyle = textStyle,
98 | textColor = textColor,
99 | selectedTextColor = selectedTextColor,
100 | backGroundColor = backGroundColor,
101 | selectedItemBackgroundColor = selectedItemBackgroundColor,
102 | onValueSelected = { index, value ->
103 | simpleDate = simpleDate.copy(
104 | year = value.toInt()
105 | )
106 | }
107 | )
108 |
109 | WheelPicker(
110 | options = months,
111 | initialValueIndex = initialDate.month - 1,
112 | fontFamily = fontFamily,
113 | textStyle = textStyle,
114 | textColor = textColor,
115 | selectedTextColor = selectedTextColor,
116 | backGroundColor = backGroundColor,
117 | selectedItemBackgroundColor = selectedItemBackgroundColor,
118 | onValueSelected = { index, _ ->
119 | simpleDate = simpleDate.copy(
120 | month = index + 1 // month is 1-based,
121 | )
122 | }
123 | )
124 |
125 | WheelPicker(
126 | modifier = Modifier,
127 | options = days,
128 | initialValueIndex = initialDate.day - 1,
129 | fontFamily = fontFamily,
130 | textStyle = textStyle,
131 | textColor = textColor,
132 | selectedTextColor = selectedTextColor,
133 | backGroundColor = backGroundColor,
134 | selectedItemBackgroundColor = selectedItemBackgroundColor,
135 | onValueSelected = { index, value ->
136 | simpleDate = simpleDate.copy(
137 | day = value.toInt(),
138 | )
139 | }
140 | )
141 |
142 |
143 | if (withTimePicker) {
144 | TimePicker(
145 | modifier = Modifier,
146 | textStyle = textStyle,
147 | fontFamily = fontFamily,
148 | textColor = textColor,
149 | selectedTextColor = selectedTextColor,
150 | backGroundColor = backGroundColor,
151 | selectedItemBackgroundColor = selectedItemBackgroundColor,
152 | ) { time ->
153 | simpleDate = simpleDate.copy(
154 | time = time
155 | )
156 | }
157 | Spacer(Modifier.width(8.dp))
158 | }
159 |
160 | }
161 | }
162 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/picker/TimePicker.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.picker
2 |
3 | import android.text.format.DateFormat
4 | import androidx.compose.foundation.layout.Row
5 | import androidx.compose.foundation.layout.Spacer
6 | import androidx.compose.foundation.layout.width
7 | import androidx.compose.foundation.layout.wrapContentSize
8 | import androidx.compose.material3.MaterialTheme
9 | import androidx.compose.material3.Text
10 | import androidx.compose.runtime.Composable
11 | import androidx.compose.runtime.CompositionLocalProvider
12 | import androidx.compose.runtime.LaunchedEffect
13 | import androidx.compose.runtime.getValue
14 | import androidx.compose.runtime.mutableStateOf
15 | import androidx.compose.runtime.remember
16 | import androidx.compose.runtime.setValue
17 | import androidx.compose.ui.Alignment
18 | import androidx.compose.ui.Modifier
19 | import androidx.compose.ui.graphics.Color
20 | import androidx.compose.ui.platform.LocalContext
21 | import androidx.compose.ui.platform.LocalLayoutDirection
22 | import androidx.compose.ui.text.TextStyle
23 | import androidx.compose.ui.text.font.FontFamily
24 | import androidx.compose.ui.text.intl.Locale
25 | import androidx.compose.ui.unit.LayoutDirection
26 | import androidx.compose.ui.unit.dp
27 | import com.hshamkhani.persiandtpicker.components.WheelPicker
28 | import com.hshamkhani.persiandtpicker.utils.ClockPeriod
29 | import com.hshamkhani.persiandtpicker.utils.SimpleTime
30 | import com.hshamkhani.persiandtpicker.utils.currentDate
31 | import com.hshamkhani.persiandtpicker.utils.initAmPm
32 | import com.hshamkhani.persiandtpicker.utils.initHours
33 | import com.hshamkhani.persiandtpicker.utils.initMinutes
34 |
35 |
36 | /**
37 | * A composable function that displays a time picker with options for hours, minutes, and AM/PM (if not in 24-hour format).
38 | *
39 | * @param modifier The modifier to be applied to the time picker.
40 | * @param textStyle The text style for the time picker options.
41 | * @param textColor The color of the text for the time picker options.
42 | * @param selectedTextColor The color of the text for the selected time picker option.
43 | * @param backGroundColor The background color of the time picker.
44 | * @param selectedItemBackgroundColor The background color of the selected item in the time picker.
45 | * @param fontFamily The font family to be used for the text in the time picker.
46 | * @param onTimeChanged A callback function that is invoked when the selected time changes,
47 | * it receives a `SimpleTime` object representing the selected time.
48 | *
49 | **/
50 |
51 | @Composable
52 | fun TimePicker(
53 | modifier: Modifier = Modifier,
54 | textStyle: TextStyle = MaterialTheme.typography.headlineMedium,
55 | textColor: Color = MaterialTheme.colorScheme.onSurface,
56 | selectedTextColor: Color = MaterialTheme.colorScheme.primary,
57 | backGroundColor: Color = MaterialTheme.colorScheme.background,
58 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
59 | fontFamily: FontFamily = FontFamily.Default,
60 | onTimeChanged: (SimpleTime) -> Unit,
61 | ) {
62 |
63 | val context = LocalContext.current
64 | val locale = Locale.current
65 |
66 | val now by remember { mutableStateOf(currentDate()) }
67 |
68 | val is24Hour = DateFormat.is24HourFormat(context)
69 |
70 | var selectedTime by remember {
71 | mutableStateOf(now)
72 | }
73 |
74 | var simpleTime by remember(selectedTime) {
75 | mutableStateOf(
76 | SimpleTime(
77 | hour = now.hour,
78 | minute = now.minute,
79 | clockPeriod = if (is24Hour) null else {
80 | if (now.hour < 12) {
81 | ClockPeriod.Am
82 | } else {
83 | ClockPeriod.Pm
84 | }
85 | }
86 | )
87 | )
88 | }
89 |
90 | val hours by remember {
91 | mutableStateOf(
92 | initHours(is24Hour)
93 | )
94 | }
95 |
96 | val minutes by remember {
97 | mutableStateOf(
98 | initMinutes()
99 | )
100 | }
101 |
102 | val amPm by remember {
103 | mutableStateOf(
104 | initAmPm(locale.language != "fa")
105 | )
106 | }
107 |
108 | LaunchedEffect(simpleTime) {
109 | onTimeChanged(simpleTime)
110 | }
111 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
112 | Row(
113 | modifier = modifier.wrapContentSize(),
114 | verticalAlignment = Alignment.CenterVertically
115 | ) {
116 | WheelPicker(
117 | modifier = Modifier,
118 | options = hours,
119 | initialValueIndex = if (is24Hour) now.hour else if (now.hour == 0 || now.hour == 12) 11 else (now.hour % 12) - 1,
120 | fontFamily = fontFamily,
121 | textStyle = textStyle,
122 | textColor = textColor,
123 | selectedTextColor = selectedTextColor,
124 | backGroundColor = backGroundColor,
125 | selectedItemBackgroundColor = selectedItemBackgroundColor,
126 | onValueSelected = { index, value ->
127 | simpleTime = simpleTime.copy(
128 | hour = value.toInt(),
129 | clockPeriod = if (is24Hour) null else {
130 | if (value.toInt() < 12) ClockPeriod.Am else ClockPeriod.Pm
131 | }
132 | )
133 | }
134 | )
135 |
136 | Text(
137 | text = ":",
138 | style = textStyle,
139 | color = MaterialTheme.colorScheme.primary
140 | )
141 |
142 | WheelPicker(
143 | modifier = Modifier,
144 | options = minutes,
145 | initialValueIndex = now.minute,
146 | fontFamily = fontFamily,
147 | textStyle = textStyle,
148 | textColor = textColor,
149 | selectedTextColor = selectedTextColor,
150 | backGroundColor = backGroundColor,
151 | selectedItemBackgroundColor = selectedItemBackgroundColor,
152 | onValueSelected = { insex, _ ->
153 | simpleTime = simpleTime.copy(
154 | minute = insex
155 | )
156 | }
157 | )
158 |
159 | if (!is24Hour) {
160 | Spacer(Modifier.width(4.dp))
161 | WheelPicker(
162 | modifier = Modifier,
163 | options = amPm.toList(),
164 | initialValueIndex = if (simpleTime.clockPeriod == ClockPeriod.Am) 0 else 1,
165 | fontFamily = fontFamily,
166 | textStyle = textStyle,
167 | textColor = textColor,
168 | selectedTextColor = selectedTextColor,
169 | backGroundColor = backGroundColor,
170 | selectedItemBackgroundColor = selectedItemBackgroundColor,
171 | onValueSelected = { index, _ ->
172 | simpleTime = simpleTime.copy(
173 | clockPeriod = if (index == 0) ClockPeriod.Am else ClockPeriod.Pm
174 | )
175 | }
176 | )
177 | }
178 | }
179 | }
180 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/utils/DatePickerUtils.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.utils
2 |
3 | import android.icu.util.Calendar
4 | import android.util.Log
5 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.padZeroToStartWithPersianDigits
6 | import kotlinx.collections.immutable.ImmutableList
7 | import kotlinx.collections.immutable.toImmutableList
8 | import java.time.LocalDate
9 | import java.time.LocalTime
10 | import java.time.ZoneId
11 | import java.util.Locale
12 |
13 | private val englishWeekDays = listOf(
14 | "Saturday",
15 | "Sunday",
16 | "Monday",
17 | "Tuesday",
18 | "Wednesday",
19 | "Thursday",
20 | "Friday",
21 | )
22 |
23 | private val persianWeekDays = listOf(
24 | "شنیه",
25 | "یک\u200Cشنبه",
26 | "دوشنبه",
27 | "سه\u200Cشنبه",
28 | "چهارشنبه",
29 | "پتج\u200Cشنبه",
30 | "جمعه",
31 | )
32 |
33 | private val farsiMonthNames = listOf(
34 | "فروردین",
35 | "اردیبهشت",
36 | "خرداد",
37 | "تیر",
38 | "مرداد",
39 | "شهریور",
40 | "مهر",
41 | "آبان",
42 | "آذر",
43 | "دی",
44 | "بهمن",
45 | "اسفند"
46 | )
47 |
48 | private val englishMonthNames = listOf(
49 | "Farvardin",
50 | "Ordibehesht",
51 | "Khordad",
52 | "Tir",
53 | "Mordad",
54 | "Shahrivar",
55 | "Mehr",
56 | "Aban",
57 | "Azar",
58 | "Day",
59 | "Bahman",
60 | "Esfand"
61 | )
62 |
63 | fun weekDays() = if (Locale.getDefault().language == "fa") persianWeekDays else englishWeekDays
64 |
65 | fun SimpleDate.dayOfWeek(): Int {
66 | val calendar = totGregorianDate().toCalendar()
67 | return when (calendar.get(Calendar.DAY_OF_WEEK)) {
68 | Calendar.SATURDAY -> 1
69 | Calendar.SUNDAY -> 2
70 | Calendar.MONDAY -> 2
71 | Calendar.TUESDAY -> 4
72 | Calendar.WEDNESDAY -> 5
73 | Calendar.THURSDAY -> 6
74 | Calendar.FRIDAY -> 7
75 | else -> 1
76 | }
77 | }
78 |
79 | fun initHours(is24h: Boolean): List {
80 | val values = mutableListOf()
81 | val repeatValue = if (is24h) 24 else 12
82 | repeat(repeatValue) { index ->
83 | val hour = if (is24h) index else index + 1
84 | values.add(hour.padZeroToStartWithPersianDigits())
85 | }
86 | return values
87 | }
88 |
89 | fun initDaysList(monthLength: Int): List {
90 | val values = mutableListOf()
91 | repeat(monthLength) { monthDay ->
92 | values.add((monthDay + 1).padZeroToStartWithPersianDigits())
93 | }
94 | return values
95 | }
96 |
97 | /**
98 | * Initializes a list of years starting from the given year.
99 | * The list will contain 20 years starting from the given year.
100 | *
101 | * @param shYear The starting year in Jalali calendar.
102 | * @return A list of integers representing the years.
103 | */
104 | fun initYearList(shYear: Int): List {
105 | val values = mutableListOf()
106 | (shYear until shYear + 20).map { year ->
107 | values.add(year)
108 | }
109 | return values
110 | }
111 |
112 | fun initMonthList(): List = if (Locale.getDefault().language == "en") {
113 | englishMonthNames
114 | } else {
115 | farsiMonthNames
116 | }
117 |
118 |
119 | /**
120 | * Initial jalali date from current date.
121 | *
122 | * Returns an IntArray with three elements:
123 | * 1st element is the year, 2nd element is the month,
124 | * and 3rd element is the day.
125 | * */
126 | fun currentJalaliDate() = LocalDate.now().let { now ->
127 | gregorianToJalali(
128 | gy = now.year,
129 | gm = now.monthValue,
130 | gd = now.dayOfMonth
131 | )
132 | }
133 |
134 | /**
135 | * Returns the current time in the system's default time zone.
136 | * */
137 | fun currentDate() = LocalTime.now(ZoneId.systemDefault() ?: ZoneId.of("UTC"))
138 |
139 | fun initMinutes(): List {
140 | val values = mutableListOf()
141 | repeat(60) { index ->
142 | val minute = index
143 | values.add(minute.padZeroToStartWithPersianDigits())
144 | }
145 | return values
146 | }
147 |
148 | fun initAmPm(isEng: Boolean): Pair {
149 | return if (isEng) "AM" to "PM" else "ق.ظ" to "ب.ظ"
150 | }
151 |
152 | private val leapYears = listOf(
153 | 1403,
154 | 1407,
155 | 1411,
156 | 1415,
157 | 1419,
158 | 1423,
159 | 1427,
160 | 1431,
161 | 1435,
162 | 1439
163 | )
164 |
165 | /**
166 | * Returns true if the given jalali year is a leap year.
167 | * */
168 | fun Int.isLeap(): Boolean = this in leapYears
169 |
170 | /**
171 | * Returns a list of integers representing the days in a month
172 | * */
173 |
174 | fun initMonthDays(monthNumber: Int, year: Int): ImmutableList {
175 | val days29 = (1..29).map { it }
176 | val days30 = (1..30).map { it }
177 | val days31 = (1..31).map { it }
178 |
179 | val days = when (monthNumber) {
180 | in 1..6 -> days31
181 | in 7..11 -> days30
182 | 12 -> if (year.isLeap()) days30 else days29
183 | else -> emptyList()
184 | }
185 |
186 | return days.toImmutableList()
187 | }
188 |
189 | fun gregorianToJalali(gy: Int, gm: Int, gd: Int): IntArray {
190 | val g_d_m: IntArray = intArrayOf(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
191 | val gy2: Int = if (gm > 2) (gy + 1) else gy
192 | var days: Int =
193 | 355666 + (365 * gy) + ((gy2 + 3) / 4) - ((gy2 + 99) / 100) + ((gy2 + 399) / 400) + gd + g_d_m[gm - 1]
194 | var jy: Int = -1595 + (33 * (days / 12053))
195 | days %= 12053
196 | jy += 4 * (days / 1461)
197 | days %= 1461
198 | if (days > 365) {
199 | jy += ((days - 1) / 365)
200 | days = (days - 1) % 365
201 | }
202 | var jm: Int;
203 | var jd: Int;
204 | if (days < 186) {
205 | jm = 1 + (days / 31)
206 | jd = 1 + (days % 31)
207 | } else {
208 | jm = 7 + ((days - 186) / 30)
209 | jd = 1 + ((days - 186) % 30)
210 | }
211 | return intArrayOf(jy, jm, jd)
212 | }
213 |
214 | /**
215 | * Returns the Gregorian date corresponding to the given Jalali date.
216 | * */
217 |
218 | fun jalaliToGregorian(jy: Int, jm: Int, jd: Int): IntArray {
219 | val jy1: Int = jy + 1595
220 | var days: Int =
221 | -355668 + (365 * jy1) + ((jy1 / 33) * 8) + (((jy1 % 33) + 3) / 4) + jd + (if (jm < 7) ((jm - 1) * 31) else (((jm - 7) * 30) + 186))
222 | var gy: Int = 400 * (days / 146097)
223 | days %= 146097
224 | if (days > 36524) {
225 | gy += 100 * (--days / 36524)
226 | days %= 36524
227 | if (days >= 365) days++
228 | }
229 | gy += 4 * (days / 1461)
230 | days %= 1461
231 | if (days > 365) {
232 | gy += ((days - 1) / 365)
233 | days = (days - 1) % 365
234 | }
235 | var gd: Int = days + 1
236 | val sal_a: IntArray = intArrayOf(
237 | 0,
238 | 31,
239 | if ((gy % 4 == 0 && gy % 100 != 0) || (gy % 400 == 0)) 29 else 28,
240 | 31,
241 | 30,
242 | 31,
243 | 30,
244 | 31,
245 | 31,
246 | 30,
247 | 31,
248 | 30,
249 | 31
250 | )
251 | var gm = 0
252 | while (gm < 13 && gd > sal_a[gm]) gd -= sal_a[gm++]
253 | return intArrayOf(gy, gm, gd)
254 | }
255 |
256 |
257 | fun main() {
258 | val jdt = gregorianToJalali(
259 | gy = 2025,
260 | gm = 10,
261 | gd = 18
262 | )
263 | val gdt = jalaliToGregorian(
264 | jy = 1404,
265 | jm = 7,
266 | jd = 26
267 | )
268 | gdt.forEach {
269 | println(it)
270 | }
271 | jdt.forEach {
272 | println(it)
273 | }
274 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/hshamkhani/persiandtpicker/calendar/PersianCalendar.kt:
--------------------------------------------------------------------------------
1 | package com.hshamkhani.persiandtpicker.calendar
2 |
3 | import android.util.Log
4 | import androidx.compose.foundation.background
5 | import androidx.compose.foundation.border
6 | import androidx.compose.foundation.clickable
7 | import androidx.compose.foundation.layout.Arrangement
8 | import androidx.compose.foundation.layout.Box
9 | import androidx.compose.foundation.layout.BoxScope
10 | import androidx.compose.foundation.layout.Column
11 | import androidx.compose.foundation.layout.FlowRow
12 | import androidx.compose.foundation.layout.FlowRowScope
13 | import androidx.compose.foundation.layout.Row
14 | import androidx.compose.foundation.layout.Spacer
15 | import androidx.compose.foundation.layout.aspectRatio
16 | import androidx.compose.foundation.layout.fillMaxWidth
17 | import androidx.compose.foundation.layout.padding
18 | import androidx.compose.material3.MaterialTheme
19 | import androidx.compose.material3.Text
20 | import androidx.compose.runtime.Composable
21 | import androidx.compose.runtime.LaunchedEffect
22 | import androidx.compose.runtime.derivedStateOf
23 | import androidx.compose.runtime.getValue
24 | import androidx.compose.runtime.mutableStateOf
25 | import androidx.compose.runtime.remember
26 | import androidx.compose.runtime.setValue
27 | import androidx.compose.ui.Alignment
28 | import androidx.compose.ui.Modifier
29 | import androidx.compose.ui.draw.clip
30 | import androidx.compose.ui.graphics.Color
31 | import androidx.compose.ui.graphics.vector.ImageVector
32 | import androidx.compose.ui.platform.LocalContext
33 | import androidx.compose.ui.res.vectorResource
34 | import androidx.compose.ui.text.TextStyle
35 | import androidx.compose.ui.text.style.TextAlign
36 | import androidx.compose.ui.text.style.TextOverflow
37 | import androidx.compose.ui.unit.dp
38 | import androidx.compose.ui.unit.sp
39 | import com.hshamkhani.persiandtpicker.R
40 | import com.hshamkhani.persiandtpicker.components.FlatIconButton
41 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.asStringMonthName
42 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.formatToHindiIfLanguageIsFa
43 | import com.hshamkhani.persiandtpicker.utils.PersianNumberUtils.padZeroToStartWithPersianDigits
44 | import com.hshamkhani.persiandtpicker.utils.SimpleDate
45 | import com.hshamkhani.persiandtpicker.utils.SimpleTime
46 | import com.hshamkhani.persiandtpicker.utils.dayOfWeek
47 | import com.hshamkhani.persiandtpicker.utils.initMonthDays
48 | import com.hshamkhani.persiandtpicker.utils.weekDays
49 |
50 | @Composable
51 | fun PersianCalendar(
52 | modifier: Modifier = Modifier,
53 | textStyle: TextStyle = MaterialTheme.typography.bodyLarge,
54 | textColor: Color = MaterialTheme.colorScheme.onSurface,
55 | backGroundColor: Color = MaterialTheme.colorScheme.background,
56 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.primaryContainer,
57 | selectedDate: SimpleDate? = null,
58 | onDateSelected: (date: SimpleDate) -> Unit,
59 | ) {
60 | PersianCalendarImpl(
61 | modifier = modifier,
62 | textStyle = textStyle,
63 | textColor = textColor,
64 | backGroundColor = backGroundColor,
65 | selectedItemBackgroundColor = selectedItemBackgroundColor,
66 | selectedDate = selectedDate,
67 | onDateSelected = onDateSelected,
68 | )
69 | }
70 |
71 | @Composable
72 | fun PersianCalendar(
73 | modifier: Modifier = Modifier,
74 | dayAlign: Alignment = Alignment.Center,
75 | textStyle: TextStyle = MaterialTheme.typography.bodyLarge,
76 | textColor: Color = MaterialTheme.colorScheme.onSurface,
77 | backGroundColor: Color = MaterialTheme.colorScheme.background,
78 | selectedItemBorderColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
79 | dayContent: @Composable BoxScope.(String, Boolean) -> Unit,
80 | selectedDate: SimpleDate? = null,
81 | onDateSelected: (date: SimpleDate) -> Unit,
82 | ) {
83 | PersianCalendarImpl(
84 | modifier = modifier,
85 | dayAlign = dayAlign,
86 | textStyle = textStyle,
87 | textColor = textColor,
88 | backGroundColor = backGroundColor,
89 | selectedItemBackgroundColor = selectedItemBorderColor,
90 | dayContent = dayContent,
91 | selectedDate = selectedDate,
92 | onDateSelected = onDateSelected,
93 | )
94 | }
95 |
96 | @Composable
97 | private fun PersianCalendarImpl(
98 | modifier: Modifier = Modifier,
99 | dayAlign: Alignment = Alignment.Center,
100 | textStyle: TextStyle = MaterialTheme.typography.bodyLarge,
101 | textColor: Color = MaterialTheme.colorScheme.onSurface,
102 | backGroundColor: Color = MaterialTheme.colorScheme.background,
103 | selectedItemBackgroundColor: Color = MaterialTheme.colorScheme.surfaceContainerLowest,
104 | dayContent: (@Composable BoxScope.(String, Boolean) -> Unit)? = null,
105 | selectedDate: SimpleDate? = null,
106 | onDateSelected: (date: SimpleDate) -> Unit,
107 | ) {
108 | val context = LocalContext.current
109 |
110 | val initialDate by remember { mutableStateOf(SimpleDate.now(context = context)) }
111 | var simpleDate by remember { mutableStateOf(selectedDate ?: initialDate) }
112 |
113 | val daysInMonth by remember(simpleDate.month) {
114 | mutableStateOf(
115 | initMonthDays(
116 | monthNumber = simpleDate.month,
117 | year = simpleDate.year
118 | )
119 | )
120 | }
121 |
122 | val daysList = remember(daysInMonth) {
123 | derivedStateOf {
124 | simpleDate = simpleDate.copy(time = SimpleTime())
125 | val start = simpleDate.copy(day = 1).dayOfWeek() - 1
126 | val end = 7 - simpleDate.copy(day = daysInMonth.last()).dayOfWeek() + 1
127 |
128 | val monthDays = (1..daysInMonth.last()).map {
129 | it.toString().formatToHindiIfLanguageIsFa()
130 | }
131 | val padStart = List(start) { "" }
132 | val padEnd = List(end) { "" }
133 |
134 | padStart + monthDays + padEnd
135 | }
136 | }
137 |
138 | LaunchedEffect(simpleDate) {
139 | onDateSelected(simpleDate)
140 | }
141 |
142 | Column(
143 | modifier = modifier
144 | ) {
145 |
146 | // Month and Year
147 | Row(
148 | modifier = Modifier.fillMaxWidth(),
149 | horizontalArrangement = Arrangement.Center,
150 | verticalAlignment = Alignment.CenterVertically
151 | ) {
152 | FlatIconButton(
153 | icon = ImageVector.vectorResource(id = R.drawable.keyboard_arrow_left),
154 | onClick = {
155 | if (initialDate.month == simpleDate.month && initialDate.year == simpleDate.year) return@FlatIconButton
156 | simpleDate = if (simpleDate.month == 1) {
157 | simpleDate.copy(
158 | month = 12,
159 | year = simpleDate.year - 1
160 | )
161 | } else {
162 | simpleDate.copy(
163 | month = simpleDate.month - 1,
164 | )
165 | }
166 | }
167 | )
168 |
169 | Text(
170 | modifier = Modifier
171 | .weight(1f),
172 | text = simpleDate.year.toString().formatToHindiIfLanguageIsFa(),
173 | textAlign = TextAlign.Center,
174 | style = textStyle.copy(
175 | fontSize = 24.sp
176 | )
177 | )
178 | Spacer(modifier = Modifier.weight(1f))
179 | Text(
180 | modifier = Modifier
181 | .weight(1f),
182 | text = simpleDate.month.asStringMonthName(),
183 | textAlign = TextAlign.Center,
184 | style = textStyle.copy(
185 | fontSize = 24.sp
186 | )
187 | )
188 | Text(
189 | modifier = Modifier
190 | .weight(1f),
191 | text = simpleDate.day.padZeroToStartWithPersianDigits(),
192 | textAlign = TextAlign.Center,
193 | style = textStyle.copy(
194 | fontSize = 24.sp
195 | )
196 | )
197 | FlatIconButton(
198 | icon = ImageVector.vectorResource(id = R.drawable.keyboard_arrow_right),
199 | onClick = {
200 | simpleDate = if (simpleDate.month == 12) {
201 | simpleDate.copy(
202 | month = 1,
203 | year = simpleDate.year + 1
204 | )
205 | } else {
206 | simpleDate.copy(
207 | month = simpleDate.month + 1,
208 | )
209 | }
210 | }
211 | )
212 | }
213 |
214 | // Weekday headers
215 | Row(
216 | modifier = Modifier.fillMaxWidth(),
217 | horizontalArrangement = Arrangement.SpaceAround,
218 | verticalAlignment = Alignment.CenterVertically
219 | ) {
220 | weekDays().forEach { weekDay ->
221 | Text(
222 | modifier = Modifier
223 | .weight(1f)
224 | .background(selectedItemBackgroundColor)
225 | .padding(vertical = 4.dp),
226 | text = weekDay.uppercase(),
227 | maxLines = 1,
228 | overflow = TextOverflow.Clip,
229 | textAlign = TextAlign.Center,
230 | style = MaterialTheme.typography.bodyLarge
231 | )
232 | }
233 | }
234 |
235 | // Calendar days
236 | FlowRow(
237 | modifier = Modifier.fillMaxWidth(),
238 | horizontalArrangement = Arrangement.SpaceAround,
239 | itemVerticalAlignment = Alignment.CenterVertically,
240 | maxItemsInEachRow = 7,
241 | maxLines = 6
242 | ) {
243 | daysList.value.forEach { day ->
244 | val isDay = day.isNotBlank()
245 | val isCurrentDay = isDay && day.toInt() == simpleDate.day
246 |
247 | // Day Filler
248 | if (!isDay) {
249 | Box(
250 | modifier = modifier
251 | .weight(1f)
252 | .aspectRatio(1f)
253 | )
254 | }
255 |
256 | // Day with content
257 | if (dayContent != null && isDay) {
258 | DayWitchContent(
259 | isCurrentDay = isCurrentDay,
260 | selectedBorderColor = selectedItemBackgroundColor,
261 | onClick = { simpleDate = simpleDate.copy(day = day.toInt()) },
262 | content = {
263 | dayContent(day, isCurrentDay)
264 | Text(
265 | modifier = Modifier
266 | .align(dayAlign)
267 | .padding(4.dp),
268 | text = day,
269 | style = textStyle,
270 | color = textColor,
271 | )
272 | }
273 | )
274 | }
275 |
276 | // Simple day
277 | if (dayContent == null && isDay) {
278 | Day(
279 | isCurrentDay = isCurrentDay,
280 | backGroundColor = backGroundColor,
281 | selectedDaybackgroundColor = selectedItemBackgroundColor,
282 | onClick = { simpleDate = simpleDate.copy(day = day.toInt()) },
283 | content = {
284 | Text(
285 | modifier = Modifier
286 | .align(Alignment.Center),
287 | text = day,
288 | style = textStyle,
289 | color = textColor,
290 | )
291 | }
292 | )
293 | }
294 | }
295 | }
296 | }
297 | }
298 |
299 | @Composable
300 | private fun FlowRowScope.DayWitchContent(
301 | modifier: Modifier = Modifier,
302 | isCurrentDay: Boolean,
303 | selectedBorderColor: Color,
304 | onClick: () -> Unit,
305 | content: @Composable BoxScope.() -> Unit,
306 | ) {
307 | Box(
308 | modifier = modifier
309 | .weight(1f)
310 | .aspectRatio(1f)
311 | .background(selectedBorderColor.copy(alpha = if (isCurrentDay) .1f else 0f))
312 | .border(
313 | width = 1.dp,
314 | color = if (isCurrentDay) {
315 | selectedBorderColor
316 | } else {
317 | Color.Transparent
318 | },
319 | )
320 | .clickable(onClick = onClick),
321 | content = content
322 | )
323 | }
324 |
325 | @Composable
326 | private fun FlowRowScope.Day(
327 | modifier: Modifier = Modifier,
328 | isCurrentDay: Boolean,
329 | backGroundColor: Color,
330 | selectedDaybackgroundColor: Color,
331 | onClick: () -> Unit,
332 | content: @Composable BoxScope.() -> Unit,
333 | ) {
334 | Box(
335 | modifier = modifier
336 | .weight(1f)
337 | .aspectRatio(1f)
338 | .padding(8.dp)
339 | .clip(MaterialTheme.shapes.large)
340 | .background(
341 | if (isCurrentDay) {
342 | selectedDaybackgroundColor
343 | } else {
344 | backGroundColor
345 | }
346 | )
347 | .clickable(onClick = onClick),
348 | content = content
349 | )
350 | }
--------------------------------------------------------------------------------