From 69567dede9bbf82ca73d1efc330b892f1fdd89ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juliano=20C=C3=A9zar=20Chagas=20Tavares?= Date: Tue, 17 Dec 2024 14:29:28 -0300 Subject: [PATCH] [Android] Display ISO8601 dates with local timestamp (#66) This updates the CredentialDate component to display ISO8601 dates with local timestamps and also disables app rotation for Android. --- .../java/com/spruceid/mobilesdkexample/MainActivity.kt | 5 +++++ .../credentials/AchievementCredentialItem.kt | 8 +++++++- .../mobilesdkexample/credentials/CredentialDate.kt | 5 +++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/example/src/main/java/com/spruceid/mobilesdkexample/MainActivity.kt b/example/src/main/java/com/spruceid/mobilesdkexample/MainActivity.kt index b249fba..aff80c7 100644 --- a/example/src/main/java/com/spruceid/mobilesdkexample/MainActivity.kt +++ b/example/src/main/java/com/spruceid/mobilesdkexample/MainActivity.kt @@ -1,7 +1,9 @@ package com.spruceid.mobilesdkexample +import android.annotation.SuppressLint import android.app.Application import android.content.Intent +import android.content.pm.ActivityInfo import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent @@ -58,9 +60,12 @@ class MainActivity : ComponentActivity() { } } + @SuppressLint("SourceLockedOrientationActivity") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + enableEdgeToEdge() setContent { MobileSdkTheme { diff --git a/example/src/main/java/com/spruceid/mobilesdkexample/credentials/AchievementCredentialItem.kt b/example/src/main/java/com/spruceid/mobilesdkexample/credentials/AchievementCredentialItem.kt index eed9f0d..e7f0a4f 100644 --- a/example/src/main/java/com/spruceid/mobilesdkexample/credentials/AchievementCredentialItem.kt +++ b/example/src/main/java/com/spruceid/mobilesdkexample/credentials/AchievementCredentialItem.kt @@ -58,6 +58,7 @@ import com.spruceid.mobilesdkexample.utils.splitCamelCase import kotlinx.coroutines.launch import org.json.JSONObject import java.time.OffsetDateTime +import java.time.ZoneId import java.time.format.DateTimeFormatter class AchievementCredentialItem : ICredentialView { @@ -386,6 +387,8 @@ class AchievementCredentialItem : ICredentialView { val ISO8601DateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]Z") val parsedDate = OffsetDateTime.parse(awardedDate, ISO8601DateFormat) + val localZoneParsedDate = + parsedDate.atZoneSameInstant(ZoneId.systemDefault()) val dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy 'at' h:mm a") @@ -396,7 +399,10 @@ class AchievementCredentialItem : ICredentialView { Pair(obj["identityType"].toString(), obj["identityHash"].toString()) } } - details?.add(0, Pair("awardedDate", parsedDate.format(dateTimeFormatter))) + details?.add( + 0, + Pair("awardedDate", localZoneParsedDate.format(dateTimeFormatter)) + ) Row( Modifier.padding(horizontal = 12.dp) diff --git a/example/src/main/java/com/spruceid/mobilesdkexample/credentials/CredentialDate.kt b/example/src/main/java/com/spruceid/mobilesdkexample/credentials/CredentialDate.kt index 566c8ca..bcced2d 100644 --- a/example/src/main/java/com/spruceid/mobilesdkexample/credentials/CredentialDate.kt +++ b/example/src/main/java/com/spruceid/mobilesdkexample/credentials/CredentialDate.kt @@ -9,10 +9,10 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.sp -import com.spruceid.mobilesdkexample.ui.theme.ColorStone500 import com.spruceid.mobilesdkexample.ui.theme.ColorStone950 import com.spruceid.mobilesdkexample.ui.theme.Inter import java.time.OffsetDateTime +import java.time.ZoneId import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @@ -25,8 +25,9 @@ fun CredentialDate(dateString: String) { try { val ISO8601DateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]Z") val parsedDate = OffsetDateTime.parse(dateString, ISO8601DateFormat) + val localZoneParsedDate = parsedDate.atZoneSameInstant(ZoneId.systemDefault()) val dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy 'at' h:mm a") - date = parsedDate.format(dateTimeFormatter) + date = localZoneParsedDate.format(dateTimeFormatter) return@LaunchedEffect } catch (_: Exception) { }