Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions writeopia_ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ kotlin {
dependencies {
}
}

val jvmTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package io.writeopia.ui.utils

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

actual fun getCurrentDateFormatted(): String {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
return now.format(formatter)
}

actual fun getCurrentDateTimeFormatted(): String {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")
return now.format(formatter)
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
return "${now.format(dateFormatter)} ${now.format(timeFormatter)}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,41 +242,52 @@ class TextDrawer(
val text = value.text
val cursorPos = value.selection.start

println("Detecting slash")

if (sizeDifference > 0) {
println("Detecting slash 1")
// Character was added
val addedChar =
if (cursorPos > 0) text.getOrNull(cursorPos - 1) else null

if (addedChar == '/') {
println("Detecting slash 2")
// Check if '/' is at the start of the line or after a space
val charBefore =
if (cursorPos > 1) text.getOrNull(cursorPos - 2) else null
if (charBefore == null || charBefore == ' ' || charBefore == '\n') {
println("Detecting slash 3")
showSlashCommandPopup = true
slashStartPosition = cursorPos - 1
slashCommandFilter = ""
}
} else if (showSlashCommandPopup && slashStartPosition >= 0) {
println("Detecting slash 3.1")
// Update filter with text after '/'
val filterText =
text.substring(slashStartPosition + 1, cursorPos)
if (filterText.contains(' ') || filterText.contains('\n')) {
println("Detecting slash 4")
// Space or newline typed, close popup
showSlashCommandPopup = false
slashStartPosition = -1
slashCommandFilter = ""
} else {
println("Detecting slash 5")
slashCommandFilter = filterText
}
}
} else if (sizeDifference < 0 && showSlashCommandPopup) {
println("Detecting slash 6")
// Character was deleted
if (cursorPos <= slashStartPosition) {
println("Detecting slash 7")
// Deleted the '/' or before it
showSlashCommandPopup = false
slashStartPosition = -1
slashCommandFilter = ""
} else {
println("Detecting slash 8")
// Update filter
slashCommandFilter =
text.substring(slashStartPosition + 1, cursorPos)
Expand All @@ -294,11 +305,13 @@ class TextDrawer(
)
}

onTextEdit(
TextInput(value.text, start, end, spans),
drawInfo.position,
lineBreakByContent,
)
if (!showSlashCommandPopup) {
onTextEdit(
TextInput(value.text, start, end, spans),
drawInfo.position,
lineBreakByContent,
)
}

if (start == 0 || end == 0) {
coroutineScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package io.writeopia.ui.utils

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

actual fun getCurrentDateFormatted(): String {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
return now.format(formatter)
}

actual fun getCurrentDateTimeFormatted(): String {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")
return now.format(formatter)
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
return "${now.format(dateFormatter)} ${now.format(timeFormatter)}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package io.writeopia.ui.utils

import java.time.LocalDate
import java.time.LocalDateTime
import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertTrue

class DateTimeUtilsTest {

@Test
fun `getCurrentDateFormatted returns non-empty string`() {
val result = getCurrentDateFormatted()
assertTrue(result.isNotEmpty(), "Date formatted string should not be empty")
}

@Test
fun `getCurrentDateTimeFormatted returns non-empty string`() {
val result = getCurrentDateTimeFormatted()
assertTrue(result.isNotEmpty(), "DateTime formatted string should not be empty")
}

@Test
fun `getCurrentDateFormatted contains current year`() {
val currentYear = LocalDate.now().year.toString()
val result = getCurrentDateFormatted()
assertTrue(
result.contains(currentYear) || result.contains(currentYear.takeLast(2)),
"Date should contain current year ($currentYear), got: $result"
)
}

@Test
fun `getCurrentDateTimeFormatted contains time separator`() {
val result = getCurrentDateTimeFormatted()
assertTrue(
result.contains(":"),
"DateTime should contain time with colon separator, got: $result"
)
}

@Test
fun `getCurrentDateFormatted uses US locale format when locale is US`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale.US)
val result = getCurrentDateFormatted()
// US format is M/d/yy or similar (month first)
// The result should have the month as the first number
val today = LocalDate.now()
val expectedMonthFirst = "${today.monthValue}/"
assertTrue(
result.startsWith(expectedMonthFirst),
"US locale should have month first. Expected to start with $expectedMonthFirst, got: $result"
)
} finally {
Locale.setDefault(originalLocale)
}
}

@Test
fun `getCurrentDateFormatted uses European locale format when locale is Germany`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale.GERMANY)
val result = getCurrentDateFormatted()
// German format is dd.MM.yy (day first with dots)
val today = LocalDate.now()
val dayString = today.dayOfMonth.toString().padStart(2, '0')
assertTrue(
result.startsWith(dayString),
"German locale should have day first. Expected to start with $dayString, got: $result"
)
} finally {
Locale.setDefault(originalLocale)
}
}

@Test
fun `getCurrentDateFormatted uses UK locale format when locale is UK`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale.UK)
val result = getCurrentDateFormatted()
// UK format is dd/MM/yy (day first with slashes)
val today = LocalDate.now()
val dayString = today.dayOfMonth.toString().padStart(2, '0')
assertTrue(
result.startsWith(dayString),
"UK locale should have day first. Expected to start with $dayString, got: $result"
)
} finally {
Locale.setDefault(originalLocale)
}
}

@Test
fun `getCurrentDateTimeFormatted uses US locale format when locale is US`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale.US)
val result = getCurrentDateTimeFormatted()
val today = LocalDate.now()
val expectedMonthFirst = "${today.monthValue}/"
assertTrue(
result.startsWith(expectedMonthFirst),
"US locale datetime should have month first. Expected to start with $expectedMonthFirst, got: $result"
)
} finally {
Locale.setDefault(originalLocale)
}
}

@Test
fun `getCurrentDateTimeFormatted contains current hour`() {
val result = getCurrentDateTimeFormatted()
val currentHour = LocalDateTime.now().hour.toString().padStart(2, '0')
assertTrue(
result.contains(currentHour),
"DateTime should contain current hour ($currentHour), got: $result"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package io.writeopia.ui.utils

import platform.Foundation.NSDate
import platform.Foundation.NSDateFormatter
import platform.Foundation.NSDateFormatterShortStyle

actual fun getCurrentDateFormatted(): String {
val formatter = NSDateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
formatter.dateStyle = NSDateFormatterShortStyle
formatter.timeStyle = platform.Foundation.NSDateFormatterNoStyle
return formatter.stringFromDate(NSDate())
}

actual fun getCurrentDateTimeFormatted(): String {
val formatter = NSDateFormatter()
formatter.dateFormat = "dd/MM/yyyy HH:mm"
formatter.dateStyle = NSDateFormatterShortStyle
formatter.timeStyle = NSDateFormatterShortStyle
return formatter.stringFromDate(NSDate())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package io.writeopia.ui.utils

actual fun getCurrentDateFormatted(): String = ""
import kotlin.js.Date

actual fun getCurrentDateTimeFormatted(): String = ""
actual fun getCurrentDateFormatted(): String {
val now = Date()
return now.toLocaleDateString()
}

actual fun getCurrentDateTimeFormatted(): String {
val now = Date()
return "${now.toLocaleDateString()} ${now.toLocaleTimeString()}"
}