Shared text, or text picked with the new "Create event" entry in the selection menu, opens the new-event form prefilled. The share's subject becomes the title, or the first line if there is no subject, and the rest of the text goes into the description. Start and length use the usual defaults (next full hour, default duration). Calendula doesn't try to read a date out of the text. Differences from the plan in the issue comment: - The line used as the title is left out of the description, so it doesn't show up twice. - Titles are capped at 255 characters. If a title gets cut, the full text stays in the description. - Sharing a plain-text file no longer sends it to the .ics importer. - A saved or dismissed prefilled form no longer reopens after rotating the phone. This also fixes it for "add to calendar" requests from other apps. - If a second request comes in while a prefilled form is open, it replaces the form when nothing was changed. Otherwise the save / discard / keep editing prompt comes first. - If Calendula was started just for the request, closing or saving the form takes you back to the app you came from. Closes #306 Co-authored-by: Jean-Luc Makiola <business@jeanlucmakiola.de> Reviewed-on: https://codeberg.org/jlmakiola/calendula/pulls/396
This commit is contained in:
@@ -151,6 +151,19 @@
|
||||
<data android:mimeType="application/ics" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- Text shared or selected in another app opens a prefilled new
|
||||
event (MainActivity.sharedTextExtrasOrNull, issue #306). -->
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="text/plain" />
|
||||
</intent-filter>
|
||||
<intent-filter android:label="@string/process_text_create_event">
|
||||
<action android:name="android.intent.action.PROCESS_TEXT" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="text/plain" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- Let another app or widget (e.g. the Todo Agenda widget) launch us
|
||||
to create a new event, the way the AOSP calendar accepts it:
|
||||
ACTION_INSERT on the events *dir* mime type, carrying the new
|
||||
|
||||
@@ -31,6 +31,7 @@ import de.jeanlucmakiola.calendula.data.prefs.ThemeMode
|
||||
import de.jeanlucmakiola.calendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.calendula.domain.EventForm
|
||||
import de.jeanlucmakiola.calendula.domain.buildInsertEventForm
|
||||
import de.jeanlucmakiola.calendula.domain.sharedTextOrNull
|
||||
import de.jeanlucmakiola.calendula.ui.RootScreen
|
||||
import de.jeanlucmakiola.calendula.ui.common.LocalShowHourGrid
|
||||
import de.jeanlucmakiola.calendula.ui.common.LocalMultiDayCount
|
||||
@@ -86,10 +87,18 @@ class MainActivity : AppCompatActivity() {
|
||||
private var requestedImportUri by mutableStateOf<Uri?>(null)
|
||||
|
||||
// A prefilled new-event form from an external ACTION_INSERT launch (another
|
||||
// app/widget asking us to create an event, issue #30). Consumed once by
|
||||
// CalendarHost, which opens it in the create form for review.
|
||||
// app/widget asking us to create an event, issue #30) or shared text (#306).
|
||||
// Consumed once by CalendarHost, which opens it in the create form for review.
|
||||
private var requestedInsert by mutableStateOf<InsertRequest?>(null)
|
||||
|
||||
// Whether such a form is still open. Only then does a recreation re-read the
|
||||
// intent to bring it back; a saved or dismissed one stays closed.
|
||||
private var insertFormOpen = false
|
||||
|
||||
// Started just for an external create, so closing its form returns to the
|
||||
// app that asked instead of leaving the calendar on top of it.
|
||||
private var launchedForInsert = false
|
||||
|
||||
// An external "edit this event" (ACTION_EDIT on content://.../events/<id>):
|
||||
// opens the occurrence in the edit form. Same occurrence-key shape as the
|
||||
// detail channel; consumed once by CalendarHost.
|
||||
@@ -128,7 +137,8 @@ class MainActivity : AppCompatActivity() {
|
||||
//
|
||||
// Only those channels are held back. An .ics import or a prefilled
|
||||
// create form keeps no saved state of its own, so re-reading the intent
|
||||
// is what carries one through a recreation.
|
||||
// is what carries one through a recreation — the create form only while
|
||||
// it's still open.
|
||||
val replaying = savedInstanceState != null || intent.isRelaunch()
|
||||
if (!replaying) {
|
||||
requestedDetailKey = intent.detailKeyOrNull() ?: intent.viewEventKeyOrNull()
|
||||
@@ -137,7 +147,11 @@ class MainActivity : AppCompatActivity() {
|
||||
requestedNav = intent.navRequestOrNull()
|
||||
?.takeUnless { replaying && it is WidgetNavRequest.OpenEvent }
|
||||
requestedImportUri = intent.importUriOrNull()
|
||||
requestedInsert = intent.insertRequestOrNull()
|
||||
val insertOpenBefore = savedInstanceState?.getBoolean(KEY_INSERT_FORM_OPEN) == true
|
||||
requestedInsert = intent.createRequestOrNull()?.takeIf { !replaying || insertOpenBefore }
|
||||
insertFormOpen = requestedInsert != null
|
||||
launchedForInsert = savedInstanceState?.getBoolean(KEY_LAUNCHED_FOR_INSERT)
|
||||
?: (requestedInsert != null)
|
||||
if (CrashReporter.shouldPrompt(this)) pendingCrashReport = CrashReporter.pendingReport(this)
|
||||
setContent {
|
||||
// One activity-scoped SettingsViewModel drives both the theme here
|
||||
@@ -208,6 +222,10 @@ class MainActivity : AppCompatActivity() {
|
||||
requestedInsertForm = requestedInsert?.form,
|
||||
requestedInsertSource = requestedInsert?.source ?: ImportSource.Insert,
|
||||
onInsertConsumed = { requestedInsert = null },
|
||||
onExternalFormClosed = {
|
||||
insertFormOpen = false
|
||||
if (launchedForInsert) finish()
|
||||
},
|
||||
requestedEditKey = requestedEditKey,
|
||||
onEditKeyConsumed = { requestedEditKey = null },
|
||||
)
|
||||
@@ -261,12 +279,21 @@ class MainActivity : AppCompatActivity() {
|
||||
CrashReporter.markHealthy(this)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putBoolean(KEY_INSERT_FORM_OPEN, insertFormOpen)
|
||||
outState.putBoolean(KEY_LAUNCHED_FOR_INSERT, launchedForInsert)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
(intent.detailKeyOrNull() ?: intent.viewEventKeyOrNull())?.let { requestedDetailKey = it }
|
||||
intent.navRequestOrNull()?.let { requestedNav = it }
|
||||
intent.importUriOrNull()?.let { requestedImportUri = it }
|
||||
intent.insertRequestOrNull()?.let { requestedInsert = it }
|
||||
intent.createRequestOrNull()?.let {
|
||||
requestedInsert = it
|
||||
insertFormOpen = true
|
||||
}
|
||||
intent.editEventKeyOrNull()?.let { requestedEditKey = it }
|
||||
}
|
||||
|
||||
@@ -288,7 +315,9 @@ class MainActivity : AppCompatActivity() {
|
||||
private fun Intent.importUriOrNull(): Uri? {
|
||||
val uri = when (action) {
|
||||
Intent.ACTION_VIEW -> data
|
||||
// Plain text is a create (#306), never a file to import.
|
||||
Intent.ACTION_SEND -> IntentCompat.getParcelableExtra(this, Intent.EXTRA_STREAM, Uri::class.java)
|
||||
.takeUnless { type == SHARED_TEXT_TYPE }
|
||||
else -> null
|
||||
} ?: return null
|
||||
// The calendar "view time" Uri (a date tap) is also ACTION_VIEW/content;
|
||||
@@ -333,6 +362,36 @@ class MainActivity : AppCompatActivity() {
|
||||
return InsertRequest(form, if (namesEnd) ImportSource.Insert else ImportSource.InsertOpenEnded)
|
||||
}
|
||||
|
||||
/** Any external request to create an event: shared text or an insert intent. */
|
||||
private fun Intent.createRequestOrNull(): InsertRequest? = sharedTextRequestOrNull() ?: insertRequestOrNull()
|
||||
|
||||
/**
|
||||
* Plain text shared into us (`ACTION_SEND`) or picked from a text-selection
|
||||
* menu (`ACTION_PROCESS_TEXT`), as a new event at the default time (#306).
|
||||
*/
|
||||
private fun Intent.sharedTextRequestOrNull(): InsertRequest? {
|
||||
val shared = when (action) {
|
||||
Intent.ACTION_SEND -> if (type == SHARED_TEXT_TYPE) {
|
||||
sharedTextOrNull(
|
||||
subject = getCharSequenceExtra(Intent.EXTRA_SUBJECT)?.toString(),
|
||||
text = getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString(),
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
Intent.ACTION_PROCESS_TEXT ->
|
||||
sharedTextOrNull(subject = null, text = getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)?.toString())
|
||||
else -> null
|
||||
} ?: return null
|
||||
val form = buildInsertEventForm(
|
||||
title = shared.title,
|
||||
description = shared.description,
|
||||
zone = TimeZone.currentSystemDefault(),
|
||||
now = Clock.System.now(),
|
||||
)
|
||||
return InsertRequest(form, ImportSource.InsertOpenEnded)
|
||||
}
|
||||
|
||||
/** A Long extra's value, or null when the extra is absent. */
|
||||
private fun Intent.longExtraOrNull(key: String): Long? =
|
||||
if (hasExtra(key)) getLongExtra(key, 0L) else null
|
||||
@@ -458,6 +517,9 @@ class MainActivity : AppCompatActivity() {
|
||||
// The calendar provider's authority/host. A date tap arrives as
|
||||
// ACTION_VIEW on content://com.android.calendar/time/<epochMillis>.
|
||||
private const val CALENDAR_PROVIDER_HOST = "com.android.calendar"
|
||||
private const val SHARED_TEXT_TYPE = "text/plain"
|
||||
private const val KEY_INSERT_FORM_OPEN = "insert_form_open"
|
||||
private const val KEY_LAUNCHED_FOR_INSERT = "launched_for_insert"
|
||||
|
||||
private const val EXTRA_EVENT_ID = "de.jeanlucmakiola.calendula.extra.EVENT_ID"
|
||||
private const val EXTRA_BEGIN_MILLIS = "de.jeanlucmakiola.calendula.extra.BEGIN"
|
||||
|
||||
@@ -24,13 +24,13 @@ import kotlin.time.Instant
|
||||
* the extras lives in `MainActivity.insertFormOrNull`.
|
||||
*/
|
||||
fun buildInsertEventForm(
|
||||
beginMillis: Long?,
|
||||
endMillis: Long?,
|
||||
isAllDay: Boolean,
|
||||
beginMillis: Long? = null,
|
||||
endMillis: Long? = null,
|
||||
isAllDay: Boolean = false,
|
||||
title: String?,
|
||||
description: String?,
|
||||
location: String?,
|
||||
rrule: String?,
|
||||
location: String? = null,
|
||||
rrule: String? = null,
|
||||
zone: TimeZone,
|
||||
now: Instant,
|
||||
): EventForm {
|
||||
@@ -71,3 +71,29 @@ internal fun nextFullHour(now: Instant): Instant {
|
||||
val hourMillis = 3_600_000L
|
||||
return Instant.fromEpochMilliseconds((now.toEpochMilliseconds() / hourMillis + 1) * hourMillis)
|
||||
}
|
||||
|
||||
/** Title and description for text shared or selected into the app (#306). */
|
||||
data class SharedText(val title: String, val description: String)
|
||||
|
||||
/**
|
||||
* Split shared text into an event title and description: the share's subject
|
||||
* becomes the title and the text the description. Without a subject the text's
|
||||
* first non-blank line is the title and the rest the description. A title longer
|
||||
* than [maxTitle] is cut, and the full text then stays in the description.
|
||||
* Null when there is nothing to use.
|
||||
*/
|
||||
fun sharedTextOrNull(subject: String?, text: String?, maxTitle: Int = 255): SharedText? {
|
||||
val body = text?.trim().orEmpty()
|
||||
val given = subject?.trim()?.takeIf { it.isNotEmpty() }
|
||||
val (title, rest) = if (given != null) {
|
||||
given to body
|
||||
} else {
|
||||
val lines = body.lines()
|
||||
val first = lines.firstOrNull()?.trim()?.takeIf { it.isNotEmpty() } ?: return null
|
||||
first to lines.drop(1).joinToString("\n").trim()
|
||||
}
|
||||
if (title.length <= maxTitle) return SharedText(title, rest)
|
||||
val cut = title.take(maxTitle).let { if (it.last().isHighSurrogate()) it.dropLast(1) else it }
|
||||
val full = if (given != null) listOf(given, body).filter { it.isNotEmpty() }.joinToString("\n\n") else body
|
||||
return SharedText(cut.trimEnd(), full)
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.material3.rememberDrawerState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.key
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
@@ -81,6 +82,7 @@ import de.jeanlucmakiola.calendula.ui.common.rememberCalendarSlideSpec
|
||||
import de.jeanlucmakiola.calendula.ui.common.selectView
|
||||
import de.jeanlucmakiola.calendula.ui.common.viewBaseStack
|
||||
import de.jeanlucmakiola.calendula.ui.detail.EventDetailScreen
|
||||
import de.jeanlucmakiola.calendula.ui.edit.EventEditViewModel
|
||||
import de.jeanlucmakiola.calendula.ui.edit.EventEditScreen
|
||||
import de.jeanlucmakiola.calendula.ui.edit.ImportSource
|
||||
import de.jeanlucmakiola.calendula.ui.imports.ImportScreen
|
||||
@@ -132,6 +134,7 @@ fun CalendarHost(
|
||||
requestedInsertForm: EventForm? = null,
|
||||
requestedInsertSource: ImportSource = ImportSource.Insert,
|
||||
onInsertConsumed: () -> Unit = {},
|
||||
onExternalFormClosed: () -> Unit = {},
|
||||
requestedEditKey: LongArray? = null,
|
||||
onEditKeyConsumed: () -> Unit = {},
|
||||
viewModel: CalendarHostViewModel = hiltViewModel(),
|
||||
@@ -277,6 +280,34 @@ fun CalendarHost(
|
||||
// Which channel filled [importForm]: an .ics file (prompt to apply the default
|
||||
// reminder) or an ACTION_INSERT intent (apply it automatically) — #49.
|
||||
var importFormSource by remember { mutableStateOf(ImportSource.File) }
|
||||
// Bumped per open, so even an equal prefill starts a fresh form.
|
||||
var importFormId by remember { mutableIntStateOf(0) }
|
||||
// Whether [importForm] came from an external create request (#30, #306);
|
||||
// closing it then hands control back to whoever asked.
|
||||
var importFromRequest by rememberSaveable { mutableStateOf(false) }
|
||||
// A second external request that arrived while a prefilled form was open:
|
||||
// it waits behind that form's unsaved-changes prompt, then opens.
|
||||
var pendingInsert by remember { mutableStateOf<Pair<EventForm, ImportSource>?>(null) }
|
||||
val importViewModel: EventEditViewModel = hiltViewModel(key = "import")
|
||||
fun openImportForm(form: EventForm, source: ImportSource, fromRequest: Boolean = false) {
|
||||
importFormSource = source
|
||||
importForm = form
|
||||
importFormId++
|
||||
importFromRequest = fromRequest
|
||||
}
|
||||
fun closeImportForm() {
|
||||
val next = pendingInsert
|
||||
pendingInsert = null
|
||||
if (next != null) {
|
||||
openImportForm(next.first, next.second, fromRequest = true)
|
||||
} else {
|
||||
importForm = null
|
||||
if (importFromRequest) {
|
||||
importFromRequest = false
|
||||
onExternalFormClosed()
|
||||
}
|
||||
}
|
||||
}
|
||||
// A restore (in-app "Restore from .ics" button) always runs the full import
|
||||
// flow — picker + summary — even for a single-event file, because the intent
|
||||
// is "restore a backup", not "add this one event". An externally opened .ics
|
||||
@@ -306,8 +337,11 @@ fun CalendarHost(
|
||||
// reveals on top of whatever was open without extra dismissal.
|
||||
LaunchedEffect(requestedInsertForm) {
|
||||
if (requestedInsertForm != null) {
|
||||
importFormSource = requestedInsertSource
|
||||
importForm = requestedInsertForm
|
||||
if (importForm == null) {
|
||||
openImportForm(requestedInsertForm, requestedInsertSource, fromRequest = true)
|
||||
} else {
|
||||
pendingInsert = requestedInsertForm to requestedInsertSource
|
||||
}
|
||||
onInsertConsumed()
|
||||
}
|
||||
}
|
||||
@@ -323,6 +357,9 @@ fun CalendarHost(
|
||||
editKey = null
|
||||
importUri = null
|
||||
importForm = null
|
||||
importFromRequest = false
|
||||
pendingInsert = null
|
||||
importViewModel.reset()
|
||||
}
|
||||
|
||||
// An external "edit this event" (ACTION_EDIT, e.g. an assistant/task app or
|
||||
@@ -570,8 +607,7 @@ fun CalendarHost(
|
||||
onEdit = onEditEvent,
|
||||
onDuplicate = { form ->
|
||||
// A fresh event, like a duplicate from the detail screen (#52).
|
||||
importFormSource = ImportSource.Insert
|
||||
importForm = form
|
||||
openImportForm(form, ImportSource.Insert)
|
||||
},
|
||||
chipShown = moveOutcome != null || selectionFailure != null,
|
||||
onDismissChip = {
|
||||
@@ -681,8 +717,7 @@ fun CalendarHost(
|
||||
// Reuse the prefilled-create overlay: a duplicate is a
|
||||
// fresh event, so it applies the default reminder like an
|
||||
// in-app new event (#52).
|
||||
importFormSource = ImportSource.Insert
|
||||
importForm = form
|
||||
openImportForm(form, ImportSource.Insert)
|
||||
detailKey = null
|
||||
},
|
||||
visible = detailKey != null,
|
||||
@@ -727,8 +762,7 @@ fun CalendarHost(
|
||||
},
|
||||
onManageCalendars = { showCalendars = true },
|
||||
onDuplicate = { form ->
|
||||
importFormSource = ImportSource.Insert
|
||||
importForm = form
|
||||
openImportForm(form, ImportSource.Insert)
|
||||
},
|
||||
// Opened straight from a tap (#178) there's no detail screen
|
||||
// below to show why, so open it.
|
||||
@@ -786,21 +820,24 @@ fun CalendarHost(
|
||||
importUri = null
|
||||
calendarsAfterImport = false
|
||||
backupAfterImport = false
|
||||
importFormSource = ImportSource.File
|
||||
importForm = form
|
||||
openImportForm(form, ImportSource.File)
|
||||
},
|
||||
)
|
||||
}
|
||||
importForm?.let { form ->
|
||||
EventEditScreen(
|
||||
initialDateIso = null,
|
||||
initialForm = form,
|
||||
initialFormSource = importFormSource,
|
||||
onClose = { importForm = null },
|
||||
onSaved = { importForm = null },
|
||||
onManageCalendars = { showCalendars = true },
|
||||
viewModel = hiltViewModel(key = "import"),
|
||||
)
|
||||
key(importFormId) {
|
||||
EventEditScreen(
|
||||
initialDateIso = null,
|
||||
initialForm = form,
|
||||
initialFormSource = importFormSource,
|
||||
onClose = ::closeImportForm,
|
||||
onSaved = ::closeImportForm,
|
||||
onManageCalendars = { showCalendars = true },
|
||||
leaveRequested = pendingInsert != null,
|
||||
onLeaveDeclined = { pendingInsert = null },
|
||||
viewModel = importViewModel,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Declared last so it covers every overlay that can open it: Settings,
|
||||
|
||||
@@ -54,6 +54,7 @@ fun RootScreen(
|
||||
requestedInsertSource: de.jeanlucmakiola.calendula.ui.edit.ImportSource =
|
||||
de.jeanlucmakiola.calendula.ui.edit.ImportSource.Insert,
|
||||
onInsertConsumed: () -> Unit = {},
|
||||
onExternalFormClosed: () -> Unit = {},
|
||||
requestedEditKey: LongArray? = null,
|
||||
onEditKeyConsumed: () -> Unit = {},
|
||||
) {
|
||||
@@ -155,6 +156,7 @@ fun RootScreen(
|
||||
requestedInsertForm = requestedInsertForm,
|
||||
requestedInsertSource = requestedInsertSource,
|
||||
onInsertConsumed = onInsertConsumed,
|
||||
onExternalFormClosed = onExternalFormClosed,
|
||||
requestedEditKey = requestedEditKey,
|
||||
onEditKeyConsumed = onEditKeyConsumed,
|
||||
)
|
||||
|
||||
@@ -225,6 +225,8 @@ fun EventEditScreen(
|
||||
onDuplicate: ((EventForm) -> Unit)? = null,
|
||||
onDeleted: () -> Unit = onSaved,
|
||||
onLoadFailed: () -> Unit = onClose,
|
||||
leaveRequested: Boolean = false,
|
||||
onLeaveDeclined: () -> Unit = {},
|
||||
visible: Boolean = true,
|
||||
viewModel: EventEditViewModel = hiltViewModel(),
|
||||
) {
|
||||
@@ -269,6 +271,10 @@ fun EventEditScreen(
|
||||
val requestClose = {
|
||||
if (hasUnsavedChanges) showLeavePrompt = true else close()
|
||||
}
|
||||
// Something else wants this form's place, e.g. a second share (#306).
|
||||
LaunchedEffect(leaveRequested) {
|
||||
if (leaveRequested) requestClose()
|
||||
}
|
||||
// The event vanished before the form could load it — fall back to the
|
||||
// detail screen, which shows its own failure state.
|
||||
LaunchedEffect(loadFailed) {
|
||||
@@ -420,7 +426,10 @@ fun EventEditScreen(
|
||||
showLeavePrompt = false
|
||||
close()
|
||||
},
|
||||
onDismiss = { showLeavePrompt = false },
|
||||
onDismiss = {
|
||||
showLeavePrompt = false
|
||||
if (leaveRequested) onLeaveDeclined()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -844,6 +844,9 @@
|
||||
<string name="shortcut_new_event_short">New event</string>
|
||||
<string name="shortcut_new_event_long">Create a new event</string>
|
||||
|
||||
<!-- Text-selection menu entry -->
|
||||
<string name="process_text_create_event">Create event</string>
|
||||
|
||||
<!-- Quick Settings tile -->
|
||||
<string name="qs_tile_new_event_label">New event</string>
|
||||
<string name="settings_qs_tile">Add Quick Settings tile</string>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package de.jeanlucmakiola.calendula.domain
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class SharedTextTest {
|
||||
|
||||
@Test
|
||||
fun `subject becomes the title and the text the description`() {
|
||||
assertThat(sharedTextOrNull("Dinner", "Friday at Luigi's\nBring wine"))
|
||||
.isEqualTo(SharedText("Dinner", "Friday at Luigi's\nBring wine"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without a subject the first line is the title and the rest the description`() {
|
||||
assertThat(sharedTextOrNull(null, "\n Dentist \n\nCheck-up, room 4\n"))
|
||||
.isEqualTo(SharedText("Dentist", "Check-up, room 4"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a single line is the title only`() {
|
||||
assertThat(sharedTextOrNull(" ", " Call mum ")).isEqualTo(SharedText("Call mum", ""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `subject without text leaves the description empty`() {
|
||||
assertThat(sharedTextOrNull("Meeting", null)).isEqualTo(SharedText("Meeting", ""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a long first line is cut and the full text kept as the description`() {
|
||||
val line = "a".repeat(300)
|
||||
val shared = sharedTextOrNull(null, "$line\nmore")!!
|
||||
assertThat(shared.title).isEqualTo("a".repeat(255))
|
||||
assertThat(shared.description).isEqualTo("$line\nmore")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a long subject is cut and kept in full above the text`() {
|
||||
val subject = "s".repeat(300)
|
||||
val shared = sharedTextOrNull(subject, "body")!!
|
||||
assertThat(shared.title).hasLength(255)
|
||||
assertThat(shared.description).isEqualTo("$subject\n\nbody")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the cut never splits a surrogate pair`() {
|
||||
val shared = sharedTextOrNull(null, "a".repeat(254) + "😀" + "b")!!
|
||||
assertThat(shared.title).isEqualTo("a".repeat(254))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nothing usable is null`() {
|
||||
assertThat(sharedTextOrNull(null, null)).isNull()
|
||||
assertThat(sharedTextOrNull(" ", "\n \n")).isNull()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user