Skip to content

Commit 8f30a37

Browse files
Changing way to display date from command (#578)
* Changing way to display date from command * ktlint * Small fix for slash box
1 parent 8a0ecd7 commit 8f30a37

7 files changed

Lines changed: 172 additions & 15 deletions

File tree

writeopia_ui/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ kotlin {
129129
dependencies {
130130
}
131131
}
132+
133+
val jvmTest by getting {
134+
dependencies {
135+
implementation(libs.kotlin.test)
136+
}
137+
}
132138
}
133139
}
134140

writeopia_ui/src/androidMain/kotlin/io/writeopia/ui/utils/DateTimeUtils.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package io.writeopia.ui.utils
22

33
import java.time.LocalDateTime
44
import java.time.format.DateTimeFormatter
5+
import java.time.format.FormatStyle
56

67
actual fun getCurrentDateFormatted(): String {
78
val now = LocalDateTime.now()
8-
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
9+
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
910
return now.format(formatter)
1011
}
1112

1213
actual fun getCurrentDateTimeFormatted(): String {
1314
val now = LocalDateTime.now()
14-
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")
15-
return now.format(formatter)
15+
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
16+
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
17+
return "${now.format(dateFormatter)} ${now.format(timeFormatter)}"
1618
}

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/drawer/content/TextDrawer.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,41 +242,52 @@ class TextDrawer(
242242
val text = value.text
243243
val cursorPos = value.selection.start
244244

245+
println("Detecting slash")
246+
245247
if (sizeDifference > 0) {
248+
println("Detecting slash 1")
246249
// Character was added
247250
val addedChar =
248251
if (cursorPos > 0) text.getOrNull(cursorPos - 1) else null
249252

250253
if (addedChar == '/') {
254+
println("Detecting slash 2")
251255
// Check if '/' is at the start of the line or after a space
252256
val charBefore =
253257
if (cursorPos > 1) text.getOrNull(cursorPos - 2) else null
254258
if (charBefore == null || charBefore == ' ' || charBefore == '\n') {
259+
println("Detecting slash 3")
255260
showSlashCommandPopup = true
256261
slashStartPosition = cursorPos - 1
257262
slashCommandFilter = ""
258263
}
259264
} else if (showSlashCommandPopup && slashStartPosition >= 0) {
265+
println("Detecting slash 3.1")
260266
// Update filter with text after '/'
261267
val filterText =
262268
text.substring(slashStartPosition + 1, cursorPos)
263269
if (filterText.contains(' ') || filterText.contains('\n')) {
270+
println("Detecting slash 4")
264271
// Space or newline typed, close popup
265272
showSlashCommandPopup = false
266273
slashStartPosition = -1
267274
slashCommandFilter = ""
268275
} else {
276+
println("Detecting slash 5")
269277
slashCommandFilter = filterText
270278
}
271279
}
272280
} else if (sizeDifference < 0 && showSlashCommandPopup) {
281+
println("Detecting slash 6")
273282
// Character was deleted
274283
if (cursorPos <= slashStartPosition) {
284+
println("Detecting slash 7")
275285
// Deleted the '/' or before it
276286
showSlashCommandPopup = false
277287
slashStartPosition = -1
278288
slashCommandFilter = ""
279289
} else {
290+
println("Detecting slash 8")
280291
// Update filter
281292
slashCommandFilter =
282293
text.substring(slashStartPosition + 1, cursorPos)
@@ -294,11 +305,13 @@ class TextDrawer(
294305
)
295306
}
296307

297-
onTextEdit(
298-
TextInput(value.text, start, end, spans),
299-
drawInfo.position,
300-
lineBreakByContent,
301-
)
308+
if (!showSlashCommandPopup) {
309+
onTextEdit(
310+
TextInput(value.text, start, end, spans),
311+
drawInfo.position,
312+
lineBreakByContent,
313+
)
314+
}
302315

303316
if (start == 0 || end == 0) {
304317
coroutineScope.launch {

writeopia_ui/src/jvmMain/kotlin/io/writeopia/ui/utils/DateTimeUtils.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package io.writeopia.ui.utils
22

33
import java.time.LocalDateTime
44
import java.time.format.DateTimeFormatter
5+
import java.time.format.FormatStyle
56

67
actual fun getCurrentDateFormatted(): String {
78
val now = LocalDateTime.now()
8-
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
9+
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
910
return now.format(formatter)
1011
}
1112

1213
actual fun getCurrentDateTimeFormatted(): String {
1314
val now = LocalDateTime.now()
14-
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")
15-
return now.format(formatter)
15+
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
16+
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
17+
return "${now.format(dateFormatter)} ${now.format(timeFormatter)}"
1618
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package io.writeopia.ui.utils
2+
3+
import java.time.LocalDate
4+
import java.time.LocalDateTime
5+
import java.util.Locale
6+
import kotlin.test.Test
7+
import kotlin.test.assertTrue
8+
9+
class DateTimeUtilsTest {
10+
11+
@Test
12+
fun `getCurrentDateFormatted returns non-empty string`() {
13+
val result = getCurrentDateFormatted()
14+
assertTrue(result.isNotEmpty(), "Date formatted string should not be empty")
15+
}
16+
17+
@Test
18+
fun `getCurrentDateTimeFormatted returns non-empty string`() {
19+
val result = getCurrentDateTimeFormatted()
20+
assertTrue(result.isNotEmpty(), "DateTime formatted string should not be empty")
21+
}
22+
23+
@Test
24+
fun `getCurrentDateFormatted contains current year`() {
25+
val currentYear = LocalDate.now().year.toString()
26+
val result = getCurrentDateFormatted()
27+
assertTrue(
28+
result.contains(currentYear) || result.contains(currentYear.takeLast(2)),
29+
"Date should contain current year ($currentYear), got: $result"
30+
)
31+
}
32+
33+
@Test
34+
fun `getCurrentDateTimeFormatted contains time separator`() {
35+
val result = getCurrentDateTimeFormatted()
36+
assertTrue(
37+
result.contains(":"),
38+
"DateTime should contain time with colon separator, got: $result"
39+
)
40+
}
41+
42+
@Test
43+
fun `getCurrentDateFormatted uses US locale format when locale is US`() {
44+
val originalLocale = Locale.getDefault()
45+
try {
46+
Locale.setDefault(Locale.US)
47+
val result = getCurrentDateFormatted()
48+
// US format is M/d/yy or similar (month first)
49+
// The result should have the month as the first number
50+
val today = LocalDate.now()
51+
val expectedMonthFirst = "${today.monthValue}/"
52+
assertTrue(
53+
result.startsWith(expectedMonthFirst),
54+
"US locale should have month first. Expected to start with $expectedMonthFirst, got: $result"
55+
)
56+
} finally {
57+
Locale.setDefault(originalLocale)
58+
}
59+
}
60+
61+
@Test
62+
fun `getCurrentDateFormatted uses European locale format when locale is Germany`() {
63+
val originalLocale = Locale.getDefault()
64+
try {
65+
Locale.setDefault(Locale.GERMANY)
66+
val result = getCurrentDateFormatted()
67+
// German format is dd.MM.yy (day first with dots)
68+
val today = LocalDate.now()
69+
val dayString = today.dayOfMonth.toString().padStart(2, '0')
70+
assertTrue(
71+
result.startsWith(dayString),
72+
"German locale should have day first. Expected to start with $dayString, got: $result"
73+
)
74+
} finally {
75+
Locale.setDefault(originalLocale)
76+
}
77+
}
78+
79+
@Test
80+
fun `getCurrentDateFormatted uses UK locale format when locale is UK`() {
81+
val originalLocale = Locale.getDefault()
82+
try {
83+
Locale.setDefault(Locale.UK)
84+
val result = getCurrentDateFormatted()
85+
// UK format is dd/MM/yy (day first with slashes)
86+
val today = LocalDate.now()
87+
val dayString = today.dayOfMonth.toString().padStart(2, '0')
88+
assertTrue(
89+
result.startsWith(dayString),
90+
"UK locale should have day first. Expected to start with $dayString, got: $result"
91+
)
92+
} finally {
93+
Locale.setDefault(originalLocale)
94+
}
95+
}
96+
97+
@Test
98+
fun `getCurrentDateTimeFormatted uses US locale format when locale is US`() {
99+
val originalLocale = Locale.getDefault()
100+
try {
101+
Locale.setDefault(Locale.US)
102+
val result = getCurrentDateTimeFormatted()
103+
val today = LocalDate.now()
104+
val expectedMonthFirst = "${today.monthValue}/"
105+
assertTrue(
106+
result.startsWith(expectedMonthFirst),
107+
"US locale datetime should have month first. Expected to start with $expectedMonthFirst, got: $result"
108+
)
109+
} finally {
110+
Locale.setDefault(originalLocale)
111+
}
112+
}
113+
114+
@Test
115+
fun `getCurrentDateTimeFormatted contains current hour`() {
116+
val result = getCurrentDateTimeFormatted()
117+
val currentHour = LocalDateTime.now().hour.toString().padStart(2, '0')
118+
assertTrue(
119+
result.contains(currentHour),
120+
"DateTime should contain current hour ($currentHour), got: $result"
121+
)
122+
}
123+
}

writeopia_ui/src/nativeMain/kotlin/io/writeopia/ui/utils/DateTimeUtils.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package io.writeopia.ui.utils
22

33
import platform.Foundation.NSDate
44
import platform.Foundation.NSDateFormatter
5+
import platform.Foundation.NSDateFormatterShortStyle
56

67
actual fun getCurrentDateFormatted(): String {
78
val formatter = NSDateFormatter()
8-
formatter.dateFormat = "dd/MM/yyyy"
9+
formatter.dateStyle = NSDateFormatterShortStyle
10+
formatter.timeStyle = platform.Foundation.NSDateFormatterNoStyle
911
return formatter.stringFromDate(NSDate())
1012
}
1113

1214
actual fun getCurrentDateTimeFormatted(): String {
1315
val formatter = NSDateFormatter()
14-
formatter.dateFormat = "dd/MM/yyyy HH:mm"
16+
formatter.dateStyle = NSDateFormatterShortStyle
17+
formatter.timeStyle = NSDateFormatterShortStyle
1518
return formatter.stringFromDate(NSDate())
1619
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package io.writeopia.ui.utils
22

3-
actual fun getCurrentDateFormatted(): String = ""
3+
import kotlin.js.Date
44

5-
actual fun getCurrentDateTimeFormatted(): String = ""
5+
actual fun getCurrentDateFormatted(): String {
6+
val now = Date()
7+
return now.toLocaleDateString()
8+
}
9+
10+
actual fun getCurrentDateTimeFormatted(): String {
11+
val now = Date()
12+
return "${now.toLocaleDateString()} ${now.toLocaleTimeString()}"
13+
}

0 commit comments

Comments
 (0)